From 0239c5137b0c3d40182c312ed0c81cb9b3699cff Mon Sep 17 00:00:00 2001 From: tristen Date: Fri, 3 Jan 2025 11:45:46 -0500 Subject: [PATCH] Use more native --- package.json | 3 +-- scripts/make-meta-icons.js | 11 ++++---- scripts/utils.js | 51 +++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index f13246c0..84e0507a 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,6 @@ "eslint": "^9.17.0", "globals": "^15.14.0", "husky": "^1.0.0", - "mkdirp": "^0.5.1", "pify": "^4.0.1", "prettier": "^1.14.3", "tap-spec": "^5.0.0", @@ -41,7 +40,7 @@ "homepage": "https://github.com/mapbox/maki", "husky": { "hooks": { - "pre-commit": "eslint ./scripts/* && prettier --write ./scripts/* && npm run build && git add ." + "pre-commit": "eslint ./scripts/* && prettier --write ./scripts/* ./browser.* && npm run build && git add ." } }, "prettier": { diff --git a/scripts/make-meta-icons.js b/scripts/make-meta-icons.js index 03c18989..163ba8b5 100644 --- a/scripts/make-meta-icons.js +++ b/scripts/make-meta-icons.js @@ -1,6 +1,6 @@ /* Create icons with stroke and m:parameter options for - custom icon authoring from our SDKs. + custom icon attribute styling in our SDKs. */ const path = require('path'); const { gatherIcons, write } = require('./utils'); @@ -57,11 +57,10 @@ function metaMap({ data }) { return data; } -function makeMetaIcons() { - return gatherIcons(path.join(__dirname, '../icons')).then(svgs => { - const metaMappedIcons = svgs.map(metaMap); - write(metaMappedIcons, path.join(__dirname, '../meta-icons')); - }); +async function makeMetaIcons() { + const svgs = await gatherIcons(path.join(__dirname, '../icons')); + const metaMappedIcons = svgs.map(metaMap); + return write(metaMappedIcons, path.join(__dirname, '../meta-icons')); } module.exports = makeMetaIcons; diff --git a/scripts/utils.js b/scripts/utils.js index c53b5994..15df45d0 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -1,5 +1,8 @@ const { promises: fs } = require('fs'); + +const { readdir, readFile } = require('node:fs/promises'); const path = require('path'); +const pify = require('pify'); const xml2js = require('xml2js'); const mkdirp = dir => fs.mkdir(dir, { recursive: true }); @@ -8,33 +11,29 @@ const builder = new xml2js.Builder({ xmldec: { version: '1.0', encoding: 'UTF-8' } }); -const pReadFile = pify(fs.readFile); -const pReaddir = pify(fs.readdir); - -function gatherIcons(iconPath) { - return pReaddir(iconPath).then(files => { - const svgFiles = files.filter(f => f.indexOf('.svg') !== -1); +async function gatherIcons(iconPath) { + const files = await readdir(iconPath); + const svgFiles = files.filter(f => f.indexOf('.svg') !== -1); - return Promise.all( - svgFiles.map(f => pReadFile(path.join(iconPath, f), 'utf8')) - ) - .then(svgs => { - return Promise.all( - svgs.map(svg => { - // Need to create a parser for each svg - const parser = new xml2js.Parser(); - const pParse = util.promisify(parser.parseString); - return pParse(svg); - }) - ); - }) - .then(parsedSvgs => { - return parsedSvgs.map((pSvg, i) => ({ - fileName: svgFiles[i], - data: pSvg.svg - })); - }); - }); + return Promise.all( + svgFiles.map(f => readFile(path.join(iconPath, f), 'utf8')) + ) + .then(svgs => { + return Promise.all( + svgs.map(svg => { + // Need to create a parser for each svg + const parser = new xml2js.Parser(); + const pParse = pify(parser.parseString); + return pParse(svg); + }) + ); + }) + .then(parsedSvgs => { + return parsedSvgs.map((pSvg, i) => ({ + fileName: svgFiles[i], + data: pSvg.svg + })); + }); } function write(cleanedSvgs, iconPath) {