-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (125 loc) · 4.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
/**
* @File: index.js
* @Author: 夏花
* @Date: 2023-09-27
*/
// 核心模块
import path from 'path';
import fs from 'fs/promises';
// 第三方模块
import inquirer from 'inquirer';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
// 自定义模块
import template from './utils/create/template.js';
import listTemplate from './utils/list.js';
import system from './utils/system.js';
import Logger from './utils/logRecord.js';
/**
* 获取当前文件的路径和目录名
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJsonPath = path.join(__dirname, 'package.json');
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
/**
* 主函数,处理用户输入和操作
*/
const main = async () => {
// 根据不同操作系统设置全局包路径
const globalPath = execSync('npm root -g').toString().trim();
let globalPackagePath;
const osType = system.osType();
switch (osType) {
case 'Windows_NT':
case 'Darwin':
case 'Linux':
globalPackagePath = path.join(globalPath, 'sfnct');
break;
default:
const msg = `当前系统为${osType}, 该系统并不是常见系统,未经测试,如果出现错误请反馈到 github:https://github.com/Dr-SummerFlower/sfnct`;
Logger.warn(msg);
globalPackagePath = path.join(globalPath, 'sfnct');
break;
}
const templateDir = path.join(globalPackagePath, 'template');
const customPath = process.cwd();
const answers = await inquirer.prompt([
{
type: 'list',
name: 'action',
message: '请选择一个操作:',
choices: [
'列出可用模板',
'创建模板',
'安装依赖',
'查看版本号',
'查看系统信息',
'退出',
],
},
]);
if (answers.action === '列出可用模板') {
listTemplate(templateDir);
} else if (answers.action === '创建模板') {
const templates = await fs.readdir(templateDir);
if (templates.length === 0) {
Logger.warn('模板目录为空,请添加模板后再试。');
return;
}
const templateAnswer = await inquirer.prompt([
{
type: 'list',
name: 'templateName',
message: '请选择要创建的模板:',
choices: templates,
default: 'express-app',
},
]);
try {
await template.createTemplateOrFile(
templateAnswer.templateName,
templateDir,
customPath
);
Logger.info('模板创建成功!');
const answerInstall = await inquirer.prompt([
{
type: 'confirm',
name: 'installDependencies',
message: '是否安装依赖?',
},
]);
if (answerInstall.installDependencies) {
try {
template.installDependencies(customPath);
Logger.info('依赖安装成功!');
} catch (err) {
Logger.error('安装依赖时出错:', err);
}
} else {
Logger.info('跳过依赖安装。');
}
} catch (err) {
Logger.error('创建模板时出错:', err);
}
} else if (answers.action === '安装依赖') {
try {
template.installDependencies(customPath);
Logger.info('依赖安装成功!');
} catch (err) {
Logger.error('安装依赖时出错:', err);
}
} else if (answers.action === '查看版本号') {
Logger.info('sfnct-' + packageJson.version);
} else if (answers.action === '查看系统信息') {
const systemInfo = system.systemInformation();
Logger.info(systemInfo);
} else if (answers.action === '退出') {
Logger.info('退出应用。');
} else {
Logger.warn('未知操作,请重试!');
}
};
main().catch((err) => Logger.error(err));