This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild-dev.js
More file actions
116 lines (90 loc) · 3.05 KB
/
Copy pathbuild-dev.js
File metadata and controls
116 lines (90 loc) · 3.05 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
#!/usr/bin/env node
// Copyright 2017-2020 @polkadot/dev authors & contributors
// SPDX-License-Identifier: Apache-2.0
const babel = require('@babel/cli/lib/babel/dir').default;
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const copySync = require('@open-web3/dev-config/scripts/copySync.cjs');
const execSync = require('@open-web3/dev-config/scripts/execSync.cjs');
const CONFIGS = ['babel.config.js', 'babel.config.cjs'];
const CPX = ['css', 'gif', 'hbs', 'jpg', 'js', 'json', 'png', 'svg', 'd.ts']
.map((ext) => `src/**/*.${ext}`)
.concat('package.json');
console.log('$ polkadot-dev-build-ts', process.argv.slice(2).join(' '));
function buildWebpack() {
execSync('yarn polkadot-exec-webpack --config webpack.config.js --mode production');
}
async function buildBabel(dir) {
// Get Root Configs
const configs = CONFIGS.map((c) => path.join(process.cwd(), `../../${c}`));
const babelConfig = configs.find((f) => fs.existsSync(f)) || configs[0];
// Get local configs
const localConfigs = CONFIGS.map((c) => path.join(process.cwd(), c));
const localBabelConfig = localConfigs.find((f) => fs.existsSync(f));
// Prefer to use local config over the root one.
const conf = localBabelConfig || babelConfig;
await babel({
babelOptions: {
configFile: conf
},
cliOptions: {
extensions: ['.ts', '.tsx'],
filenames: ['src'],
ignore: '**/*.d.ts',
outDir: path.join(process.cwd(), 'build'),
outFileExtension: '.js'
}
});
[...CPX]
.concat(`../../build/${dir}/src/**/*.d.ts`, `../../build/packages/${dir}/src/**/*.d.ts`)
.forEach((src) => copySync(src, 'build'));
}
async function buildJs(dir) {
if (!fs.existsSync(path.join(process.cwd(), '.skip-build'))) {
const { name, version } = require(path.join(process.cwd(), './package.json'));
// if (!name.startsWith('@polkadot/')) {
// return;
// }
console.log(`*** ${name} ${version}`);
mkdirp.sync('build');
if (fs.existsSync(path.join(process.cwd(), 'public'))) {
buildWebpack(dir);
} else {
await buildBabel(dir);
}
console.log();
}
}
async function buildMonorepo() {
execSync('yarn polkadot-dev-clean-build');
const cw = process.cwd();
const packages = path.join(cw, 'packages');
execSync(`cd ` + packages + ' && yarn build');
process.chdir('packages');
execSync('tsc --emitDeclarationOnly --outdir ../build');
const dirs = fs
.readdirSync('.')
.filter((dir) => fs.statSync(dir).isDirectory() && fs.existsSync(path.join(process.cwd(), dir, 'src')));
for (const dir of dirs) {
process.chdir(dir);
await buildJs(dir);
process.chdir('..');
}
process.chdir('..');
}
async function buildPolyrepo() {
execSync('yarn polkadot-exec-tsc --outdir ./build');
[...CPX].forEach((src) => copySync(src, './build'));
}
async function main() {
if (!fs.existsSync(path.join(process.cwd(), 'packages'))) {
buildPolyrepo();
} else {
buildMonorepo();
}
}
main().catch((error) => {
console.error(error);
process.exit(-1);
});