-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·171 lines (135 loc) · 4.41 KB
/
Copy pathbuild.js
File metadata and controls
executable file
·171 lines (135 loc) · 4.41 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
const { join, dirname, basename } = require('path')
const { spawn } = require('child_process')
const { red, green, grey } = require('chalk')
const { copy } = require('fs-extra')
const { build, glob, watch, cliopts, fmtDuration } = require('estrella')
const [_, args] = cliopts.parse(
['package', 'Build a specific package', '[package]'],
)
const [SPECIFIC_PACKAGE] = args
const common = {
sourcemap: true,
platform: 'node',
format: 'esm',
target: ['esnext'],
tslint: false,
bundle: false,
}
const packages = glob(join(__dirname, 'packages', '*', 'package.json'))
const tasks = packages.map(fpath => {
const pkg = require(fpath)
const dir = dirname(fpath)
const name = basename(dir)
if (pkg.build === undefined) return
if (SPECIFIC_PACKAGE && name !== SPECIFIC_PACKAGE) return
if (pkg.build.esbuild !== undefined)
runBuild(pkg, dir)
if (pkg.build.scripts !== undefined)
runScripts(pkg, dir)
})
tasks.push(buildTypes(cliopts.watch))
Promise.all(tasks)
.catch(e => {
console.error(e)
process.exit(1)
})
function runBuild(pkg, dir) {
const entries = glob(join(dir, pkg.build.esbuild.entry))
const outdir = join(dir, pkg.build.esbuild.outdir)
build({
...common,
entryPoints: entries,
outdir: outdir,
format: pkg.build.esbuild.format || common.format,
tsconfig: join(dir, 'tsconfig.json'),
})
}
function runScripts(pkg, dir) {
pkg.build.scripts.forEach(script => runScript(pkg, dir, script))
if (cliopts.watch) {
if (pkg.build.watch === undefined)
return console.info(grey(`No watch specified for: ${pkg.name}`))
const directories = pkg.build.watch.map(path => glob(join(dir, path)))
watch(directories, () => {
pkg.build.scripts.forEach(script => runScript(pkg, dir, script))
})
}
}
function runScript(pkg, dir, script) {
const start = Date.now()
const errorLines = []
const proc = spawn('npm', ['run', script], { cwd: dir })
proc.stderr.on('data', chunk => errorLines.push(chunk))
proc.on('close', () => {
const duration = fmtDuration(Date.now() - start)
if (errorLines.length > 0) {
console.info(red(`\nError running ${script} in ${pkg.name} (${duration})`))
console.error(errorLines.join(''))
} else {
console.info(green(`Ran ${script} in ${pkg.name} (${duration})`))
}
})
}
function buildTypes(watch = false) {
const command = join(__dirname, 'node_modules', 'typescript', 'bin', 'tsc')
const args = [
'--declaration',
'--emitDeclarationOnly',
'--project', '.',
'--outDir', 'types',
'--jsx', 'react-jsx'
]
if (watch) args.unshift('--watch')
const proc = spawn(command, args)
proc.stdout.on('data', chunk => {
logTscOutput(chunk, 'info')
})
proc.stderr.on('data', chunk => {
logTscOutput(chunk, 'error')
})
if (watch) watchTypes()
proc.on('close', () => {
copyTypes()
})
}
async function watchTypes() {
await copyTypes()
return watch(join(__dirname, 'types'), copyTypes)
}
async function copyTypes() {
const start = Date.now()
try {
await Promise.all(packages.map(fpath => {
const pkg = require(fpath)
const dir = dirname(fpath)
const name = basename(dir)
if (pkg.build === undefined || pkg.build.esbuild === undefined) return
const packageTypesDir = join(__dirname, 'types', name, 'src')
const packageTypesDestDir = join(dir, pkg.build.esbuild.outdir)
return copy(packageTypesDir, packageTypesDestDir)
}))
const duration = fmtDuration(Date.now() - start)
console.info(green(`Built types (${duration})`))
} catch (e) {
const duration = fmtDuration(Date.now() - start)
console.info(red(`\nError building types (${duration})`))
console.info(e.stack)
}
}
function logTscOutput(chunk, level) {
const lines = chunk.toString().split('\n')
.map(cleanAnsiString)
.filter(line => !(line.trim().includes('File change detected. Starting incremental compilation...')))
.filter(line => !(line.trim().includes('Found 0 errors. Watching for file changes.')))
.filter(line => !(line.trim().includes('Starting compilation in watch mode...')))
.filter(line => line.length > 0)
lines.forEach(line => {
console[level](`tsc: "${line}"`)
})
}
// https://stackoverflow.com/a/29497680
const ANSI_CODE_REGEXP = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
function cleanAnsiString(str) {
return str.replace(ANSI_CODE_REGEXP, '')
}