import cp from "child_process"; import alert from "cli-alerts"; import welcome from "cli-welcome"; import unhandled from "cli-handle-unhandled"; import chalk from "chalk"; import crypto from "crypto"; import { Spinner } from "@topcli/spinner"; import pkg from "../../package.json" with { type: "json" }; import sshKeygen from "ssh-keygen-lite"; import inquirer from "inquirer"; import md5 from "md5"; const execute = (comm, { isMultipleParameter = false, isWithLoading = true, successMessage, startMessage, }) => { return new Promise((resolve, reject) => { const spinner = new Spinner(); if (isWithLoading) { spinner.start(startMessage); } if (isMultipleParameter) { const parameters = comm.split(" "); const commandProcessing = cp.spawn(parameters[0], parameters.slice(1), { shell: process.platform === "win32" ? true : undefined, stdio: ["inherit", "pipe"], cwd: process.cwd() }); commandProcessing.stdout.on("data", (data) => { if (process.platform === "win32" && data.toString().indexOf("y/N") !== -1) { if (isWithLoading) spinner.succeed(); } console.log(data.toString()); }); commandProcessing.on("close", () => { if (isWithLoading) spinner.succeed(); console.log( chalk.blue( successMessage ) ); resolve(true); }); commandProcessing.on("error", (err) => { if (isWithLoading) spinner.failed(); console.log( chalk.red( err.toString() ) ); reject(false); }); commandProcessing.on("disconnect", (err) => { if (isWithLoading) spinner.failed(); console.log( chalk.red( err.toString() ) ); reject(false); }); } else { cp.exec(comm, (err) => { if (err) { if (isWithLoading) spinner.failed(); console.log( chalk.red( err.message ) ); reject(false); } if (isWithLoading) spinner.succeed(); console.log( chalk.blue( successMessage ) ); resolve(true); }); } }); }; const generateSSHKeygen = (resp: { keyPassword: string; keyComment: string; }, location: string): Promise<{ pubKey: string; key: string; }> => { return new Promise((resolve, reject) => { const passmd5 = md5(resp.keyPassword); const pass = md5(passmd5); // @ts-ignore sshKeygen({ comment: resp.keyComment, location: location, password: pass, format: "PEM", read: true }, function (err, out) { if (err) { reject(err); return console.log(err); } resolve(out); return out; }); }); }; export default { init: ({ clear = true }: { clear: unknown }) => { unhandled(); welcome({ title: "ncicd", tagLine: "by nibgat", description: pkg.description, version: pkg.version, bgColor: "#00c2a9", color: "#444444", bold: true, clear }); }, alert: info => { alert({ type: "warning", name: "DEBUG LOG", msg: "" }); console.log(info); console.log(); }, execute, generateKeyPair: (): Promise<{ privateKey: string; publicKey: string; }> => new Promise((resolve, reject) => { crypto.generateKeyPair( "rsa", { modulusLength: 2048, publicKeyEncoding: { type: "spki", format: "pem" }, privateKeyEncoding: { type: "pkcs8", format: "pem" }, }, (err, publicKey, privateKey) => { if (err) reject(err); resolve({ publicKey, privateKey }); } ); }), quest: (questions: Array<{ name: string; message: string; }>) => { return new Promise((resolve, reject) => { inquirer .prompt(questions) .then((answers) => { resolve(answers); }) .catch((err) => { reject(err); }); }); }, generateSSHKeygen: async (location: string, name: string): Promise<{ pubKey: string; key: string; }> => { const resp = await inquirer.prompt([ { name: "keyComment", message: `${name} - Enter your key mail:` }, { name: "keyPassword", message: `${name} - Enter your key password:`, type: "password" } ]); const keys = await generateSSHKeygen(resp, location); return keys; }, asyncForEach: async (array, callback) => { for(let index = 0; index < array.length; index++) { await callback(array[index], index, array); } } };