@arc-fusion/cli
Version:
CLI for running Arc Fusion on your local machine
90 lines (74 loc) • 2.34 kB
JavaScript
const path = require('path')
const {
BOOTSTRAP_ROOT,
PROJECT_ROOT
} = require('../environment')
const {
fileExists,
gentleCopy,
readdir,
stat,
writeFile
} = require('../utils/promises')
const spawn = require('../utils/spawn')
async function addScripts (packageFile) {
const packageJson = require(packageFile)
packageJson.scripts = packageJson.scripts || {}
packageJson.scripts.fusion = 'fusion'
packageJson.scripts.start = 'fusion start'
packageJson.scripts = Object.assign(
{},
...Object.keys(packageJson.scripts)
.sort()
.map((cmd) => ({
[cmd]: packageJson.scripts[cmd]
}))
)
await writeFile(packageFile, JSON.stringify(packageJson, null, 2))
}
async function walk (dir) {
const entries = await readdir(dir)
return [].concat(
...await Promise.all(
entries
.map(async (entry) => {
const fullPath = path.join(dir, entry)
return stat(fullPath)
.then(async (s) =>
(s.isDirectory())
? walk(fullPath)
: fullPath
)
})
)
)
}
async function init () {
const cwd = PROJECT_ROOT
// await here to make sure we know before we start copying
const hasDotGitIgnore = await fileExists(path.join(PROJECT_ROOT, '.gitignore'))
await spawn('npm', ['init', '-y'], { cwd })
try {
// npm list will return exit code 1 and reject the promise if @arc-fusion/cli is not installed
await spawn('npm', ['list', '@arc-fusion/cli'], { cwd })
} catch (error) {
console.log('Installing @arc-fusion/cli package...')
await spawn('npm', ['install', '--save-dev', '@arc-fusion/cli'], { cwd, stdio: 'inherit' })
console.log('installing @arc-fusion/cli package complete')
}
console.log('Bootstrapping project...')
await addScripts(path.join(cwd, 'package.json'))
await Promise.all(
(await walk(BOOTSTRAP_ROOT))
.filter((src) => path.relative(BOOTSTRAP_ROOT, src) !== 'gitignore')
.map(async (src) => {
return gentleCopy(src, path.join(PROJECT_ROOT, path.relative(BOOTSTRAP_ROOT, src)))
})
)
if (!hasDotGitIgnore) {
await gentleCopy(path.join(BOOTSTRAP_ROOT, 'gitignore'), path.join(PROJECT_ROOT, '.gitignore'))
}
console.log('Bootstrapping project complete')
}
module.exports = init