25 lines
1.6 KiB
JavaScript
25 lines
1.6 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { ownedChild, samePath } from './desktop/file-state.mjs';
|
|
|
|
// A starter project is user data. Versioned program directories remain immutable,
|
|
// and opening a new runtime version never inserts or reorders existing projects.
|
|
export function initializeExampleProject(store, appDirectory) {
|
|
const destination = path.join(store.directory, 'projects', 'starter-project');
|
|
if (store.projects.length) return store.projects.find(value => samePath(value.root, destination) || value.root.replaceAll('\\', '/').endsWith('/examples/starter-project')) ?? null;
|
|
if (!fs.existsSync(path.join(appDirectory, 'examples/starter-project'))) return null;
|
|
const source = ownedChild(appDirectory, 'examples/starter-project');
|
|
const parent = ownedChild(store.directory, 'projects', { missing: true }); fs.mkdirSync(parent, { recursive: true });
|
|
const target = ownedChild(store.directory, 'projects/starter-project', { missing: true });
|
|
if (!fs.existsSync(target)) {
|
|
const temporary = ownedChild(parent, '.starter-project-' + randomUUID(), { missing: true });
|
|
// A failed copy remains in its exclusive temporary directory for inspection;
|
|
// an existing destination is never merged with or overwritten by a template.
|
|
fs.cpSync(source, temporary, { recursive: true, errorOnExist: true, force: false });
|
|
if (fs.existsSync(target)) throw Error('示例目标目录已被另一个操作创建,保留原文件。');
|
|
fs.renameSync(temporary, target);
|
|
}
|
|
return store.addProject(target, { name: '示例工程' });
|
|
}
|