/
clean-and-run-downlevel-dts.js
48 lines (35 loc) · 1.59 KB
/
clean-and-run-downlevel-dts.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
/**
* @script clean-and-run-downlevel-dts.js
* @description Removes '{repo-root}/ts3.5' directory, re-creates it, runs
* `npm run downlevel-dts` from repo-root, and copies the root tsconfig.json file into new directory.
*/
const fs = require('fs').promises,
path = require('path'),
{spawn} = require('child_process'),
{log, warn, error} = console,
rootDir = path.join(__dirname, '..'),
outputDir = path.resolve(path.join(__dirname, '../ts3.5')),
tsConfigFilePath = path.join(rootDir, 'tsconfig.json'),
tsConfigOutFilePath = path.join(outputDir, 'tsconfig.json');
// Run 'clean-and-run' process
(async () =>
fs.rmdir(outputDir, {recursive: true})
.then(() => fs.mkdir(outputDir))
.then(() => fs.copyFile(tsConfigFilePath, tsConfigOutFilePath))
// Run downlevel-dts
.then(() => new Promise((resolve, reject) => {
// Start downlevel-dts package script
const subProcess = spawn('npm', ['run', 'downlevel-dts'], {cwd: rootDir});
// Log child process buffer data as `string`
subProcess.stdout.on('data', data => log(data.toString().trim() + '\n'));
// Log stderr, child process, buffer data as a `string`
subProcess.stderr.on('data', data => warn(data.toString().trim() + '\n'));
// Handle process end
subProcess.on('close', (code) => code !== 0 ?
reject(`Child process existed with code ${code}.\n`) :
resolve('"clean-and-run-downlevel-dts" completed successfully.\n')
);
// Catch process start errors
subProcess.on('error', reject);
}))
)().then(log, error);