@handsupmin/gc-tree 0.8.1 → 0.8.2
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/src/settings.js +55 -3
- package/package.json +1 -1
package/dist/src/settings.js
CHANGED
|
@@ -1,9 +1,59 @@
|
|
|
1
|
-
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { access, mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
2
5
|
import { settingsPath } from './paths.js';
|
|
6
|
+
async function pathExists(target) {
|
|
7
|
+
try {
|
|
8
|
+
await access(target, constants.F_OK);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function isTemporaryScaffoldTarget(targetDir) {
|
|
16
|
+
if (!targetDir)
|
|
17
|
+
return false;
|
|
18
|
+
const normalized = resolve(targetDir);
|
|
19
|
+
const tempRoot = resolve(tmpdir());
|
|
20
|
+
return normalized.startsWith(tempRoot) || normalized.includes('/gctree-');
|
|
21
|
+
}
|
|
22
|
+
async function sanitizeScaffoldedHosts(records) {
|
|
23
|
+
const input = records || [];
|
|
24
|
+
const next = [];
|
|
25
|
+
for (const record of input) {
|
|
26
|
+
if (record.scope === 'local') {
|
|
27
|
+
if (!record.target_dir)
|
|
28
|
+
continue;
|
|
29
|
+
if (isTemporaryScaffoldTarget(record.target_dir))
|
|
30
|
+
continue;
|
|
31
|
+
if (!(await pathExists(record.target_dir)))
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (next.some((entry) => entry.host === record.host &&
|
|
35
|
+
entry.scope === record.scope &&
|
|
36
|
+
entry.target_dir === record.target_dir)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
next.push(record);
|
|
40
|
+
}
|
|
41
|
+
return next;
|
|
42
|
+
}
|
|
3
43
|
export async function readSettings(home) {
|
|
4
44
|
try {
|
|
5
45
|
const raw = await readFile(settingsPath(home), 'utf8');
|
|
6
|
-
|
|
46
|
+
const parsed = JSON.parse(raw);
|
|
47
|
+
const sanitizedHosts = await sanitizeScaffoldedHosts(parsed.scaffolded_hosts);
|
|
48
|
+
const hostsChanged = JSON.stringify(sanitizedHosts) !== JSON.stringify(parsed.scaffolded_hosts || []);
|
|
49
|
+
const next = hostsChanged
|
|
50
|
+
? { ...parsed, scaffolded_hosts: sanitizedHosts, updated_at: new Date().toISOString() }
|
|
51
|
+
: { ...parsed, scaffolded_hosts: sanitizedHosts };
|
|
52
|
+
if (hostsChanged) {
|
|
53
|
+
await mkdir(home, { recursive: true });
|
|
54
|
+
await writeFile(settingsPath(home), `${JSON.stringify(next, null, 2)}\n`, 'utf8');
|
|
55
|
+
}
|
|
56
|
+
return next;
|
|
7
57
|
}
|
|
8
58
|
catch {
|
|
9
59
|
return null;
|
|
@@ -22,10 +72,12 @@ export async function writeSettings({ home, providerMode, preferredProvider, pre
|
|
|
22
72
|
return settings;
|
|
23
73
|
}
|
|
24
74
|
export async function appendScaffoldedHost(home, record) {
|
|
75
|
+
if (record.scope === 'local' && isTemporaryScaffoldTarget(record.target_dir))
|
|
76
|
+
return;
|
|
25
77
|
const settings = await readSettings(home);
|
|
26
78
|
if (!settings)
|
|
27
79
|
return;
|
|
28
|
-
const existing = settings.scaffolded_hosts
|
|
80
|
+
const existing = await sanitizeScaffoldedHosts(settings.scaffolded_hosts);
|
|
29
81
|
const filtered = existing.filter((h) => !(h.host === record.host && h.scope === record.scope && h.target_dir === record.target_dir));
|
|
30
82
|
filtered.push({ ...record, scaffolded_at: new Date().toISOString() });
|
|
31
83
|
const updated = { ...settings, scaffolded_hosts: filtered, updated_at: new Date().toISOString() };
|