Skip to content

Commit

Permalink
feat: Bump version to 0.1.4, improve openFile function, and add globa…
Browse files Browse the repository at this point in the history
…l config option
  • Loading branch information
longlongago2 committed Jul 15, 2024
1 parent 613758b commit d81c35a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- ⚡ Supports individual step execution, such as `connect`, `clean`, `backup`, `upload`.

- 🌍 Support global configuration

## Translations

[简体中文](./docs/README.zh.md)
Expand Down Expand Up @@ -54,6 +56,7 @@ init(generate) deploy config file
Options:
-t, --type <type> file type: "json" | "yaml" | "javascript" (default: "javascript")
-m, --module <module> javascript module type: "commonjs" | "cjs" | "esm" | "mjs" (default: "cjs")
-g, --global generate global config file
-h, --help display help for command
```

Expand All @@ -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,
Expand Down Expand Up @@ -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] <config>

view deploy config file info

Options:
-c, --config <config> config file path
-h, --help display help for command
```
27 changes: 25 additions & 2 deletions docs/README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

- 🪄 支持多种配置文件格式,如 `json``yaml``js`

- 🚩 支持配置多个任务
- 🚩 支持灵活配置多个任务

- ⚡ 支持单独步骤执行,如 `connect``clean``backup``upload`

- 🌍 支持全局配置

## 翻译

[English](../README.md)
Expand Down Expand Up @@ -54,6 +56,7 @@ init 生成部署配置文件
选项:
-t, --type <type> 文件类型: "json" | "yaml" | "javascript" (默认: "javascript")
-m, --module <module> javascript 模块类型: "commonjs" | "cjs" | "esm" | "mjs" (默认: "cjs")
-g, --global 生成全局配置文件(系统用户目录下)
-h, --help 显示命令帮助
```

Expand All @@ -70,6 +73,8 @@ module.exports = {
// autoClean: false, // 如果任务的该属性不存在,此处属性将生效
tasks: [
{
name: 'task name',
disabled: false, // 是否禁用
target: 'your/dist/path',
remoteDir: '/your/server/path',
autoBackup: true,
Expand Down Expand Up @@ -137,4 +142,22 @@ CLI 用于将项目部署到服务器
},
```

然后,使用 `npm run deploy`
然后,使用 `npm run deploy`

### 5. 其他命令

```bash
deploy view config
```

用法:

```bash
Usage: deploy view [options] <config>

查看部署配置信息

Options:
-c, --config <config> 配置文件路径
-h, --help 显示命令帮助
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 4 additions & 8 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface DeployArgv {
export interface InitArgv {
type: 'json' | 'yaml' | 'javascript';
module?: 'commonjs' | 'cjs' | 'esm' | 'mjs';
global?: boolean;
}

export interface ConnectArgv {
Expand Down Expand Up @@ -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); // 以错误状态退出,会在控制台输出错误信息
Expand All @@ -84,11 +84,11 @@ export function initCommands(): void {
.alias('gen')
.option('-t, --type <type>', 'file type: "json" | "yaml" | "javascript"', 'javascript')
.option('-m, --module <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<InitArgv>) => {
try {
init(argv);
process.exit(0);
} catch (error) {
console.error(`😭 初始化失败:${chalk.red((error as Error).message)}\n`);
process.exit(1);
Expand Down Expand Up @@ -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) 为正常退出
}
});

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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>', 'config file path')
.action(viewConfig);

Expand Down
32 changes: 27 additions & 5 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -88,6 +109,7 @@ export function init(options: InitOptions): void {
fs.writeFileSync(outputFilePath, result, 'utf-8');

spinner.succeed(`配置文件创建成功:${outputFilePath}`);

// 打开配置文件
openFile(outputFilePath);
} catch (error) {
Expand Down
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,22 +566,30 @@ export function readProjectPackageJson(startDir: string): PackageJson | null {
* @param filePath - 文件路径
*/
export function openFile(filePath: string): ReturnType<typeof spawn> | 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;
}

0 comments on commit d81c35a

Please sign in to comment.