Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/momentum-design/momentum-constructor#readme",
"devDependencies": {
"@jsdevtools/npm-publish": "^1.4.3",
"@types/jest": "^28.1.8",
"@types/node": "^17.0.26",
"jest": "28.1.3",
"lerna": "^5.3.0",
Expand Down
28 changes: 18 additions & 10 deletions tools/font-icon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function replaceLast(str, ch, cr) {
return `${str.substring(0, lp)}${cr}${str.substring(lp + 1)}`;
}

async function generateSVGPattern(svgPath, content, tokenRegExp) {
async function generateSVGPattern(svgPath, content, tokenRegExp, isProd) {
if (!isProd) {
return `${svgPath}/*.svg`;
}
Expand All @@ -71,8 +71,11 @@ async function generateIconsJson(fontName, fontPath, tokenPath) {
return [`icon-${name}`, codePointHex]
})

const iconsJson = Object.fromEntries(glyphs);
const tokenFile = `${tokenPath}/icons.json`
return Object.fromEntries(glyphs);
}

async function writeIconsJson(iconsJson, tokenPath) {
const tokenFile = path.resolve(tokenPath, 'icons.json')
await fs.writeFile(tokenFile, JSON.stringify(iconsJson, null, 2), 'utf-8');
}

Expand All @@ -81,22 +84,26 @@ async function loadSvgFont(file) {
return await xml2js.parseStringPromise(svgFile);
}

async function build(){
let config;
async function build(_config, _isProd = isProd, _dry = false){
let config = _config;
try {
config = require(path.resolve(configFile));
if (!config) {
config = require(path.resolve(configFile));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we only read config from _config? Use Object.assign?

Copy link
Contributor Author

@ivanchenhz ivanchenhz Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still want to support the config file way,
So the user doesn't have to write js, if they prefer cli

} catch(e) {
console.error(`Please run 'npx ${NAME} --i', before build!`)
return
}

const { svgPath, fontName, fontPath, content, tokenRegExp, tokenPath } = config;
const svgPattern = await generateSVGPattern(svgPath, content, tokenRegExp);
const svgPattern = await generateSVGPattern(svgPath, content, tokenRegExp, _isProd);
await generateFonts(fontName, svgPattern, fontPath, {ts: Date.now(), fontHeight: 1000});
await generateIconsJson(fontName, fontPath, tokenPath);

const iconsJson = await generateIconsJson(fontName, fontPath);
return _dry ? iconsJson : await writeIconsJson(iconsJson, tokenPath);
}

async function init() {
async function init(_dry = false) {
const pattern = /tw-icon-([\w-]+)/g;
const config = {
svgPath: 'node_modules/momentum-abstract/icon',
Expand All @@ -110,7 +117,8 @@ async function init() {
},
tokenPath: '.',
}
await fs.writeFile(configFile, `module.exports = ${JSON.stringify(config, null, 2)}`, 'utf-8');

return _dry ? config : await fs.writeFile(configFile, `module.exports = ${JSON.stringify(config, null, 2)}`, 'utf-8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need print config if we use dry mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The init would return the config in dry run,
How to use the return stuff would up to the user,

}

module.exports = {
Expand Down
18 changes: 18 additions & 0 deletions tools/font-icon/test/font-icon.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fontIcon = require('../src')

describe("Font icon: ",()=>{
test('Init', async () => {
const config = await fontIcon.init(true)
expect(config).toBeDefined();
});

// tw-icon-arrow-down-bold
// tw-icon-arrow-up-filled
test('Build', async () => {
const config = await fontIcon.init(true)
config.content = ['tools/**/font-icon.test.js']
const iconsJson = await fontIcon.build(config, true, true);
expect(iconsJson).toHaveProperty('icon-arrow-down-bold')
expect(iconsJson).toHaveProperty('icon-arrow-up-filled')
})
});