diff --git a/packages/core/cli/src/commands/index.js b/packages/core/cli/src/commands/index.js index 1d50eba26b..b2322cf3ed 100644 --- a/packages/core/cli/src/commands/index.js +++ b/packages/core/cli/src/commands/index.js @@ -34,6 +34,7 @@ module.exports = (cli) => { require('./postinstall')(cli); require('./pkg')(cli); require('./instance-id')(cli); + require('./view-license-key')(cli); if (isPackageValid('@umijs/utils')) { require('./create-plugin')(cli); } diff --git a/packages/core/cli/src/commands/instance-id.js b/packages/core/cli/src/commands/instance-id.js index 59e73055d5..d072cebbda 100644 --- a/packages/core/cli/src/commands/instance-id.js +++ b/packages/core/cli/src/commands/instance-id.js @@ -22,20 +22,25 @@ module.exports = (cli) => { cli .command('generate-instance-id') .description('Generate InstanceID') - .allowUnknownOption() - .action(async () => { + .option('--force', 'Force generate InstanceID') + .action(async (options) => { console.log('Generating InstanceID...'); - try { - const dir = path.resolve(process.cwd(), 'storage/.license'); + const dir = path.resolve(process.cwd(), 'storage/.license'); + const filePath = path.resolve(dir, 'instance-id'); + if (fs.existsSync(filePath) && !options.force) { + console.log('InstanceID already exists at ' + filePath); + return; + } else { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } - const filePath = path.resolve(dir, 'InstanceID'); - const instanceId = await getInstanceIdAsync(); - fs.writeFileSync(filePath, instanceId + '\n'); - console.log(chalk.greenBright(`InstanceID saved to ${filePath}`)); - } catch (e) { - console.log(e); + try { + const instanceId = await getInstanceIdAsync(); + fs.writeFileSync(filePath, instanceId + '\n'); + console.log(chalk.greenBright(`InstanceID saved to ${filePath}`)); + } catch (e) { + console.log(e); + } } }); }; diff --git a/packages/core/cli/src/commands/view-license-key.js b/packages/core/cli/src/commands/view-license-key.js new file mode 100644 index 0000000000..316d6fa279 --- /dev/null +++ b/packages/core/cli/src/commands/view-license-key.js @@ -0,0 +1,44 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +const chalk = require('chalk'); +const { Command } = require('commander'); +const { keyDecrypt } = require('@nocobase/license-kit'); +const path = require('path'); +const fs = require('fs'); + +/** + * + * @param {Command} cli + */ +module.exports = (cli) => { + cli + .command('view-license-key') + .description('View License Key') + .action(async (options) => { + const dir = path.resolve(process.cwd(), 'storage/.license'); + const filePath = path.resolve(dir, 'license-key'); + if (!fs.existsSync(filePath)) { + console.log('License key not found at ' + filePath); + return; + } + const key = fs.readFileSync(filePath, 'utf-8'); + let keyDataStr; + try { + keyDataStr = keyDecrypt(key); + } catch (e) { + console.log('License key decrypt failed', e); + return; + } + const keyData = JSON.parse(keyDataStr); + const { accessKeyId, accessSecret } = keyData; + console.log(chalk.greenBright(`Access Key ID: ${accessKeyId}`)); + console.log(chalk.greenBright(`Access Secret: ${accessSecret}`)); + }); +};