u
This commit is contained in:
@@ -3,6 +3,7 @@ import path from 'path';
|
||||
import { getStore } from '../db/store.js';
|
||||
import env from '../config/env.js';
|
||||
import { getDefaultSkills } from './skill.js';
|
||||
import { getDefaultApplets, DEFAULT_APPLETS_VERSION, APPLET_EXT } from './applet.js';
|
||||
|
||||
const FULL_HASH_REGEX = /^[a-zA-Z0-9]{8}$/;
|
||||
const VIRTUAL_IMAGE_FOLDERS = new Set(['image', 'images']);
|
||||
@@ -164,7 +165,39 @@ export async function loadDefaultSkills({ googleAuthId, store: passedStore, vfsR
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the VFS for the selected company with default SKILL files.
|
||||
* Loads the shipped example `.applet.js` files into the selected company's VFS.
|
||||
*
|
||||
* Version-gated: each example is written under a `v1-` prefixed name and only when that exact
|
||||
* file is absent, so an operator's edits survive and a new example revision ships as a new file.
|
||||
* Must stay in step with {@link loadDefaultSkills}, which runs alongside it.
|
||||
*
|
||||
* @param {{ googleAuthId: number, store?: object, vfsRoot?: string }} ctx
|
||||
* @returns {Promise<Array<string>>} List of seeded applet file names
|
||||
*/
|
||||
export async function loadDefaultApplets({ googleAuthId, store: passedStore, vfsRoot: passedVfsRoot } = {}) {
|
||||
const store = passedStore || await getStore();
|
||||
const vfsRoot = passedVfsRoot || await vfsRootFor(store, googleAuthId);
|
||||
fs.mkdirSync(vfsRoot, { recursive: true });
|
||||
|
||||
const seeded = [];
|
||||
for (const applet of getDefaultApplets()) {
|
||||
const name = `${DEFAULT_APPLETS_VERSION}-${applet.name}`;
|
||||
try {
|
||||
const diskPath = path.resolve(vfsRoot, name);
|
||||
if (!fs.existsSync(diskPath)) {
|
||||
fs.writeFileSync(diskPath, applet.content, 'utf8');
|
||||
}
|
||||
seeded.push(name);
|
||||
} catch (err) {
|
||||
console.error(`[vfs] failed to write default applet "${name}" to disk:`, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
return seeded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the VFS for the selected company with default SKILL files and example applets.
|
||||
*
|
||||
* @param {{ googleAuthId: number, store?: object, vfsRoot?: string }} ctx
|
||||
* @returns {Promise<string>} vfsRoot directory path
|
||||
@@ -175,11 +208,13 @@ export async function initCompanyVfs({ googleAuthId, store: passedStore, vfsRoot
|
||||
fs.mkdirSync(vfsRoot, { recursive: true });
|
||||
|
||||
await loadDefaultSkills({ googleAuthId, store, vfsRoot });
|
||||
await loadDefaultApplets({ googleAuthId, store, vfsRoot });
|
||||
return vfsRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the local directory for this company's VFS exists and default skills are loaded.
|
||||
* Ensure the local directory for this company's VFS exists and the default skills and example
|
||||
* applets are loaded.
|
||||
*
|
||||
* @param {{ googleAuthId: number }} ctx
|
||||
* @returns {Promise<string>} vfsRoot directory path
|
||||
@@ -195,6 +230,12 @@ export async function prepareVfs({ googleAuthId }) {
|
||||
console.warn('[vfs] prepare warning:', err.message);
|
||||
}
|
||||
|
||||
try {
|
||||
await loadDefaultApplets({ googleAuthId, store, vfsRoot });
|
||||
} catch (err) {
|
||||
console.warn('[vfs] prepare applet warning:', err.message);
|
||||
}
|
||||
|
||||
return vfsRoot;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user