Skip to content

Commit

Permalink
Use more native
Browse files Browse the repository at this point in the history
  • Loading branch information
tristen committed Jan 3, 2025
1 parent db6c2d8 commit 0239c51
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
11 changes: 5 additions & 6 deletions scripts/make-meta-icons.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 25 additions & 26 deletions scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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 });

Expand All @@ -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) {
Expand Down

0 comments on commit 0239c51

Please sign in to comment.