更新 lib/skill-sync.js
This commit is contained in:
parent
f54408e2c7
commit
12044dc45f
|
|
@ -103,6 +103,9 @@ export function parseSkillFrontmatter(markdown) {
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
async function isDirectory(path) {
|
||||||
|
return stat(path).then((value) => value.isDirectory()).catch(() => false);
|
||||||
|
}
|
||||||
export class SkillSync {
|
export class SkillSync {
|
||||||
deps;
|
deps;
|
||||||
running = false;
|
running = false;
|
||||||
|
|
@ -121,6 +124,10 @@ export class SkillSync {
|
||||||
const dir = this.deps.settings().skillsDir;
|
const dir = this.deps.settings().skillsDir;
|
||||||
return dir !== '' ? resolve(dir) : join(dshHome(), 'skills');
|
return dir !== '' ? resolve(dir) : join(dshHome(), 'skills');
|
||||||
}
|
}
|
||||||
|
/** The parking area where disabled skills wait, outside the root. */
|
||||||
|
holdingRoot() {
|
||||||
|
return this.deps.holdingDir?.() ?? join(dshHome(), 'skills-disabled');
|
||||||
|
}
|
||||||
statePath() {
|
statePath() {
|
||||||
return join(dshHome(), 'cloud-memory-skills.json');
|
return join(dshHome(), 'cloud-memory-skills.json');
|
||||||
}
|
}
|
||||||
|
|
@ -286,7 +293,11 @@ export class SkillSync {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const dir = join(root, item.name);
|
const dir = join(root, item.name);
|
||||||
const exists = await stat(dir).then((s) => s.isDirectory()).catch(() => false);
|
const parkedDir = join(this.holdingRoot(), item.name);
|
||||||
|
const inRoot = await isDirectory(dir);
|
||||||
|
const parked = !inRoot && (await isDirectory(parkedDir));
|
||||||
|
const home = parked ? parkedDir : dir;
|
||||||
|
const exists = inRoot || parked;
|
||||||
const removed = state.removed[item.name];
|
const removed = state.removed[item.name];
|
||||||
if (removed !== undefined && exists) {
|
if (removed !== undefined && exists) {
|
||||||
// The user put it back by hand; the removal is over.
|
// The user put it back by hand; the removal is over.
|
||||||
|
|
@ -303,10 +314,10 @@ export class SkillSync {
|
||||||
delete state.removed[item.name];
|
delete state.removed[item.name];
|
||||||
stateDirty = true;
|
stateDirty = true;
|
||||||
}
|
}
|
||||||
const delivery = await this.readDelivery(dir);
|
const delivery = await this.readDelivery(home);
|
||||||
if (exists && delivery !== undefined) {
|
if (exists && delivery !== undefined) {
|
||||||
// Delivered before: local integrity is judged before any download.
|
// Delivered before: local integrity is judged before any download.
|
||||||
if (!(await this.deliveredIntact(dir, delivery))) {
|
if (!(await this.deliveredIntact(home, delivery))) {
|
||||||
this.conflict(stats, item.name, 'locally_modified');
|
this.conflict(stats, item.name, 'locally_modified');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -342,7 +353,7 @@ export class SkillSync {
|
||||||
this.conflict(stats, item.name, 'script_review_required');
|
this.conflict(stats, item.name, 'script_review_required');
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
await this.install(item, bundle.data, root);
|
await this.install(item, bundle.data, parked ? this.holdingRoot() : root);
|
||||||
state.delivered[item.name] = { manifest: bundle.data.manifest_sha256 };
|
state.delivered[item.name] = { manifest: bundle.data.manifest_sha256 };
|
||||||
stateDirty = true;
|
stateDirty = true;
|
||||||
(exists ? stats.updated : stats.downloaded).push(item.name);
|
(exists ? stats.updated : stats.downloaded).push(item.name);
|
||||||
|
|
@ -429,17 +440,23 @@ export class SkillSync {
|
||||||
stats.updated.push(skill.name);
|
stats.updated.push(skill.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A vanished local folder is remembered as a deliberate removal (so
|
// Presence counts the parking area too: a parked skill is disabled,
|
||||||
// the next pass does not resurrect it); the cloud copy always stays.
|
// not deleted. Anything missing from both is a deliberate removal
|
||||||
|
// (remembered, so it is not resurrected); the cloud copy stays.
|
||||||
|
const presentFolders = new Set(seenFolders);
|
||||||
|
for (const entry of await readdir(this.holdingRoot(), { withFileTypes: true }).catch(() => [])) {
|
||||||
|
if (entry.isDirectory())
|
||||||
|
presentFolders.add(entry.name);
|
||||||
|
}
|
||||||
for (const [name, record] of Object.entries(state.uploaded)) {
|
for (const [name, record] of Object.entries(state.uploaded)) {
|
||||||
if (!seenFolders.has(record.folder)) {
|
if (!presentFolders.has(record.folder)) {
|
||||||
state.removed[name] = { manifest: cloudByName.get(name)?.manifest_sha256 ?? record.manifest };
|
state.removed[name] = { manifest: cloudByName.get(name)?.manifest_sha256 ?? record.manifest };
|
||||||
delete state.uploaded[name];
|
delete state.uploaded[name];
|
||||||
stateDirty = true;
|
stateDirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const [name, record] of Object.entries(state.delivered)) {
|
for (const [name, record] of Object.entries(state.delivered)) {
|
||||||
if (!seenFolders.has(name)) {
|
if (!presentFolders.has(name)) {
|
||||||
state.removed[name] = { manifest: record.manifest };
|
state.removed[name] = { manifest: record.manifest };
|
||||||
delete state.delivered[name];
|
delete state.delivered[name];
|
||||||
stateDirty = true;
|
stateDirty = true;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue