diff --git a/packages/create-portal-app/helpers/copy.js b/packages/create-portal-app/helpers/copy.js index 85a086c8..2e1cfc36 100644 --- a/packages/create-portal-app/helpers/copy.js +++ b/packages/create-portal-app/helpers/copy.js @@ -1,23 +1,23 @@ -const path = require("path"); -const fs = require('fs'); -const spawn = require('cross-spawn'); +const path = require('path') +const fs = require('fs') +const spawn = require('cross-spawn') -function parserPath(projectPath){ +function parserPath(projectPath) { return [process.cwd(), projectPath].join(path.sep) } -function copy(root,destination){ +function copy(root, destination) { const destinationPath = parserPath(destination) - return spawn.sync('cp', ['-r', root, destinationPath]); + return spawn.sync('cp', ['-r', root, destinationPath]) } -function isPathInUse(projectPath){ +function isPathInUse(projectPath) { const fullPath = parserPath(projectPath) const isPathExists = fs.existsSync(fullPath) - if(isPathExists) { + if (isPathExists) { return fs.readdirSync(fullPath).length } return isPathExists } -module.exports = { parserPath, copy, isPathInUse } \ No newline at end of file +module.exports = { parserPath, copy, isPathInUse } diff --git a/packages/create-portal-app/helpers/install.js b/packages/create-portal-app/helpers/install.js index 60ade0a3..a4a62545 100644 --- a/packages/create-portal-app/helpers/install.js +++ b/packages/create-portal-app/helpers/install.js @@ -1,7 +1,7 @@ -const spawn = require("cross-spawn"); -const path = require("path"); -const execSync = require('child_process').execSync; -const semver = require('semver'); +const spawn = require('cross-spawn') +const path = require('path') +const execSync = require('child_process').execSync +const semver = require('semver') /** * @@ -10,43 +10,43 @@ const semver = require('semver'); */ function install(projectName, isYarn) { return new Promise((resolve, reject) => { - const appPath = [process.cwd(), projectName].join(path.sep); + const appPath = [process.cwd(), projectName].join(path.sep) //change the directory to the app directory - process.chdir(appPath); + process.chdir(appPath) - const command = isYarn ? "yarn" : "npm"; - const args = isYarn ? [""] : ["install"]; + const command = isYarn ? 'yarn' : 'npm' + const args = isYarn ? [''] : ['install'] const exec = spawn(command, args, { - stdio: "pipe", - env: { ...process.env, ADBLOCK: "1", DISABLE_OPENCOLLECTIVE: "1" }, - }); + stdio: 'pipe', + env: { ...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1' }, + }) - exec.on("close", (code) => { + exec.on('close', (code) => { if (code !== 0) { - reject({ command: `${command} ${args[0]}` }); - return; + reject({ command: `${command} ${args[0]}` }) + return } - resolve(); - }); - }); + resolve() + }) + }) } /** * Method to initialize git repo on the new project */ async function initGit() { - spawn(`git`, [`init`, `-q`]); + spawn(`git`, [`init`, `-q`]) } /** * Check the version for npm and Yarn - * @param {*} pname + * @param {*} pname * @returns Boolean */ -function checkPackageVersion(pname){ - let userVersion = execSync(`${pname} --version`).toString(); - let expectedVersion = pname === 'yarn' ? '1.22.10' : '6.14.5'; - return !semver.lt(userVersion,expectedVersion) +function checkPackageVersion(pname) { + let userVersion = execSync(`${pname} --version`).toString() + let expectedVersion = pname === 'yarn' ? '1.22.10' : '6.14.5' + return !semver.lt(userVersion, expectedVersion) } -module.exports = { install, initGit, checkPackageVersion }; +module.exports = { install, initGit, checkPackageVersion } diff --git a/packages/create-portal-app/helpers/replace.js b/packages/create-portal-app/helpers/replace.js index 3f3567f7..1ed906df 100644 --- a/packages/create-portal-app/helpers/replace.js +++ b/packages/create-portal-app/helpers/replace.js @@ -1,21 +1,18 @@ -const fs = require('fs'); -const path = require("path"); -const {parserPath } = require('./copy') -function _replacePackage(projectName){ +const fs = require('fs') +const path = require('path') +const { parserPath } = require('./copy') +function _replacePackage(projectName) { const appPath = parserPath(projectName) const packagePath = `${appPath}/package.json` const data = JSON.parse(fs.readFileSync(packagePath, 'utf8')) data.name = projectName delete data.homepage - + fs.writeFileSync(packagePath, JSON.stringify(data, null, 2)) } - - - -function replaceContent(projectName){ +function replaceContent(projectName) { _replacePackage(projectName) } -module.exports = replaceContent \ No newline at end of file +module.exports = replaceContent diff --git a/packages/create-portal-app/index.js b/packages/create-portal-app/index.js index b13c26b4..020c1aac 100755 --- a/packages/create-portal-app/index.js +++ b/packages/create-portal-app/index.js @@ -1,32 +1,31 @@ #!/usr/bin/env node -const Listr = require("listr"); -const { program } = require("commander"); -const chalk = require("chalk"); -const prompts = require("prompts"); -const path = require("path"); -const figlet = require("figlet"); -const package = require("./package.json"); -const { copy, isPathInUse } = require("./helpers/copy"); -const { install, initGit, checkPackageVersion } = require("./helpers/install"); -const replace = require("./helpers/replace"); +const Listr = require('listr') +const { program } = require('commander') +const chalk = require('chalk') +const prompts = require('prompts') +const path = require('path') +const figlet = require('figlet') +const package = require('./package.json') +const { copy, isPathInUse } = require('./helpers/copy') +const { install, initGit, checkPackageVersion } = require('./helpers/install') +const replace = require('./helpers/replace') // Output path to create new portal app -let projectPath = ""; - +let projectPath = '' // Commander parameters to specify CLI behavior program .name(package.name) .version(package.version) - .arguments("[dir]") - .usage(`${chalk.yellow("[dir]")}`) + .arguments('[dir]') + .usage(`${chalk.yellow('[dir]')}`) .description({ - dir: "Directory to be used on install Portal.js", + dir: 'Directory to be used on install Portal.js', }) .action((name) => (projectPath = name)) - .option("--use-npm") - .parse(process.argv); + .option('--use-npm') + .parse(process.argv) /** * Method to ask a custon name if was not passed as parameter @@ -34,125 +33,123 @@ program */ async function promptPath() { return prompts({ - type: "text", - name: "path", - message: "Choose a name to your project", - initial: "", + type: 'text', + name: 'path', + message: 'Choose a name to your project', + initial: '', validate: (name) => { projectPath = name - if(isPathInUse(projectPath)){ - return `${chalk.yellow( - "Path " + - chalk.redBright(projectPath) + - " is already in use and is not empty." - )}` + if (isPathInUse(projectPath)) { + return `${chalk.yellow( + 'Path ' + + chalk.redBright(projectPath) + + ' is already in use and is not empty.' + )}` } return true }, - }); + }) } - /** * Main method to start CLI and validate inputs */ async function run() { - if (typeof projectPath === "string") { - projectPath = projectPath.trim(); + if (typeof projectPath === 'string') { + projectPath = projectPath.trim() } if (!projectPath) { - const response = await promptPath(); - if (typeof response.path === "string") { - projectPath = response.path.trim(); + const response = await promptPath() + if (typeof response.path === 'string') { + projectPath = response.path.trim() } } if (!projectPath) { //TODO separate log methods - console.log(); - console.log("Please choose a name to your project:"); - console.log(); - console.log("Example:"); + console.log() + console.log('Please choose a name to your project:') + console.log() + console.log('Example:') console.log( - `${chalk.cyan(program.name())} ${chalk.yellow("ny-portal-app")}` - ); - console.log(); + `${chalk.cyan(program.name())} ${chalk.yellow('ny-portal-app')}` + ) + console.log() - process.exit(1); + process.exit(1) } - const root = path.join(__dirname + "/../portal"); + const root = path.join(__dirname + '/../portal') if (isPathInUse(projectPath)) { - console.log(); + console.log() console.log( `${chalk.yellow( - "Path " + + 'Path ' + chalk.redBright(projectPath) + - " is already in use and is not empty." + ' is already in use and is not empty.' )}` - ); - console.log(); - process.exit(1); + ) + console.log() + process.exit(1) } // print a fancy Portal.js in the terminal console.log( - chalk.yellow(figlet.textSync("Portal.Js", { horizontalLayout: "full" })) - ); + chalk.yellow(figlet.textSync('Portal.Js', { horizontalLayout: 'full' })) + ) - console.log(); - console.log(`Creating new portal.js app in ${chalk.cyan(projectPath)}`); - console.log(); + console.log() + console.log(`Creating new portal.js app in ${chalk.cyan(projectPath)}`) + console.log() //Tasks workflow const tasks = new Listr([ { - title: "Fetching Content", + title: 'Fetching Content', task: () => copy(root, projectPath), }, { - title: "Updating Content", + title: 'Updating Content', task: () => replace(projectPath), }, { - title: "Installing Dependencies", + title: 'Installing Dependencies', task: () => install(projectPath, program.useNpm), }, { - title: "Git Init", + title: 'Git Init', task: () => initGit(projectPath), }, - ]); + ]) tasks.run().then(() => { - console.log(); - console.log(`${chalk.greenBright("Instalation Completed Successfully")}`); - console.log(); + console.log() + console.log(`${chalk.greenBright('Instalation Completed Successfully')}`) + console.log() console.log( - `Run ${chalk.cyan("cd " + projectPath)} and ${chalk.green( - "yarn dev" - )} or ${chalk.yellow("npm run dev")}` - ); - console.log(); - console.log("Enjoy =)"); - }); + `Run ${chalk.cyan('cd ' + projectPath)} and ${chalk.green( + 'yarn dev' + )} or ${chalk.yellow('npm run dev')}` + ) + console.log() + console.log('Enjoy =)') + }) } //Main CLI execution workflow run().catch((error) => { - console.log(error.name); + console.log(error.name) if (error.install) { - console.log(); + console.log() console.log( - `${chalk.redBright("Error on Create App :")}${chalk.yellow( + `${chalk.redBright('Error on Create App :')}${chalk.yellow( error.message.toString() )}` - ); + ) } else { - console.log(`${chalk.red("Unexpected Error. Please report it as a bug")}`); - console.log(error); + console.log(`${chalk.red('Unexpected Error. Please report it as a bug')}`) + console.log(error) } - console.log(); - process.exit(1); -}); - + console.log() + process.exit(1) +})