Skip to content

Commit

Permalink
feat(cli): if no command and in a project, run dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Jan 19, 2019
1 parent e08a01f commit 576e336
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
46 changes: 44 additions & 2 deletions packages/@nodepack/cli/src/bin/commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const program = require('commander')
const { chalk, checkDebug } = require('@nodepack/utils')

checkDebug(process.cwd())
const cwd = process.cwd()

checkDebug(cwd)

program
.version(require('../../package.json').version)
Expand Down Expand Up @@ -101,7 +103,7 @@ enhanceErrorMessages('optionMissingArgument', (option, flag) => {
program.parse(process.argv)

if (!process.argv.slice(2).length) {
program.outputHelp()
noCommand()
}

function camelize (str) {
Expand All @@ -122,3 +124,43 @@ function cleanArgs (cmd) {
})
return args
}

async function noCommand () {
const path = require('path')
const fs = require('fs-extra')

// In a nodepack project
if (fs.existsSync(path.resolve(cwd, '.nodepack')) || fs.existsSync(path.resolve(cwd, 'nodepack.config.js'))) {
const { installDeps, loadGlobalOptions, getPkgCommand, readPkg } = require('@nodepack/utils')
const execa = require('execa')

const packageManager = (
loadGlobalOptions().packageManager ||
getPkgCommand(cwd)
)

// Auto-install
if (!fs.existsSync(path.resolve(cwd, 'node_modules'))) {
await installDeps(cwd, packageManager)
}

// Run command
const pkg = readPkg(cwd)
let command = 'nodepack-service'
let args = ['dev']
if (pkg.scripts && pkg.scripts.dev) {
// Prefer 'run' script in package.json
command = packageManager
args = ['run', 'dev']
}
execa(command, args, {
stdio: [process.stdin, process.stdout, process.stderr],
cwd: process.cwd(),
cleanup: true,
shell: false,
env: process.env,
})
} else {
program.outputHelp()
}
}
2 changes: 1 addition & 1 deletion packages/@nodepack/cli/src/lib/ProjectCreateJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = class ProjectCreateJob {
log(
`👉 Get started with the following commands:\n\n` +
(cwd === process.cwd() ? `` : chalk.cyan(` ${chalk.gray('$')} cd ${projectName}\n`)) +
chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn dev' : 'npm run dev'}`)
chalk.cyan(` ${chalk.gray('$')} nodepack`)
)
log()

Expand Down

0 comments on commit 576e336

Please sign in to comment.