@onereach/billing-shared 30.7.5 → 30.7.6-beta.12172.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/dev-single.mjs +67 -15
package/package.json
CHANGED
package/scripts/dev-single.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { resolve, dirname } from 'node:path';
|
|
4
4
|
import { fileURLToPath } from 'node:url';
|
|
5
5
|
|
|
@@ -8,32 +8,84 @@ const __dirname = dirname(__filename);
|
|
|
8
8
|
|
|
9
9
|
const lockDir = '/tmp/shared-esm-build.lock';
|
|
10
10
|
const pidFile = resolve(lockDir, 'pid');
|
|
11
|
+
let ownsLock = false;
|
|
12
|
+
const startupDeadline = Date.now() + 1000;
|
|
11
13
|
|
|
12
14
|
const isAlive = (pid) => { try { process.kill(pid, 0);return true; } catch { return false; } };
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const reclaimLock = () => {
|
|
16
|
+
const staleLockDir = `${lockDir}.stale-${process.pid}-${Date.now()}`;
|
|
17
|
+
try {
|
|
18
|
+
renameSync(lockDir, staleLockDir);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (error.code === 'ENOENT') return;
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
rmSync(staleLockDir, {
|
|
24
|
+
recursive: true,
|
|
25
|
+
force: true,
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
while (!ownsLock) {
|
|
29
|
+
try {
|
|
30
|
+
mkdirSync(lockDir);
|
|
31
|
+
ownsLock = true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (error.code !== 'EEXIST') throw error;
|
|
34
|
+
|
|
35
|
+
let oldPid = NaN;
|
|
36
|
+
// eslint-disable-next-line no-empty
|
|
37
|
+
try { oldPid = Number(readFileSync(pidFile, 'utf8').trim()); } catch {}
|
|
38
|
+
if (!Number.isNaN(oldPid) && isAlive(oldPid)) {
|
|
39
|
+
// eslint-disable-next-line no-console
|
|
40
|
+
console.log(`shared already running (pid=${oldPid})`);
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (Number.isNaN(oldPid)) {
|
|
45
|
+
if (Date.now() < startupDeadline) {
|
|
46
|
+
await new Promise(resolve => { setTimeout(resolve, 25); });
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
reclaimLock();
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
reclaimLock();
|
|
55
|
+
}
|
|
22
56
|
}
|
|
23
57
|
|
|
24
58
|
writeFileSync(pidFile, String(process.pid), 'utf8');
|
|
25
|
-
|
|
26
|
-
|
|
59
|
+
const cleanup = () => {
|
|
60
|
+
if (!ownsLock) return;
|
|
61
|
+
ownsLock = false;
|
|
62
|
+
rmSync(lockDir, {
|
|
63
|
+
recursive: true,
|
|
64
|
+
force: true,
|
|
65
|
+
});
|
|
66
|
+
};
|
|
27
67
|
process.on('exit', cleanup);
|
|
28
|
-
process.on('SIGINT', () => { cleanup();process.exit(130); });
|
|
29
|
-
process.on('SIGTERM', () => { cleanup();process.exit(143); });
|
|
30
68
|
|
|
31
69
|
const child = spawn('pnpm', ['build:watch:esm'], {
|
|
32
70
|
stdio: 'inherit',
|
|
33
71
|
cwd: resolve(__dirname, '..'),
|
|
34
72
|
});
|
|
73
|
+
|
|
74
|
+
let shuttingDown = false;
|
|
75
|
+
const shutdown = (signal) => {
|
|
76
|
+
if (shuttingDown) return;
|
|
77
|
+
shuttingDown = true;
|
|
78
|
+
child.kill(signal);
|
|
79
|
+
};
|
|
80
|
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
81
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
82
|
+
|
|
35
83
|
child.on('exit', (code, signal) => {
|
|
36
84
|
cleanup();
|
|
37
|
-
if (signal)
|
|
85
|
+
if (signal) {
|
|
86
|
+
process.removeAllListeners(signal);
|
|
87
|
+
process.kill(process.pid, signal);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
38
90
|
process.exit(code ?? 0);
|
|
39
91
|
});
|