dotwar
Version:
WAR FILE CREATE AND EXTRACT CLI
200 lines (163 loc) • 5.91 kB
JavaScript
import { existsSync, createWriteStream, mkdirSync, readdirSync, createReadStream } from 'fs';
import archiver from 'archiver';
import { resolve, basename, dirname } from 'path';
import inquirer from 'inquirer';
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') {
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') {
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') {
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) {
console.log("please wait...");
if (!existsSync(folderPath)) {
mkdirSync(folderPath, { recursive: true });
}
const output = createWriteStream(`DOTWAR/${filename}.war`);
const archive = archiver('zip', {
zlib: { level: 9 }
});
output.on('close', () => {
console.log(`WAR file created 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');
console.log("please wait...");
if (!existsSync(revPath)) {
mkdirSync(revPath, { recursive: true });
}
revPath = resolve(process.cwd(), 'DOTWAR-REVERSE', appName);
createReadStream(inputPath)
.pipe(unzipper.Extract({ path: revPath }))
.on('close', () => {
console.log(`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 ❌');
});
}