[prettier] [s]: update files using prettier

This commit is contained in:
Thadeu Cotts
2020-11-26 16:16:49 -03:00
parent 2468e19aa3
commit 6c34a1056e
4 changed files with 116 additions and 122 deletions

View File

@@ -1,6 +1,6 @@
const path = require("path"); const path = require('path')
const fs = require('fs'); const fs = require('fs')
const spawn = require('cross-spawn'); const spawn = require('cross-spawn')
function parserPath(projectPath) { function parserPath(projectPath) {
return [process.cwd(), projectPath].join(path.sep) return [process.cwd(), projectPath].join(path.sep)
@@ -8,7 +8,7 @@ function parserPath(projectPath){
function copy(root, destination) { function copy(root, destination) {
const destinationPath = parserPath(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) {

View File

@@ -1,7 +1,7 @@
const spawn = require("cross-spawn"); const spawn = require('cross-spawn')
const path = require("path"); const path = require('path')
const execSync = require('child_process').execSync; const execSync = require('child_process').execSync
const semver = require('semver'); const semver = require('semver')
/** /**
* *
@@ -10,32 +10,32 @@ const semver = require('semver');
*/ */
function install(projectName, isYarn) { function install(projectName, isYarn) {
return new Promise((resolve, reject) => { 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 //change the directory to the app directory
process.chdir(appPath); process.chdir(appPath)
const command = isYarn ? "yarn" : "npm"; const command = isYarn ? 'yarn' : 'npm'
const args = isYarn ? [""] : ["install"]; const args = isYarn ? [''] : ['install']
const exec = spawn(command, args, { const exec = spawn(command, args, {
stdio: "pipe", stdio: 'pipe',
env: { ...process.env, ADBLOCK: "1", DISABLE_OPENCOLLECTIVE: "1" }, env: { ...process.env, ADBLOCK: '1', DISABLE_OPENCOLLECTIVE: '1' },
}); })
exec.on("close", (code) => { exec.on('close', (code) => {
if (code !== 0) { if (code !== 0) {
reject({ command: `${command} ${args[0]}` }); reject({ command: `${command} ${args[0]}` })
return; return
} }
resolve(); resolve()
}); })
}); })
} }
/** /**
* Method to initialize git repo on the new project * Method to initialize git repo on the new project
*/ */
async function initGit() { async function initGit() {
spawn(`git`, [`init`, `-q`]); spawn(`git`, [`init`, `-q`])
} }
/** /**
@@ -44,9 +44,9 @@ async function initGit() {
* @returns Boolean * @returns Boolean
*/ */
function checkPackageVersion(pname) { function checkPackageVersion(pname) {
let userVersion = execSync(`${pname} --version`).toString(); let userVersion = execSync(`${pname} --version`).toString()
let expectedVersion = pname === 'yarn' ? '1.22.10' : '6.14.5'; let expectedVersion = pname === 'yarn' ? '1.22.10' : '6.14.5'
return !semver.lt(userVersion, expectedVersion) return !semver.lt(userVersion, expectedVersion)
} }
module.exports = { install, initGit, checkPackageVersion }; module.exports = { install, initGit, checkPackageVersion }

View File

@@ -1,5 +1,5 @@
const fs = require('fs'); const fs = require('fs')
const path = require("path"); const path = require('path')
const { parserPath } = require('./copy') const { parserPath } = require('./copy')
function _replacePackage(projectName) { function _replacePackage(projectName) {
const appPath = parserPath(projectName) const appPath = parserPath(projectName)
@@ -11,9 +11,6 @@ function _replacePackage(projectName){
fs.writeFileSync(packagePath, JSON.stringify(data, null, 2)) fs.writeFileSync(packagePath, JSON.stringify(data, null, 2))
} }
function replaceContent(projectName) { function replaceContent(projectName) {
_replacePackage(projectName) _replacePackage(projectName)
} }

View File

@@ -1,32 +1,31 @@
#!/usr/bin/env node #!/usr/bin/env node
const Listr = require("listr"); const Listr = require('listr')
const { program } = require("commander"); const { program } = require('commander')
const chalk = require("chalk"); const chalk = require('chalk')
const prompts = require("prompts"); const prompts = require('prompts')
const path = require("path"); const path = require('path')
const figlet = require("figlet"); const figlet = require('figlet')
const package = require("./package.json"); const package = require('./package.json')
const { copy, isPathInUse } = require("./helpers/copy"); const { copy, isPathInUse } = require('./helpers/copy')
const { install, initGit, checkPackageVersion } = require("./helpers/install"); const { install, initGit, checkPackageVersion } = require('./helpers/install')
const replace = require("./helpers/replace"); const replace = require('./helpers/replace')
// Output path to create new portal app // Output path to create new portal app
let projectPath = ""; let projectPath = ''
// Commander parameters to specify CLI behavior // Commander parameters to specify CLI behavior
program program
.name(package.name) .name(package.name)
.version(package.version) .version(package.version)
.arguments("[dir]") .arguments('[dir]')
.usage(`${chalk.yellow("[dir]")}`) .usage(`${chalk.yellow('[dir]')}`)
.description({ .description({
dir: "Directory to be used on install Portal.js", dir: 'Directory to be used on install Portal.js',
}) })
.action((name) => (projectPath = name)) .action((name) => (projectPath = name))
.option("--use-npm") .option('--use-npm')
.parse(process.argv); .parse(process.argv)
/** /**
* Method to ask a custon name if was not passed as parameter * Method to ask a custon name if was not passed as parameter
@@ -34,125 +33,123 @@ program
*/ */
async function promptPath() { async function promptPath() {
return prompts({ return prompts({
type: "text", type: 'text',
name: "path", name: 'path',
message: "Choose a name to your project", message: 'Choose a name to your project',
initial: "", initial: '',
validate: (name) => { validate: (name) => {
projectPath = name projectPath = name
if (isPathInUse(projectPath)) { if (isPathInUse(projectPath)) {
return `${chalk.yellow( return `${chalk.yellow(
"Path " + 'Path ' +
chalk.redBright(projectPath) + chalk.redBright(projectPath) +
" is already in use and is not empty." ' is already in use and is not empty.'
)}` )}`
} }
return true return true
}, },
}); })
} }
/** /**
* Main method to start CLI and validate inputs * Main method to start CLI and validate inputs
*/ */
async function run() { async function run() {
if (typeof projectPath === "string") { if (typeof projectPath === 'string') {
projectPath = projectPath.trim(); projectPath = projectPath.trim()
} }
if (!projectPath) { if (!projectPath) {
const response = await promptPath(); const response = await promptPath()
if (typeof response.path === "string") { if (typeof response.path === 'string') {
projectPath = response.path.trim(); projectPath = response.path.trim()
} }
} }
if (!projectPath) { if (!projectPath) {
//TODO separate log methods //TODO separate log methods
console.log(); console.log()
console.log("Please choose a name to your project:"); console.log('Please choose a name to your project:')
console.log(); console.log()
console.log("Example:"); console.log('Example:')
console.log( console.log(
`${chalk.cyan(program.name())} ${chalk.yellow("ny-portal-app")}` `${chalk.cyan(program.name())} ${chalk.yellow('ny-portal-app')}`
); )
console.log(); console.log()
process.exit(1); process.exit(1)
} }
const root = path.join(__dirname + "/../portal"); const root = path.join(__dirname + '/../portal')
if (isPathInUse(projectPath)) { if (isPathInUse(projectPath)) {
console.log(); console.log()
console.log( console.log(
`${chalk.yellow( `${chalk.yellow(
"Path " + 'Path ' +
chalk.redBright(projectPath) + chalk.redBright(projectPath) +
" is already in use and is not empty." ' is already in use and is not empty.'
)}` )}`
); )
console.log(); console.log()
process.exit(1); process.exit(1)
} }
// print a fancy Portal.js in the terminal // print a fancy Portal.js in the terminal
console.log( console.log(
chalk.yellow(figlet.textSync("Portal.Js", { horizontalLayout: "full" })) chalk.yellow(figlet.textSync('Portal.Js', { horizontalLayout: 'full' }))
); )
console.log(); console.log()
console.log(`Creating new portal.js app in ${chalk.cyan(projectPath)}`); console.log(`Creating new portal.js app in ${chalk.cyan(projectPath)}`)
console.log(); console.log()
//Tasks workflow //Tasks workflow
const tasks = new Listr([ const tasks = new Listr([
{ {
title: "Fetching Content", title: 'Fetching Content',
task: () => copy(root, projectPath), task: () => copy(root, projectPath),
}, },
{ {
title: "Updating Content", title: 'Updating Content',
task: () => replace(projectPath), task: () => replace(projectPath),
}, },
{ {
title: "Installing Dependencies", title: 'Installing Dependencies',
task: () => install(projectPath, program.useNpm), task: () => install(projectPath, program.useNpm),
}, },
{ {
title: "Git Init", title: 'Git Init',
task: () => initGit(projectPath), task: () => initGit(projectPath),
}, },
]); ])
tasks.run().then(() => { tasks.run().then(() => {
console.log(); console.log()
console.log(`${chalk.greenBright("Instalation Completed Successfully")}`); console.log(`${chalk.greenBright('Instalation Completed Successfully')}`)
console.log(); console.log()
console.log( console.log(
`Run ${chalk.cyan("cd " + projectPath)} and ${chalk.green( `Run ${chalk.cyan('cd ' + projectPath)} and ${chalk.green(
"yarn dev" 'yarn dev'
)} or ${chalk.yellow("npm run dev")}` )} or ${chalk.yellow('npm run dev')}`
); )
console.log(); console.log()
console.log("Enjoy =)"); console.log('Enjoy =)')
}); })
} }
//Main CLI execution workflow //Main CLI execution workflow
run().catch((error) => { run().catch((error) => {
console.log(error.name); console.log(error.name)
if (error.install) { if (error.install) {
console.log(); 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() error.message.toString()
)}` )}`
); )
} else { } else {
console.log(`${chalk.red("Unexpected Error. Please report it as a bug")}`); console.log(`${chalk.red('Unexpected Error. Please report it as a bug')}`)
console.log(error); console.log(error)
} }
console.log(); console.log()
process.exit(1); process.exit(1)
}); })