From f15c67afd5745ccf37b5303f2bf8d61513d62183 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 4 Nov 2022 00:32:25 +0800 Subject: [PATCH] feat: more console log --- .../core/create-nocobase-app/src/generator.js | 1 + .../templates/app/.env.tpl | 2 ++ packages/core/server/src/application.ts | 13 +++++++------ packages/core/server/src/commands/install.ts | 2 ++ packages/core/server/src/commands/pm.ts | 5 +++++ .../src/plugin-manager/PluginManager.ts | 19 ++++++++++++------- packages/presets/nocobase/src/index.ts | 5 +++++ 7 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/core/create-nocobase-app/src/generator.js b/packages/core/create-nocobase-app/src/generator.js index b9c5d7b8d..cb6fb67eb 100644 --- a/packages/core/create-nocobase-app/src/generator.js +++ b/packages/core/create-nocobase-app/src/generator.js @@ -114,6 +114,7 @@ class AppGenerator extends Generator { APP_ENV: 'development', DB_DIALECT: dbDialect, APP_KEY: crypto.randomBytes(256).toString('base64'), + PLUGIN_PACKAGE_PREFIX: `@nocobase/plugin-,@nocobase/preset-,@${this.context.name}/plugin-`, ...env, }, }; diff --git a/packages/core/create-nocobase-app/templates/app/.env.tpl b/packages/core/create-nocobase-app/templates/app/.env.tpl index 486923b3e..2271f8d44 100644 --- a/packages/core/create-nocobase-app/templates/app/.env.tpl +++ b/packages/core/create-nocobase-app/templates/app/.env.tpl @@ -5,6 +5,8 @@ APP_PORT={{{env.APP_PORT}}} API_BASE_PATH=/api/ API_BASE_URL= +PLUGIN_PACKAGE_PREFIX={{{env.PLUGIN_PACKAGE_PREFIX}}} + DB_LOGGING=off DB_DIALECT={{{env.DB_DIALECT}}} {{{envs}}} diff --git a/packages/core/server/src/application.ts b/packages/core/server/src/application.ts index f13d4b180..b55bd8a89 100644 --- a/packages/core/server/src/application.ts +++ b/packages/core/server/src/application.ts @@ -9,7 +9,6 @@ import { Server } from 'http'; import { i18n, InitOptions } from 'i18next'; import Koa, { DefaultContext as KoaDefaultContext, DefaultState as KoaDefaultState } from 'koa'; import compose from 'koa-compose'; -import { isBoolean } from 'lodash'; import semver from 'semver'; import { promisify } from 'util'; import { createACL } from './acl'; @@ -314,6 +313,7 @@ export class Application exten async load(options?: any) { if (options?.reload) { + console.log(`Reload the application configuration`); const oldDb = this._db; this.init(); await oldDb.close(); @@ -427,17 +427,18 @@ export class Application exten return; } - if (options?.clean) { - await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean); + console.log('Database dialect: ' + this.db.sequelize.getDialect()); + + if (options?.clean || options?.sync?.force) { + console.log('Truncate database and reload app configuration'); + await this.db.clean({ drop: true }); await this.reload({ method: 'install' }); } await this.emitAsync('beforeInstall', this, options); - - await this.db.sync(options?.sync); + await this.db.sync(); await this.pm.install(options); await this.version.update(); - await this.emitAsync('afterInstall', this, options); } diff --git a/packages/core/server/src/commands/install.ts b/packages/core/server/src/commands/install.ts index 03005f2b8..fdacfa26a 100644 --- a/packages/core/server/src/commands/install.ts +++ b/packages/core/server/src/commands/install.ts @@ -35,6 +35,8 @@ export default (app: Application) => { let command = '$ yarn nocobase install -f'; console.log(chalk.yellow(command)); console.log(); + console.log(chalk.red('This operation will clear the database!!!')); + console.log(); } return; } diff --git a/packages/core/server/src/commands/pm.ts b/packages/core/server/src/commands/pm.ts index 5eb15bb20..2b9e0195b 100644 --- a/packages/core/server/src/commands/pm.ts +++ b/packages/core/server/src/commands/pm.ts @@ -6,6 +6,11 @@ export default (app: Application) => { .argument('') .arguments('') .action(async (method, plugins, options, ...args) => { + if (method === 'add') { + const { run } = require('@nocobase/cli/src/util'); + console.log('Install dependencies and rebuild workspaces'); + await run('yarn', ['install']); + } app.pm.clientWrite({ method, plugins }); }); }; diff --git a/packages/core/server/src/plugin-manager/PluginManager.ts b/packages/core/server/src/plugin-manager/PluginManager.ts index 6f923a9fb..491ecfa66 100644 --- a/packages/core/server/src/plugin-manager/PluginManager.ts +++ b/packages/core/server/src/plugin-manager/PluginManager.ts @@ -176,7 +176,7 @@ export class PluginManager { }); const pluginName = instance.getName(); if (this.plugins.has(pluginName)) { - throw new Error(`plugin name [${pluginName}] exists`); + throw new Error(`plugin name [${pluginName}] already exists`); } this.plugins.set(pluginName, instance); return instance; @@ -194,9 +194,7 @@ export class PluginManager { throw error; } } - console.log(`adding ${plugin} plugin`); const packageName = await PluginManager.findPackage(plugin); - console.log(`adding ${packageName}`); const packageJson = require(`${packageName}/package.json`); const instance = this.addStatic(plugin, { ...options, @@ -318,7 +316,7 @@ export class PluginManager { } static getPackageName(name: string) { - const prefixes = (process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-,@nocobase/plugin-pro-').split(','); + const prefixes = this.getPluginPkgPrefix(); for (const prefix of prefixes) { try { require.resolve(`${prefix}${name}`); @@ -330,25 +328,32 @@ export class PluginManager { throw new Error(`${name} plugin does not exist`); } + static getPluginPkgPrefix() { + return (process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-,@nocobase/plugin-pro-').split(','); + } + static async findPackage(name: string) { try { const packageName = this.getPackageName(name); return packageName; } catch (error) { - const prefixes = (process.env.PLUGIN_PACKAGE_PREFIX || '@nocobase/plugin-,@nocobase/preset-,@nocobase/plugin-pro-').split(','); + console.log(`\`${name}\` plugin not found locally`); + const prefixes = this.getPluginPkgPrefix(); for (const prefix of prefixes) { try { const packageName = `${prefix}${name}`; + console.log(`Try to find ${packageName}`); await execa('npm', ['v', packageName, 'versions']); - console.log(`${packageName} is downloading...`); + console.log(`${packageName} downloading`); await execa('yarn', ['add', packageName, '-W']); + console.log(`${packageName} downloaded`); return packageName; } catch (error) { continue; } } } - throw new Error(`${name} plugin does not exist`); + throw new Error(`No available packages found, ${name} plugin does not exist`); } static resolvePlugin(pluginName: string) { diff --git a/packages/presets/nocobase/src/index.ts b/packages/presets/nocobase/src/index.ts index 651f3a209..8d63e6ade 100644 --- a/packages/presets/nocobase/src/index.ts +++ b/packages/presets/nocobase/src/index.ts @@ -35,10 +35,13 @@ export class PresetNocoBase extends Plugin { if (options?.method !== 'upgrade') { return; } + const version = await this.app.version.get(); + console.log(`The version number before upgrade is ${version}`); const result = await this.app.version.satisfies('<0.8.0-alpha.1'); if (result) { const r = await this.db.collectionExistsInDb('applicationPlugins'); if (r) { + console.log(`Clear the installed application plugins`); await this.db.getRepository('applicationPlugins').destroy({ truncate: true }); await this.app.reload(); } @@ -47,10 +50,12 @@ export class PresetNocoBase extends Plugin { this.app.on('beforeUpgrade', async () => { const result = await this.app.version.satisfies('<0.8.0-alpha.1'); if (result) { + console.log(`Initialize all built-in plugins`); await this.addBuiltInPlugins(); } }); this.app.on('beforeInstall', async () => { + console.log(`Initialize all built-in plugins`); await this.addBuiltInPlugins(); }); }