okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-021-iE' into f-021-int3616148bb

main
1.4 KiB20 linesraw
1import { execFileSync } from 'node:child_process';
2import { readFileSync } from 'node:fs';
3import { homedir } from 'node:os';
4import { posix, win32 } from 'node:path';
5import { utf8Script } from './powershell-utf8.mjs';
6
7/** Resolve the user's Documents folder without evaluating shell configuration. */
8export function defaultModelDirectory({platform=process.platform,home=homedir(),env=process.env,read=readFileSync,windowsDocuments=()=>execFileSync('powershell.exe',['-NoProfile','-NonInteractive','-Command',utf8Script("[Environment]::GetFolderPath('MyDocuments')")],{encoding:'utf8',timeout:5000,windowsHide:true}).trim()}={}) {
9 const path=platform==='win32'?win32:posix;
10 if(env.STRATA_MODELS_DIR)return path.resolve(env.STRATA_MODELS_DIR);
11 let documents=env.STRATA_DOCUMENTS_DIR;
12 if(!documents&&platform==='win32'){try{documents=windowsDocuments();}catch{} }
13 if(!documents&&platform==='linux'){
14  let value=env.XDG_DOCUMENTS_DIR;
15  if(!value){try{value=read(path.join(env.XDG_CONFIG_HOME||path.join(home,'.config'),'user-dirs.dirs'),'utf8').match(/^\s*XDG_DOCUMENTS_DIR="([^"\n]+)"/m)?.[1];}catch{} }
16  if(value){const expanded=value.replace(/\$\{HOME\}|\$HOME(?=\/|$)/g,home);if(!expanded.includes('$')&&path.isAbsolute(expanded))documents=expanded;}
17 }
18 if(!documents||!path.isAbsolute(documents))documents=path.join(home,'Documents');
19 return path.join(documents,'Strata','Models');
20}