UNPKG

dotwar

Version:

WAR FILE CREATE AND EXTRACT CLI

232 lines (195 loc) 6.8 kB
#!/usr/bin/env node import { existsSync, createWriteStream, mkdirSync, readdirSync, createReadStream } from 'fs'; import archiver from 'archiver'; import { resolve, basename, dirname } from 'path'; import inquirer from 'inquirer'; import ora from 'ora'; import { execSync } from "child_process"; import * as unzipper from 'unzipper'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const args = process.argv.slice(2)[0]; async function askQuestion(query) { try { const answers = await inquirer.prompt([ { type: 'input', name: 'answer', message: query, validate: function (input) { if (!input || input.trim() === '') { return 'This field is required.'; } return true; } } ]); return answers.answer.trim(); } catch (error) { console.log('DOTWAR process exited, try again ❌'); process.exit(); } } const folderPath = resolve(process.cwd(), 'DOTWAR'); if (args === 'reverse') { unzipWarFile(); } else { commandQuestions(); } async function commandQuestions() { let answers; try { answers = await inquirer.prompt([ { type: 'list', name: 'framework', message: 'Select a framework:', choices: ['Angular', 'React CRA', 'React Vite', 'Other'] }, ]); } catch (error) { // console.log('Error' +error); console.log('DOTWAR process exited, try again ❌'); process.exit(); } if (answers.framework === 'Angular') { try { const output = execSync("ng build", { stdio: "inherit" }); console.log("🚀 Build completed!"); } catch (error) { console.error("❌ Build failed", error); return; } const distPath = resolve(process.cwd(), 'dist'); if (existsSync(distPath)) { let appName = readdirSync(distPath) if (appName.length === 1) { appName = appName[0]; } else if (appName.length > 1) { const filename = await askQuestion('Multiple App detected, Please specify your App name : '); appName = filename } else { console.log('No App found in your dist folder, Please run ng build first.'); return; } const appPath = resolve(process.cwd(), 'dist', appName, 'browser'); if (existsSync(appPath)) { WarCreation(appName, appPath); } else { console.log('browser folder not found or empty app folder.'); } } else { console.log('dist folder not found in current directory, Please run ng build first'); } } else if (answers.framework === 'React CRA') { try { execSync("npm run build", { stdio: "inherit" }); console.log("🚀 Build completed!"); } catch (error) { console.error("❌ Build failed", error); return; } const buildPath = resolve(process.cwd(), 'build'); if (existsSync(buildPath)) { const currentFolderName = basename(process.cwd()); WarCreation(currentFolderName, buildPath); } else { console.log('build folder not found in current directory, Please run npm run build first'); } } else if (answers.framework === 'React Vite') { try { execSync("npm run build", { stdio: "inherit" }); console.log("🚀 Build completed!"); } catch (error) { console.error("❌ Build failed", error); return; } const distPath = resolve(process.cwd(), 'dist'); if (existsSync(distPath)) { const currentFolderName = basename(process.cwd()); WarCreation(currentFolderName, distPath); } else { console.log('dist folder not found in current directory, Please run npm run build first'); } } else { const otherPath = await askQuestion('Please specify location of your file(s) from current directory separeted by (\\) : '); const pathArr = otherPath.split('\\'); const customPath = resolve(process.cwd(), ...pathArr); if (existsSync(customPath)) { const fname = await askQuestion('Please enter your war file name : '); if (fname.trim().length === 0) { console.log("invali file name, try again"); return } WarCreation(fname, customPath); } else { console.log('Target folder not found in current directory. Please retry'); } } } function WarCreation(filename, customPath) { const spinner = ora({ text: 'Creating WAR file...', spinner: 'dots', color: 'yellow' }).start(); if (!existsSync(folderPath)) { mkdirSync(folderPath, { recursive: true }); } const output = createWriteStream(`DOTWAR/${filename}.war`); const archive = archiver('zip', { zlib: { level: 9 } }); output.on('close', () => { spinner.succeed('WAR file created on DOTWAR folder!'); console.log(`Name : ${filename}.war, Size : ${(archive.pointer() / 1048576).toFixed(3)}mb`); console.log("Thank you for using DOTWAR 😊 -Bikram Sahoo"); }); archive.pipe(output); archive.directory(customPath, false); archive.finalize(); } async function unzipWarFile() { let inputPath; if (!existsSync(folderPath)) { console.log('DOTWAR folder not found, try again'); process.exit(); } let appList = readdirSync(folderPath); let appName; if (appList.length > 1) { let getAppName = await askQuestion('Multiple file detected, Please specify your file name : '); const isWarIncluded = getAppName.split('.'); if (isWarIncluded.length > 1) { inputPath = resolve(process.cwd(), 'DOTWAR', getAppName); appName = isWarIncluded[0]; } else { inputPath = resolve(process.cwd(), 'DOTWAR', getAppName + '.war'); appName = getAppName; } } else { inputPath = resolve(process.cwd(), 'DOTWAR', appList[0]); appName = appList[0].split('.')[0]; } let revPath = resolve(process.cwd(), 'DOTWAR-REVERSE'); const spinner = ora({ text: 'Reversing WAR file...', spinner: 'dots', color: 'yellow' }).start(); if (!existsSync(revPath)) { mkdirSync(revPath, { recursive: true }); } revPath = resolve(process.cwd(), 'DOTWAR-REVERSE', appName); createReadStream(inputPath) .pipe(unzipper.Extract({ path: revPath })) .on('close', () => { spinner.succeed("WAR file extracted to DOTWAR-REVERSE folder!"); console.log("Thank you for using DOTWAR 😊 -Bikram Sahoo"); }) .on('error', (err) => { // console.error('Extraction error:', err); console.log('DOTWAR process exited, try again ❌'); }); }