cli - new and run handlers

This commit is contained in:
Michael Shanks 2020-04-26 21:13:14 +01:00
parent e2b393a077
commit c2c4f5b174
21 changed files with 70 additions and 95 deletions

View File

@ -24,6 +24,7 @@
"@budibase/server": "^0.0.32", "@budibase/server": "^0.0.32",
"@inquirer/password": "^0.0.6-alpha.0", "@inquirer/password": "^0.0.6-alpha.0",
"chalk": "^2.4.2", "chalk": "^2.4.2",
"dotenv": "^8.2.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"inquirer": "^7.0.0", "inquirer": "^7.0.0",
"lodash": "^4.17.15", "lodash": "^4.17.15",

View File

@ -1,12 +1,11 @@
const inquirer = require("inquirer") const inquirer = require("inquirer")
const { exists, readFile, writeFile, ensureDir } = require("fs-extra") const { exists, readFile, writeFile, ensureDir } = require("fs-extra")
const chalk = require("chalk") const chalk = require("chalk")
const { serverFileName } = require("../../common") const { serverFileName, xPlatHomeDir } = require("../../common")
const { join } = require("path") const { join } = require("path")
const initialiseClientDb = require("@budibase/server/db/initialiseClientDb") const initialiseClientDb = require("@budibase/server/db/initialiseClientDb")
const Sqrl = require("squirrelly") const Sqrl = require("squirrelly")
const uuid = require("uuid") const uuid = require("uuid")
const { homedir } = require("os")
const CouchDb = require("@budibase/server/db/client") const CouchDb = require("@budibase/server/db/client")
module.exports = opts => { module.exports = opts => {
@ -26,9 +25,7 @@ const run = async opts => {
} }
const ensureAppDir = async opts => { const ensureAppDir = async opts => {
if (opts.dir.startsWith("~")) { opts.dir = xPlatHomeDir(opts.dir)
opts.dir = join(homedir(), opts.dir.substring(1))
}
await ensureDir(opts.dir) await ensureDir(opts.dir)
if (opts.database === "local") { if (opts.database === "local") {

View File

@ -1,4 +0,0 @@
{
"levels": [],
"version": 0
}

View File

@ -1,13 +0,0 @@
{
"hierarchy": {
"name": "root",
"type": "root",
"children": [],
"pathMaps":[],
"indexes":[],
"nodeId": 0
},
"triggers": [],
"actions": [],
"props": {}
}

View File

@ -1,19 +0,0 @@
{
"title": "Test App",
"favicon": "./_shared/favicon.png",
"stylesheets": [],
"componentLibraries": ["@budibase/standard-components", "@budibase/materialdesign-components"],
"props" : {
"_component": "@budibase/standard-components/container",
"_children": [],
"_id": 1,
"type": "div",
"_styles": {
"layout": {},
"position": {}
},
"_code": ""
},
"_css": "",
"uiFunctions": ""
}

View File

@ -1 +0,0 @@
whats the craic big lawd ?

View File

@ -1,7 +1,7 @@
const handler = require("./newHandler") const handler = require("./newHandler")
module.exports = { module.exports = {
command: "new <name> [config]", command: "new <name> [dir]",
desc: "Create a new Budibase app", desc: "Create a new Budibase app",
builder: yargs => { builder: yargs => {
yargs.positional("name", { yargs.positional("name", {
@ -9,11 +9,11 @@ module.exports = {
describe: "the name of your app", describe: "the name of your app",
alias: "n", alias: "n",
}) })
yargs.positional("config", { yargs.positional("dir", {
type: "string", type: "string",
describe: "config file to use - optional, defaults to config.js", describe: "budibase apps directory",
alias: "c", alias: "d",
default: "config.js", default: "~/budibase",
}) })
}, },
handler, handler,

View File

@ -1,10 +1,9 @@
const { getAppContext } = require("../../common") const { xPlatHomeDir } = require("../../common")
const { const dotenv = require("dotenv")
getMasterApisWithFullAccess, const createInstance = require("@budibase/server/middleware/controllers/instance").create
} = require("@budibase/server/utilities/budibaseApi") const createApplication = require("@budibase/server/middleware/controllers/application").create
const { copy, readJSON, writeJSON, remove, exists } = require("fs-extra") const { copy, readJSON, writeJSON, remove, exists } = require("fs-extra")
const { resolve, join } = require("path") const { resolve, join } = require("path")
const thisPackageJson = require("../../../package.json")
const chalk = require("chalk") const chalk = require("chalk")
const { exec } = require("child_process") const { exec } = require("child_process")
@ -13,49 +12,54 @@ module.exports = opts => {
console.log(chalk.green(`Budibase app ${opts.name} created!`)) console.log(chalk.green(`Budibase app ${opts.name} created!`))
} }
const run2 = async opts => { const run = async opts => {
// create a brand new app in couch opts.dir = xPlatHomeDir(opts.dir)
// create an empty app package locally process.chdir(opts.dir)
exec(`cd ${join(opts.config.latestPackagesFolder, opts.name)} && npm install`) dotenv.config()
await createRecords(opts)
await createEmptyAppPackage(opts)
exec(`cd ${join(opts.dir, opts.name)} && npm install`)
} }
const run = async opts => { const createRecords = async opts => {
const context = await getAppContext({ const createAppCtx = {
configName: opts.config, params: { clientId: process.env.CLIENT_ID },
masterIsCreated: true, request: {
body: { name: opts.name },
},
body: {},
}
await createApplication(createAppCtx)
opts.applicationId = createAppCtx.body.id
await createInstance({
params: {
clientId: process.env.CLIENT_ID,
applicationId: opts.applicationId,
},
request: {
body: { name: `dev-${process.env.CLIENT_ID}` },
},
}) })
opts.config = context.config
const bb = await getMasterApisWithFullAccess(context)
const app = bb.recordApi.getNew("/applications", "application")
app.name = opts.name
await bb.recordApi.save(app)
await createEmptyAppPackage(opts)
exec(`cd ${join(opts.config.latestPackagesFolder, opts.name)} && npm install`)
} }
const createEmptyAppPackage = async opts => { const createEmptyAppPackage = async opts => {
const templateFolder = resolve(__dirname, "appPackageTemplate") const templateFolder = resolve(__dirname, "appDirectoryTemplate")
const appsFolder = opts.config.latestPackagesFolder || "." const appsFolder = opts.dir
const destinationFolder = resolve(appsFolder, opts.name) const destinationFolder = resolve(appsFolder, opts.applicationId)
if (await exists(destinationFolder)) { if (await exists(destinationFolder)) {
console.log(chalk.red(`App ${opts.name} already exists.`)) console.log(chalk.red(`App ${opts.name} already exists.`))
return return
} }
await copy(templateFolder, destinationFolder) await copy(templateFolder, destinationFolder)
const packageJsonPath = join(appsFolder, opts.name, "package.json") const packageJsonPath = join(appsFolder, opts.applicationId, "package.json")
const packageJson = await readJSON(packageJsonPath) const packageJson = await readJSON(packageJsonPath)
packageJson.name = opts.name packageJson.name = opts.name
packageJson.dependencies[
"@budibase/standard-components"
] = `^${thisPackageJson.version}`
await writeJSON(packageJsonPath, packageJson) await writeJSON(packageJsonPath, packageJson)

View File

@ -1,17 +1,16 @@
const handler = require("./runHandler") const handler = require("./runHandler")
module.exports = { module.exports = {
command: "run [config]", command: "run [dir]",
aliases: ["$0"], aliases: ["$0"],
desc: desc:
"Start budibase Server. You can access your apps and the builder from here if you have dev=true in your config", "Start budibase Server. You can access your apps and the builder from here if you have dev=true in your config",
builder: yargs => { builder: yargs => {
yargs.positional("config", { yargs.positional("dir", {
type: "string", type: "string",
describe: describe: "your budibase apps directory",
"config file to use. optional, defaults to config.js. Use 'dev' as shorthand for 'config.dev.js' ", alias: "d",
alias: "c", default: "~/budibase",
default: "config.js",
}) })
}, },
handler, handler,

View File

@ -1,9 +1,9 @@
const { getAppContext } = require("../../common")
const app = require("@budibase/server/app") const app = require("@budibase/server/app")
const { xPlatHomeDir } = require("../../common")
module.exports = ({ config }) => { module.exports = ({ dir }) => {
getAppContext({ configName: config, masterIsCreated: true }).then(context => { dir = xPlatHomeDir(dir)
app(context) process.chdir(dir)
console.log(`Budibase Builder running on port ${context.config.port}..`) app()
}) console.log(`Budibase Builder running on port ${process.env.PORT}..`)
} }

View File

@ -1,7 +1,7 @@
const { resolve } = require("path") const { resolve, join } = require("path")
const { cwd } = require("process") const { cwd } = require("process")
const buildAppContext = require("@budibase/server/initialise/buildAppContext") const buildAppContext = require("@budibase/server/initialise/buildAppContext")
const { homedir } = require("os")
module.exports.serverFileName = relativePath => module.exports.serverFileName = relativePath =>
resolve(__dirname, "..", "node_modules", "@budibase", "server", relativePath) resolve(__dirname, "..", "node_modules", "@budibase", "server", relativePath)
@ -17,3 +17,10 @@ module.exports.getAppContext = async ({ configName, masterIsCreated }) => {
const config = require(resolve(cwd(), configName))() const config = require(resolve(cwd(), configName))()
return await buildAppContext(config, masterIsCreated) return await buildAppContext(config, masterIsCreated)
} }
module.exports.xPlatHomeDir = dir => {
if (dir.startsWith("~")) {
dir = join(homedir(), dir.substring(1))
}
return dir
}

View File

@ -1509,6 +1509,11 @@ destroy@^1.0.4:
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA= integrity sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=
dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
ee-first@1.1.1: ee-first@1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d" resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"

View File

@ -6,7 +6,7 @@ module.exports = () => {
process.env.COUCH_DB_URL || "http://admin:password@localhost:5984" process.env.COUCH_DB_URL || "http://admin:password@localhost:5984"
const DATABASE_TYPE = process.env.DATABASE_TYPE || "couch" const DATABASE_TYPE = process.env.DATABASE_TYPE || "couch"
const pouch = PouchDB.defaults({ const pouch = PouchDB.defaults({
prefix: COUCH_DB_URL, prefix: COUCH_DB_URL,
}) })
allDbs(pouch) allDbs(pouch)