@kntic/kntic 0.3.1 → 0.4.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/src/commands/start.js +34 -2
- package/src/commands/start.test.js +72 -0
package/package.json
CHANGED
package/src/commands/start.js
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { execSync } = require("child_process");
|
|
4
|
+
const { readFileSync } = require("fs");
|
|
5
|
+
const { basename } = require("path");
|
|
6
|
+
|
|
7
|
+
function isScreenAvailable() {
|
|
8
|
+
try {
|
|
9
|
+
execSync("which screen", { stdio: "ignore" });
|
|
10
|
+
return true;
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getScreenName() {
|
|
17
|
+
try {
|
|
18
|
+
const content = readFileSync(".kntic.env", "utf8");
|
|
19
|
+
const match = content.match(/^KNTIC_PRJ_PREFIX=(.+)$/m);
|
|
20
|
+
if (match && match[1].trim()) {
|
|
21
|
+
return match[1].trim();
|
|
22
|
+
}
|
|
23
|
+
} catch {
|
|
24
|
+
// .kntic.env not found or unreadable — fall through
|
|
25
|
+
}
|
|
26
|
+
return basename(process.cwd());
|
|
27
|
+
}
|
|
4
28
|
|
|
5
29
|
function start() {
|
|
6
|
-
|
|
7
|
-
|
|
30
|
+
const composeCmd = "docker compose -f kntic.yml --env-file .kntic.env up --build";
|
|
31
|
+
|
|
32
|
+
if (isScreenAvailable()) {
|
|
33
|
+
const screenName = getScreenName();
|
|
34
|
+
console.log(`Starting KNTIC services in screen session "${screenName}"…`);
|
|
35
|
+
execSync(`screen -S ${screenName} ${composeCmd}`, { stdio: "inherit" });
|
|
36
|
+
} else {
|
|
37
|
+
console.log("Starting KNTIC services…");
|
|
38
|
+
execSync(composeCmd, { stdio: "inherit" });
|
|
39
|
+
}
|
|
8
40
|
}
|
|
9
41
|
|
|
10
42
|
module.exports = start;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { describe, it, beforeEach, afterEach } = require("node:test");
|
|
4
|
+
const assert = require("node:assert/strict");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const os = require("os");
|
|
8
|
+
|
|
9
|
+
// We test the helper logic by requiring the module and inspecting behaviour.
|
|
10
|
+
// Since start.js uses execSync (side-effecting), we test the screen-name
|
|
11
|
+
// resolution logic and the screen-detection path indirectly.
|
|
12
|
+
|
|
13
|
+
describe("start — getScreenName resolution", () => {
|
|
14
|
+
let tmpDir, origCwd;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "kntic-start-test-"));
|
|
18
|
+
origCwd = process.cwd();
|
|
19
|
+
process.chdir(tmpDir);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
process.chdir(origCwd);
|
|
24
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("uses KNTIC_PRJ_PREFIX from .kntic.env when present", () => {
|
|
28
|
+
fs.writeFileSync(path.join(tmpDir, ".kntic.env"), "UID=1000\nKNTIC_PRJ_PREFIX=myproject\nGID=1000\n");
|
|
29
|
+
// Re-require to get fresh module (clear cache)
|
|
30
|
+
delete require.cache[require.resolve("./start")];
|
|
31
|
+
// We can't call start() directly (it execs docker), so we extract getScreenName
|
|
32
|
+
// by reading the source and evaluating the helper in isolation.
|
|
33
|
+
const src = fs.readFileSync(require.resolve("./start"), "utf8");
|
|
34
|
+
const getScreenName = new Function("require", "process",
|
|
35
|
+
src.replace(/^"use strict";\s*/, "")
|
|
36
|
+
.replace(/module\.exports\s*=\s*start;/, "return getScreenName;")
|
|
37
|
+
)(require, process);
|
|
38
|
+
assert.equal(getScreenName(), "myproject");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("falls back to current directory name when KNTIC_PRJ_PREFIX is missing", () => {
|
|
42
|
+
fs.writeFileSync(path.join(tmpDir, ".kntic.env"), "UID=1000\nGID=1000\n");
|
|
43
|
+
delete require.cache[require.resolve("./start")];
|
|
44
|
+
const src = fs.readFileSync(require.resolve("./start"), "utf8");
|
|
45
|
+
const getScreenName = new Function("require", "process",
|
|
46
|
+
src.replace(/^"use strict";\s*/, "")
|
|
47
|
+
.replace(/module\.exports\s*=\s*start;/, "return getScreenName;")
|
|
48
|
+
)(require, process);
|
|
49
|
+
assert.equal(getScreenName(), path.basename(tmpDir));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("falls back to current directory name when .kntic.env does not exist", () => {
|
|
53
|
+
delete require.cache[require.resolve("./start")];
|
|
54
|
+
const src = fs.readFileSync(require.resolve("./start"), "utf8");
|
|
55
|
+
const getScreenName = new Function("require", "process",
|
|
56
|
+
src.replace(/^"use strict";\s*/, "")
|
|
57
|
+
.replace(/module\.exports\s*=\s*start;/, "return getScreenName;")
|
|
58
|
+
)(require, process);
|
|
59
|
+
assert.equal(getScreenName(), path.basename(tmpDir));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("ignores empty KNTIC_PRJ_PREFIX value", () => {
|
|
63
|
+
fs.writeFileSync(path.join(tmpDir, ".kntic.env"), "KNTIC_PRJ_PREFIX=\nUID=1000\n");
|
|
64
|
+
delete require.cache[require.resolve("./start")];
|
|
65
|
+
const src = fs.readFileSync(require.resolve("./start"), "utf8");
|
|
66
|
+
const getScreenName = new Function("require", "process",
|
|
67
|
+
src.replace(/^"use strict";\s*/, "")
|
|
68
|
+
.replace(/module\.exports\s*=\s*start;/, "return getScreenName;")
|
|
69
|
+
)(require, process);
|
|
70
|
+
assert.equal(getScreenName(), path.basename(tmpDir));
|
|
71
|
+
});
|
|
72
|
+
});
|