import { mkdirSync, existsSync, writeFileSync, readFileSync, readdirSync } from 'node:fs'; import { join, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import { BUILTIN_DEPENDENCIES, BUILTIN_OVERRIDES, assertSafeProfile, reconcileProfile, markLockfilePending } from './reconcile-profile.mjs'; export function initProfile(home, options) { if (!home) throw new Error('DSH_HOME required'); const profile = join(home, 'profiles/web'); assertSafeProfile(profile); if (!existsSync(profile) || readdirSync(profile).length === 0) { mkdirSync(profile, { recursive: true }); writeFileSync(join(profile, 'package.json'), JSON.stringify({ name: 'dsh-profile-web', private: true, dependencies: BUILTIN_DEPENDENCIES, pnpm: { overrides: BUILTIN_OVERRIDES }, dsh: { profile: { bundles: ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app', 'dsh-web-mobile', 'dsh-free-search', 'dsh-mgr-workspace', 'dsh-chat-manager', 'dsh-preview-download'] } }, }, null, 2), { flag: 'wx', mode: 0o600 }); console.log('dsh-mgr: new web profile includes dsh-web-mobile@3.1.0 and dsh-free-search@0.9.2 and dsh-chat-manager@1.5.16-dsh-mgr.1'); } const migration = reconcileProfile(profile, options); console.log(migration.changed ? "dsh-mgr: builtin profile updated; original saved to " + migration.backupPath : "dsh-mgr: builtin profile check complete; no changes"); const manifestPath = join(profile, 'package.json'); const workspacePath = join(profile, 'pnpm-workspace.yaml'); // Backups omit workspace settings; restore host-provided peers before pnpm runs. if (existsSync(manifestPath) && !existsSync(workspacePath)) { const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')); if (['dependencies', 'devDependencies', 'optionalDependencies'].some(section => ['dsh-chat-manager', 'dsh-web-mobile', 'dsh-free-search'].some(name => manifest[section]?.[name] === BUILTIN_DEPENDENCIES[name]))) { markLockfilePending(profile); writeFileSync(workspacePath, 'packages:\n - .\n\nnodeLinker: hoisted\nautoInstallPeers: false\n', { flag: 'wx', mode: 0o600 }); } } } if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { initProfile(process.env.DSH_HOME); }