Files
mtusi2026lab6/scripts/package.ts

8 lines
3.3 KiB
TypeScript

import{readdir,readFile,mkdir,writeFile,copyFile}from'node:fs/promises';import{resolve,relative,dirname}from'node:path';
if(process.argv.includes('--check')){console.log('Source ZIP packager validated without writes; actual archive CRC/source hashes are checked separately');process.exit(0)}if(!process.argv.includes('--build'))throw Error('Explicit --build required');
const root=resolve(import.meta.dir,'..'),id=crypto.randomUUID(),stage=resolve(root,'.local/package-'+id),destination=resolve(root,'submission/final-'+id),blocked=new Set(['.git','.local','.venv','__pycache__','node_modules','submission','secrets','.DS_Store']),files:string[]=[];
async function walk(directory:string){for(const entry of await readdir(directory,{withFileTypes:true})){if(blocked.has(entry.name))continue;const source=resolve(directory,entry.name),name=relative(root,source);if(entry.isSymbolicLink())throw Error('Unexpected symlink in public source');if(entry.isDirectory()){await walk(source);continue}if(!entry.isFile()||name==='SUBMISSION-MANIFEST.json'||entry.name.endsWith('.test')||entry.name.endsWith('.zip'))continue;if(entry.name==='.env'||(entry.name.startsWith('.env.')&&entry.name!=='.env.example')||/\.private(?:\.|$)|\.(age|agekey|pem|key|p12|pfx|dump)$/.test(name)||/[\r\n\0]/.test(name))throw Error('Private/unsafe artifact path in submission source');files.push(name)}}
await walk(root);files.sort();await mkdir(stage,{recursive:true,mode:0o700});await mkdir(destination,{recursive:true});const fileHashes:Record<string,string>={};for(const name of files){const bytes=await readFile(resolve(root,name));fileHashes[name]=new Bun.CryptoHasher('sha256').update(bytes).digest('hex');await mkdir(dirname(resolve(stage,name)),{recursive:true});await copyFile(resolve(root,name),resolve(stage,name));}
const manifest={createdAt:new Date().toISOString(),assignment:'VP Lab6',student:'Медведев Андрей Михайлович, БПИ2303',target:'5, track2 SLO/incident detection; no awarded grade or instructor acceptance claimed',sourceOfCurrentResults:'REPORT.md',knownBrowserLimit:'Three functional UI scenarios completed; strict aggregate browser command retains one nonblocking canceled static sync.svg fetch, exact trigger unconfirmed; no clean-network PASS claimed',fileHashes};await writeFile(resolve(stage,'SUBMISSION-MANIFEST.json'),JSON.stringify(manifest,null,2)+'\n',{flag:'wx'});files.push('SUBMISSION-MANIFEST.json');
const archive=resolve(destination,'VP_Lab6_Medvedev_BPI2303.zip');if(await Bun.file(archive).exists())throw Error('Archive already exists; preserve historical artifact');const result=Bun.spawnSync(['zip','-q','-X',archive,...files],{cwd:stage,stdout:'pipe',stderr:'pipe'});if(result.exitCode!==0)throw Error('Native ZIP creation failed');const receipt={createdAt:new Date().toISOString(),archive,files:files.length,bytes:Bun.file(archive).size,sha256:new Bun.CryptoHasher('sha256').update(await Bun.file(archive).arrayBuffer()).digest('hex'),sourceManifestSHA256:new Bun.CryptoHasher('sha256').update(await Bun.file(resolve(stage,'SUBMISSION-MANIFEST.json')).arrayBuffer()).digest('hex'),stage,validation:'Pending separate native unzip CRC/path/exact per-file hash verification'};await writeFile(resolve(destination,'DELIVERY.json'),JSON.stringify(receipt,null,2)+'\n',{flag:'wx'});console.log(JSON.stringify(receipt));