@egawilldoit/factory 0.0.0-stage → 1.0.1
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/README.md +121 -2
- package/bin/factory.js +6 -0
- package/bundle/bundle.json +105 -0
- package/bundle/files/docs/authorization-and-completion-scenarios.md +21 -0
- package/bundle/files/docs/v4-acceptance-status.md +11 -0
- package/bundle/files/references/improvement-and-metrics.md +59 -0
- package/bundle/files/references/planning.md +83 -0
- package/bundle/files/references/pr-workflows.md +105 -0
- package/bundle/files/references/risk-and-delivery.md +67 -0
- package/bundle/files/references/skill-compatibility.md +11 -0
- package/bundle/files/references/synchronization.md +99 -0
- package/bundle/files/references/takeover-and-modernize.md +68 -0
- package/bundle/files/references/task-summary.md +36 -0
- package/bundle/files/references/verification-and-review.md +74 -0
- package/bundle/files/references/web-guidance.md +88 -0
- package/bundle/files/references/workflow.md +10 -0
- package/bundle/files/skills/factory/SKILL.md +96 -0
- package/bundle/files/skills/factory-improve/SKILL.md +54 -0
- package/bundle/files/skills/factory-plan/SKILL.md +47 -0
- package/bundle/files/skills/factory-verify/SKILL.md +73 -0
- package/bundle/files/templates/task-summary.yaml +21 -0
- package/lib/boundary.js +77 -0
- package/lib/bundle.js +104 -0
- package/lib/cli.js +173 -0
- package/lib/constants.js +49 -0
- package/lib/doctor.js +72 -0
- package/lib/errors.js +30 -0
- package/lib/fs-util.js +177 -0
- package/lib/install.js +84 -0
- package/lib/manifest.js +45 -0
- package/lib/ops-util.js +92 -0
- package/lib/paths.js +94 -0
- package/lib/recovery.js +247 -0
- package/lib/references.js +51 -0
- package/lib/rollback.js +78 -0
- package/lib/uninstall.js +57 -0
- package/lib/update.js +75 -0
- package/package.json +27 -4
package/lib/fs-util.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
|
|
7
|
+
function pathExists(p) {
|
|
8
|
+
try {
|
|
9
|
+
fs.accessSync(p);
|
|
10
|
+
return true;
|
|
11
|
+
} catch (_) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isDirectory(p) {
|
|
17
|
+
try {
|
|
18
|
+
return fs.statSync(p).isDirectory();
|
|
19
|
+
} catch (_) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isFile(p) {
|
|
25
|
+
try {
|
|
26
|
+
return fs.statSync(p).isFile();
|
|
27
|
+
} catch (_) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function sha256Buffer(buf) {
|
|
33
|
+
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function sha256File(p) {
|
|
37
|
+
return sha256Buffer(fs.readFileSync(p));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function ensureDir(dir) {
|
|
41
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function readJsonOrNull(p) {
|
|
45
|
+
if (!isFile(p)) return null;
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(fs.readFileSync(p, 'utf8'));
|
|
48
|
+
} catch (_) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function writeFileAtomic(p, data) {
|
|
54
|
+
ensureDir(path.dirname(p));
|
|
55
|
+
const tmp = path.join(
|
|
56
|
+
path.dirname(p),
|
|
57
|
+
`.${path.basename(p)}.tmp-${process.pid}-${crypto.randomBytes(4).toString('hex')}`
|
|
58
|
+
);
|
|
59
|
+
try {
|
|
60
|
+
fs.writeFileSync(tmp, data);
|
|
61
|
+
fs.renameSync(tmp, p);
|
|
62
|
+
} finally {
|
|
63
|
+
if (pathExists(tmp)) {
|
|
64
|
+
try { fs.unlinkSync(tmp); } catch (_) {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function writeJsonAtomic(p, obj) {
|
|
70
|
+
writeFileAtomic(p, JSON.stringify(obj, null, 2) + '\n');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function copyFileAtomic(src, dst) {
|
|
74
|
+
ensureDir(path.dirname(dst));
|
|
75
|
+
writeFileAtomic(dst, fs.readFileSync(src));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function removeTree(p) {
|
|
79
|
+
if (!pathExists(p)) return;
|
|
80
|
+
fs.rmSync(p, { recursive: true, force: true });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Remove everything inside dir but keep dir itself.
|
|
84
|
+
function emptyDir(dir) {
|
|
85
|
+
if (!isDirectory(dir)) return;
|
|
86
|
+
for (const name of fs.readdirSync(dir)) {
|
|
87
|
+
removeTree(path.join(dir, name));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Copy a tree of regular files; returns the list of relative paths copied.
|
|
92
|
+
function copyTree(src, dst) {
|
|
93
|
+
const copied = [];
|
|
94
|
+
if (!isDirectory(src)) return copied;
|
|
95
|
+
const walk = (cwd, rel) => {
|
|
96
|
+
for (const name of fs.readdirSync(cwd).sort()) {
|
|
97
|
+
const from = path.join(cwd, name);
|
|
98
|
+
const toRel = rel ? path.join(rel, name) : name;
|
|
99
|
+
const st = fs.statSync(from);
|
|
100
|
+
if (st.isDirectory()) {
|
|
101
|
+
walk(from, toRel);
|
|
102
|
+
} else if (st.isFile()) {
|
|
103
|
+
copyFileAtomic(from, path.join(dst, toRel));
|
|
104
|
+
copied.push(toRel.split(path.sep).join('/'));
|
|
105
|
+
}
|
|
106
|
+
// Symlinks and special files are intentionally not copied: the bundle
|
|
107
|
+
// contains only regular files and portable relative links.
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
walk(src, '');
|
|
111
|
+
return copied;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function listFiles(root) {
|
|
115
|
+
const out = [];
|
|
116
|
+
if (!isDirectory(root)) return out;
|
|
117
|
+
const walk = (cwd, rel) => {
|
|
118
|
+
for (const name of fs.readdirSync(cwd).sort()) {
|
|
119
|
+
const p = path.join(cwd, name);
|
|
120
|
+
const toRel = rel ? path.join(rel, name) : name;
|
|
121
|
+
const st = fs.statSync(p);
|
|
122
|
+
if (st.isDirectory()) walk(p, toRel);
|
|
123
|
+
else if (st.isFile()) out.push(toRel.split(path.sep).join('/'));
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
walk(root, '');
|
|
127
|
+
return out.sort();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function moveFile(src, dst) {
|
|
131
|
+
ensureDir(path.dirname(dst));
|
|
132
|
+
try {
|
|
133
|
+
fs.renameSync(src, dst);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
if (err.code === 'EXDEV') {
|
|
136
|
+
copyFileAtomic(src, dst);
|
|
137
|
+
fs.unlinkSync(src);
|
|
138
|
+
} else {
|
|
139
|
+
throw err;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Remove empty directories under root, deepest first, optionally skipping root.
|
|
145
|
+
function pruneEmptyDirs(root, skip) {
|
|
146
|
+
if (!isDirectory(root)) return;
|
|
147
|
+
const skipSet = new Set(skip || []);
|
|
148
|
+
const walk = (dir) => {
|
|
149
|
+
for (const name of fs.readdirSync(dir)) {
|
|
150
|
+
const p = path.join(dir, name);
|
|
151
|
+
if (isDirectory(p) && !skipSet.has(p)) {
|
|
152
|
+
walk(p);
|
|
153
|
+
try { fs.rmdirSync(p); } catch (_) {}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
walk(root);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = {
|
|
161
|
+
pathExists,
|
|
162
|
+
isDirectory,
|
|
163
|
+
isFile,
|
|
164
|
+
sha256Buffer,
|
|
165
|
+
sha256File,
|
|
166
|
+
ensureDir,
|
|
167
|
+
readJsonOrNull,
|
|
168
|
+
writeFileAtomic,
|
|
169
|
+
writeJsonAtomic,
|
|
170
|
+
copyFileAtomic,
|
|
171
|
+
removeTree,
|
|
172
|
+
emptyDir,
|
|
173
|
+
copyTree,
|
|
174
|
+
listFiles,
|
|
175
|
+
moveFile,
|
|
176
|
+
pruneEmptyDirs,
|
|
177
|
+
};
|
package/lib/install.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fsu = require('./fs-util');
|
|
4
|
+
const { readManifest, buildManifest } = require('./manifest');
|
|
5
|
+
const { checkInstalledIntegrity, runRecovery, acquireLock, releaseLock, updateLockPhase } = require('./recovery');
|
|
6
|
+
const { stageBundle, writePendingManifest, replaceManagedFiles, clearTransientFiles } = require('./ops-util');
|
|
7
|
+
const { installedPath, detectConflicts } = require('./paths');
|
|
8
|
+
const { refuse } = require('./errors');
|
|
9
|
+
const { assertInstallationBoundary } = require('./boundary');
|
|
10
|
+
const { EXIT } = require('./constants');
|
|
11
|
+
|
|
12
|
+
function install({ paths, bundle, opts }) {
|
|
13
|
+
assertInstallationBoundary(paths, bundle.descriptor.files.map((f) => f.path));
|
|
14
|
+
const recovery = runRecovery(paths);
|
|
15
|
+
const existing = readManifest(paths);
|
|
16
|
+
|
|
17
|
+
if (existing) {
|
|
18
|
+
const check = checkInstalledIntegrity(paths, existing);
|
|
19
|
+
if (!check.ok) {
|
|
20
|
+
throw refuse(
|
|
21
|
+
'An existing installation is modified or incomplete; refusing to overwrite it.',
|
|
22
|
+
`Run doctor for details. Installed version: ${existing.version}.`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
if (existing.version === bundle.version) {
|
|
26
|
+
return {
|
|
27
|
+
ok: true,
|
|
28
|
+
changed: false,
|
|
29
|
+
version: existing.version,
|
|
30
|
+
recovery,
|
|
31
|
+
notes: ['Already installed at this version; nothing to do.'],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
throw refuse(
|
|
35
|
+
`Installed version ${existing.version} differs from requested ${bundle.version}.`,
|
|
36
|
+
'Use the update command to change versions.'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const collisions = [];
|
|
41
|
+
for (const entry of bundle.descriptor.files) {
|
|
42
|
+
if (fsu.pathExists(installedPath(paths, entry.path))) collisions.push(entry.path);
|
|
43
|
+
}
|
|
44
|
+
if (fsu.pathExists(paths.metaDir)) collisions.push('.ega-factory (incomplete installation state)');
|
|
45
|
+
if (collisions.length) {
|
|
46
|
+
throw refuse(
|
|
47
|
+
'Refusing to install over files not owned by @egawilldoit/factory.',
|
|
48
|
+
'Conflicting paths:\n ' + collisions.join('\n ') +
|
|
49
|
+
'\nUse --destination for an isolated install, or move the existing files aside.'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const projectConflicts = detectConflicts(process.cwd());
|
|
54
|
+
|
|
55
|
+
acquireLock(paths, 'install');
|
|
56
|
+
try {
|
|
57
|
+
const manifest = buildManifest(bundle, opts.host, paths.dest);
|
|
58
|
+
writePendingManifest(paths, manifest);
|
|
59
|
+
stageBundle(bundle, paths);
|
|
60
|
+
updateLockPhase(paths, 'replacing');
|
|
61
|
+
replaceManagedFiles(paths, paths.stagingDir, null, manifest);
|
|
62
|
+
fsu.writeJsonAtomic(paths.manifestPath, manifest);
|
|
63
|
+
clearTransientFiles(paths);
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
ok: true,
|
|
67
|
+
changed: true,
|
|
68
|
+
version: bundle.version,
|
|
69
|
+
sourceCommit: bundle.sourceCommit,
|
|
70
|
+
recovery,
|
|
71
|
+
notes: projectConflicts.length
|
|
72
|
+
? ['Advisory: same-named skills also exist in this checkout:\n ' + projectConflicts.join('\n ')]
|
|
73
|
+
: [],
|
|
74
|
+
exitCode: EXIT.OK,
|
|
75
|
+
};
|
|
76
|
+
} catch (err) {
|
|
77
|
+
try { runRecovery(paths); } catch (_) {}
|
|
78
|
+
throw err;
|
|
79
|
+
} finally {
|
|
80
|
+
releaseLock(paths);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = { install };
|
package/lib/manifest.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fsu = require('./fs-util');
|
|
4
|
+
const { integrity } = require('./errors');
|
|
5
|
+
|
|
6
|
+
function validateManifest(manifest, where) {
|
|
7
|
+
const bad = (msg) => { throw integrity(`Invalid installation manifest at ${where}: ${msg}`); };
|
|
8
|
+
if (!manifest || typeof manifest !== 'object') bad('not an object');
|
|
9
|
+
if (manifest.schemaVersion !== 1) bad('unsupported schemaVersion');
|
|
10
|
+
if (typeof manifest.version !== 'string') bad('missing version');
|
|
11
|
+
if (!manifest.files || typeof manifest.files !== 'object' || Array.isArray(manifest.files)) bad('missing files map');
|
|
12
|
+
for (const [rel, hash] of Object.entries(manifest.files)) {
|
|
13
|
+
if (typeof hash !== 'string' || !/^[a-f0-9]{64}$/.test(hash)) bad(`bad hash for ${rel}`);
|
|
14
|
+
if (rel.includes('..') || rel.startsWith('/')) bad(`unsafe path ${rel}`);
|
|
15
|
+
}
|
|
16
|
+
return manifest;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readManifest(paths) {
|
|
20
|
+
const raw = fsu.readJsonOrNull(paths.manifestPath);
|
|
21
|
+
return raw ? validateManifest(raw, paths.manifestPath) : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function buildManifest(bundle, host, dest, extra) {
|
|
25
|
+
const files = {};
|
|
26
|
+
for (const entry of bundle.descriptor.files) files[entry.path] = entry.sha256;
|
|
27
|
+
const now = new Date().toISOString();
|
|
28
|
+
return {
|
|
29
|
+
schemaVersion: 1,
|
|
30
|
+
package: bundle.descriptor.name || '@egawilldoit/factory',
|
|
31
|
+
version: bundle.version,
|
|
32
|
+
sourceCommit: bundle.sourceCommit,
|
|
33
|
+
host: host || 'shared',
|
|
34
|
+
destination: dest,
|
|
35
|
+
installedAt: (extra && extra.installedAt) || now,
|
|
36
|
+
updatedAt: (extra && extra.updatedAt) || now,
|
|
37
|
+
files,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function writeManifest(paths, manifest) {
|
|
42
|
+
fsu.writeJsonAtomic(paths.manifestPath, manifest);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = { validateManifest, readManifest, buildManifest, writeManifest };
|
package/lib/ops-util.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fsu = require('./fs-util');
|
|
6
|
+
const { completeSet } = require('./recovery');
|
|
7
|
+
const { installedPath } = require('./paths');
|
|
8
|
+
const { integrity, refuse } = require('./errors');
|
|
9
|
+
const { assertManagedBoundary } = require('./boundary');
|
|
10
|
+
|
|
11
|
+
function stageBundle(bundle, paths) {
|
|
12
|
+
fsu.removeTree(paths.stagingDir);
|
|
13
|
+
fsu.ensureDir(paths.stagingDir);
|
|
14
|
+
for (const entry of bundle.descriptor.files) {
|
|
15
|
+
fsu.copyFileAtomic(
|
|
16
|
+
path.join(bundle.filesRoot, entry.path),
|
|
17
|
+
path.join(paths.stagingDir, entry.path)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
const asManifest = { files: {} };
|
|
21
|
+
for (const entry of bundle.descriptor.files) asManifest.files[entry.path] = entry.sha256;
|
|
22
|
+
if (!completeSet(paths.stagingDir, asManifest)) {
|
|
23
|
+
throw integrity('Staged bundle failed hash verification.', paths.stagingDir);
|
|
24
|
+
}
|
|
25
|
+
return paths.stagingDir;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function writePendingManifest(paths, manifest) {
|
|
29
|
+
fsu.writeJsonAtomic(paths.pendingManifest, manifest);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function removeManagedFiles(paths, manifest) {
|
|
33
|
+
if (!manifest) return;
|
|
34
|
+
for (const rel of Object.keys(manifest.files)) {
|
|
35
|
+
const p = installedPath(paths, rel);
|
|
36
|
+
if (fsu.isFile(p)) fs.unlinkSync(p);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function replaceManagedFiles(paths, sourceRoot, currentManifest, newManifest) {
|
|
41
|
+
assertManagedBoundary(paths, currentManifest);
|
|
42
|
+
assertManagedBoundary(paths, newManifest);
|
|
43
|
+
for (const rel of Object.keys(newManifest.files)) {
|
|
44
|
+
if (!Object.hasOwn(currentManifest?.files || {}, rel) && fsu.pathExists(installedPath(paths, rel))) {
|
|
45
|
+
throw refuse('Refusing to replace a file not owned by @egawilldoit/factory.', rel);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
removeManagedFiles(paths, currentManifest);
|
|
49
|
+
for (const rel of Object.keys(newManifest.files)) {
|
|
50
|
+
fsu.copyFileAtomic(path.join(sourceRoot, rel), installedPath(paths, rel));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function savePrevious(paths, currentManifest) {
|
|
55
|
+
fsu.removeTree(paths.previousDir);
|
|
56
|
+
fsu.ensureDir(paths.previousDir);
|
|
57
|
+
for (const rel of Object.keys(currentManifest.files)) {
|
|
58
|
+
fsu.copyFileAtomic(installedPath(paths, rel), path.join(paths.previousDir, rel));
|
|
59
|
+
}
|
|
60
|
+
fsu.writeJsonAtomic(path.join(paths.previousDir, 'manifest.json'), currentManifest);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function clearTransientFiles(paths) {
|
|
64
|
+
fsu.removeTree(paths.stagingDir);
|
|
65
|
+
if (fsu.pathExists(paths.pendingManifest)) {
|
|
66
|
+
try { fs.unlinkSync(paths.pendingManifest); } catch (_) {}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Keep a copy of locally modified owned files before anything overwrites or
|
|
71
|
+
// deletes them. One timestamped directory; no separate tracking machinery.
|
|
72
|
+
function preserveModified(paths, manifest, issues) {
|
|
73
|
+
const modified = issues.filter((i) => i.kind === 'modified');
|
|
74
|
+
if (modified.length === 0) return null;
|
|
75
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
76
|
+
const dir = path.join(paths.preservedDir, `${manifest.version}-${stamp}`);
|
|
77
|
+
for (const issue of modified) {
|
|
78
|
+
const src = installedPath(paths, issue.path);
|
|
79
|
+
if (fsu.isFile(src)) fsu.copyFileAtomic(src, path.join(dir, issue.path));
|
|
80
|
+
}
|
|
81
|
+
return dir;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
stageBundle,
|
|
86
|
+
writePendingManifest,
|
|
87
|
+
removeManagedFiles,
|
|
88
|
+
replaceManagedFiles,
|
|
89
|
+
savePrevious,
|
|
90
|
+
clearTransientFiles,
|
|
91
|
+
preserveModified,
|
|
92
|
+
};
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const fsu = require('./fs-util');
|
|
6
|
+
const { META_DIR, PRESERVED_DIR, defaultDestination, usage } = require('./constants');
|
|
7
|
+
|
|
8
|
+
function isDefaultDestination(dest) {
|
|
9
|
+
const def = defaultDestination();
|
|
10
|
+
return !!def && path.normalize(dest) === path.normalize(def);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Canonicalise the destination once, including symlinked parents, even when
|
|
14
|
+
// the destination itself does not exist yet. Boundary checks then start from
|
|
15
|
+
// the canonical root and only inspect paths below it.
|
|
16
|
+
function canonicalize(dest) {
|
|
17
|
+
let current = dest;
|
|
18
|
+
const tail = [];
|
|
19
|
+
while (!fsu.pathExists(current)) {
|
|
20
|
+
const parent = path.dirname(current);
|
|
21
|
+
if (parent === current) break;
|
|
22
|
+
tail.unshift(path.basename(current));
|
|
23
|
+
current = parent;
|
|
24
|
+
}
|
|
25
|
+
try { current = fs.realpathSync(current); } catch (_) {}
|
|
26
|
+
return tail.length ? path.join(current, ...tail) : current;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function resolveDestination(opts) {
|
|
30
|
+
let dest = opts.destination || defaultDestination();
|
|
31
|
+
if (!dest) {
|
|
32
|
+
throw usage('No destination given and HOME/USERPROFILE is not set.', 'Pass --destination /absolute/path.');
|
|
33
|
+
}
|
|
34
|
+
if (!path.isAbsolute(dest)) dest = path.resolve(process.cwd(), dest);
|
|
35
|
+
dest = path.normalize(dest);
|
|
36
|
+
if (fsu.pathExists(dest) && !fsu.isDirectory(dest)) {
|
|
37
|
+
throw usage(`Destination is not a directory: ${dest}`);
|
|
38
|
+
}
|
|
39
|
+
return canonicalize(dest);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function makePaths(dest) {
|
|
43
|
+
const metaDir = path.join(dest, META_DIR);
|
|
44
|
+
return {
|
|
45
|
+
dest,
|
|
46
|
+
skillsRoot: path.join(dest, 'skills'),
|
|
47
|
+
metaDir,
|
|
48
|
+
manifestPath: path.join(metaDir, 'manifest.json'),
|
|
49
|
+
lockFile: path.join(metaDir, 'lock.json'),
|
|
50
|
+
stagingDir: path.join(metaDir, 'staging'),
|
|
51
|
+
pendingManifest: path.join(metaDir, 'pending-manifest.json'),
|
|
52
|
+
previousDir: path.join(metaDir, 'previous'),
|
|
53
|
+
previousManifest: path.join(metaDir, 'previous', 'manifest.json'),
|
|
54
|
+
preservedDir: path.join(dest, PRESERVED_DIR),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function installedPath(paths, rel) {
|
|
59
|
+
return path.join(paths.dest, rel);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function hostHint(host, skillsRoot, dest) {
|
|
63
|
+
if (isDefaultDestination(dest)) {
|
|
64
|
+
return `Skills are installed at ${skillsRoot}, which both Codex and OpenCode auto-discover. Restart the host or start a fresh session to load them.`;
|
|
65
|
+
}
|
|
66
|
+
const label = host === 'codex' ? 'Codex' : host === 'opencode' ? 'OpenCode' : 'Your host';
|
|
67
|
+
return `${label}: this custom destination is not auto-discovered. Register ${skillsRoot} as a skills directory in the host's project config, then restart or start a fresh session.`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Look for a check-out / plugin installation of the same skills near the cwd.
|
|
71
|
+
// This is advisory: it explains a conflict instead of silently making a second
|
|
72
|
+
// competing copy. It never edits the detected project.
|
|
73
|
+
function detectConflicts(cwd) {
|
|
74
|
+
const found = [];
|
|
75
|
+
const skillFile = path.join(cwd, 'skills', 'factory', 'SKILL.md');
|
|
76
|
+
if (fsu.isFile(skillFile)) found.push(`project skills at ${path.dirname(path.dirname(skillFile))}`);
|
|
77
|
+
|
|
78
|
+
const plugin = fsu.readJsonOrNull(path.join(cwd, '.codex-plugin', 'plugin.json'));
|
|
79
|
+
if (plugin && plugin.skills) found.push(`Codex plugin at ${path.join(cwd, '.codex-plugin')}`);
|
|
80
|
+
|
|
81
|
+
const opencode = fsu.readJsonOrNull(path.join(cwd, '.opencode', 'opencode.json'));
|
|
82
|
+
if (opencode && Array.isArray(opencode.skills)) found.push(`OpenCode config at ${path.join(cwd, '.opencode', 'opencode.json')}`);
|
|
83
|
+
|
|
84
|
+
return found;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = {
|
|
88
|
+
resolveDestination,
|
|
89
|
+
makePaths,
|
|
90
|
+
installedPath,
|
|
91
|
+
isDefaultDestination,
|
|
92
|
+
hostHint,
|
|
93
|
+
detectConflicts,
|
|
94
|
+
};
|