-
Notifications
You must be signed in to change notification settings - Fork 2
feat(ut): Add test #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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`; | ||
| } | ||
|
|
@@ -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'); | ||
| } | ||
|
|
||
|
|
@@ -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)); | ||
| } | ||
| } 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', | ||
|
|
@@ -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'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need print config if we use dry mode?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The init would return the config in dry run, |
||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
| 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') | ||
| }) | ||
| }); |
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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