From 99ad54afb6785c9180c46dd3e9e01e25da368218 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 5 Dec 2022 11:38:25 +0800 Subject: [PATCH] feat(cli): quickstart --- docker/nocobase/docker-entrypoint.sh | 5 +---- packages/core/cli/src/commands/dev.js | 1 + packages/core/cli/src/commands/start.js | 1 + packages/core/server/src/application.ts | 18 ++++++++++++++++++ packages/core/server/src/commands/install.ts | 16 +--------------- packages/core/server/src/commands/start.ts | 3 +++ 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/docker/nocobase/docker-entrypoint.sh b/docker/nocobase/docker-entrypoint.sh index 7ebf1defad..0d18f6b8cf 100755 --- a/docker/nocobase/docker-entrypoint.sh +++ b/docker/nocobase/docker-entrypoint.sh @@ -13,10 +13,7 @@ if [ ! -f "/app/nocobase/package.json" ]; then tar -zxf /app/nocobase.tar.gz --absolute-names -C /app/nocobase fi -cd /app/nocobase && yarn nocobase db:auth --retry=30 -cd /app/nocobase && yarn nocobase install -s -cd /app/nocobase && yarn nocobase upgrade -S -cd /app/nocobase && yarn start +cd /app/nocobase && yarn start --quickstart # Run command with node if the first argument contains a "-" or is not a system command. The last # part inside the "{}" is a workaround for the following bug in ash/dash: diff --git a/packages/core/cli/src/commands/dev.js b/packages/core/cli/src/commands/dev.js index 1a293d477c..43fbb06278 100644 --- a/packages/core/cli/src/commands/dev.js +++ b/packages/core/cli/src/commands/dev.js @@ -15,6 +15,7 @@ module.exports = (cli) => { .option('--client') .option('--server') .option('--db-sync') + .option('--quickstart') .allowUnknownOption() .action(async (opts) => { promptForTs(); diff --git a/packages/core/cli/src/commands/start.js b/packages/core/cli/src/commands/start.js index b31ff1a3bd..ff0e7afaee 100644 --- a/packages/core/cli/src/commands/start.js +++ b/packages/core/cli/src/commands/start.js @@ -15,6 +15,7 @@ module.exports = (cli) => { .option('-p, --port [port]') .option('-d, --daemon') .option('--db-sync') + .option('--quickstart') .allowUnknownOption() .action(async (opts) => { if (opts.port) { diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index 1f045f0ada..674997d6c5 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -88,6 +88,7 @@ interface StartOptions { cliArgs?: any[]; dbSync?: boolean; listen?: ListenOptions; + quickstart?: boolean; } export class ApplicationVersion { @@ -386,6 +387,17 @@ export class Application exten await this.db.sync(); } + if (options.quickstart) { + if (await this.isInstalled()) { + console.log('upgrading...'); + await this.upgrade({ ...options, method: 'upgrade' }); + } else { + console.log('installing...'); + await this.install(options); + } + await this.reload({ ...options, method: 'load' }); + } + await this.emitAsync('beforeStart', this, options); if (options?.listen?.port) { @@ -478,6 +490,12 @@ export class Application exten return true; } + async isInstalled() { + return ( + (await this.db.collectionExistsInDb('applicationVersion')) || (await this.db.collectionExistsInDb('collections')) + ); + } + async install(options: InstallOptions = {}) { console.log('Database dialect: ' + this.db.sequelize.getDialect()); diff --git a/packages/core/server/src/commands/install.ts b/packages/core/server/src/commands/install.ts index fdacfa26a4..bd28144479 100644 --- a/packages/core/server/src/commands/install.ts +++ b/packages/core/server/src/commands/install.ts @@ -12,22 +12,8 @@ export default (app: Application) => { let installed = false; const [opts] = cliArgs; - try { - await app.db.auth({ retry: opts.retry || 1 }); - } catch (error) { - console.log( - chalk.red( - 'Unable to connect to the database. Please check the database environment variables in the .env file.', - ), - ); - return; - } - if (!opts?.clean && !opts?.force) { - if ( - (await app.db.collectionExistsInDb('applicationVersion')) || - (await app.db.collectionExistsInDb('collections')) - ) { + if (await app.isInstalled()) { installed = true; if (!opts.silent) { console.log('NocoBase is already installed. To reinstall, please execute:'); diff --git a/packages/core/server/src/commands/start.ts b/packages/core/server/src/commands/start.ts index 605e2963d9..f87548f5cf 100644 --- a/packages/core/server/src/commands/start.ts +++ b/packages/core/server/src/commands/start.ts @@ -7,12 +7,15 @@ export default (app: Application) => { .option('-p, --port [post]') .option('-h, --host [host]') .option('--db-sync') + .option('--quickstart') .action(async (...cliArgs) => { const [opts] = cliArgs; const port = opts.port || process.env.APP_PORT || 13000; const host = opts.host || process.env.APP_HOST || '0.0.0.0'; + const quickstart = opts.quickstart; await app.start({ + quickstart, dbSync: opts?.dbSync, cliArgs, listen: {