Skip to content

Commit 3e20b78

Browse files
committed
chore: improve exiting of sync script
1 parent 755cf5a commit 3e20b78

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

packages/compass-web/scripts/sync-dist-to-mms.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const path = require('path');
44
const child_process = require('child_process');
55
const os = require('os');
6+
const util = require('util');
67
const { debounce } = require('lodash');
78

89
if (!process.env.MMS_HOME) {
@@ -58,11 +59,42 @@ const webpackWatchProcess = child_process.spawn('npm', ['run', 'watch'], {
5859
stdio: 'inherit',
5960
});
6061

62+
const failProofRunner = () =>
63+
new (class FailProofRunner extends Array {
64+
append(...fns) {
65+
this.push(...fns);
66+
return this;
67+
}
68+
69+
run() {
70+
const errors = this.map((f) => {
71+
try {
72+
f();
73+
} catch (e) {
74+
return e;
75+
}
76+
}).filter((e) => e);
77+
78+
if (errors.length) {
79+
fs.writeSync(
80+
process.stdout.fd,
81+
util.inspect(errors, { depth: 20 }) + '\n'
82+
);
83+
}
84+
85+
return errors.length;
86+
}
87+
})();
88+
6189
function cleanup(signalName) {
62-
distWatcher.close();
63-
webpackWatchProcess.kill(signalName);
64-
fs.cpSync(tmpDir, destDir, { recursive: true });
65-
fs.rmSync(tmpDir, { recursive: true, force: true });
90+
const errorCount = failProofRunner()
91+
.append(() => distWatcher.close())
92+
.append(() => webpackWatchProcess.kill(signalName))
93+
.append(() => fs.cpSync(tmpDir, destDir, { recursive: true }))
94+
.append(() => fs.rmSync(tmpDir, { recursive: true, force: true }))
95+
.run();
96+
fs.writeSync(process.stdout.fd, 'Exit compass-web sync...\n');
97+
process.exit(errorCount);
6698
}
6799

68100
for (const evt of ['SIGINT', 'SIGTERM']) {

0 commit comments

Comments
 (0)