| 1186 | * the very first time a vault is opened. |
| 1187 | */ |
| 1188 | export async function ensureVaultLayout(root: string): Promise<void> { |
| 1189 | await fs.mkdir(root, { recursive: true }) |
| 1190 | const wasEmpty = await vaultLooksEmpty(root) |
| 1191 | const settings = await getVaultSettings(root) |
| 1192 | for (const f of FOLDERS) { |
| 1193 | if (f === 'inbox' && settings.primaryNotesLocation === 'root') continue |
| 1194 | await fs.mkdir(path.join(root, f), { recursive: true }) |
| 1195 | } |
| 1196 | if (wasEmpty) { |
| 1197 | const welcomeDir = await primaryNotesRoot(root) |
| 1198 | await fs.mkdir(welcomeDir, { recursive: true }) |
| 1199 | const welcomePath = path.join(welcomeDir, 'Welcome.md') |
| 1200 | try { |
| 1201 | await fs.access(welcomePath) |
| 1202 | } catch { |
| 1203 | await fs.writeFile(welcomePath, WELCOME_NOTE, 'utf8') |
| 1204 | } |
| 1205 | } |
| 1206 | if (!wasEmpty) { |
| 1207 | try { |
| 1208 | await migrateLegacyDatabases(root) |
| 1209 | } catch (err) { |
| 1210 | console.error('legacy database migration failed', err) |
| 1211 | } |
| 1212 | try { |
| 1213 | await migrateLooseAssets(root) |
| 1214 | } catch (err) { |
| 1215 | console.error('loose asset migration failed', err) |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * One-time, idempotent migration: unify assets under `assets/`. Moves loose |