Initial commit
This commit is contained in:
79
exam/zip.js
Normal file
79
exam/zip.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { installDeps } from './common.js';
|
||||
import packageJson from '../package.json' with { type: 'json' };
|
||||
|
||||
async function main() {
|
||||
installDeps();
|
||||
|
||||
const prompts = await import('prompts');
|
||||
console.log(`Lets create a zip archive for your submission.`);
|
||||
const { studentId } = await prompts.default({
|
||||
type: 'text',
|
||||
name: 'studentId',
|
||||
message: 'What is your student ID?',
|
||||
format: (value) => value.trim().replace(/[^A-Za-z0-9]/g, ''),
|
||||
});
|
||||
|
||||
if (!studentId) {
|
||||
console.error('Student ID is required');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let parentDirectory = process.cwd();
|
||||
if (parentDirectory.includes('exam') && !fs.existsSync(path.join(parentDirectory, 'exam'))) {
|
||||
parentDirectory = path.join(parentDirectory, '..');
|
||||
}
|
||||
|
||||
const archiveName = `${packageJson.name}-${studentId}.zip`;
|
||||
const zipPath = path.join(parentDirectory, archiveName);
|
||||
|
||||
if (fs.existsSync(zipPath)) {
|
||||
console.log(`Removing existing archive at ${zipPath}`);
|
||||
fs.unlinkSync(zipPath);
|
||||
}
|
||||
|
||||
const archiver = await import('archiver');
|
||||
const archive = archiver.default('zip');
|
||||
archive.on('error', (err) => {
|
||||
console.error(err);
|
||||
throw err;
|
||||
});
|
||||
archive.glob('**/*', {
|
||||
cwd: parentDirectory,
|
||||
ignore: [
|
||||
'node_modules/**',
|
||||
'exam/node_modules/**',
|
||||
'hardhat/node_modules/**',
|
||||
'.git/**',
|
||||
'.react-router/**',
|
||||
'hardhat/artifacts/**',
|
||||
'hardhat/ignition/deployments/**',
|
||||
'hardhat/build/**',
|
||||
'hardhat/cache/**',
|
||||
'build/**',
|
||||
'dist/**',
|
||||
'.env',
|
||||
'hardhat/.env',
|
||||
'hardhat/install-deps.js',
|
||||
'.DS_Store',
|
||||
'*.zip',
|
||||
'exam/common.js',
|
||||
'exam/exam.js',
|
||||
'exam/install-deps.js',
|
||||
'exam/index.html',
|
||||
'exam/zip.js',
|
||||
'package-lock.json',
|
||||
'exam/package-lock.json',
|
||||
'hardhat/package-lock.json'
|
||||
]
|
||||
|
||||
});
|
||||
archive.pipe(fs.createWriteStream(zipPath));
|
||||
await archive.finalize();
|
||||
|
||||
console.log();
|
||||
console.log(`Archive created: ${zipPath}`);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user