insta 0.0.8 → 0.0.9
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/dist/config.js +26 -4
- package/package.json +1 -1
package/dist/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// CLI config: global (~/.insta/config.json: api url + tokens) and per-project (./.insta/project.json).
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
|
-
import { join } from 'node:path';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
4
|
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
5
5
|
const GLOBAL_DIR = join(homedir(), '.insta');
|
|
6
6
|
const GLOBAL_FILE = join(GLOBAL_DIR, 'config.json');
|
|
@@ -23,16 +23,38 @@ export async function writeGlobal(c) {
|
|
|
23
23
|
await mkdir(GLOBAL_DIR, { recursive: true });
|
|
24
24
|
await writeFile(GLOBAL_FILE, JSON.stringify(c, null, 2));
|
|
25
25
|
}
|
|
26
|
+
/** Git-style ancestor lookup: the nearest directory at-or-above `cwd` containing
|
|
27
|
+
* .insta/project.json — so "link once" works from any subdirectory of the project. */
|
|
28
|
+
export async function findProjectRoot(cwd = process.cwd()) {
|
|
29
|
+
let dir = resolve(cwd);
|
|
30
|
+
for (;;) {
|
|
31
|
+
try {
|
|
32
|
+
await readFile(join(dir, PROJECT_DIR, PROJECT_FILE), 'utf8');
|
|
33
|
+
return dir;
|
|
34
|
+
}
|
|
35
|
+
catch { /* keep climbing */ }
|
|
36
|
+
const parent = dirname(dir);
|
|
37
|
+
if (parent === dir)
|
|
38
|
+
return null; // filesystem root
|
|
39
|
+
dir = parent;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
26
42
|
export async function readProject(cwd = process.cwd()) {
|
|
43
|
+
const root = await findProjectRoot(cwd);
|
|
44
|
+
if (!root)
|
|
45
|
+
return null;
|
|
27
46
|
try {
|
|
28
|
-
return JSON.parse(await readFile(join(
|
|
47
|
+
return JSON.parse(await readFile(join(root, PROJECT_DIR, PROJECT_FILE), 'utf8'));
|
|
29
48
|
}
|
|
30
49
|
catch {
|
|
31
50
|
return null;
|
|
32
51
|
}
|
|
33
52
|
}
|
|
53
|
+
/** Writes to the existing project root when inside a linked project (branch switches from a
|
|
54
|
+
* subdirectory must not mint a nested link); a fresh `link` in an unlinked tree writes to cwd. */
|
|
34
55
|
export async function writeProject(c, cwd = process.cwd()) {
|
|
35
|
-
await
|
|
36
|
-
await
|
|
56
|
+
const target = (await findProjectRoot(cwd)) ?? cwd;
|
|
57
|
+
await mkdir(join(target, PROJECT_DIR), { recursive: true });
|
|
58
|
+
await writeFile(join(target, PROJECT_DIR, PROJECT_FILE), JSON.stringify(c, null, 2));
|
|
37
59
|
}
|
|
38
60
|
//# sourceMappingURL=config.js.map
|