Skip to content

Commit 5fda5ae

Browse files
committed
chore(js-toolkit): polish build scripts
Due to the addition of new projects I've had to fix some issues so I've taken advantage of that and polished the build scripts, which were a bit outdated and rusty.
1 parent 648ce8b commit 5fda5ae

File tree

10 files changed

+156
-317
lines changed

10 files changed

+156
-317
lines changed

projects/js-toolkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"preversion": "echo Cannot version private package; false",
1313
"qa": "node scripts/qa/index.js",
1414
"test": "liferay-workspace-scripts test",
15-
"watch": "node scripts/watch.js"
15+
"watch": "node scripts/watch.js --all"
1616
},
1717
"version": "0.1.0"
1818
}

projects/js-toolkit/scripts/build.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,35 @@
88
const copyfiles = require('copyfiles');
99
const path = require('path');
1010

11+
const abort = require('./util/abort');
1112
const getProjectDirectories = require('./util/getProjectDirectories');
12-
const {runNodeBin} = require('./util/run');
13+
const spawn = require('./util/spawn');
1314

14-
function copyAssets() {
15+
function compile(dir) {
16+
try {
17+
spawn('yarn', ['run', 'tsc'], {cwd: dir});
18+
}
19+
catch (error) {
20+
throw new Error(
21+
`Compilation failed for project: ${path.basename(dir)}`
22+
);
23+
}
24+
}
25+
26+
async function copyAssets(dir) {
1527
return new Promise((resolve) => {
16-
console.log('copy:', path.basename(process.cwd()));
28+
const dirUp = dir.split(path.sep).length;
1729

1830
copyfiles(
19-
['src/**/*', 'lib/'],
31+
[`${dir}/src/**/*`, `${dir}/lib/`],
2032
{
2133
all: true,
2234
exclude: ['**/*.js', '**/*.ts', '**/__tests__/**/*'],
23-
up: 1,
35+
up: dirUp + 1,
2436
},
2537
(error) => {
2638
if (error) {
27-
console.error(error);
28-
process.exit(1);
39+
abort(error);
2940
}
3041

3142
resolve();
@@ -34,38 +45,28 @@ function copyAssets() {
3445
});
3546
}
3647

37-
async function main() {
38-
if (process.argv.includes('--all')) {
39-
const projectDirectories = getProjectDirectories();
40-
41-
for (const projectDirectory of getProjectDirectories()) {
42-
process.chdir(projectDirectory);
48+
async function build(dir) {
49+
try {
50+
console.log('copy:', path.basename(dir));
4351

44-
try {
45-
await copyAssets();
46-
}
47-
finally {
48-
process.chdir('../..');
49-
}
50-
}
52+
await copyAssets(dir);
5153

52-
console.log('build:', path.basename(process.cwd()));
54+
console.log('compile:', path.basename(dir));
5355

54-
runNodeBin.pipe(
55-
'tsc',
56-
'--build',
57-
...projectDirectories.map((projectDirectory) =>
58-
path.join(projectDirectory, 'tsconfig.json')
59-
)
60-
);
56+
compile(dir);
6157
}
62-
else {
63-
await copyAssets();
64-
65-
console.log('build:', path.basename(process.cwd()));
66-
67-
runNodeBin.pipe('tsc');
58+
catch (error) {
59+
abort(error.toString());
6860
}
6961
}
7062

71-
main();
63+
(async () => {
64+
if (process.argv.includes('--all')) {
65+
for (const projectDir of getProjectDirectories()) {
66+
await build(projectDir);
67+
}
68+
}
69+
else {
70+
build(path.resolve('.'));
71+
}
72+
})();

projects/js-toolkit/scripts/clean.js

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,58 @@
55

66
/* eslint-disable no-console */
77

8-
const fs = require('fs-extra');
8+
const fs = require('fs');
99
const path = require('path');
1010

11-
const {abort} = require('./util/report');
11+
const abort = require('./util/abort');
12+
const getProjectDirectories = require('./util/getProjectDirectories');
1213

13-
function hasValidOutDir(json) {
14+
function assertValidOutDir(json) {
1415
const {compilerOptions} = json;
1516

16-
if (!compilerOptions) {
17-
return false;
17+
if (!compilerOptions || !compilerOptions.outDir) {
18+
abort('No compilerOptions.outDir found in tsconfig.json');
1819
}
1920

20-
const {outDir} = compilerOptions;
21-
22-
if (!outDir) {
23-
return false;
24-
}
25-
26-
const absOutDir = path.resolve(outDir);
21+
const absOutDir = path.resolve(compilerOptions.outDir);
2722
const projectDir = path.resolve('.');
2823

2924
if (path.relative(projectDir, absOutDir).startsWith('../')) {
30-
return false;
25+
abort('Invalid compilerOptions.outDir found in tsconfig.json');
3126
}
3227

3328
return true;
3429
}
3530

36-
function clean() {
37-
console.log('clean:', path.basename(process.cwd()));
31+
function clean(dir) {
32+
console.log('clean:', path.basename(dir));
33+
34+
const cwd = process.cwd();
35+
36+
process.chdir(dir);
3837

3938
try {
40-
const json = fs.readJSONSync('tsconfig.json');
39+
/* eslint-disable-next-line @liferay/no-dynamic-require */
40+
const json = require(path.resolve(dir, 'tsconfig.json'));
4141

42-
if (!hasValidOutDir(json)) {
43-
abort(
44-
`Invalid compilerOptions.outDir found in ` +
45-
`${path.basename(path.resolve('.'))}/tsconfig.json`
46-
);
47-
}
42+
assertValidOutDir(json);
4843

49-
fs.removeSync(json.compilerOptions.outDir);
44+
fs.rmdirSync(json.compilerOptions.outDir, {recursive: true});
45+
fs.unlinkSync(path.resolve(dir, 'tsconfig.tsbuildinfo'));
5046
}
5147
catch (error) {
5248
if (error.code !== 'ENOENT') {
5349
abort(error.toString());
5450
}
5551
}
56-
57-
fs.removeSync('tsconfig.tsbuildinfo');
52+
finally {
53+
process.chdir(cwd);
54+
}
5855
}
5956

6057
if (process.argv.includes('--all')) {
61-
62-
// Clean all packages.
63-
64-
const cwd = process.cwd();
65-
66-
for (const entry of fs.readdirSync('packages', {withFileTypes: true})) {
67-
if (entry.isDirectory()) {
68-
const pkg = path.join('packages', entry.name);
69-
70-
try {
71-
process.chdir(pkg);
72-
73-
clean();
74-
}
75-
finally {
76-
process.chdir(cwd);
77-
}
78-
}
79-
}
58+
getProjectDirectories().forEach(clean);
8059
}
8160
else {
82-
83-
// Clean just the current working directory.
84-
85-
clean();
61+
clean(path.resolve('.'));
8662
}

projects/js-toolkit/scripts/topological.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

projects/js-toolkit/scripts/util/report.js renamed to projects/js-toolkit/scripts/util/abort.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
* SPDX-License-Identifier: LGPL-3.0-or-later
44
*/
55

6-
function abort(...msgs) {
6+
module.exports = function abort(...msgs) {
77
console.error(`\n❌ ${msgs.join('\n')}\n`);
88

99
process.exit(1);
10-
}
11-
12-
module.exports = {
13-
abort,
1410
};

projects/js-toolkit/scripts/util/get-dep-versions.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)