Skip to content

Commit

Permalink
remove fs-ext due to its deps, use kill(pid, 0) for instance lock
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Feb 1, 2025
1 parent a1152c7 commit 782bb51
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependency-graph.png
dump.rdb

RUNNING_PID
instance.lock
.DS_Store

# Eclipse auto-generated files
Expand Down
2 changes: 0 additions & 2 deletions ui/.build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
"sass-embedded-win32-x64": "1.83.3"
},
"dependencies": {
"@types/fs-ext": "^2.0.3",
"@types/micromatch": "^4.0.9",
"@types/node": "22.12.0",
"@types/tinycolor2": "1.4.6",
"esbuild": "0.24.2",
"fast-glob": "3.3.3",
"fast-xml-parser": "4.5.1",
"fs-ext": "^2.1.1",
"micromatch": "4.0.8",
"tinycolor2": "1.6.0",
"typescript": "5.7.3"
Expand Down
26 changes: 0 additions & 26 deletions ui/.build/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ui/.build/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const env = new (class {
readonly themeDir = p.join(this.uiDir, 'common', 'css', 'theme');
readonly themeGenDir = p.join(this.themeDir, 'gen');
readonly buildDir = p.join(this.uiDir, '.build');
readonly lockFile = p.join(this.buildDir, 'instance.lock');
readonly cssTempDir = p.join(this.buildDir, 'build', 'css');
readonly buildSrcDir = p.join(this.uiDir, '.build', 'src');
readonly buildTempDir = p.join(this.buildDir, 'build');
Expand Down
32 changes: 24 additions & 8 deletions ui/.build/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ps from 'node:process';
import fs from 'node:fs';
import { flockSync, constants } from 'fs-ext';
import { deepClean } from './clean.ts';
import { build } from './build.ts';
import { startConsole } from './console.ts';
import { env, errorMark } from './env.ts';

// main entry point
['SIGINT', 'SIGTERM', 'SIGHUP'].forEach(sig => ps.on(sig, () => ps.exit(2)));

const args: Record<string, string> = {
'--tsc': '',
Expand Down Expand Up @@ -112,17 +112,33 @@ if (argv.includes('--help') || oneDashArgs.includes('h')) {
ps.exit(0);
}

try {
const fd = fs.openSync(env.buildDir, 'r');
ps.on('exit', () => fs.closeSync(fd));
flockSync(fd, constants.LOCK_EX | constants.LOCK_NB);
} catch {
env.exit(`${errorMark} - Another instance is already running`);
}
instanceLock();

if (env.clean) {
await deepClean();
if (argv.includes('--clean-exit')) ps.exit(0);
}

startConsole();
build(argv.filter(x => !x.startsWith('-')));

function instanceLock(checkStale = true) {
try {
const fd = fs.openSync(env.lockFile, 'wx');
fs.writeFileSync(fd, String(ps.pid), { flag: 'w' });
fs.closeSync(fd);
console.log('ooya');
ps.on('exit', () => fs.unlinkSync(env.lockFile));
} catch {
const pid = parseInt(fs.readFileSync(env.lockFile, 'utf8'), 10);
if (!isNaN(pid) && pid > 0 && ps.platform !== 'win32') {
try {
ps.kill(pid, 0);
env.exit(`${errorMark} - Another instance is already running`);
} catch {
fs.unlinkSync(env.lockFile); // it's a craplet
if (checkStale) return instanceLock(false);
}
}
}
}
1 change: 1 addition & 0 deletions ui/.build/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function sync(): Promise<any> {
}

async function syncOne(absSrc: string, absDest: string): Promise<boolean> {
// TODO are these stats unnecessary now?
const [src, dest] = (
await Promise.allSettled([
fs.promises.stat(absSrc),
Expand Down

0 comments on commit 782bb51

Please sign in to comment.