esa-cli 0.0.2-beta.2 → 0.0.2-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/commit/index.js +45 -117
- package/dist/commands/commit/prodBuild.js +2 -3
- package/dist/commands/common/constant.js +0 -19
- package/dist/commands/common/utils.js +419 -0
- package/dist/commands/config.js +1 -1
- package/dist/commands/deploy/helper.js +51 -72
- package/dist/commands/deploy/index.js +48 -187
- package/dist/commands/deployments/delete.js +32 -22
- package/dist/commands/deployments/index.js +3 -3
- package/dist/commands/deployments/list.js +22 -38
- package/dist/commands/dev/build.js +3 -3
- package/dist/commands/dev/doProcess.js +5 -5
- package/dist/commands/dev/ew2/cacheService.js +33 -0
- package/dist/commands/dev/ew2/devEntry.js +2 -1
- package/dist/commands/dev/ew2/devPack.js +30 -34
- package/dist/commands/dev/ew2/kvService.js +46 -0
- package/dist/commands/dev/ew2/mock/cache.js +99 -15
- package/dist/commands/dev/ew2/mock/kv.js +142 -21
- package/dist/commands/dev/ew2/server.js +165 -28
- package/dist/commands/dev/index.js +16 -16
- package/dist/commands/dev/mockWorker/devPack.js +32 -21
- package/dist/commands/dev/mockWorker/server.js +7 -6
- package/dist/commands/domain/add.js +3 -3
- package/dist/commands/domain/delete.js +7 -7
- package/dist/commands/domain/index.js +3 -3
- package/dist/commands/domain/list.js +10 -10
- package/dist/commands/init/helper.js +761 -0
- package/dist/commands/init/index.js +88 -220
- package/dist/commands/init/snippets/nextjs/next.config.mjs +6 -0
- package/dist/commands/init/snippets/nextjs/next.config.ts +7 -0
- package/dist/commands/init/snippets/react-router/react-router.config.ts +7 -0
- package/dist/commands/init/template.jsonc +84 -0
- package/dist/commands/init/types.js +1 -0
- package/dist/commands/lang.js +2 -2
- package/dist/commands/login/index.js +74 -34
- package/dist/commands/logout.js +6 -6
- package/dist/commands/route/add.js +138 -49
- package/dist/commands/route/delete.js +33 -27
- package/dist/commands/route/helper.js +124 -0
- package/dist/commands/route/index.js +3 -3
- package/dist/commands/route/list.js +56 -17
- package/dist/commands/routine/delete.js +2 -2
- package/dist/commands/routine/index.js +3 -3
- package/dist/commands/routine/list.js +9 -21
- package/dist/commands/site/index.js +2 -2
- package/dist/commands/site/list.js +6 -7
- package/dist/commands/utils.js +55 -19
- package/dist/components/descriptionInput.js +1 -1
- package/dist/components/mutiLevelSelect.js +43 -55
- package/dist/components/mutiSelectTable.js +1 -1
- package/dist/components/selectInput.js +2 -3
- package/dist/components/selectItem.js +1 -1
- package/dist/docs/Commands_en.md +142 -131
- package/dist/docs/Commands_zh_CN.md +139 -127
- package/dist/i18n/index.js +2 -2
- package/dist/i18n/locales.json +401 -21
- package/dist/index.js +27 -20
- package/dist/libs/api.js +32 -9
- package/dist/libs/apiService.js +262 -84
- package/dist/libs/git/index.js +86 -9
- package/dist/libs/interface.js +0 -1
- package/dist/libs/logger.js +162 -10
- package/dist/libs/service.js +2 -2
- package/dist/libs/templates/index.js +3 -2
- package/dist/utils/checkAssetsExist.js +80 -0
- package/dist/utils/checkDevPort.js +3 -17
- package/dist/utils/checkEntryFileExist.js +10 -0
- package/dist/utils/checkIsRoutineCreated.js +27 -26
- package/dist/utils/checkVersion.js +119 -1
- package/dist/utils/command.js +149 -0
- package/dist/utils/compress.js +136 -0
- package/dist/utils/download.js +182 -0
- package/dist/utils/fileMd5.js +1 -1
- package/dist/utils/fileUtils/base.js +1 -1
- package/dist/utils/fileUtils/index.js +69 -28
- package/dist/utils/installDeno.js +8 -8
- package/dist/utils/installEw2.js +7 -7
- package/dist/utils/openInBrowser.js +1 -1
- package/dist/utils/prompt.js +97 -0
- package/package.json +20 -12
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { execSync } from 'child_process';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import { exit } from 'process';
|
|
13
|
+
import { confirm as clackConfirm, isCancel, log, outro } from '@clack/prompts';
|
|
14
|
+
import chalk from 'chalk';
|
|
15
|
+
import fs from 'fs-extra';
|
|
16
|
+
import Haikunator from 'haikunator';
|
|
17
|
+
import t from '../../i18n/index.js';
|
|
18
|
+
import logger from '../../libs/logger.js';
|
|
19
|
+
import Template from '../../libs/templates/index.js';
|
|
20
|
+
import { execCommand } from '../../utils/command.js';
|
|
21
|
+
import { getDirName } from '../../utils/fileUtils/base.js';
|
|
22
|
+
import { generateConfigFile, getCliConfig, getProjectConfig, getTemplatesConfig, templateHubPath, updateProjectConfigFile } from '../../utils/fileUtils/index.js';
|
|
23
|
+
import promptParameter from '../../utils/prompt.js';
|
|
24
|
+
import { commitAndDeployVersion } from '../common/utils.js';
|
|
25
|
+
export const getTemplateInstances = (templateHubPath) => {
|
|
26
|
+
return fs
|
|
27
|
+
.readdirSync(templateHubPath)
|
|
28
|
+
.filter((item) => {
|
|
29
|
+
const itemPath = path.join(templateHubPath, item);
|
|
30
|
+
return (fs.statSync(itemPath).isDirectory() &&
|
|
31
|
+
!['.git', 'node_modules', 'lib'].includes(item));
|
|
32
|
+
})
|
|
33
|
+
.map((item) => {
|
|
34
|
+
var _a;
|
|
35
|
+
const projectPath = path.join(templateHubPath, item);
|
|
36
|
+
const projectConfig = getProjectConfig(projectPath);
|
|
37
|
+
const templateName = (_a = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.name) !== null && _a !== void 0 ? _a : '';
|
|
38
|
+
return new Template(projectPath, templateName);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
export const transferTemplatesToSelectItem = (configs, templateInstanceList, lang) => {
|
|
42
|
+
if (!configs)
|
|
43
|
+
return [];
|
|
44
|
+
return configs.map((config) => {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
const title = config.Title_EN;
|
|
47
|
+
const value = (_b = (_a = templateInstanceList.find((template) => {
|
|
48
|
+
return title === template.title;
|
|
49
|
+
})) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : '';
|
|
50
|
+
const children = transferTemplatesToSelectItem(config.children, templateInstanceList, lang);
|
|
51
|
+
return {
|
|
52
|
+
label: lang === 'en' ? config.Title_EN : config.Title_ZH,
|
|
53
|
+
value: value,
|
|
54
|
+
hint: lang === 'en' ? config.Desc_EN : config.Desc_ZH,
|
|
55
|
+
children
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
export const preInstallDependencies = (targetPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
+
const packageJsonPath = path.join(targetPath, 'package.json');
|
|
61
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
62
|
+
logger.log(t('init_install_dependence').d('⌛️ Installing dependencies...'));
|
|
63
|
+
execSync('npm install', { stdio: 'inherit', cwd: targetPath });
|
|
64
|
+
logger.success(t('init_install_dependencies_success').d('Dependencies installed successfully.'));
|
|
65
|
+
// Read and parse package.json to check for build script
|
|
66
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
67
|
+
if (packageJson.scripts && packageJson.scripts.build) {
|
|
68
|
+
logger.log(t('init_build_project').d('⌛️ Building project...'));
|
|
69
|
+
execSync('npm run build', { stdio: 'inherit', cwd: targetPath });
|
|
70
|
+
logger.success(t('init_build_project_success').d('Project built successfully.'));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
logger.log(t('no_build_script').d('No build script found in package.json, skipping build step.'));
|
|
74
|
+
}
|
|
75
|
+
// After build, try to infer assets directory if not explicitly known
|
|
76
|
+
try {
|
|
77
|
+
const candidates = ['dist', 'build', 'out'];
|
|
78
|
+
for (const dir of candidates) {
|
|
79
|
+
const abs = path.join(targetPath, dir);
|
|
80
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
81
|
+
// Update config file if present and assets not set
|
|
82
|
+
const projectConfig = getProjectConfig(targetPath);
|
|
83
|
+
if (projectConfig) {
|
|
84
|
+
const { updateProjectConfigFile } = yield import('../../utils/fileUtils/index.js');
|
|
85
|
+
if (!projectConfig.assets || !projectConfig.assets.directory) {
|
|
86
|
+
yield updateProjectConfigFile({ assets: { directory: dir } }, targetPath);
|
|
87
|
+
logger.success(`Detected build output "${dir}" and updated assets.directory`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (_a) { }
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
export function checkAndUpdatePackage(packageName) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
try {
|
|
100
|
+
const spinner = logger.ora;
|
|
101
|
+
spinner.text = t('checking_template_update').d('Checking esa-template updates...');
|
|
102
|
+
spinner.start();
|
|
103
|
+
// 获取当前安装的版本
|
|
104
|
+
const __dirname = getDirName(import.meta.url);
|
|
105
|
+
const packageJsonPath = path.join(__dirname, '../../../');
|
|
106
|
+
let versionInfo;
|
|
107
|
+
try {
|
|
108
|
+
versionInfo = execSync(`npm list ${packageName}`, {
|
|
109
|
+
cwd: packageJsonPath
|
|
110
|
+
}).toString();
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
spinner.text = t('template_updating').d('Updating templates to latest...');
|
|
114
|
+
execSync(`rm -rf node_modules/${packageName}`, {
|
|
115
|
+
cwd: packageJsonPath
|
|
116
|
+
});
|
|
117
|
+
execSync(`npm install ${packageName}@latest`, {
|
|
118
|
+
cwd: packageJsonPath,
|
|
119
|
+
stdio: 'inherit'
|
|
120
|
+
});
|
|
121
|
+
spinner.stop();
|
|
122
|
+
logger.log(`├ ${t('template_updated_to_latest').d('Templates updated to latest.')}`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const match = versionInfo.match(new RegExp(`(${packageName})@([0-9.]+)`));
|
|
126
|
+
const currentVersion = match ? match[2] : '';
|
|
127
|
+
// 获取最新版本
|
|
128
|
+
const latestVersion = execSync(`npm view ${packageName} version`, {
|
|
129
|
+
cwd: packageJsonPath
|
|
130
|
+
})
|
|
131
|
+
.toString()
|
|
132
|
+
.trim();
|
|
133
|
+
if (currentVersion !== latestVersion) {
|
|
134
|
+
spinner.stop();
|
|
135
|
+
logger.log(t('display_current_esa_template_version').d(`Current esa-template version:`) +
|
|
136
|
+
chalk.green(currentVersion) +
|
|
137
|
+
' ' +
|
|
138
|
+
t('display_latest_esa_template_version').d(`Latest esa-template version:`) +
|
|
139
|
+
chalk.green(latestVersion));
|
|
140
|
+
logger.stopSpinner();
|
|
141
|
+
const isUpdate = yield clackConfirm({
|
|
142
|
+
message: t('is_update_to_latest_version').d('Do you want to update templates to latest version?')
|
|
143
|
+
});
|
|
144
|
+
if (!isCancel(isUpdate) && isUpdate) {
|
|
145
|
+
spinner.start(t('template_updating').d('Updating templates to latest...'));
|
|
146
|
+
execSync(`rm -rf node_modules/${packageName}`, {
|
|
147
|
+
cwd: packageJsonPath
|
|
148
|
+
});
|
|
149
|
+
execSync(`rm -rf package-lock.json`, {
|
|
150
|
+
cwd: packageJsonPath
|
|
151
|
+
});
|
|
152
|
+
execSync(`npm install ${packageName}@latest`, {
|
|
153
|
+
cwd: packageJsonPath,
|
|
154
|
+
stdio: 'inherit'
|
|
155
|
+
});
|
|
156
|
+
spinner.stop();
|
|
157
|
+
logger.log(`├ ${t('updated_esa_template_to_latest_version', { packageName }).d(`${packageName} updated successfully`)}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
spinner.stop();
|
|
162
|
+
logger.log(` ${t('checking_esa_template_finished').d(`Checking esa-template finished.`)}`);
|
|
163
|
+
t('esa_template_is_latest_version', { packageName }).d(`${packageName} is latest.`);
|
|
164
|
+
logger.divider();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
console.log(error);
|
|
169
|
+
if (error instanceof Error) {
|
|
170
|
+
logger.ora.fail(t('check_and_update_package_error').d('Error: An error occurred while checking and updating the package, skipping template update'));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
export const getFrameworkConfig = (framework) => {
|
|
176
|
+
// 从init目录读取template.jsonc
|
|
177
|
+
const templatePath = path.join(getDirName(import.meta.url), 'template.jsonc');
|
|
178
|
+
const jsonc = fs.readFileSync(templatePath, 'utf-8');
|
|
179
|
+
const json = JSON.parse(jsonc);
|
|
180
|
+
console.log(json);
|
|
181
|
+
return json[framework];
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* 获取框架全部配置
|
|
185
|
+
* @returns 框架全部配置
|
|
186
|
+
*/
|
|
187
|
+
export const getAllFrameworkConfig = () => {
|
|
188
|
+
// 从init目录读取template.jsonc
|
|
189
|
+
const templatePath = path.join(getDirName(import.meta.url), 'template.jsonc');
|
|
190
|
+
const jsonc = fs.readFileSync(templatePath, 'utf-8');
|
|
191
|
+
const json = JSON.parse(jsonc);
|
|
192
|
+
return json;
|
|
193
|
+
};
|
|
194
|
+
export function getInitParamsFromArgv(argv) {
|
|
195
|
+
const a = argv;
|
|
196
|
+
const HaikunatorCtor = Haikunator;
|
|
197
|
+
const haikunator = new HaikunatorCtor();
|
|
198
|
+
const params = {
|
|
199
|
+
name: ''
|
|
200
|
+
};
|
|
201
|
+
if (a.yes) {
|
|
202
|
+
params.name = haikunator.haikunate();
|
|
203
|
+
params.git = true;
|
|
204
|
+
params.deploy = true;
|
|
205
|
+
params.template = 'Hello World';
|
|
206
|
+
params.framework = undefined;
|
|
207
|
+
params.language = undefined;
|
|
208
|
+
params.yes = true;
|
|
209
|
+
}
|
|
210
|
+
if (typeof a.name === 'string')
|
|
211
|
+
params.name = a.name;
|
|
212
|
+
if (typeof a.template === 'string' && a.template) {
|
|
213
|
+
params.template = a.template;
|
|
214
|
+
params.framework = undefined;
|
|
215
|
+
params.language = undefined;
|
|
216
|
+
params.category = 'template';
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
const fw = a.framework;
|
|
220
|
+
const lang = a.language;
|
|
221
|
+
if (fw) {
|
|
222
|
+
params.framework = fw;
|
|
223
|
+
params.category = 'framework';
|
|
224
|
+
}
|
|
225
|
+
if (lang) {
|
|
226
|
+
params.language = lang;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (typeof a.git === 'boolean')
|
|
230
|
+
params.git = Boolean(a.git);
|
|
231
|
+
if (typeof a.deploy === 'boolean')
|
|
232
|
+
params.deploy = Boolean(a.deploy);
|
|
233
|
+
return params;
|
|
234
|
+
}
|
|
235
|
+
// 配置项目名称
|
|
236
|
+
export const configProjectName = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
237
|
+
if (initParams.name) {
|
|
238
|
+
log.step(`Project name configured ${initParams.name}`);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const HaikunatorCtor = Haikunator;
|
|
242
|
+
const haikunator = new HaikunatorCtor();
|
|
243
|
+
const defaultName = haikunator.haikunate();
|
|
244
|
+
const name = (yield promptParameter({
|
|
245
|
+
type: 'text',
|
|
246
|
+
question: `${t('init_input_name').d('Enter the name of edgeRoutine:')}`,
|
|
247
|
+
label: 'Project name',
|
|
248
|
+
defaultValue: defaultName,
|
|
249
|
+
validate: (input) => {
|
|
250
|
+
if (input === '' || input === undefined) {
|
|
251
|
+
initParams.name = defaultName;
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
const regex = /^[a-z0-9-]{2,}$/;
|
|
255
|
+
if (!regex.test(input)) {
|
|
256
|
+
return t('init_name_error').d('Error: The project name must be at least 2 characters long and can only contain lowercase letters, numbers, and hyphens.');
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
}));
|
|
261
|
+
initParams.name = name;
|
|
262
|
+
});
|
|
263
|
+
export const configCategory = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
264
|
+
if (initParams.category || initParams.framework || initParams.template) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const initMode = (yield promptParameter({
|
|
268
|
+
type: 'select',
|
|
269
|
+
question: 'How would you like to initialize the project?',
|
|
270
|
+
label: 'Init mode',
|
|
271
|
+
choices: [
|
|
272
|
+
{ name: 'Framework Starter', value: 'framework' },
|
|
273
|
+
{
|
|
274
|
+
name: 'Function Template',
|
|
275
|
+
value: 'template'
|
|
276
|
+
}
|
|
277
|
+
]
|
|
278
|
+
}));
|
|
279
|
+
initParams.category = initMode;
|
|
280
|
+
});
|
|
281
|
+
/*
|
|
282
|
+
选择模板
|
|
283
|
+
如果选择的是framework,则选择具体的模版 vue /react等
|
|
284
|
+
如果选择的是template,则选择具体的模版 esa template
|
|
285
|
+
*/
|
|
286
|
+
export const configTemplate = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
287
|
+
if (initParams.template) {
|
|
288
|
+
log.step(`Template configured ${initParams.template}`);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (initParams.framework) {
|
|
292
|
+
log.step(`Framework configured ${initParams.framework}`);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (initParams.category === 'template') {
|
|
296
|
+
const templateItems = prepareTemplateItems();
|
|
297
|
+
const selectedTemplatePath = yield promptParameter({
|
|
298
|
+
type: 'multiLevelSelect',
|
|
299
|
+
question: 'Select a template:',
|
|
300
|
+
treeItems: templateItems
|
|
301
|
+
});
|
|
302
|
+
if (!selectedTemplatePath)
|
|
303
|
+
return null;
|
|
304
|
+
// TODO
|
|
305
|
+
initParams.template = selectedTemplatePath;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const allFrameworkConfig = getAllFrameworkConfig();
|
|
309
|
+
const fw = (yield promptParameter({
|
|
310
|
+
type: 'select',
|
|
311
|
+
question: 'Select a framework',
|
|
312
|
+
label: 'Framework',
|
|
313
|
+
choices: Object.keys(allFrameworkConfig).map((fw) => {
|
|
314
|
+
var _a;
|
|
315
|
+
return ({
|
|
316
|
+
name: allFrameworkConfig[fw].label,
|
|
317
|
+
value: fw,
|
|
318
|
+
hint: (_a = allFrameworkConfig[fw]) === null || _a === void 0 ? void 0 : _a.hint
|
|
319
|
+
});
|
|
320
|
+
})
|
|
321
|
+
}));
|
|
322
|
+
initParams.framework = fw;
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
export const configLanguage = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
326
|
+
if (initParams.language) {
|
|
327
|
+
log.info(`Language configured ${initParams.language}`);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const framework = initParams.framework;
|
|
331
|
+
if (!framework) {
|
|
332
|
+
log.info('Framework config not configured, language skipped');
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const frameworkConfig = getFrameworkConfig(framework);
|
|
336
|
+
if (frameworkConfig.language) {
|
|
337
|
+
const language = (yield promptParameter({
|
|
338
|
+
type: 'select',
|
|
339
|
+
question: t('init_language_select').d('Select programming language:'),
|
|
340
|
+
label: 'Language',
|
|
341
|
+
choices: [
|
|
342
|
+
{
|
|
343
|
+
name: t('init_language_typescript').d('TypeScript (.ts) - Type-safe JavaScript, recommended'),
|
|
344
|
+
value: 'typescript'
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
name: t('init_language_javascript').d('JavaScript (.js) - Traditional JavaScript'),
|
|
348
|
+
value: 'javascript'
|
|
349
|
+
}
|
|
350
|
+
],
|
|
351
|
+
defaultValue: 'typescript'
|
|
352
|
+
}));
|
|
353
|
+
initParams.language = language;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
export const createProject = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
357
|
+
var _a;
|
|
358
|
+
if (initParams.template) {
|
|
359
|
+
// resolve template value: it may be a filesystem path or a template title
|
|
360
|
+
let selectedTemplatePath = initParams.template;
|
|
361
|
+
if (!path.isAbsolute(selectedTemplatePath) ||
|
|
362
|
+
!fs.existsSync(selectedTemplatePath)) {
|
|
363
|
+
const instances = getTemplateInstances(templateHubPath);
|
|
364
|
+
const matched = instances.find((it) => it.title === initParams.template);
|
|
365
|
+
if (matched) {
|
|
366
|
+
selectedTemplatePath = matched.path;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
if (!fs.existsSync(selectedTemplatePath)) {
|
|
370
|
+
outro(`Project creation failed: cannot resolve template "${initParams.template}"`);
|
|
371
|
+
exit(1);
|
|
372
|
+
}
|
|
373
|
+
const res = yield initializeProject(selectedTemplatePath, initParams.name);
|
|
374
|
+
if (!res) {
|
|
375
|
+
outro(`Project creation failed`);
|
|
376
|
+
exit(1);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (initParams.framework) {
|
|
380
|
+
const framework = initParams.framework;
|
|
381
|
+
const frameworkConfig = getFrameworkConfig(framework);
|
|
382
|
+
const command = frameworkConfig.command;
|
|
383
|
+
const templateFlag = ((_a = frameworkConfig.language) === null || _a === void 0 ? void 0 : _a[initParams.language || 'typescript']) || '';
|
|
384
|
+
const extraParams = frameworkConfig.params || '';
|
|
385
|
+
const full = `${command} ${initParams.name} ${templateFlag} ${extraParams}`.trim();
|
|
386
|
+
const res = yield execCommand(['sh', '-lc', full], {
|
|
387
|
+
interactive: true,
|
|
388
|
+
startText: `Starting to execute framework command ${chalk.gray(full)}`,
|
|
389
|
+
doneText: `Framework command executed ${chalk.gray(full)}`
|
|
390
|
+
});
|
|
391
|
+
if (!res.success) {
|
|
392
|
+
outro(`Framework command execution failed`);
|
|
393
|
+
exit(1);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
export const installDependencies = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
398
|
+
if (initParams.template) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
402
|
+
const res = yield execCommand(['npm', 'install'], {
|
|
403
|
+
cwd: targetPath,
|
|
404
|
+
useSpinner: true,
|
|
405
|
+
silent: true,
|
|
406
|
+
startText: 'Installing dependencies',
|
|
407
|
+
doneText: 'Dependencies installed'
|
|
408
|
+
});
|
|
409
|
+
if (!res.success) {
|
|
410
|
+
outro(`Dependencies installation failed`);
|
|
411
|
+
exit(1);
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
/**
|
|
415
|
+
* Apply configured file edits (方式1: overwrite) after project scaffold
|
|
416
|
+
*/
|
|
417
|
+
export const applyFileEdits = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
418
|
+
var _a;
|
|
419
|
+
if (!initParams.framework) {
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
const frameworkConfig = getFrameworkConfig(initParams.framework || '');
|
|
423
|
+
const edits = frameworkConfig.fileEdits || [];
|
|
424
|
+
if (!edits.length)
|
|
425
|
+
return true;
|
|
426
|
+
logger.startSubStep(`Applying file edits`);
|
|
427
|
+
const __dirname = getDirName(import.meta.url);
|
|
428
|
+
try {
|
|
429
|
+
const toRegexFromGlob = (pattern) => {
|
|
430
|
+
// Very small glob subset: *, ?, {a,b,c}
|
|
431
|
+
let escaped = pattern
|
|
432
|
+
.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&') // escape regex specials first
|
|
433
|
+
.replace(/\\\*/g, '.*')
|
|
434
|
+
.replace(/\\\?/g, '.');
|
|
435
|
+
// restore and convert {a,b} to (a|b)
|
|
436
|
+
escaped = escaped.replace(/\\\{([^}]+)\\\}/g, (_, inner) => {
|
|
437
|
+
const parts = inner.split(',').map((s) => s.trim());
|
|
438
|
+
return `(${parts.join('|')})`;
|
|
439
|
+
});
|
|
440
|
+
return new RegExp('^' + escaped + '$');
|
|
441
|
+
};
|
|
442
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
443
|
+
const listRootFiles = () => {
|
|
444
|
+
try {
|
|
445
|
+
return fs.readdirSync(targetPath);
|
|
446
|
+
}
|
|
447
|
+
catch (_a) {
|
|
448
|
+
return [];
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
for (const edit of edits) {
|
|
452
|
+
if (((_a = edit.when) === null || _a === void 0 ? void 0 : _a.language) && initParams.language) {
|
|
453
|
+
if (edit.when.language !== initParams.language)
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
const createIfMissing = edit.createIfMissing !== false;
|
|
457
|
+
let matchedFiles = [];
|
|
458
|
+
if (edit.matchType === 'exact') {
|
|
459
|
+
matchedFiles = [edit.match];
|
|
460
|
+
}
|
|
461
|
+
else if (edit.matchType === 'glob') {
|
|
462
|
+
const regex = toRegexFromGlob(edit.match);
|
|
463
|
+
matchedFiles = listRootFiles().filter((name) => regex.test(name));
|
|
464
|
+
}
|
|
465
|
+
else if (edit.matchType === 'regex') {
|
|
466
|
+
const regex = new RegExp(edit.match);
|
|
467
|
+
matchedFiles = listRootFiles().filter((name) => regex.test(name));
|
|
468
|
+
}
|
|
469
|
+
// if no matched files and allowed to create, provide a reasonable default for common patterns
|
|
470
|
+
if (!matchedFiles.length && createIfMissing) {
|
|
471
|
+
if (edit.matchType === 'exact') {
|
|
472
|
+
matchedFiles = [edit.match];
|
|
473
|
+
}
|
|
474
|
+
else if (edit.matchType === 'glob' &&
|
|
475
|
+
/next\.config\.\{.*\}/.test(edit.match)) {
|
|
476
|
+
matchedFiles = ['next.config.ts'];
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
if (!matchedFiles.length)
|
|
480
|
+
continue;
|
|
481
|
+
// resolve content
|
|
482
|
+
let payload = null;
|
|
483
|
+
if (edit.fromFile) {
|
|
484
|
+
const absFrom = path.isAbsolute(edit.fromFile)
|
|
485
|
+
? edit.fromFile
|
|
486
|
+
: path.join(__dirname, edit.fromFile);
|
|
487
|
+
payload = fs.readFileSync(absFrom, 'utf-8');
|
|
488
|
+
}
|
|
489
|
+
else if (typeof edit.content === 'string') {
|
|
490
|
+
payload = edit.content;
|
|
491
|
+
}
|
|
492
|
+
for (const rel of matchedFiles) {
|
|
493
|
+
const abs = path.join(targetPath, rel);
|
|
494
|
+
if (payload == null)
|
|
495
|
+
continue;
|
|
496
|
+
fs.ensureDirSync(path.dirname(abs));
|
|
497
|
+
fs.writeFileSync(abs, payload, 'utf-8');
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
logger.endSubStep('File edits applied');
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
catch (_b) {
|
|
504
|
+
outro(`File edits application failed`);
|
|
505
|
+
exit(1);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
export const installESACli = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
509
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
510
|
+
const res = yield execCommand(['npm', 'install', '-D', 'esa-cli'], {
|
|
511
|
+
cwd: targetPath,
|
|
512
|
+
useSpinner: true,
|
|
513
|
+
silent: true,
|
|
514
|
+
startText: 'Installing ESA CLI',
|
|
515
|
+
doneText: 'ESA CLI installed in the project'
|
|
516
|
+
});
|
|
517
|
+
if (!res.success) {
|
|
518
|
+
outro(`ESA CLI installation failed`);
|
|
519
|
+
exit(1);
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
export const updateConfigFile = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
523
|
+
var _a, _b;
|
|
524
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
525
|
+
const configFormat = 'jsonc';
|
|
526
|
+
logger.startSubStep(`Updating config file`);
|
|
527
|
+
try {
|
|
528
|
+
if (initParams.framework) {
|
|
529
|
+
const frameworkConfig = getFrameworkConfig(initParams.framework);
|
|
530
|
+
const assetsDirectory = (_a = frameworkConfig.assets) === null || _a === void 0 ? void 0 : _a.directory;
|
|
531
|
+
const notFoundStrategy = (_b = frameworkConfig.assets) === null || _b === void 0 ? void 0 : _b.notFoundStrategy;
|
|
532
|
+
yield generateConfigFile(initParams.name, {
|
|
533
|
+
assets: assetsDirectory ? { directory: assetsDirectory } : undefined
|
|
534
|
+
}, targetPath, configFormat, notFoundStrategy);
|
|
535
|
+
}
|
|
536
|
+
else {
|
|
537
|
+
// TODO revise template config file later
|
|
538
|
+
// console.log(
|
|
539
|
+
// 'test:',
|
|
540
|
+
// initParams.name,
|
|
541
|
+
// undefined,
|
|
542
|
+
// targetPath,
|
|
543
|
+
// configFormat
|
|
544
|
+
// );
|
|
545
|
+
// logger.startSubStep(`Updating config file`);
|
|
546
|
+
// await generateConfigFile(initParams.name, undefined, targetPath, 'toml');
|
|
547
|
+
}
|
|
548
|
+
logger.endSubStep('Config file updated');
|
|
549
|
+
}
|
|
550
|
+
catch (_c) {
|
|
551
|
+
outro(`Config file update failed`);
|
|
552
|
+
exit(1);
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
export const initGit = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
556
|
+
var _a;
|
|
557
|
+
const frameworkConfig = getFrameworkConfig(initParams.framework || '');
|
|
558
|
+
if ((frameworkConfig === null || frameworkConfig === void 0 ? void 0 : frameworkConfig.useGit) === false) {
|
|
559
|
+
log.step('Git skipped');
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
const gitInstalled = yield isGitInstalled();
|
|
563
|
+
if (!gitInstalled) {
|
|
564
|
+
log.step('You have not installed Git, Git skipped');
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
if (!initParams.git) {
|
|
568
|
+
const initGit = (yield promptParameter({
|
|
569
|
+
type: 'confirm',
|
|
570
|
+
question: t('init_git').d('Do you want to init git in your project?'),
|
|
571
|
+
label: 'Init git',
|
|
572
|
+
defaultValue: false
|
|
573
|
+
}));
|
|
574
|
+
initParams.git = initGit;
|
|
575
|
+
}
|
|
576
|
+
if (initParams.git) {
|
|
577
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
578
|
+
const res = yield execCommand(['git', 'init'], {
|
|
579
|
+
cwd: targetPath,
|
|
580
|
+
silent: true,
|
|
581
|
+
startText: 'Initializing git',
|
|
582
|
+
doneText: 'Git initialized'
|
|
583
|
+
});
|
|
584
|
+
if (!res.success) {
|
|
585
|
+
outro(`Git initialization failed`);
|
|
586
|
+
exit(1);
|
|
587
|
+
}
|
|
588
|
+
// Ensure .gitignore exists and has sensible defaults
|
|
589
|
+
yield ensureGitignore(targetPath, (_a = frameworkConfig === null || frameworkConfig === void 0 ? void 0 : frameworkConfig.assets) === null || _a === void 0 ? void 0 : _a.directory);
|
|
590
|
+
}
|
|
591
|
+
return true;
|
|
592
|
+
});
|
|
593
|
+
export function getGitVersion() {
|
|
594
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
595
|
+
try {
|
|
596
|
+
const stdout = yield execCommand(['git', '--version'], {
|
|
597
|
+
useSpinner: false,
|
|
598
|
+
silent: true,
|
|
599
|
+
captureOutput: true
|
|
600
|
+
});
|
|
601
|
+
const gitVersion = stdout.stdout.replace(/^git\s+version\s+/, '');
|
|
602
|
+
return gitVersion;
|
|
603
|
+
}
|
|
604
|
+
catch (_a) {
|
|
605
|
+
log.error('Failed to get Git version');
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
export function isGitInstalled() {
|
|
611
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
612
|
+
return (yield getGitVersion()) !== null;
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Create or update .gitignore in project root with sensible defaults.
|
|
617
|
+
* - Preserves existing entries and comments
|
|
618
|
+
* - Avoids duplicates
|
|
619
|
+
* - Adds framework assets directory if provided
|
|
620
|
+
*/
|
|
621
|
+
function ensureGitignore(projectRoot, assetsDirectory) {
|
|
622
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
623
|
+
try {
|
|
624
|
+
const gitignorePath = path.join(projectRoot, '.gitignore');
|
|
625
|
+
const defaults = [
|
|
626
|
+
'# Logs',
|
|
627
|
+
'logs',
|
|
628
|
+
'*.log',
|
|
629
|
+
'npm-debug.log*',
|
|
630
|
+
'yarn-debug.log*',
|
|
631
|
+
'yarn-error.log*',
|
|
632
|
+
'pnpm-debug.log*',
|
|
633
|
+
'',
|
|
634
|
+
'# Node modules',
|
|
635
|
+
'node_modules/',
|
|
636
|
+
'',
|
|
637
|
+
'# Build output',
|
|
638
|
+
'dist/',
|
|
639
|
+
'build/',
|
|
640
|
+
'out/',
|
|
641
|
+
'.next/',
|
|
642
|
+
'.nuxt/',
|
|
643
|
+
'coverage/',
|
|
644
|
+
'.vite/',
|
|
645
|
+
'',
|
|
646
|
+
'# Env files',
|
|
647
|
+
'.env',
|
|
648
|
+
'.env.local',
|
|
649
|
+
'.env.development.local',
|
|
650
|
+
'.env.test.local',
|
|
651
|
+
'.env.production.local',
|
|
652
|
+
'',
|
|
653
|
+
'# IDE/editor',
|
|
654
|
+
'.DS_Store',
|
|
655
|
+
'.idea/',
|
|
656
|
+
'.vscode/',
|
|
657
|
+
'',
|
|
658
|
+
'# Misc caches',
|
|
659
|
+
'.eslintcache',
|
|
660
|
+
'.parcel-cache/',
|
|
661
|
+
'.turbo/',
|
|
662
|
+
'.cache/'
|
|
663
|
+
];
|
|
664
|
+
// Include assets directory if provided and not a common default
|
|
665
|
+
if (assetsDirectory &&
|
|
666
|
+
!['dist', 'build', 'out'].includes(assetsDirectory.replace(/\/$/, ''))) {
|
|
667
|
+
defaults.push('', '# Project assets output', `${assetsDirectory}/`);
|
|
668
|
+
}
|
|
669
|
+
let existingContent = '';
|
|
670
|
+
if (fs.existsSync(gitignorePath)) {
|
|
671
|
+
existingContent = fs.readFileSync(gitignorePath, 'utf-8');
|
|
672
|
+
}
|
|
673
|
+
const existingLines = new Set(existingContent.split(/\r?\n/).map((l) => l.trimEnd()));
|
|
674
|
+
const toAppend = [];
|
|
675
|
+
for (const line of defaults) {
|
|
676
|
+
if (!existingLines.has(line)) {
|
|
677
|
+
toAppend.push(line);
|
|
678
|
+
existingLines.add(line);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
// If nothing to add, keep as is
|
|
682
|
+
if (!toAppend.length)
|
|
683
|
+
return;
|
|
684
|
+
const newContent = existingContent
|
|
685
|
+
? `${existingContent.replace(/\n$/, '')}\n${toAppend.join('\n')}\n`
|
|
686
|
+
: `${toAppend.join('\n')}\n`;
|
|
687
|
+
fs.writeFileSync(gitignorePath, newContent, 'utf-8');
|
|
688
|
+
logger.log('Updated .gitignore');
|
|
689
|
+
}
|
|
690
|
+
catch (_a) {
|
|
691
|
+
// Do not fail init due to .gitignore issues
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
export const buildProject = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
696
|
+
if (initParams.template) {
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
700
|
+
const res = yield execCommand(['npm', 'run', 'build'], {
|
|
701
|
+
useSpinner: true,
|
|
702
|
+
silent: true,
|
|
703
|
+
startText: 'Building project',
|
|
704
|
+
doneText: 'Project built',
|
|
705
|
+
cwd: targetPath
|
|
706
|
+
});
|
|
707
|
+
if (!res.success) {
|
|
708
|
+
outro(`Build project failed`);
|
|
709
|
+
exit(1);
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
export function prepareTemplateItems() {
|
|
713
|
+
var _a;
|
|
714
|
+
const templateInstanceList = getTemplateInstances(templateHubPath);
|
|
715
|
+
const templateConfig = getTemplatesConfig();
|
|
716
|
+
const cliConfig = getCliConfig();
|
|
717
|
+
const lang = (_a = cliConfig === null || cliConfig === void 0 ? void 0 : cliConfig.lang) !== null && _a !== void 0 ? _a : 'en';
|
|
718
|
+
return transferTemplatesToSelectItem(templateConfig, templateInstanceList, lang);
|
|
719
|
+
}
|
|
720
|
+
export const deployProject = (initParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
721
|
+
if (!initParams.deploy) {
|
|
722
|
+
log.step('Deploy project skipped');
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const targetPath = path.join(process.cwd(), initParams.name);
|
|
726
|
+
const res = yield commitAndDeployVersion(initParams.name, undefined, undefined, 'Init project', targetPath, 'all');
|
|
727
|
+
if (!res) {
|
|
728
|
+
outro(`Deploy project failed`);
|
|
729
|
+
exit(1);
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
export function initializeProject(selectedTemplatePath, name) {
|
|
733
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
734
|
+
const selectTemplate = new Template(selectedTemplatePath, name);
|
|
735
|
+
const projectConfig = getProjectConfig(selectedTemplatePath);
|
|
736
|
+
if (!projectConfig) {
|
|
737
|
+
logger.notInProject();
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
const targetPath = path.join(process.cwd(), name);
|
|
741
|
+
if (fs.existsSync(targetPath)) {
|
|
742
|
+
logger.block();
|
|
743
|
+
logger.tree([
|
|
744
|
+
`${chalk.bgRed(' ERROR ')} ${chalk.bold.red(t('init_abort').d('Initialization aborted'))}`,
|
|
745
|
+
`${chalk.gray(t('reason').d('Reason:'))} ${chalk.red(t('dir_already_exists').d('Target directory already exists'))}`,
|
|
746
|
+
`${chalk.gray(t('path').d('Path:'))} ${chalk.cyan(targetPath)}`,
|
|
747
|
+
chalk.gray(t('try').d('Try one of the following:')),
|
|
748
|
+
`- ${chalk.white(t('try_diff_name').d('Choose a different project name'))}`,
|
|
749
|
+
`- ${chalk.white(t('try_remove').d('Remove the directory:'))} ${chalk.yellow(`rm -rf "${name}”`)}`,
|
|
750
|
+
`- ${chalk.white(t('try_another_dir').d('Run the command in another directory'))}`
|
|
751
|
+
]);
|
|
752
|
+
logger.block();
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
yield fs.copy(selectedTemplatePath, targetPath);
|
|
756
|
+
projectConfig.name = name;
|
|
757
|
+
yield updateProjectConfigFile(projectConfig, targetPath);
|
|
758
|
+
yield preInstallDependencies(targetPath);
|
|
759
|
+
return { template: selectTemplate, targetPath };
|
|
760
|
+
});
|
|
761
|
+
}
|