CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/init-script.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
This runs a script configured via the --init [str] parameter.
8
*/
9
10
import { spawn } from "node:child_process";
11
import { openSync, constants } from "node:fs";
12
import { join } from "node:path";
13
import { homedir } from "node:os";
14
import { access } from "node:fs/promises";
15
16
import { change_filename_extension } from "@cocalc/util/misc";
17
18
import { getOptions } from "./init-program";
19
import { getLogger } from "./logger";
20
21
const { info } = getLogger("init-script");
22
23
export async function run() {
24
if (!getOptions().init) return;
25
26
const initScript = join(homedir(), getOptions().init);
27
28
try {
29
await access(initScript, constants.R_OK);
30
} catch {
31
info(`"${initScript}" does not exist`);
32
return;
33
}
34
35
try {
36
info(`running "${initScript}"`);
37
38
const out = openSync(change_filename_extension(initScript, "log"), "w");
39
const err = openSync(change_filename_extension(initScript, "err"), "w");
40
41
// we don't detach the process, because otherwise it stays around when restarting the project
42
spawn("bash", [initScript], {
43
stdio: ["ignore", out, err],
44
});
45
} catch (err) {
46
info(`Problem running "${initScript}" -- ${err}`);
47
}
48
}
49
50