-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-npm.js
77 lines (61 loc) · 2.23 KB
/
build-npm.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
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const ts = require('typescript');
const { main: downlevel } = require('downlevel-dts');
// eslint-disable-next-line import/extensions
const tsConfig = require('../tsconfig.json');
const {
transformLoadFileStaticallyFromNPM,
} = require('./load-statically-from-npm');
const { rmdirRecursive, readdirRecursive, showDirStats } = require('./utils');
if (require.main === module) {
rmdirRecursive('./npmDist');
fs.mkdirSync('./npmDist');
const srcFiles = readdirRecursive('./src', { ignoreDir: /^__.*__$/ });
const { options } = ts.convertCompilerOptionsFromJson(
{ ...tsConfig.compilerOptions, outDir: 'npmDist' },
process.cwd(),
);
const program = ts.createProgram({
rootNames: srcFiles.map((filepath) => path.join('./src', filepath)),
options,
});
program.emit(undefined, undefined, undefined, undefined, {
after: [transformLoadFileStaticallyFromNPM],
});
downlevel('./npmDist', './npmDist/ts3.4', '3.4.0');
fs.copyFileSync('./LICENSE', './npmDist/LICENSE');
fs.copyFileSync('./README.md', './npmDist/README.md');
// Should be done as the last step so only valid packages can be published
const packageJSON = buildPackageJSON();
fs.writeFileSync(
'./npmDist/package.json',
JSON.stringify(packageJSON, null, 2),
);
showDirStats('./npmDist');
}
function buildPackageJSON() {
// eslint-disable-next-line import/extensions
const packageJSON = require('../package.json');
delete packageJSON.private;
delete packageJSON.scripts;
delete packageJSON.devDependencies;
const { version } = packageJSON;
const versionMatch = /^\d+\.\d+\.\d+-?(?<preReleaseTag>.*)?$/.exec(version);
if (!versionMatch) {
throw new Error('Version does not match semver spec: ' + version);
}
const { preReleaseTag } = versionMatch.groups;
if (preReleaseTag != null) {
const [tag] = preReleaseTag.split('.');
assert(
tag.startsWith('experimental-') || ['alpha', 'beta', 'rc'].includes(tag),
`"${tag}" tag is supported.`,
);
assert(!packageJSON.publishConfig, 'Can not override "publishConfig".');
packageJSON.publishConfig = { tag: tag || 'latest' };
}
return packageJSON;
}