UNPKG

apollon-cli

Version:

118 lines (104 loc) 4.34 kB
#!/usr/bin/env node const program = require("commander"); const shell = require('shelljs'); const path = require('path'); shell.config.silent = true; program .version('0.1.5', '-v, --version') .option('-d, --devel', 'Use the develepment version instead of the stable') .option('-i, --install [path]', 'Create a new Apollon API at the specified path or in current directory if no path specified and --force option used. "./" will not work') .option('-f, --full [package manager]', 'Install apollon dependencies if param not specified uses npm') .option('-F, --force', 'Install apollon dependencies if param not specified uses npm') .option('-w, --nodemon', 'Use nodemon when launching Apollon') .option('-a, --add <items>', 'Elements to add -> comments:resolver,users:resolver,mysql:connector', str => str.split(",").map(item => item.split(":")), [] ) .parse(process.argv) ; if (!shell.which('npm')) { shell.echo('Sorry, this tool requires "npm"'); shell.exit(1); } if (program.install) { if (!shell.which('git')) { shell.echo('Sorry, in the installation context this script requires "git"'); shell.exit(1); } console.log("Downloading files ..."); let clone; if(program.install === true ){ if(!program.force){ console.log("You are trying to install in local dir. If this is you intention add --force") return false; } clone = shell.exec(`git clone --depth=1 --branch=${program.devel ? "develop" : "master"} https://github.com/lymeo/apollon.git ./.apollon`); shell.rm('-rf', "./.apollon/.git"); shell.cp("-r", "./.apollon/*", "./"); shell.cp("-r", "./.apollon/.*", "./"); if (clone.code === 0) { console.log("Cleaning up ") shell.rm('-rf', "./.apollon"); if (program.full) { let depCommand = program.full === true ? "npm i" : program.full; console.log("Installing dependencies"); shell.cd(program.install); shell.config.silent = false; shell.exec(depCommand); shell.config.silent = true; } console.log("Apollon instance is ready"); } else { console.log(clone.stderr); shell.exit(clone.code); }; } else { clone = shell.exec(`git clone --depth=1 --branch=${program.devel ? "develop" : "master"} https://github.com/lymeo/apollon.git ${program.install}`); if (clone.code === 0) { console.log("Cleaning up ") shell.rm('-rf', path.join(program.install, ".git")); if (program.full) { let depCommand = program.full === true ? "npm i" : program.full; console.log("Installing dependencies"); shell.cd(program.install); shell.config.silent = false; shell.exec(depCommand); shell.config.silent = true; } console.log("Apollon instance is ready here: " + program.install); } else { console.log(clone.stderr); shell.exit(clone.code); }; } } else if (program.nodemon) { if (!shell.which('nodemon')) { shell.echo('Sorry, in this context this script requires "nodemon"'); shell.exit(1); } shell.config.silent = false; shell.exec("npm run nodemon"); } else if(!(program.add && program.add.length)) { shell.config.silent = false; if (program.devel) { shell.exec("npm run dev"); } else { shell.exec("npm run start"); } } if (program.add && program.add.length) { for(let [name, type] of program.add){ switch(type){ case "resolver": console.log(shell.pwd().toString()); shell.cp(path.join(__dirname, "../ressources/resolver.js"),path.join(shell.pwd().toString(), `/resolvers/${name}.js`)); break; case "connector": shell.cp(path.join(__dirname, "../ressources/connector.js"),path.join(shell.pwd().toString(), `/connectors/${name}.js`)); break; default: console.log("This type is not managed"); break; } } }