Skip to content

Commit

Permalink
feat: support select npm/pnpm/yarn/tnpm/cnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyunhe committed Apr 4, 2024
1 parent 98681d6 commit 606a5b0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
4 changes: 4 additions & 0 deletions packages/f2elint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 更新日志

## 4.4.0 - 2024-04-04

- 支持手动选择 npm/pnpm/yarn/tnpm/cnpm update

## 4.3.1 - 2024-04-04

- 修复 Windows 下项目路径解析失败的问题
Expand Down
37 changes: 27 additions & 10 deletions packages/f2elint/src/f2elint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { readFileSync } from 'fs';
import { dirname, join, resolve } from 'path';
import { fileURLToPath } from 'url';
import { f2elint } from '.';
import { install } from './private/install';
import { runCommand } from './private/runCommand';
import { TemplateType } from './types';

const __dirname = dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -136,16 +136,33 @@ if (process.argv.length > 2 && !process.argv.includes('init')) {
process.exit(1);
}

const s2 = spinner();
s2.start('🚧 正在安装依赖');
const npmCommand = await select<any, string>({
message: '📦 安装或更新依赖',
options: [
'npm update',
'pnpm update',
'yarn update',
'tnpm update',
'cnpm update',
{ value: 'skip', label: '跳过' },
],
});

try {
await install(projectPath);
s2.stop('✅ 安装依赖成功');
} catch (error) {
s2.stop('❌ 安装依赖失败');
console.error(error);
process.exit(1);
if (isCancel(npmCommand)) {
cancel('👋 已取消');
process.exit(0);
}

if (npmCommand !== 'skip') {
const s2 = spinner();
s2.start('🚧 正在安装依赖');
try {
await runCommand(projectPath, npmCommand);
s2.stop('✅ 安装依赖成功');
} catch (error) {
s2.stop('❌ 安装依赖失败,请尝试手动运行命令');
process.exit(1);
}
}

outro('🎉 规约初始化完成,建议安装推荐插件并重启 VS Code');
Expand Down
39 changes: 0 additions & 39 deletions packages/f2elint/src/private/install.ts

This file was deleted.

18 changes: 18 additions & 0 deletions packages/f2elint/src/private/runCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { spawn } from 'child_process';

export async function runCommand(projectPath: string, command: string) {
await new Promise<void>((resolve, reject) => {
const child = spawn(command, {
cwd: projectPath,
shell: true,
});

child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject();
}
});
});
}

0 comments on commit 606a5b0

Please sign in to comment.