diff --git a/index.js b/index.js index 8410d35..5ea331a 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ const PAGE = { ICON_INPUT: '.menuList2.menuList3 .file input[type="file"]', FIRST_ICON_BOX: '#set0 .miBox:not(.mi-selected)', REMOVE_SET_BUTTON: '.menuList2.menuList3 li:last-child button', + OPEN_FEATURES_MENU: '#setH0 > button', SELECT_ALL_BUTTON: 'button[ng-click="selectAllNone($index, true)"]', GENERATE_LINK: 'a[href="#/select/font"]', GLYPH_SET: '#glyphSet0', @@ -66,7 +67,7 @@ const checkDownload = dest => new Promise((resolve, reject) => { }, interval); }); -const checkDuplicateName = ({ selectionPath, icons, names }, forceOverride) => { +const checkDuplicateName = ({selectionPath, icons, names}, forceOverride) => { const iconNames = icons.map((icon, index) => { if (names[index]) { return names[index]; @@ -75,9 +76,9 @@ const checkDuplicateName = ({ selectionPath, icons, names }, forceOverride) => { }); const duplicates = []; const selection = fs.readJSONSync(selectionPath); - selection.icons.forEach(({ properties }, index) => { + selection.icons.forEach(({properties}, index) => { if (iconNames.includes(properties.name)) { - duplicates.push({ name: properties.name, index }); + duplicates.push({name: properties.name, index}); } }); if (!duplicates.length) { @@ -85,7 +86,7 @@ const checkDuplicateName = ({ selectionPath, icons, names }, forceOverride) => { } if (forceOverride) { selection.icons = selection.icons.filter((icon, index) => !duplicates.some(d => d.index === index)); - fs.writeJSONSync(selectionPath, selection, { spaces: 2 }); + fs.writeJSONSync(selectionPath, selection, {spaces: 2}); } else { throw new Error(`Found duplicate icon names: ${duplicates.map(d => d.name).join(',')}`); } @@ -106,7 +107,7 @@ async function pipeline(options = {}) { logger('Preparing...'); if (!icons || !icons.length) { if (whenFinished) { - whenFinished({ outputDir }); + whenFinished({outputDir}); } return logger('No new icons found.'); } @@ -122,7 +123,7 @@ async function pipeline(options = {}) { await fs.remove(outputDir); await fs.ensureDir(outputDir); - const browser = await puppeteer.launch({ headless: !visible }); + const browser = await puppeteer.launch({headless: !visible}); logger('Started a new chrome instance, going to load icomoon.io.'); const page = await (await browser).newPage(); await page._client.send('Page.setDownloadBehavior', { @@ -138,13 +139,13 @@ async function pipeline(options = {}) { const importInput = await page.waitForSelector(PAGE.IMPORT_SELECTION_INPUT); await importInput.uploadFile(absoluteSelectionPath); - await page.waitForSelector(PAGE.OVERLAY_CONFIRM, { visible: true }); + await page.waitForSelector(PAGE.OVERLAY_CONFIRM, {visible: true}); await page.click(PAGE.OVERLAY_CONFIRM); const selection = fs.readJSONSync(selectionPath); if (selection.icons.length === 0) { logger('Selection icons is empty, going to create an empty set'); await page.click(PAGE.MAIN_MENU_BUTTON); - await page.waitForSelector(PAGE.NEW_SET_BUTTON, { visible: true }); + await page.waitForSelector(PAGE.NEW_SET_BUTTON, {visible: true}); await page.click(PAGE.NEW_SET_BUTTON); } logger('Uploaded config, going to upload new icon files'); @@ -153,6 +154,9 @@ async function pipeline(options = {}) { const iconPaths = icons.map(getAbsolutePath); await iconInput.uploadFile(...iconPaths); await page.waitForSelector(PAGE.FIRST_ICON_BOX); + await sleep(1000); + await page.click(PAGE.OPEN_FEATURES_MENU); + await sleep(1000); await page.click(PAGE.SELECT_ALL_BUTTON); logger('Uploaded and selected all new icons'); await page.click(PAGE.GENERATE_LINK); @@ -163,20 +167,20 @@ async function pipeline(options = {}) { await sleep(1000); await page.evaluate(names => { const request = indexedDB.open('IDBWrapper-storage', 1); - request.onsuccess = function() { + request.onsuccess = function () { const db = request.result; const tx = db.transaction('storage', 'readwrite'); const store = tx.objectStore('storage'); const keys = store.getAllKeys(); - keys.onsuccess = function() { + keys.onsuccess = function () { let timestamp; - keys.result.forEach(function(key) { + keys.result.forEach(function (key) { if (typeof key === 'number') { timestamp = key; } }); const main = store.get(timestamp); - main.onsuccess = function() { + main.onsuccess = function () { const data = main.result; for (let i = 0; i < names.length; i++) { data.obj.iconSets[0].selection[i].name = names[i]; @@ -205,16 +209,19 @@ async function pipeline(options = {}) { logger('Successfully downloaded, going to unzip it.'); await page.close(); // unzip stage - extract(zipPath, { dir: outputDir }, async err => { + extract(zipPath, {dir: outputDir}, async err => { if (err) { throw err; } await fs.remove(zipPath); logger(`Finished. The output directory is ${outputDir}.`); if (whenFinished) { - whenFinished({ outputDir }); + whenFinished({outputDir}); } }); + + await browser.close(); + } catch (error) { console.error(error); } diff --git a/index.test.js b/index.test.js index 495debb..307d471 100644 --- a/index.test.js +++ b/index.test.js @@ -14,11 +14,12 @@ const runCase = (name, fn) => new Promise((resolve, reject) => { resolve(); } } + console.log(`\r\n============= Start test '${name}' =============`); fn(done); }); -(async function() { +(async function () { await runCase('test with selection file which already has icons', done => { const names = ['new1', 'new2']; pipeline({ @@ -26,7 +27,8 @@ const runCase = (name, fn) => new Promise((resolve, reject) => { names, selectionPath: 'test-assets/selection.json', forceOverride: true, - whenFinished (result) { + // visible: true, + whenFinished(result) { const newSelection = JSON.parse(fs.readFileSync(path.resolve(result.outputDir, 'selection.json'))); assert.deepEqual( newSelection.icons.slice(0, names.length).map(icon => icon.properties.name), @@ -44,7 +46,7 @@ const runCase = (name, fn) => new Promise((resolve, reject) => { names, selectionPath: 'test-assets/selection-empty.json', forceOverride: true, - whenFinished (result) { + whenFinished(result) { const newSelection = JSON.parse(fs.readFileSync(path.resolve(result.outputDir, 'selection.json'))); assert.deepEqual( newSelection.icons.map(icon => icon.properties.name), diff --git a/package.json b/package.json index 74d6c6d..d065711 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "icomoon-cli", - "version": "1.3.2", + "version": "1.3.3", "description": "", "main": "index.js", "bin": { @@ -17,12 +17,12 @@ "author": "", "license": "ISC", "dependencies": { - "extract-zip": "^1.6.6", - "fs-extra": "^4.0.2", - "yargs": "^10.0.3", - "puppeteer": "2.0.0" + "extract-zip": "^2.0.1", + "fs-extra": "^9.1.0", + "yargs": "^16.2.0", + "puppeteer": "5.5.0" }, "devDependencies": { - "eslint": "^4.10.0" + "eslint": "^7.18.0" } }