From e672f41e1e026b6a5064e6a32f676e641d7db31f Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Mon, 25 Sep 2017 10:00:19 +0800 Subject: [PATCH] Use promise --- scripts/generateColorLess.js | 50 +++++++++++++++++------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/scripts/generateColorLess.js b/scripts/generateColorLess.js index 0dcad3ac11..35ba563fe9 100644 --- a/scripts/generateColorLess.js +++ b/scripts/generateColorLess.js @@ -49,28 +49,26 @@ const reducePlugin = postcss.plugin('reducePlugin', () => { }; }); -async function generateCss() { - const antd = path.resolve(__dirname, '../'); - const entry = path.join(antd, 'components/style/index.less'); - let content = fs.readFileSync(entry).toString(); - const styles = glob.sync(path.join(antd, 'components/*/style/index.less')); - content += '\n'; - styles.forEach((style) => { - content += `@import "${style}";\n`; - }); - content += `@import "${path.join(antd, 'site/theme/static/index.less')}";\n`; - fs.writeFileSync('/tmp/style.less', content); +const antd = path.resolve(__dirname, '../'); +const entry = path.join(antd, 'components/style/index.less'); +let content = fs.readFileSync(entry).toString(); +const styles = glob.sync(path.join(antd, 'components/*/style/index.less')); +content += '\n'; +styles.forEach((style) => { + content += `@import "${style}";\n`; +}); +content += `@import "${path.join(antd, 'site/theme/static/index.less')}";\n`; +fs.writeFileSync('/tmp/style.less', content); - let result = (await less.render.call(less, content, { - paths: [path.join(antd, 'components/style')], - })).css; - - result = (await postcss([ +less.render.call(less, content, { + paths: [path.join(antd, 'components/style')], +}).then(({ css }) => { + return postcss([ reducePlugin, - ]).process(result, { parser: less.parser, from: entry })).css; - + ]).process(css, { parser: less.parser, from: entry }); +}).then(({ css }) => { Object.keys(COLOR_MAP).forEach((key) => { - result = result.replace(new RegExp(key, 'g'), COLOR_MAP[key]); + css = css.replace(new RegExp(key, 'g'), COLOR_MAP[key]); }); const bezierEasing = fs.readFileSync(path.join(antd, 'components/style/color/bezierEasing.less')).toString(); @@ -80,16 +78,14 @@ async function generateCss() { .replace('@import "bezierEasing";', '') .replace('@import "tinyColor";', ''); - result = `${colorPalette}\n${result}`; - result = `${tinyColor}\n${result}`; - result = `${bezierEasing}\n${result}`; - result = `@primary-color: #108ee9;\n${result}`; + css = `${colorPalette}\n${css}`; + css = `${tinyColor}\n${css}`; + css = `${bezierEasing}\n${css}`; + css = `@primary-color: #108ee9;\n${css}`; const siteDir = path.resolve(__dirname, '../_site'); if (!fs.existsSync(siteDir)) { fs.mkdirSync(siteDir); } - fs.writeFileSync(path.resolve(__dirname, '../_site/color.less'), result); -} - -generateCss(); + fs.writeFileSync(path.resolve(__dirname, '../_site/color.less'), css); +});