fusox
Version:
Command line wrapper for fuse-box
64 lines (50 loc) • 2.2 kB
JavaScript
const util = require('util')
const {execSync} = require('child_process')
const {readdirSync, existsSync, mkdirSync} = require('fs')
const chalk = require('chalk')
const path = require('path')
const {parseFlag} = require('../helpers')
module.exports = {parseInitFlags, initCommand}
function parseInitFlags (args) {
let target = parseFlag(args.i, 'browser') || parseFlag(args.init, 'browser')
let destination = args._.shift() || '.'
return {target, destination}
}
function initCommand (flags) {
if (['browser', 'electron', 'server', 'library'].indexOf(flags.target) === -1) {
throw new Error(util.format('Invalid target "%s", valid targets are "browser", "electron", "server" and "library"', flags.target))
}
if ( ! flags.destination) {
throw new Error('Missing destination argument')
}
let destinationPath = path.resolve(flags.destination)
if (flags.destination === '.') {
if (readdirSync(destinationPath).length > 0) {
throw new Error('Current directory must be empty')
}
} else {
if (existsSync(destinationPath)) {
throw new Error(util.format('Directory "%s" already exists', flags.destination))
}
mkdirSync(flags.destination)
if ( ! existsSync(destinationPath)) {
throw new Error(util.format('Could not create directory "%s"', flags.destination))
}
}
let repositoryUrl = 'https://github.com/maximkott/fusox-examples-' + flags.target
let cloneCommand = util.format('git clone %s . && rm -rf .git && git init', repositoryUrl)
let installCommand = 'npm install'
let runCommand = 'npm run dev'
let execOptions = {shell: true, stdio: 'inherit', cwd: destinationPath}
console.log('Cloning example project for target %s from %s into %s', chalk.yellow(flags.target), chalk.yellow(repositoryUrl), chalk.yellow(path.resolve(flags.destination)))
console.log(chalk.gray('$', cloneCommand))
execSync(cloneCommand, execOptions)
console.log()
console.log('Installing dependencies')
console.log(chalk.gray('$', installCommand))
execSync(installCommand, execOptions)
console.log()
console.log('Starting project')
console.log(chalk.gray('$', runCommand))
execSync(runCommand, execOptions)
}