-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
448 lines (392 loc) · 10.8 KB
/
cli.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
import { basename, dirname } from 'path'
import { cwd } from 'process'
function main() {
let pkg = readJSON<pkg>('package.json')
let args = parseArgs(process.argv.slice(2), pkg)
setupPackageJSON(pkg, args)
setupMainTsconfig()
setupEsbuildTsconfig(args)
setupEsbuildJs(args)
setupBrowserFile(args)
setupEntryFile(args)
setupGitIgnore(args)
}
function setupPackageJSON(pkg: pkg, args: args) {
let { outDir, entryFile, testFile } = args
applyTemplate('package.json', pkg, {
type: 'module',
main: `${outDir}/cjs.js`,
types: `${outDir}/${basename(entryFile).replace(/\.ts$/, '.d.ts')}`,
module: `${outDir}/esm.js`,
browser: `${outDir}/browser.js`,
unpkg: `${outDir}/browser.js`,
})
pkg.files ||= []
addToArray(pkg.files, outDir)
pkg.scripts ||= {}
applyTemplate('package.json scripts', pkg.scripts, {
test: `ts-mocha ${testFile}`,
coverage: 'nyc npm test',
build: 'run-s clean transpile',
clean: 'rimraf dist',
transpile: 'run-p esbuild tsc',
esbuild: 'node scripts/esbuild.js',
tsc: 'tsc -p tsconfig.esbuild.json',
})
pkg.devDependencies ||= {}
applyTemplate(
'package.json devDependencies',
pkg.devDependencies,
{
'@types/chai': '^4.3.5',
'@types/mocha': '^10.0.1',
'@types/node': '^20.6.2',
'chai': '^4.3.7',
'esbuild': '^0.19.3',
'esbuild-node-externals': '^1.9.0',
'mocha': '^10.2.0',
'npm-run-all': '^4.1.5',
'nyc': '^15.1.0',
'rimraf': '^5.0.1',
'ts-mocha': '^10.0.0',
'ts-node': '^10.9.1',
'ts-node-dev': '^2.0.0',
'typescript': '^5.2.2',
},
'casual',
)
pkg.devDependencies = sortObject(pkg.devDependencies)
writeJSON('package.json', pkg)
}
function setupMainTsconfig() {
let tsconfig = readJSON<tsconfig>('tsconfig.json')
tsconfig.compilerOptions ||= {}
applyTemplate(
'tsconfig.json compilerOptions',
tsconfig.compilerOptions,
{
target: 'es2022',
module: 'commonjs',
esModuleInterop: true,
forceConsistentCasingInFileNames: true,
strict: true,
skipLibCheck: true,
incremental: true,
outDir: 'dist',
},
'casual',
)
tsconfig.exclude ||= []
addToArray(tsconfig.exclude, 'dist')
writeJSON('tsconfig.json', tsconfig)
}
function setupEsbuildTsconfig(args: args) {
let tsconfig = readJSON<tsconfig>('tsconfig.esbuild.json')
tsconfig.compilerOptions ||= {}
applyTemplate(
'tsconfig.esbuild.json compilerOptions',
tsconfig.compilerOptions,
{
target: 'es2018',
strict: true,
noImplicitAny: true,
moduleResolution: 'node',
emitDeclarationOnly: true,
declaration: true,
skipLibCheck: true,
outDir: 'dist',
},
'casual',
)
tsconfig.files ||= []
addToArray(tsconfig.files, args.entryFile)
writeJSON('tsconfig.esbuild.json', tsconfig)
}
function setupEsbuildJs(args: args) {
let { entryFile, browserFile, outDir } = args
let code = `
#!/usr/bin/env node
if (typeof require == 'function') {
let esbuild = require('esbuild')
let { nodeExternalsPlugin } = require('esbuild-node-externals')
main(esbuild, nodeExternalsPlugin)
} else {
Promise.all([
import('esbuild'),
import('esbuild-node-externals'),
]).then(
([esbuild, { nodeExternalsPlugin }]) => main(esbuild, nodeExternalsPlugin),
)
}
function main(esbuild, nodeExternalsPlugin) {
Promise.all([
esbuild.build({
entryPoints: ['${entryFile}'],
outfile: '${outDir}/cjs.js',
bundle: true,
minify: false,
format: 'cjs',
platform: 'node',
sourcemap: false,
sourcesContent: false,
target: 'node12',
plugins: [nodeExternalsPlugin()],
}),
esbuild.build({
entryPoints: ['${entryFile}'],
outfile: '${outDir}/esm.js',
bundle: true,
minify: false,
format: 'esm',
platform: 'node',
sourcemap: false,
sourcesContent: false,
target: 'node14',
plugins: [nodeExternalsPlugin()],
}),
esbuild.build({
entryPoints: ['${browserFile}'],
outfile: '${outDir}/browser.js',
bundle: true,
minify: false,
format: 'iife',
platform: 'browser',
sourcemap: false,
sourcesContent: false,
target: ['chrome58', 'firefox57', 'safari11', 'edge16'],
plugins: [],
}),
]).catch(error => {
console.error(error)
process.exit(1)
})
}
`
mkdirSync('scripts', { recursive: true })
writeCode('scripts/esbuild.js', code)
}
function setupBrowserFile(args: args) {
let { globalName, entryFile, browserFile } = args
let importName = basename(entryFile).replace(/\.ts$/, '')
let code = `
import * as ${globalName} from './${importName}'
declare const window: any
window.${globalName} = ${globalName}
`
mkdirSync(dirname(browserFile), { recursive: true })
writeCode(browserFile, code)
}
function setupEntryFile(args: args) {
try {
readFileSync(args.entryFile)
} catch (error) {
let code = `
export let name = '${args.globalName}'
`
writeCode(args.entryFile, code)
}
}
function setupGitIgnore(args: args) {
let { outDir } = args
let outDir_ = outDir + '/'
let file = '.gitignore'
let text: string
try {
text = readFileSync(file).toString()
} catch (error) {
// file not found
text = ''
}
const originalText = text
let match = text.split('\n').find(line => {
line = line.trim().replace(/#.*/, '').trim()
return line == outDir || line == outDir_
})
if (!match) {
if (text.trim() && !text.endsWith('\n')) text += '\n'
text += outDir
}
text = text.split('\r').join('')
if (!text.endsWith('\n')) text += '\n'
if (text != originalText) {
writeFileSync(file, text)
}
}
function addToArray<T>(xs: T[], x: T) {
if (xs.includes(x)) return
xs.push(x)
}
function sortObject<T extends object>(object: T): T {
return Object.fromEntries(
Object.entries(object).sort((a, b) => compare(a[0], b[0])),
) as T
}
function applyTemplate<T extends object>(
name: string,
target: T,
patch: T,
mode?: 'casual',
) {
for (let key of Object.keys(patch)) {
if (!(key in target)) {
// @ts-ignore
target[key] = patch[key]
continue
}
// @ts-ignore
if (target[key] == patch[key]) {
continue
}
if (mode != 'casual') {
console.warn('Conflicting config:', { name, key })
}
}
}
function compare<T>(a: T, b: T): number {
if (a < b) return -1
if (a > b) return +1
return 0
}
function parseArgs(args: string[], pkg: pkg) {
let entryFile = ''
let globalName = ''
let outDir = 'dist'
for (let i = 0; i < args.length; i++) {
let arg = args[i]
if (arg.startsWith('--entryFile')) {
entryFile = parseArgValue(arg)
continue
}
if (
arg.startsWith('--name') ||
arg.startsWith('--global') ||
arg.startsWith('--globalName')
) {
globalName = parseArgValue(arg)
continue
}
if (arg.startsWith('--outDir')) {
outDir = parseArgValue(arg)
continue
}
if (arg == '-h' || arg == '--help') {
console.log(helpMessage())
process.exit(0)
}
console.error('Unknown argument: ' + JSON.stringify(arg))
process.exit(1)
}
let packageName = getPackageName(pkg)
globalName ||= packageName
if (globalName.startsWith('@')) {
globalName = globalName.split('/').pop()!
}
globalName = camelCase(globalName)
entryFile ||= detectFile([
'index.ts',
`${packageName}.ts`,
`${packageName}`,
'src/index.ts',
`src/${packageName}.ts`,
`src/${packageName}`,
])
let testFile = detectFile([
entryFile.replace(/\.ts$/, '.spec.ts'),
entryFile.replace(/\.ts$/, '.test.ts'),
])
let browserFile = entryFile.replace(basename(entryFile), 'browser.ts')
return { globalName, entryFile, browserFile, testFile, outDir }
}
type args = ReturnType<typeof parseArgs>
function parseArgValue(arg: string): string {
let value = arg.split('=')[1]
if (value) return value
console.error('Missing argument value for: ' + JSON.stringify(arg))
console.error('Example: ' + arg.replace('=', '') + '=value')
process.exit(1)
}
function helpMessage(): string {
return `
Usage: setup-tsc [options]
Optional Options:
--entryFile=<path> Specify the entry .ts file.
If not specified, it's automatically inferred (see details below).
--name|--global|--globalName=<string>
Specify the global name for the browser .js file.
If not specified, it's derived from the package name or directory name.
--outDir=<path> Specify the output directory for build output.
Default is 'dist'.
-h, --help Display usage information.
Options Details:
--entryFile:
If this option is not specified, the entry file is inferred from a list of potential names.
Here, '<packageName>' is either the package name or the current working directory's name.
Possible names include 'index.ts', '<packageName>.ts', <packageName>,
'src/index.ts', 'src/<packageName>.ts', and 'src/<packageName>'.
Note: If the package name ends with '.ts', '<packageName>' is also a valid entry file name.
--globalName:
If the global name begins with '@', the name is derived from the part after the '/' in the package name,
and it is converted into camelCase.
For instance, '--globalName=@my-scope/my-package' results in a global name of 'myPackage'.
Examples:
$ setup-tsc --entryFile=./src/core.ts --globalName=oklab
$ setup-tsc --outDir=./build
$ npx setup-tsc
`
}
function detectFile(files: string[]): string {
for (let file of files) {
if (existsSync(file)) return file
}
return files[0]
}
function writeJSON(file: string, object: object) {
writeCode(file, JSON.stringify(object, null, 2))
}
function writeCode(file: string, code: string) {
writeFileSync(file, code.trim() + '\n')
}
function readJSON<T>(file: string): Partial<T> {
try {
return JSON.parse(readFileSync(file).toString())
} catch (error) {
// file not found or invalid json
return {}
}
}
type pkg = Partial<{
type: string
name: string
main: string
types: string
module: string
browser: string
unpkg: string
files: string[]
scripts: Partial<{
test: string
coverage: string
build: string
clean: string
transpile: string
esbuild: string
tsc: string
}>
devDependencies: Record<string, string>
}>
function getPackageName(pkg: pkg): string {
return pkg.name || basename(cwd())
}
type tsconfig = Partial<{
compilerOptions: Record<string, any>
files: string[]
exclude: string[]
}>
function camelCase(name: string): string {
return name
.split('-')
.map((s, i) => (i == 0 ? s : s.slice(0, 1).toUpperCase() + s.slice(1)))
.join('')
}
main()