Skip to content

Commit

Permalink
Add init command
Browse files Browse the repository at this point in the history
Signed-off-by: macdonst <[email protected]>
  • Loading branch information
macdonst committed Oct 6, 2023
1 parent 6d97816 commit 0e8e4e1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
let dev = require('./dev')
let generate = require('./generate')
let help = require('./help')
let init = require('./init')
let newProj = require('./new')
let version = require('./version')
let commands = [
dev, generate, help, newProj, version
dev, generate, help, init, newProj, version
]

let helper = require('../helper')
Expand Down
74 changes: 74 additions & 0 deletions src/commands/init/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module.exports = async function (params) {
let { existsSync, writeFileSync } = require('fs')
let { join, resolve } = require('path')

let utils = require('../../lib')
let { npmCommands: { initialInstall } } = utils
let error = require('./errors')(params, utils)
let _inventory = require('@architect/inventory')
let c = require('@colors/colors/safe')

let dest = resolve('.')

// Error out if folder already exists and it has an arc project already
if (existsSync(dest)) {
let inventory = await _inventory({ cwd: dest })
let invalid = inventory.inv._project.manifest
if (invalid) return error('project_found')
}

// write arc file
writeFileSync(join(dest, '.arc'),
`@app
enhance-app
@static
prune true
@plugins
enhance/arc-plugin-enhance
`
)

const pkgFilePath = join(dest, 'package.json')
if (existsSync(pkgFilePath)) {
const pkgFile = require(join(dest, 'package.json'))
pkgFile.scripts.enhance = 'enhance'
pkgFile.scripts.start = 'npx enhance dev'
pkgFile.devDependencies['@enhance/cli'] = 'latest'
pkgFile.devDependencies['@enhance/types'] = 'latest'
pkgFile.dependencies['@enhance/arc-plugin-enhance'] = 'latest'

writeFileSync(
pkgFilePath,
JSON.stringify(pkgFile, null, 2),
)
}
else {
writeFileSync(
pkgFilePath,
`{
"name": "enhance-app",
"version": "0.0.1",
"scripts": {
"start": "npx enhance dev",
"enhance": "enhance"
},
"devDependencies": {
"@enhance/cli": "latest",
"@enhance/types": "latest"
},
"dependencies": {
"@enhance/arc-plugin-enhance": "latest"
}
}
`
)
}

// Need to install enhance/arc-plugin-enhance or 💥
await initialInstall(params, dest)

// Success message
console.error(`Project successfully initialized! To get started run: ${c.bold(c.green(`npx enhance dev`))}`)
}
15 changes: 15 additions & 0 deletions src/commands/init/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function error (params, utils) {
return function (err) {
let { lang } = params
let { backtickify, runtimes } = utils
let errors = {
en: {
no_path: 'Project path not found, please run with -p or --path',
project_found: 'Existing Enhance app already found in this directory',
invalid_appname: `Invalid app name`,
invalid_runtime: `Function runtime must be one of: ${backtickify(runtimes)}`,
}
}
return Error(errors[lang][err])
}
}
21 changes: 21 additions & 0 deletions src/commands/init/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let names = { en: [ 'init' ] }
let action = require('./action')

module.exports = {
names,
action,
help: () => {
return {
en: {
usage: [ 'init' ],
description: 'Initialize a new empty Enhance project',
examples: [
{
name: 'Create a new project in the current folder',
example: 'npx @enhance/cli init',
},
]
},
}
}
}
12 changes: 4 additions & 8 deletions src/commands/new/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let names = { en: [ 'new', 'init' ] }
let names = { en: [ 'new' ] }
let action = require('./action')

module.exports = {
Expand All @@ -20,17 +20,13 @@ module.exports = {
],
},
examples: [
{
name: 'Create a new project in the current folder',
example: 'npx enhance new',
},
{
name: 'Create a new project in the ./my-proj folder',
example: 'npx enhance new my-proj',
example: 'npx @enhance/cli new my-proj',
},
{
name: 'Create a new project with the name my-app',
example: 'npx enhance new project -n my-app',
name: 'Create a new project in the ./my-proj folder with the name my-app',
example: 'npx @enhance/cli new my-proj -n my-app',
},
]
},
Expand Down

0 comments on commit 0e8e4e1

Please sign in to comment.