diff --git a/README.md b/README.md index f3bc769..0bc9f94 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ - ⚡ Supports individual step execution, such as `connect`, `clean`, `backup`, `upload`. +- 🌍 Support global configuration + ## Translations [简体中文](./docs/README.zh.md) @@ -54,6 +56,7 @@ init(generate) deploy config file Options: -t, --type file type: "json" | "yaml" | "javascript" (default: "javascript") -m, --module javascript module type: "commonjs" | "cjs" | "esm" | "mjs" (default: "cjs") + -g, --global generate global config file -h, --help display help for command ``` @@ -71,6 +74,8 @@ module.exports = { // autoClean: false, // If the task attribute does not exist, it will take effect tasks: [ { + name: 'task name', + disabled: false, target: 'your/dist/path', remoteDir: '/your/server/path', autoBackup: true, @@ -140,3 +145,21 @@ you can add scripts to **package.json** ``` then, use `npm run deploy` + +### 5. Other commands + +```bash +deploy view config +``` + +useage: + +```bash +Usage: deploy view [options] + +view deploy config file info + +Options: + -c, --config config file path + -h, --help display help for command +``` \ No newline at end of file diff --git a/docs/README.zh.md b/docs/README.zh.md index bb8436a..ac1cec6 100644 --- a/docs/README.zh.md +++ b/docs/README.zh.md @@ -6,10 +6,12 @@ - 🪄 支持多种配置文件格式,如 `json`、`yaml`、`js`。 -- 🚩 支持配置多个任务。 +- 🚩 支持灵活配置多个任务。 - ⚡ 支持单独步骤执行,如 `connect`、`clean`、`backup`、`upload`。 +- 🌍 支持全局配置 + ## 翻译 [English](../README.md) @@ -54,6 +56,7 @@ init 生成部署配置文件 选项: -t, --type 文件类型: "json" | "yaml" | "javascript" (默认: "javascript") -m, --module javascript 模块类型: "commonjs" | "cjs" | "esm" | "mjs" (默认: "cjs") + -g, --global 生成全局配置文件(系统用户目录下) -h, --help 显示命令帮助 ``` @@ -70,6 +73,8 @@ module.exports = { // autoClean: false, // 如果任务的该属性不存在,此处属性将生效 tasks: [ { + name: 'task name', + disabled: false, // 是否禁用 target: 'your/dist/path', remoteDir: '/your/server/path', autoBackup: true, @@ -137,4 +142,22 @@ CLI 用于将项目部署到服务器 }, ``` -然后,使用 `npm run deploy`。 \ No newline at end of file +然后,使用 `npm run deploy`。 + +### 5. 其他命令 + +```bash +deploy view config +``` + +用法: + +```bash +Usage: deploy view [options] + +查看部署配置信息 + +Options: + -c, --config 配置文件路径 + -h, --help 显示命令帮助 +``` \ No newline at end of file diff --git a/package.json b/package.json index 60118e9..cbbc2a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nebulae-cli/deploy", - "version": "0.1.3", + "version": "0.1.4", "description": "Command line tools for deploying", "private": false, "type": "module", diff --git a/src/commands/index.ts b/src/commands/index.ts index 5295836..07eaaa2 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -23,6 +23,7 @@ export interface DeployArgv { export interface InitArgv { type: 'json' | 'yaml' | 'javascript'; module?: 'commonjs' | 'cjs' | 'esm' | 'mjs'; + global?: boolean; } export interface ConnectArgv { @@ -71,7 +72,6 @@ export function initCommands(): void { const result = await readDeployConfig(configFilePath); console.log(chalk.green(`⚡ Load config file: ${result.path}\n`)); await deploy(result.config); - process.exit(0); // 以正常状态退出 } catch (error) { console.error(`😭 部署失败:${chalk.red((error as Error).message)}\n`); process.exit(1); // 以错误状态退出,会在控制台输出错误信息 @@ -84,11 +84,11 @@ export function initCommands(): void { .alias('gen') .option('-t, --type ', 'file type: "json" | "yaml" | "javascript"', 'javascript') .option('-m, --module ', 'javascript module type: "commonjs" | "cjs" | "esm" | "mjs"', 'cjs') + .option('-g, --global', 'generate global config file | 生成全局配置文件') .description('init(generate) deploy config file | 生成配置文件') .action((argv: Partial) => { try { init(argv); - process.exit(0); } catch (error) { console.error(`😭 初始化失败:${chalk.red((error as Error).message)}\n`); process.exit(1); @@ -122,10 +122,9 @@ export function initCommands(): void { }; const conn = await connect(connectOptions); conn.end(); // 测试完成后立即断开连接 - process.exit(0); // 以正常状态退出 } catch (error) { console.error(`😭 连接失败:${chalk.red((error as Error).message)}\n`); - process.exit(1); // 以错误状态退出,会在控制台输出错误信息 + process.exit(1); // 以错误状态退出,会在控制台输出错误信息, process.exit(0) 为正常退出 } }); @@ -162,7 +161,6 @@ export function initCommands(): void { dest, }; await backup(backupOptions, conn); - process.exit(0); } catch (error) { console.error(`😭 备份失败:${chalk.red((error as Error).message)}\n`); process.exit(1); @@ -185,7 +183,6 @@ export function initCommands(): void { console.log(chalk.green(`⚡ Load config file: ${result.path}\n`)); const conn = await connect(result.config); await clean(_cleanOptions, conn); - process.exit(0); } catch (error) { console.error(`😭 清理失败:${chalk.red((error as Error).message)}\n`); process.exit(1); @@ -216,7 +213,6 @@ export function initCommands(): void { dir: _uploadOptions.dir, }; await upload(uploadOptions, conn); - process.exit(0); } catch (error) { console.error(`😭 上传失败:${chalk.red((error as Error).message)}\n`); process.exit(1); @@ -225,7 +221,7 @@ export function initCommands(): void { program .command('view config') - .description('view deploy config file | 查看配置文件') + .description('view deploy config info | 查看部署配置信息') .option('-c, --config ', 'config file path') .action(viewConfig); diff --git a/src/commands/init.ts b/src/commands/init.ts index 9159afd..4c92705 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -39,17 +39,38 @@ export function init(options: InitOptions): void { const type = options.type ?? 'javascript'; const module = options.module ?? 'cjs'; const global = options.global ?? false; + let ext = '.cjs'; if (type === 'javascript') { ext = mapToExt[type][module]; } else { ext = mapToExt[type]; } - const exists = DEFAULT_CONFIG_PATHS.some((configPath) => - fs.existsSync(path.resolve(process.cwd(), configPath)), - ); - if (exists) { - throw new Error('Function init: config file already exists'); + + let existFilePath = ''; + if (global) { + // 全局配置文件检测 + for (const relativePath of DEFAULT_CONFIG_PATHS) { + const globalPath = path.resolve(os.homedir(), relativePath); + if (fs.existsSync(globalPath)) { + existFilePath = globalPath; + break; + } + } + } else { + // 项目配置文件检测 + for (const relativePath of DEFAULT_CONFIG_PATHS) { + const cwdPath = path.resolve(process.cwd(), relativePath); + if (fs.existsSync(cwdPath)) { + existFilePath = cwdPath; + break; + } + } + } + if (existFilePath) { + throw new Error( + `Function init: ${global ? 'global ' : ''}config file already exists: ${existFilePath}`, + ); } const spinner = ora(`正在创建配置文件:deploy.config${ext}`).start(); @@ -88,6 +109,7 @@ export function init(options: InitOptions): void { fs.writeFileSync(outputFilePath, result, 'utf-8'); spinner.succeed(`配置文件创建成功:${outputFilePath}`); + // 打开配置文件 openFile(outputFilePath); } catch (error) { diff --git a/src/utils.ts b/src/utils.ts index 52aaea3..494d303 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -566,22 +566,30 @@ export function readProjectPackageJson(startDir: string): PackageJson | null { * @param filePath - 文件路径 */ export function openFile(filePath: string): ReturnType | null { + const _filePath = slash(filePath); + let cmd = ''; + let args: string[] = []; + switch (process.platform) { case 'darwin': cmd = 'open'; + args = [_filePath]; break; case 'win32': - cmd = 'start'; + cmd = 'cmd'; + args = ['/c', 'start', _filePath]; break; case 'linux': cmd = 'xdg-open'; + args = [_filePath]; break; default: break; } + if (cmd) { - return spawn(cmd, [filePath], { stdio: 'inherit' }); + return spawn(cmd, args, { stdio: 'inherit' }); } return null; }