import fs from 'fs'; import mkpath from 'mkpath'; import { promisify } from 'util'; import { APP_COMMAND_FILE_PATH, USER_FILE_PATH } from '../config'; import isGlobalNpmInstall from 'is-installed-globally'; import { CommandModule } from '../command-types'; function mkpathWithMode(path: string, mode: number, callback: (err: any) => void) { return mkpath(path, mode, callback); } const makePathAsync = promisify(mkpathWithMode); const newCommandTemplate = `import { CommandModule } from 'auditboard-cli'; interface CommandArgs { firstParam: string; option?: number; } const makeCommand: CommandModule = { /** * args get defined by the commandOptions interface below * { * "firstParam": "users", <-- required params * "options": 11 <-- optional params * } */ async command(args) { console.log("HELLO WORLD From my new command", args); return "Return a message and the cli will log it!"; }, commandOptions: { help: "This help string supports markdown!", description: "This is your command description", required: [ /** * This is where you put required params * arguments are parsed sequentially * example: ./auditboard make:command users -o 11 * * This segment is how it got to this */ { name: "firstParam", // <-- this value will be users message: 'Something went wrong...', // <-- (optional) failure message if param does not exist }, // ... more required params ], args: { // these are optional parameters // https://www.npmjs.com/package/args '--option': Number, '-o': '--option', } } }; export default makeCommand; `; const helpContent = ` # Great help should be written here. And you can use **markdown** as you please, even though you're a \`terminal\` lover. # Usage: auditboard make:command # Optional Parameters: * -g, --global Creates the command under your HOME folder, allowing it to be used anywhere (in your machine) `; const makeCommand: CommandModule<{ commandName: string, global?: boolean }> = { /** * args get defined by the commandOptions interface below * { * "firstParam": "users", <-- required params * "options": 11 <-- optional params * } */ async command(args) { // if installed globally or global flag passed install in USER_FILE_PATH const folderPath = (isGlobalNpmInstall || args.global) ? USER_FILE_PATH : APP_COMMAND_FILE_PATH; if (!fs.existsSync(folderPath)) { // write folder await makePathAsync(folderPath, parseInt("0700", 8)); } const commandName = args.commandName; const commandFileName = commandName.replace(/\:/g, '_') + '.ts' const newCommandPath = `${folderPath}/${commandFileName}`; fs.writeFileSync(newCommandPath, newCommandTemplate); return `Your new command: ${commandName} was successfully created. \n Edit ${newCommandPath} to customize it.`; }, commandOptions: { description: "Create commands that will get executed by the cli", help: helpContent, required: [ { name: "commandName", // <-- this value will be users message: 'You need a command name to create a command... duh', // <-- failure message if param does not exist }, ], args: { "--global": Boolean, "-g": "--global" } } }; export default makeCommand;