diff --git a/.gitignore b/.gitignore index 4ff2ca8a8b..fba6f1455d 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ ncc-cache/ yarn--** v8-compile-cache-** vitest.config.mts.timestamp-* +.key +InstanceID.* diff --git a/packages/core/cli/package.json b/packages/core/cli/package.json index ce3687b37e..fbc6cd7f43 100644 --- a/packages/core/cli/package.json +++ b/packages/core/cli/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "@nocobase/app": "1.7.0-beta.1", + "@nocobase/license-kit": "^0.1.0", "@types/fs-extra": "^11.0.1", "@umijs/utils": "3.5.20", "chalk": "^4.1.1", diff --git a/packages/core/cli/src/commands/inst.js b/packages/core/cli/src/commands/inst.js index daa335f8de..5a83c99539 100644 --- a/packages/core/cli/src/commands/inst.js +++ b/packages/core/cli/src/commands/inst.js @@ -10,8 +10,9 @@ const chalk = require('chalk'); const { Command } = require('commander'); const { run, isDev } = require('../util'); -const { keygen } = require('../environment-keygen'); +const { genInstanceId } = require('@nocobase/license-kit'); const path = require('path'); +const fs = require('fs'); /** * * @param {Command} cli @@ -19,16 +20,17 @@ const path = require('path'); module.exports = (cli) => { cli .command('inst') + .description('Generate InstanceID') .allowUnknownOption() .action(() => { - // if (!isDev()) { - // return; - // } - // run('rimraf', ['-rf', './storage/app-dev']); - // run('rimraf', ['-rf', 'packages/*/*/{lib,esm,es,dist,node_modules}']); - // run('rimraf', ['-rf', 'packages/*/@*/*/{lib,esm,es,dist,node_modules}']); - const filePath = path.resolve(process.cwd(), 'instance.enc'); - keygen({ filePath }); - console.log(chalk.greenBright(`Instance key saved to ${filePath}`)); + console.log('Generating InstanceID...'); + try { + const filePath = path.resolve(process.cwd(), 'InstanceID.txt'); + const instanceId = genInstanceId(); + fs.writeFileSync(filePath, instanceId); + console.log(chalk.greenBright(`InstanceID saved to ${filePath}`)); + } catch (e) { + console.log(e); + } }); }; diff --git a/packages/core/cli/src/commands/pkg.js b/packages/core/cli/src/commands/pkg.js index a0a4e883dd..1ae186a9e2 100644 --- a/packages/core/cli/src/commands/pkg.js +++ b/packages/core/cli/src/commands/pkg.js @@ -15,6 +15,8 @@ const tar = require('tar'); const path = require('path'); const { createStoragePluginsSymlink } = require('@nocobase/utils/plugin-symlink'); const chalk = require('chalk'); +const { getKey } = require('../util'); +const { keyDecrypt } = require('@nocobase/license-kit'); class Package { data; @@ -248,10 +250,15 @@ module.exports = (cli) => { NOCOBASE_PKG_USERNAME, NOCOBASE_PKG_PASSWORD, } = process.env; - if (!(NOCOBASE_PKG_USERNAME && NOCOBASE_PKG_PASSWORD)) { + const key = getKey(); // TODO 需执行本地环境校验 + if (!(NOCOBASE_PKG_USERNAME && NOCOBASE_PKG_PASSWORD) && !key) { return; } - const credentials = { username: NOCOBASE_PKG_USERNAME, password: NOCOBASE_PKG_PASSWORD }; + const keyDataStr = keyDecrypt(key); + const { accessKeyId, accessSecret } = JSON.parse(keyDataStr); + const credentials = accessKeyId + ? { username: accessKeyId, password: accessSecret } + : { username: NOCOBASE_PKG_USERNAME, password: NOCOBASE_PKG_PASSWORD }; const pm = new PackageManager({ baseURL: NOCOBASE_PKG_URL }); await pm.login(credentials); const file = path.resolve(__dirname, '../../package.json'); diff --git a/packages/core/cli/src/util.js b/packages/core/cli/src/util.js index f48dbd4c1d..14855f9939 100644 --- a/packages/core/cli/src/util.js +++ b/packages/core/cli/src/util.js @@ -165,10 +165,11 @@ exports.promptForTs = () => { }; exports.downloadPro = async () => { - const { NOCOBASE_PKG_USERNAME, NOCOBASE_PKG_PASSWORD } = process.env; - if (!(NOCOBASE_PKG_USERNAME && NOCOBASE_PKG_PASSWORD)) { - return; - } + // 此处不再判定,由pkgg命令处理 + // const { NOCOBASE_PKG_USERNAME, NOCOBASE_PKG_PASSWORD } = process.env; + // if (!(NOCOBASE_PKG_USERNAME && NOCOBASE_PKG_PASSWORD)) { + // return; + // } await exports.run('yarn', ['nocobase', 'pkg', 'download-pro']); }; @@ -471,3 +472,11 @@ exports.generatePlugins = function () { return; } }; + +exports.getKey = function () { + const keyFile = resolve(process.cwd(), './.key'); + if (!fs.existsSync(keyFile)) { + return; + } + return fs.readFileSync(keyFile, 'utf-8'); +};