sdtk-spec-kit 0.3.16 → 0.3.17

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/src/lib/state.js CHANGED
@@ -1,55 +1,50 @@
1
1
  "use strict";
2
2
 
3
3
  const fs = require("fs");
4
- const os = require("os");
5
4
  const path = require("path");
6
- const { execFileSync } = require("child_process");
5
+ const { ensureDir, getAuthFile, hardenFilePermissions } = require("./suite-state");
6
+ const { migrateLegacyAuthStateIfNeeded } = require("./legacy-auth-migration");
7
7
 
8
- function ensureDir(dirPath) {
9
- fs.mkdirSync(dirPath, { recursive: true });
8
+ function readAuthPayload(authFile) {
9
+ try {
10
+ const payload = JSON.parse(fs.readFileSync(authFile, "utf8"));
11
+ if (!payload || typeof payload !== "object") {
12
+ return null;
13
+ }
14
+ return payload;
15
+ } catch (_error) {
16
+ return null;
17
+ }
10
18
  }
11
19
 
12
- function getAuthFile() {
13
- return path.join(os.homedir(), ".sdtkrc");
20
+ function normalizeAuthState(payload) {
21
+ const token = typeof payload.github_token === "string" ? payload.github_token : "";
22
+ return { authenticated: token.length > 0, token };
14
23
  }
15
24
 
16
- /**
17
- * Harden file permissions so only the current user can read/write.
18
- * - POSIX: chmod 600
19
- * - Windows: icacls to grant only current user Full Control
20
- * @returns {boolean} true if hardening succeeded, false otherwise.
21
- */
22
- function hardenFilePermissions(filePath) {
23
- try {
24
- if (process.platform === "win32") {
25
- const username = os.userInfo().username;
26
- execFileSync("icacls", [
27
- filePath,
28
- "/inheritance:r",
29
- "/grant:r",
30
- `${username}:(F)`,
31
- ], { stdio: "ignore" });
32
- } else {
33
- fs.chmodSync(filePath, 0o600);
34
- }
35
- return true;
36
- } catch (_err) {
37
- return false;
38
- }
25
+ function writeAuthPayload(payload) {
26
+ const authFile = getAuthFile();
27
+ ensureDir(path.dirname(authFile));
28
+ fs.writeFileSync(authFile, JSON.stringify(payload, null, 2), "utf8");
29
+ const hardened = hardenFilePermissions(authFile);
30
+ return { path: authFile, hardened };
39
31
  }
40
32
 
41
33
  function readAuthState() {
42
34
  const authFile = getAuthFile();
43
35
  if (!fs.existsSync(authFile)) {
44
- return { authenticated: false, token: "" };
36
+ const migration = migrateLegacyAuthStateIfNeeded();
37
+ if (migration.malformedLegacy || !fs.existsSync(authFile)) {
38
+ return { authenticated: false, token: "" };
39
+ }
45
40
  }
46
- try {
47
- const payload = JSON.parse(fs.readFileSync(authFile, "utf8"));
48
- const token = payload.github_token || "";
49
- return { authenticated: token.length > 0, token };
50
- } catch (_error) {
41
+
42
+ const payload = readAuthPayload(authFile);
43
+ if (!payload) {
51
44
  return { authenticated: false, token: "" };
52
45
  }
46
+
47
+ return normalizeAuthState(payload);
53
48
  }
54
49
 
55
50
  /**
@@ -57,21 +52,17 @@ function readAuthState() {
57
52
  * @returns {{ path: string, hardened: boolean }}
58
53
  */
59
54
  function writeAuthState(token) {
60
- const authFile = getAuthFile();
61
- const payload = {
55
+ return writeAuthPayload({
62
56
  github_token: token,
63
57
  updated_at: new Date().toISOString(),
64
- };
65
- fs.writeFileSync(authFile, JSON.stringify(payload, null, 2), "utf8");
66
- const hardened = hardenFilePermissions(authFile);
67
- return { path: authFile, hardened };
58
+ });
68
59
  }
69
60
 
70
61
  function clearAuthState() {
71
- const authFile = getAuthFile();
72
- if (fs.existsSync(authFile)) {
73
- fs.unlinkSync(authFile);
74
- }
62
+ return writeAuthPayload({
63
+ github_token: "",
64
+ updated_at: new Date().toISOString(),
65
+ });
75
66
  }
76
67
 
77
68
  module.exports = {
@@ -80,4 +71,4 @@ module.exports = {
80
71
  readAuthState,
81
72
  writeAuthState,
82
73
  clearAuthState,
83
- };
74
+ };
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const os = require("os");
5
+ const path = require("path");
6
+ const { execFileSync } = require("child_process");
7
+
8
+ function ensureDir(dirPath) {
9
+ fs.mkdirSync(dirPath, { recursive: true });
10
+ }
11
+
12
+ function getSuiteRoot() {
13
+ return path.join(os.homedir(), ".sdtk");
14
+ }
15
+
16
+ function getAuthFile() {
17
+ return path.join(getSuiteRoot(), "auth.json");
18
+ }
19
+
20
+ function getEntitlementsFile() {
21
+ return path.join(getSuiteRoot(), "entitlements.json");
22
+ }
23
+
24
+ function getPacksRoot() {
25
+ return path.join(getSuiteRoot(), "packs");
26
+ }
27
+
28
+ function getLogsRoot() {
29
+ return path.join(getSuiteRoot(), "logs");
30
+ }
31
+
32
+ /**
33
+ * Harden file permissions so only the current user can read/write.
34
+ * - POSIX: chmod 600
35
+ * - Windows: icacls to grant only current user Full Control
36
+ * @returns {boolean} true if hardening succeeded, false otherwise.
37
+ */
38
+ function hardenFilePermissions(filePath) {
39
+ try {
40
+ if (process.platform === "win32") {
41
+ const username = os.userInfo().username;
42
+ execFileSync("icacls", [
43
+ filePath,
44
+ "/inheritance:r",
45
+ "/grant:r",
46
+ `${username}:(F)`,
47
+ ], { stdio: "ignore" });
48
+ } else {
49
+ fs.chmodSync(filePath, 0o600);
50
+ }
51
+ return true;
52
+ } catch (_err) {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ module.exports = {
58
+ ensureDir,
59
+ getSuiteRoot,
60
+ getAuthFile,
61
+ getEntitlementsFile,
62
+ getPacksRoot,
63
+ getLogsRoot,
64
+ hardenFilePermissions,
65
+ };