Treasury & Bank Transfer Management System — سامانه مدیریت خزانه‌داری و حواله‌های بانکی

1 import {cpSync,existsSync,mkdirSync,readFileSync} from 'node:fs';
2 import {join,resolve,sep} from 'node:path';
3 import {createRequire} from 'node:module';
4
5 const root=process.cwd();
6 const standalone=join(root,'.next','standalone');
7 if(!existsSync(join(standalone,'server.js'))) {
8 throw new Error('Next standalone server.js missing; run npm run build first');
9 }
10
11 const publicDir=join(root,'public');
12 if(existsSync(publicDir))cpSync(publicDir,join(standalone,'public'),{recursive:true});
13
14 const nextPackage=JSON.parse(readFileSync(join(root,'node_modules','next','package.json'),'utf8'));
15 const mustPackage=['next','react','react-dom'];
16 const runtimePackages=new Set([
17 ...mustPackage,
18 ...Object.keys(nextPackage.dependencies||{}),
19 ...Object.keys(nextPackage.optionalDependencies||{})
20 ]);
21
22 const copied=[];
23 for(const name of runtimePackages){
24 if(!/^(?:@[a-z0-9._-]+\/)?[a-z0-9._-]+$/i.test(name))throw Error('INVALID_RUNTIME_PACKAGE_NAME');
25 const components=name.split('/');
26 const source=join(root,'node_modules',...components);
27 if(!existsSync(join(source,'package.json'))){
28 if(mustPackage.includes(name))throw Error('REQUIRED_NODE_RUNTIME_PACKAGE_MISSING_'+name);
29 continue;
30 }
31 const destination=join(standalone,'node_modules',...components);
32 mkdirSync(destination,{recursive:true});
33 // Next 16 on Windows can omit parts of next from output tracing.
34 // Merge verified lockfile-installed packages, not an arbitrary network fetch.
35 cpSync(source,destination,{recursive:true,force:true,dereference:true});
36 copied.push(name);
37 }
38
39 const standaloneRequire=createRequire(join(standalone,'server.js'));
40 for(const name of mustPackage){
41 const actual=resolve(standaloneRequire.resolve(name));
42 const expectedRoot=resolve(join(standalone,'node_modules',...name.split('/')));
43 if(actual!==expectedRoot && !actual.startsWith(expectedRoot+sep)) {
44 throw Error('STANDALONE_MODULE_ESCAPED_PACKAGE_'+name);
45 }
46 }
47 console.log('standalone-package: READY, verified local Next/React runtime packages:',copied.join(', '));