forked from clouDr-f2e/monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
105 lines (98 loc) · 3.05 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const path = require('path')
const chalk = require('chalk')
const fs = require('fs-extra')
const { targets: allTargets, fuzzyMatchTarget, getArgv, binRun } = require('./utils')
let buildTypes = true
// local debug
let LOCALDIR = ''
let rollupWatch = false
run()
async function run() {
const argv = getArgv()
console.log(argv)
// accept npm run build web browser...
const paramTarget = argv._
LOCALDIR = argv.local
buildTypes = argv.types !== 'false'
rollupWatch = argv.watch === 'true'
if (paramTarget.length === 0) {
buildAll(allTargets)
} else {
buildAll(paramTarget)
}
}
function buildAll(targets) {
runParallel(10, targets, rollupBuild)
}
async function runParallel(maxConcurrency, sources, iteratorFn) {
const ret = []
// const executing = []
for (const item of sources) {
const p = Promise.resolve().then(() => iteratorFn(item))
ret.push(p)
// if (maxConcurrency <= source.length) {
// const e = p.then(() => executing.splice(executing.indexOf(e)), 1)
// executing.push(e)
// if (executing.length >= maxConcurrency) {
// await Promise.race(executing)
// }
// }
}
return Promise.all(ret)
}
/**
*
* @param {*} target packages下的文件夹名称
*/
async function rollupBuild(target) {
const pkgDir = path.resolve(`packages/${target}`)
const pkg = require(`${pkgDir}/package.json`)
if (pkg.private) {
return
}
// const env = [pkg.buildOption && pkg.buildOption.env]
const args = [
'-c',
'--environment',
[
// `COMMIT:${commit}`,
// `NODE_ENV:${env}`,
`TARGET:${target}`,
`TYPES:${buildTypes}`,
`LOCALDIR:${LOCALDIR}`
]
.filter(Boolean)
.join(',')
]
rollupWatch && args.push('--watch')
const result = await binRun('rollup', args)
if (buildTypes && pkg.types) {
console.log(chalk.bold(chalk.yellow(`Rolling up type definitions for ${target}...`)))
// build types
const { Extractor, ExtractorConfig } = require('@microsoft/api-extractor')
const extractorConfigPath = path.resolve(pkgDir, `api-extractor.json`)
const extractorConfig = ExtractorConfig.loadFileAndPrepare(extractorConfigPath)
const extractorResult = Extractor.invoke(extractorConfig, {
localBuild: true,
showVerboseMessages: false
})
if (extractorResult.succeeded) {
// const typesDir = path.resolve(pkgDir, 'types')
// if (await fs.exists(typesDir)) {
// const dtsPath = path.resolve(pkgDir, pkg.types)
// const existing = await fs.readFile(dtsPath, 'utf-8')
// const typeFiles = await fs.readdir(typesDir)
// const toAdd = await Promise.all(
// typeFiles.map((file) => {
// return fs.readFile(path.resolve(typesDir, file), 'utf-8')
// })
// )
// console.log('add', toAdd)
// await fs.writeFile(dtsPath, existing + '\n' + toAdd.join('\n'))
// }
console.log(chalk.bold(chalk.green(`API Extractor completed successfully.`)))
}
console.log('pkgDir', pkgDir)
await fs.remove(`${pkgDir}/dist/packages`)
}
}