Skip to content

Commit c94672e

Browse files
committed
refactor: 🎨 improve code structure
1 parent b099b83 commit c94672e

File tree

4 files changed

+67
-69
lines changed

4 files changed

+67
-69
lines changed

scripts/buildAssets.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ npm run assets:build -- vue-quill
1010
;(async () => {
1111
const fs = require('fs-extra')
1212
const path = require('path')
13+
const chalk = require('chalk')
1314
const execa = require('execa')
1415
const logger = require('./logger')
1516
const {
16-
targetAssets: allTargets,
17+
targets: allTargets,
1718
getPackageDir,
19+
getAssetsConfigJson,
1820
fuzzyMatchTarget,
1921
checkAssetsSize,
2022
runParallel,
@@ -36,11 +38,14 @@ npm run assets:build -- vue-quill
3638
await fs.remove(path.resolve(__dirname, '../node_modules/.rts2_cache'))
3739
}
3840
if (!targets.length) {
41+
logger.header(allTargets, 'BUILD ASSETS')
3942
await buildAll(allTargets)
4043
checkAllSizes(allTargets)
4144
} else {
42-
await buildAll(fuzzyMatchTarget(allTargets, targets, buildAllMatching))
43-
checkAllSizes(fuzzyMatchTarget(allTargets, targets, buildAllMatching))
45+
const matchedTargets = fuzzyMatchTarget(allTargets, targets, buildAllMatching)
46+
logger.header(matchedTargets, 'BUILD ASSETS')
47+
await buildAll(matchedTargets)
48+
checkAllSizes(matchedTargets)
4449
}
4550

4651
async function buildAll(targets: string[]) {
@@ -49,13 +54,21 @@ npm run assets:build -- vue-quill
4954

5055
async function buildAssets(target: string) {
5156
const pkgDir = getPackageDir(target)
52-
const assets = require(path.resolve(pkgDir, 'assets.config.json'))
57+
const assets = getAssetsConfigJson(target)
5358

5459
// only build published packages for release
5560
if (isRelease && assets.private) return
56-
if (!assets.css.length) return
61+
if (!assets || !assets.css.length) {
62+
logger.warning(
63+
target,
64+
`Can't find assets configuration or file configuration ${chalk.underline(
65+
target + '/assets.config.json'
66+
)}`
67+
)
68+
return
69+
}
5770

58-
logger.header(target, 'BUILD ASSETS')
71+
logger.info(target, 'Compiling assets...')
5972

6073
for (const css of assets.css) {
6174
const input: string = path.resolve(pkgDir, css.input)

scripts/dev.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ __DEV__=false npm run dev
1717
*/
1818
;(async () => {
1919
const execa = require('execa')
20-
const { fuzzyMatchTarget } = require('./utils')
20+
const logger = require('./logger')
21+
const { targets: allTargets, fuzzyMatchTarget } = require('./utils')
2122

2223
const args = require('minimist')(process.argv.slice(2))
23-
const target: string = args._.length ? fuzzyMatchTarget(args._)[0] : 'vue-quill'
24+
const target: string = args._.length ? fuzzyMatchTarget(allTargets, args._)[0] : ''
2425
const formats: string[] = args.formats || args.f
2526
const sourceMap: boolean = args.sourcemap || args.s
2627
const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)
2728

29+
if (target === '') {
30+
logger.warning('', 'You must specify the target e.g. npm run dev -- vue-quill')
31+
return
32+
}
33+
2834
await execa(
2935
'rollup',
3036
[

scripts/devAssets.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,42 @@ npm run assets:build -- vue-quill
88
```
99
*/
1010
;(async () => {
11-
const fs = require('fs-extra')
1211
const path = require('path')
1312
const execa = require('execa')
1413
const logger = require('./logger')
15-
const {
16-
targetAssets: allTargets,
17-
getPackageDir,
18-
fuzzyMatchTarget,
19-
runParallel,
20-
} = require('./utils')
14+
const { targets: allTargets, getPackageDir, fuzzyMatchTarget } = require('./utils')
2115

2216
const args = require('minimist')(process.argv.slice(2))
23-
const targets: string[] = args._
17+
const target: string = args._.length ? fuzzyMatchTarget(allTargets, args._)[0] : ''
2418
const sourceMap = args.sourcemap || args.s
25-
const isRelease: boolean = args.release
26-
const buildAllMatching: boolean = args.all || args.a
2719

28-
if (isRelease) {
29-
// remove build cache for release builds to avoid outdated enum values
30-
await fs.remove(path.resolve(__dirname, '../node_modules/.rts2_cache'))
31-
}
32-
if (!targets.length) {
33-
await buildAll(allTargets)
34-
} else {
35-
await buildAll(fuzzyMatchTarget(allTargets, targets, buildAllMatching))
36-
}
37-
38-
async function buildAll(targets: string[]) {
39-
await runParallel(require('os').cpus().length, targets, build)
20+
if (target === '') {
21+
logger.warning('', 'You must specify the target e.g. npm run assets:dev -- vue-quill')
22+
return
4023
}
4124

42-
async function build(target: string) {
43-
const pkgDir = getPackageDir(target)
44-
const assets = require(path.resolve(pkgDir, 'assets.config.json'))
25+
const pkgDir = getPackageDir(target)
26+
const assets = require(path.resolve(pkgDir, 'assets.config.json'))
4527

46-
if (assets.css) {
47-
logger.info(target, `Waiting for changes...`)
48-
assets.css.forEach((css: any) => {
49-
const input = path.resolve(pkgDir, css.input)
50-
const output = path.resolve(pkgDir, css.output)
51-
const inputExt = path.extname(input)
28+
if (assets.css) {
29+
const inputs: string[] = []
30+
const outputs: string[] = []
5231

53-
if (inputExt === '.styl' || inputExt === '.css') {
54-
const commands: string[] = ['stylus', '-w', input, '-o', output]
55-
if (sourceMap) commands.push('--sourcemap')
56-
execa('npx', commands)
57-
} else {
58-
logger.error(target, `File extention not supported: ${input}`)
59-
}
60-
})
32+
for (const css of assets.css) {
33+
const input = path.resolve(pkgDir, css.input)
34+
const output = path.resolve(pkgDir, css.output)
35+
const inputExt = path.extname(input)
36+
if (inputExt === '.styl' || inputExt === '.css') {
37+
inputs.push(input)
38+
outputs.push(output)
39+
} else {
40+
logger.error(target, `File extention not supported: ${input}`)
41+
}
6142
}
43+
const commands: string[] = ['stylus', '-w', ...inputs, '-o', ...outputs]
44+
if (sourceMap) commands.push('--sourcemap')
45+
await execa('npx', commands, { stdio: 'inherit' })
6246
}
47+
48+
logger.info(target, `${new Date().getUTCDate()} Waiting for changes...`)
6349
})()

scripts/utils.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,24 @@ const targets: string[] = fs.readdirSync(packagesDir).filter((targetDir: string)
2323
return true
2424
})
2525

26-
const targetAssets: string[] = fs.readdirSync(packagesDir).filter((targetDir: string) => {
27-
const pkgDir = path.resolve(packagesDir, targetDir)
28-
if (!fs.statSync(pkgDir).isDirectory()) {
29-
return false
30-
}
31-
const assetsPath = path.resolve(pkgDir, 'assets.config.json')
32-
if (!fs.existsSync(assetsPath)) {
33-
return false
34-
}
35-
const assets = require(assetsPath)
36-
if (assets.private && !assets.css) {
37-
return false
38-
}
39-
return true
40-
})
41-
4226
function getPackageDir(target: string) {
4327
return path.resolve(packagesDir, target)
4428
}
4529

4630
function getPackageJson(target = '') {
47-
if (target === '') {
48-
return require(path.resolve(rootDir, 'package.json'))
49-
}
50-
return require(path.resolve(packagesDir, target, 'package.json'))
31+
const pkgPath =
32+
target === ''
33+
? path.resolve(rootDir, 'package.json')
34+
: path.resolve(packagesDir, target, 'package.json')
35+
36+
if (!fs.existsSync(pkgPath)) return null
37+
return require(pkgPath)
38+
}
39+
40+
function getAssetsConfigJson(target = '') {
41+
const pkgPath = path.resolve(packagesDir, target, 'assets.config.json')
42+
if (!fs.existsSync(pkgPath)) return null
43+
return require(pkgPath)
5144
}
5245

5346
function fuzzyMatchTarget(
@@ -115,7 +108,7 @@ function checkAssetsSize(target: string, ext = '.css') {
115108
tableSizes.push(getTableSize(path.resolve(distDir, file)))
116109
}
117110
})
118-
logger.table(tableSizes)
111+
if (tableSizes.length) logger.table(tableSizes)
119112
})
120113
}
121114

@@ -184,9 +177,9 @@ module.exports = {
184177
rootDir,
185178
packagesDir,
186179
targets,
187-
targetAssets,
188180
getPackageDir,
189181
getPackageJson,
182+
getAssetsConfigJson,
190183
fuzzyMatchTarget,
191184
runParallel,
192185
checkBuildSize,

0 commit comments

Comments
 (0)