Skip to content

Commit

Permalink
chore: 1.Bump version to 0.1.3; 2.add 'view config' command; 3.read c…
Browse files Browse the repository at this point in the history
…onfig from path cwd or homedir; 4.add command argv 'init -g', will generate config file to homedir
  • Loading branch information
longlongago2 committed Jul 15, 2024
1 parent 6530e05 commit 613758b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
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.2",
"version": "0.1.3",
"description": "Command line tools for deploying",
"private": false,
"type": "module",
Expand Down
7 changes: 7 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { backup } from './backup.js';
import { clean } from './clean.js';
import { init } from './init.js';
import { upload } from './upload.js';
import { viewConfig } from './viewConfig.js';

export interface DeployArgv {
config: string;
Expand Down Expand Up @@ -222,5 +223,11 @@ export function initCommands(): void {
}
});

program
.command('view config')
.description('view deploy config file | 查看配置文件')
.option('-c, --config <config>', 'config file path')
.action(viewConfig);

program.parse(process.argv);
}
25 changes: 19 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'node:path';
import url from 'node:url';
import os from 'node:os';
import process from 'node:process';
import fs from 'fs-extra';
import Handlebars from 'handlebars';
import ora from 'ora';
import { DEFAULT_CONFIG_PATHS, findProjectRoot, readProjectPackageJson } from '../utils.js';
import { DEFAULT_CONFIG_PATHS, openFile, readProjectPackageJson } from '../utils.js';

export interface InitOptions {
/**
Expand All @@ -16,6 +17,11 @@ export interface InitOptions {
* javascript 模块导出方式
*/
module?: 'commonjs' | 'cjs' | 'esm' | 'mjs';

/**
* 是否生成全局配置:默认 false,在 cwd 下生成,如果为 true,则在用户目录下生成
*/
global?: boolean;
}

const mapToExt = {
Expand All @@ -32,6 +38,7 @@ const mapToExt = {
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];
Expand All @@ -50,10 +57,9 @@ export function init(options: InitOptions): void {
try {
const filename = url.fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const rootPath = findProjectRoot(dirname);
const pkg = readProjectPackageJson(dirname);
if (!(rootPath && pkg?.name)) {
throw new Error('Function init: project package.json name not found');
if (!pkg?.name) {
throw new Error('Function init: CLI project package.json name not found');
}
let templateFilePath = ''; // 模板文件路径
let data = {}; // 模板参数
Expand All @@ -73,10 +79,17 @@ export function init(options: InitOptions): void {
const result = template(data);

// 写入配置文件
const outputFilePath = path.resolve(process.cwd(), `deploy.config${ext}`);
let outputFilePath = '';
if (global) {
outputFilePath = path.resolve(os.homedir(), `deploy.config${ext}`);
} else {
outputFilePath = path.resolve(process.cwd(), `deploy.config${ext}`);
}
fs.writeFileSync(outputFilePath, result, 'utf-8');

spinner.succeed(`配置文件创建成功:deploy.config${ext}`);
spinner.succeed(`配置文件创建成功:${outputFilePath}`);
// 打开配置文件
openFile(outputFilePath);
} catch (error) {
spinner.clear();
throw new Error(`Generate deploy.config${ext} failed: ${(error as Error).message}`);
Expand Down
12 changes: 12 additions & 0 deletions src/commands/viewConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readDeployConfig } from '../utils.js';

export interface ViewConfigOptions {
config: string;
}

export async function viewConfig(options: ViewConfigOptions): Promise<void> {
const { config } = options;
const result = await readDeployConfig(config);
// depth: null 是 console.dir 方法的一个选项,表示对象的递归深度。设置为 null 意味着没有深度限制,所有嵌套的属性都会被完全展开和显示。
console.dir(result, { depth: null, colors: true });
}
49 changes: 44 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import path from 'node:path';
import url from 'node:url';
import os from 'node:os';
import process from 'node:process';
import { createRequire } from 'node:module';
import { spawn } from 'node:child_process';
import fs from 'fs-extra';
import slash from 'slash';
import archiver from 'archiver';
import yaml from 'js-yaml';
import { createRequire } from 'node:module';
import type { ArchiverOptions } from 'archiver';
import type { FileEntryWithStats, SFTPWrapper } from 'ssh2';
import type { ConfigOptions } from './index.js';
Expand Down Expand Up @@ -145,15 +148,26 @@ export async function readDeployConfig(
): Promise<{ config: DeployOptions; path: string }> {
// 配置文件绝对路径,把所有路径都转为绝对路径,因为配置文件有可能是相对路径
let configAbsolutePath = '';
// 用户不提供,使用默认路径
if (!configPath) {
// 用户不提供,使用默认路径
// 优先在当前目录(cwd)查找
for (const relativePath of DEFAULT_CONFIG_PATHS) {
const absolutePath = path.resolve(process.cwd(), relativePath);
if (fs.pathExistsSync(absolutePath)) {
configAbsolutePath = absolutePath;
const absoluteCwdPath = path.resolve(process.cwd(), relativePath);
if (fs.pathExistsSync(absoluteCwdPath)) {
configAbsolutePath = absoluteCwdPath;
break;
}
}
// 当前目录(cwd)不存在,再在用户目录(homedir)查找
if (!configAbsolutePath) {
for (const relativePath of DEFAULT_CONFIG_PATHS) {
const absoluteHomePath = path.resolve(os.homedir(), relativePath);
if (fs.pathExistsSync(absoluteHomePath)) {
configAbsolutePath = absoluteHomePath;
break;
}
}
}
} else if (path.isAbsolute(configPath)) {
// 用户指定,绝对路径
configAbsolutePath = configPath;
Expand Down Expand Up @@ -546,3 +560,28 @@ export function readProjectPackageJson(startDir: string): PackageJson | null {
}
return pkg;
}

/**
* 打开文件(异步返回 ChildProcess,可通过监听获取进程状态)
* @param filePath - 文件路径
*/
export function openFile(filePath: string): ReturnType<typeof spawn> | null {
let cmd = '';
switch (process.platform) {
case 'darwin':
cmd = 'open';
break;
case 'win32':
cmd = 'start';
break;
case 'linux':
cmd = 'xdg-open';
break;
default:
break;
}
if (cmd) {
return spawn(cmd, [filePath], { stdio: 'inherit' });
}
return null;
}

0 comments on commit 613758b

Please sign in to comment.