esa-cli 0.0.2-beta.8 โ 0.0.5
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 +2 -14
- package/dist/commands/common/constant.js +0 -19
- package/dist/commands/deploy/helper.js +9 -24
- package/dist/commands/deploy/index.js +10 -25
- package/dist/commands/deployments/list.js +5 -8
- package/dist/commands/dev/ew2/server.js +14 -13
- package/dist/commands/domain/delete.js +4 -4
- package/dist/commands/domain/list.js +3 -3
- package/dist/commands/init/helper.js +118 -0
- package/dist/commands/init/index.js +121 -189
- package/dist/commands/route/add.js +49 -51
- package/dist/commands/route/delete.js +29 -23
- package/dist/commands/route/helper.js +124 -0
- package/dist/commands/route/list.js +53 -14
- package/dist/commands/utils.js +5 -5
- package/dist/components/mutiLevelSelect.js +44 -55
- package/dist/i18n/locales.json +60 -0
- package/dist/libs/api.js +29 -3
- package/dist/libs/apiService.js +84 -28
- package/dist/libs/templates/index.js +2 -1
- package/dist/utils/checkIsRoutineCreated.js +7 -12
- package/package.json +2 -2
|
@@ -9,13 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { getProjectConfig, readEdgeRoutineFile } from '../../utils/fileUtils/index.js';
|
|
11
11
|
import { checkDirectory, checkIsLoginSuccess } from '../utils.js';
|
|
12
|
-
import { displaySelectSpec } from '../deploy/index.js';
|
|
13
12
|
import { descriptionInput } from '../../components/descriptionInput.js';
|
|
14
13
|
import { ApiService } from '../../libs/apiService.js';
|
|
15
14
|
import prodBuild from './prodBuild.js';
|
|
16
15
|
import logger from '../../libs/logger.js';
|
|
17
16
|
import t from '../../i18n/index.js';
|
|
18
|
-
import { exit } from 'process';
|
|
19
17
|
const commit = {
|
|
20
18
|
command: 'commit [entry]',
|
|
21
19
|
describe: `๐ฅ ${t('commit_describe').d('Commit your code, save as a new version')}`,
|
|
@@ -40,7 +38,6 @@ const commit = {
|
|
|
40
38
|
export default commit;
|
|
41
39
|
export function handleCommit(argv) {
|
|
42
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
var _a, _b, _c;
|
|
44
41
|
if (!checkDirectory())
|
|
45
42
|
return;
|
|
46
43
|
const projectConfig = getProjectConfig();
|
|
@@ -53,19 +50,11 @@ export function handleCommit(argv) {
|
|
|
53
50
|
const server = yield ApiService.getInstance();
|
|
54
51
|
const req = { Name: projectConfig.name };
|
|
55
52
|
const response = yield server.getRoutine(req, false);
|
|
56
|
-
let specName = (_a = response === null || response === void 0 ? void 0 : response.data.Envs[0].SpecName) !== null && _a !== void 0 ? _a : '50ms';
|
|
57
53
|
let action = 'Creating';
|
|
58
54
|
let description;
|
|
59
55
|
if (!response) {
|
|
60
56
|
logger.log(`๐
${t('commit_er_not_exist').d('No routine found, creating a new one')}`);
|
|
61
57
|
description = yield descriptionInput(`๐๏ธ ${t('commit_er_description').d('Enter a description for the routine')}:`, false);
|
|
62
|
-
const specList = ((_c = (_b = (yield server.ListRoutineOptionalSpecs())) === null || _b === void 0 ? void 0 : _b.data.Specs) !== null && _c !== void 0 ? _c : []).reduce((acc, item) => {
|
|
63
|
-
if (item.IsAvailable) {
|
|
64
|
-
acc.push(item.SpecName);
|
|
65
|
-
}
|
|
66
|
-
return acc;
|
|
67
|
-
}, []);
|
|
68
|
-
specName = yield displaySelectSpec(specList);
|
|
69
58
|
}
|
|
70
59
|
else {
|
|
71
60
|
logger.log(`๐ ${t('commit_er_exist').d('Routine exists, updating the code')}`);
|
|
@@ -76,8 +65,7 @@ export function handleCommit(argv) {
|
|
|
76
65
|
const edgeRoutine = {
|
|
77
66
|
name: projectConfig.name,
|
|
78
67
|
code,
|
|
79
|
-
description
|
|
80
|
-
specName
|
|
68
|
+
description
|
|
81
69
|
};
|
|
82
70
|
if (action === 'Creating') {
|
|
83
71
|
yield createEdgeRoutine(edgeRoutine);
|
|
@@ -87,7 +75,6 @@ export function handleCommit(argv) {
|
|
|
87
75
|
return;
|
|
88
76
|
yield releaseOfficialVersion(edgeRoutine);
|
|
89
77
|
}
|
|
90
|
-
exit(0);
|
|
91
78
|
}
|
|
92
79
|
catch (error) {
|
|
93
80
|
logger.error(`${t('common_error_occurred').d('An error occurred:')} ${error}`);
|
|
@@ -126,6 +113,7 @@ export function uploadEdgeRoutineCode(edgeRoutine) {
|
|
|
126
113
|
return true;
|
|
127
114
|
}
|
|
128
115
|
catch (error) {
|
|
116
|
+
logger.error('123');
|
|
129
117
|
logger.error(`${t('common_error_occurred').d('An error occurred:')} ${error}`);
|
|
130
118
|
process.exit(0);
|
|
131
119
|
}
|
|
@@ -1,23 +1,4 @@
|
|
|
1
1
|
import t from '../../i18n/index.js';
|
|
2
|
-
export const SUMMARIES_LIST = [
|
|
3
|
-
{
|
|
4
|
-
title: t('summery_cd').d('Enter your routine project folder'),
|
|
5
|
-
command: '๐ก cd [Your Routine Name]'
|
|
6
|
-
},
|
|
7
|
-
{
|
|
8
|
-
title: t('summery_dev').d('Start a local development server for your project'),
|
|
9
|
-
command: '๐ก esa dev'
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
title: t('summery_commit').d('Save a new version of code'),
|
|
13
|
-
command: '๐ก esa commit'
|
|
14
|
-
},
|
|
15
|
-
// Use Deploy or Release?
|
|
16
|
-
{
|
|
17
|
-
title: t('summery_deploy').d('Deploy your project to different environments'),
|
|
18
|
-
command: '๐ก esa deploy'
|
|
19
|
-
}
|
|
20
|
-
];
|
|
21
2
|
export const getSummary = (routineName) => {
|
|
22
3
|
return [
|
|
23
4
|
{
|
|
@@ -13,7 +13,6 @@ import { PublishType } from '../../libs/interface.js';
|
|
|
13
13
|
import { ApiService } from '../../libs/apiService.js';
|
|
14
14
|
import { descriptionInput } from '../../components/descriptionInput.js';
|
|
15
15
|
import { readEdgeRoutineFile } from '../../utils/fileUtils/index.js';
|
|
16
|
-
import { displaySelectSpec } from './index.js';
|
|
17
16
|
import { createEdgeRoutine, releaseOfficialVersion, uploadEdgeRoutineCode } from '../commit/index.js';
|
|
18
17
|
import logger from '../../libs/logger.js';
|
|
19
18
|
import t from '../../i18n/index.js';
|
|
@@ -33,11 +32,14 @@ export function yesNoPromptAndExecute(message, execute) {
|
|
|
33
32
|
}
|
|
34
33
|
export function promptSelectVersion(versionList) {
|
|
35
34
|
const items = versionList
|
|
36
|
-
.filter((version) => version.
|
|
37
|
-
.map((version, index) =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
.filter((version) => version.codeVersion !== 'unstable')
|
|
36
|
+
.map((version, index) => {
|
|
37
|
+
var _a;
|
|
38
|
+
return ({
|
|
39
|
+
label: (_a = version.codeVersion) !== null && _a !== void 0 ? _a : '',
|
|
40
|
+
value: String(index)
|
|
41
|
+
});
|
|
42
|
+
});
|
|
41
43
|
return new Promise((resolve) => {
|
|
42
44
|
const handleSelection = (item) => __awaiter(this, void 0, void 0, function* () {
|
|
43
45
|
resolve(item.label);
|
|
@@ -64,7 +66,6 @@ export function displaySelectDeployType() {
|
|
|
64
66
|
}
|
|
65
67
|
export function createAndDeployVersion(projectConfig_1) {
|
|
66
68
|
return __awaiter(this, arguments, void 0, function* (projectConfig, createUnstable = false, customEntry) {
|
|
67
|
-
var _a, _b, _c, _d;
|
|
68
69
|
try {
|
|
69
70
|
const server = yield ApiService.getInstance();
|
|
70
71
|
const description = yield descriptionInput(createUnstable
|
|
@@ -72,26 +73,10 @@ export function createAndDeployVersion(projectConfig_1) {
|
|
|
72
73
|
: `๐๏ธ ${t('deploy_description_version').d('Enter the description of the code version')}:`, false);
|
|
73
74
|
yield prodBuild(false, customEntry);
|
|
74
75
|
const code = readEdgeRoutineFile();
|
|
75
|
-
const specList = ((_b = (_a = (yield server.ListRoutineOptionalSpecs())) === null || _a === void 0 ? void 0 : _a.data.Specs) !== null && _b !== void 0 ? _b : []).reduce((acc, item) => {
|
|
76
|
-
if (item.IsAvailable) {
|
|
77
|
-
acc.push(item.SpecName);
|
|
78
|
-
}
|
|
79
|
-
return acc;
|
|
80
|
-
}, []);
|
|
81
|
-
let specName;
|
|
82
|
-
if (createUnstable) {
|
|
83
|
-
specName = yield displaySelectSpec(specList);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
const req = { Name: (_c = projectConfig.name) !== null && _c !== void 0 ? _c : '' };
|
|
87
|
-
const response = yield server.getRoutine(req);
|
|
88
|
-
specName = (_d = response === null || response === void 0 ? void 0 : response.data.Envs[0].SpecName) !== null && _d !== void 0 ? _d : '50ms';
|
|
89
|
-
}
|
|
90
76
|
const edgeRoutine = {
|
|
91
77
|
name: projectConfig.name,
|
|
92
78
|
code: code || '',
|
|
93
|
-
description: description
|
|
94
|
-
specName: specName
|
|
79
|
+
description: description
|
|
95
80
|
};
|
|
96
81
|
if (createUnstable) {
|
|
97
82
|
return yield createEdgeRoutine(edgeRoutine);
|
|
@@ -9,12 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import chalk from 'chalk';
|
|
11
11
|
import { getProjectConfig, readEdgeRoutineFile } from '../../utils/fileUtils/index.js';
|
|
12
|
-
import SelectItems from '../../components/selectInput.js';
|
|
13
12
|
import { Environment, PublishType } from '../../libs/interface.js';
|
|
14
13
|
import logger from '../../libs/logger.js';
|
|
15
14
|
import { checkDirectory, checkIsLoginSuccess, getRoutineVersionList } from '../utils.js';
|
|
16
15
|
import { displayMultiSelectTable } from '../../components/mutiSelectTable.js';
|
|
17
|
-
import { Base64 } from 'js-base64';
|
|
18
16
|
import { ApiService } from '../../libs/apiService.js';
|
|
19
17
|
import { createAndDeployVersion, displaySelectDeployType, promptSelectVersion, yesNoPromptAndExecute } from './helper.js';
|
|
20
18
|
import t from '../../i18n/index.js';
|
|
@@ -59,13 +57,13 @@ export function quickDeploy(entry, projectConfig) {
|
|
|
59
57
|
}
|
|
60
58
|
else {
|
|
61
59
|
logger.error(t('quick_deploy_failed').d('Quick deploy failed'));
|
|
62
|
-
|
|
60
|
+
throw Error(t('quick_deploy_failed').d('Quick deploy failed'));
|
|
63
61
|
}
|
|
64
62
|
});
|
|
65
63
|
}
|
|
66
64
|
export function handleDeploy(argv) {
|
|
67
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
var _a, _b, _c, _d
|
|
66
|
+
var _a, _b, _c, _d;
|
|
69
67
|
if (!checkDirectory()) {
|
|
70
68
|
return;
|
|
71
69
|
}
|
|
@@ -80,10 +78,10 @@ export function handleDeploy(argv) {
|
|
|
80
78
|
yield checkRoutineExist(projectConfig.name, entry);
|
|
81
79
|
const req = { Name: projectConfig.name };
|
|
82
80
|
const routineDetail = yield server.getRoutine(req, false);
|
|
83
|
-
const versionList = (
|
|
81
|
+
const versionList = yield getRoutineVersionList(projectConfig.name);
|
|
84
82
|
const customEntry = argv.entry;
|
|
85
|
-
const stagingVersion = (
|
|
86
|
-
const productionVersion = (
|
|
83
|
+
const stagingVersion = (_b = (_a = routineDetail === null || routineDetail === void 0 ? void 0 : routineDetail.data) === null || _a === void 0 ? void 0 : _a.Envs[1]) === null || _b === void 0 ? void 0 : _b.CodeVersion;
|
|
84
|
+
const productionVersion = (_d = (_c = routineDetail === null || routineDetail === void 0 ? void 0 : routineDetail.data) === null || _c === void 0 ? void 0 : _c.Envs[0]) === null || _d === void 0 ? void 0 : _d.CodeVersion;
|
|
87
85
|
if (argv.quick) {
|
|
88
86
|
yield quickDeploy(customEntry, projectConfig);
|
|
89
87
|
exit(0);
|
|
@@ -101,20 +99,6 @@ export function handleDeploy(argv) {
|
|
|
101
99
|
}
|
|
102
100
|
});
|
|
103
101
|
}
|
|
104
|
-
export function displaySelectSpec(specList) {
|
|
105
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
logger.log(`๐ ${t('deploy_spec_select').d('Please select the spec of the routine you want to create')}`);
|
|
107
|
-
const selectItems = specList.map((spec) => {
|
|
108
|
-
return { label: spec, value: spec };
|
|
109
|
-
});
|
|
110
|
-
return new Promise((resolve) => {
|
|
111
|
-
const handleSelection = (item) => __awaiter(this, void 0, void 0, function* () {
|
|
112
|
-
resolve(item.value);
|
|
113
|
-
});
|
|
114
|
-
SelectItems({ items: selectItems, handleSelect: handleSelection });
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
102
|
function handleNoVersionsFound(projectConfig, customEntry) {
|
|
119
103
|
return __awaiter(this, void 0, void 0, function* () {
|
|
120
104
|
logger.log(`๐ ${t('deploy_first_time').d("This is first time to deploy. Let's create a version first!")}`);
|
|
@@ -194,6 +178,7 @@ export function deploySelectedCodeVersion(name, selectedType, version) {
|
|
|
194
178
|
}
|
|
195
179
|
export function displayVersionList(versionList_1) {
|
|
196
180
|
return __awaiter(this, arguments, void 0, function* (versionList, stagingVersion = 'unstable', productionVersion = 'unstable') {
|
|
181
|
+
var _a;
|
|
197
182
|
logger.log(`${chalk.bgYellow('Active')} ${t('deploy_env_staging').d('Staging')}`);
|
|
198
183
|
logger.log(`${chalk.bgGreen('Active')} ${t('deploy_env_production').d('Production')}`);
|
|
199
184
|
const data = [];
|
|
@@ -201,13 +186,13 @@ export function displayVersionList(versionList_1) {
|
|
|
201
186
|
const version = versionList[i];
|
|
202
187
|
const createTime = moment(version.CreateTime).format('YYYY/MM/DD HH:mm:ss');
|
|
203
188
|
const tags = [
|
|
204
|
-
version.
|
|
205
|
-
version.
|
|
189
|
+
version.codeVersion === stagingVersion ? chalk.bgYellow('Active') : '',
|
|
190
|
+
version.codeVersion === productionVersion ? chalk.bgGreen('Active') : ''
|
|
206
191
|
];
|
|
207
192
|
data.push([
|
|
208
|
-
`${version.
|
|
193
|
+
`${version.codeVersion} ${tags.join(' ')}`,
|
|
209
194
|
createTime,
|
|
210
|
-
|
|
195
|
+
(_a = version.codeDescription) !== null && _a !== void 0 ? _a : ''
|
|
211
196
|
]);
|
|
212
197
|
}
|
|
213
198
|
logger.table([
|
|
@@ -62,11 +62,8 @@ function displayListPrompt(routineDetail) {
|
|
|
62
62
|
const coloredStagingIpList = stagingIpList.map((ip) => {
|
|
63
63
|
return chalk.green(ip);
|
|
64
64
|
});
|
|
65
|
-
const showEnvTable = (version,
|
|
66
|
-
const data = [
|
|
67
|
-
{ Version: version },
|
|
68
|
-
{ Specification: spec }
|
|
69
|
-
];
|
|
65
|
+
const showEnvTable = (version, region) => {
|
|
66
|
+
const data = [{ Version: version }];
|
|
70
67
|
if (region) {
|
|
71
68
|
data.push({ Region: region });
|
|
72
69
|
}
|
|
@@ -76,13 +73,13 @@ function displayListPrompt(routineDetail) {
|
|
|
76
73
|
if (stagingIpList.length > 0) {
|
|
77
74
|
logger.log(`Staging IP: ${coloredStagingIpList.join(', ')}`);
|
|
78
75
|
}
|
|
79
|
-
showEnvTable(stagingEnv.CodeVersion
|
|
76
|
+
showEnvTable(stagingEnv.CodeVersion);
|
|
80
77
|
logger.block();
|
|
81
78
|
logger.log(`${chalk.bold(`${t('deploy_env_production').d('Production')} ${!isCanary ? chalk.green('โ') : ''}`)}`);
|
|
82
|
-
showEnvTable(canaryEnv.CodeVersion
|
|
79
|
+
showEnvTable(canaryEnv.CodeVersion);
|
|
83
80
|
logger.block();
|
|
84
81
|
logger.log(`${chalk.bold(`${t('deploy_env_canary').d('Canary')} ${isCanary ? chalk.green('โ') : ''}`)}`);
|
|
85
|
-
showEnvTable((_d = canaryEnv.CanaryCodeVersion) !== null && _d !== void 0 ? _d : '',
|
|
82
|
+
showEnvTable((_d = canaryEnv.CanaryCodeVersion) !== null && _d !== void 0 ? _d : '', (_e = canaryEnv.CanaryAreaList) === null || _e === void 0 ? void 0 : _e.join(', '));
|
|
86
83
|
logger.log(`${t('show_default_url').d(`You can visit:`)} ${chalk.yellowBright(routineDetail.data.DefaultRelatedRecord)}`);
|
|
87
84
|
logger.info(routineDetail.data.DefaultRelatedRecord);
|
|
88
85
|
logger.block();
|
|
@@ -128,7 +128,6 @@ class Ew2Server {
|
|
|
128
128
|
}
|
|
129
129
|
createServer() {
|
|
130
130
|
this.server = http.createServer((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
-
var _a;
|
|
132
131
|
try {
|
|
133
132
|
const host = req.headers.host;
|
|
134
133
|
const url = req.url;
|
|
@@ -148,18 +147,20 @@ class Ew2Server {
|
|
|
148
147
|
// ่งฃๅณ gzip ๅ
ผๅฎนๆง้ฎ้ข๏ผ้ฒๆญขnet::ERR_CONTENT_DECODING_FAILED
|
|
149
148
|
workerHeaders['content-encoding'] = 'identity';
|
|
150
149
|
if (workerRes.body) {
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
else {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
150
|
+
// if (workerRes.headers.get('content-type')?.includes('text/')) {
|
|
151
|
+
// const text = await workerRes.text();
|
|
152
|
+
// // ๅบ็ฐๆข่ก็ฌฆไน็ฑปไผๅฏผ่ด content-length ไธไธ่ด
|
|
153
|
+
// workerHeaders['content-length'] =
|
|
154
|
+
// Buffer.byteLength(text).toString();
|
|
155
|
+
// console.log(workerHeaders['content-length']);
|
|
156
|
+
// res.writeHead(workerRes.status, workerHeaders);
|
|
157
|
+
// res.end(text);
|
|
158
|
+
// } else {
|
|
159
|
+
// res.writeHead(workerRes.status, workerHeaders);
|
|
160
|
+
// workerRes.body.pipe(res);
|
|
161
|
+
// }
|
|
162
|
+
res.writeHead(workerRes.status, workerHeaders);
|
|
163
|
+
workerRes.body.pipe(res);
|
|
163
164
|
logger.log(`[ESA Dev] ${req.method} ${url} ${getColorForStatusCode(workerRes.status, workerRes.statusText)}`);
|
|
164
165
|
}
|
|
165
166
|
else {
|
|
@@ -31,7 +31,7 @@ const deleteDomain = {
|
|
|
31
31
|
export default deleteDomain;
|
|
32
32
|
export function handleDeleteDomain(argv) {
|
|
33
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
var _a
|
|
34
|
+
var _a;
|
|
35
35
|
if (!checkDirectory()) {
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
@@ -44,10 +44,10 @@ export function handleDeleteDomain(argv) {
|
|
|
44
44
|
yield validRoutine(projectConfig.name);
|
|
45
45
|
const server = yield ApiService.getInstance();
|
|
46
46
|
const req = { Name: projectConfig.name || '' };
|
|
47
|
-
const
|
|
48
|
-
if (!
|
|
47
|
+
const listRoutineRelatedRecordRes = yield server.listRoutineRelatedRecords(req);
|
|
48
|
+
if (!listRoutineRelatedRecordRes)
|
|
49
49
|
return;
|
|
50
|
-
const relatedRecords = (
|
|
50
|
+
const relatedRecords = ((_a = listRoutineRelatedRecordRes.data) === null || _a === void 0 ? void 0 : _a.RelatedRecords) || [];
|
|
51
51
|
const relatedDomain = argv.domain;
|
|
52
52
|
const matchedSite = relatedRecords.find((item) => {
|
|
53
53
|
return String(item.RecordName) === relatedDomain;
|
|
@@ -36,10 +36,10 @@ export function handleListDomains(argv) {
|
|
|
36
36
|
yield validRoutine(projectConfig.name);
|
|
37
37
|
const server = yield ApiService.getInstance();
|
|
38
38
|
const req = { Name: projectConfig.name };
|
|
39
|
-
const
|
|
40
|
-
if (!
|
|
39
|
+
const res = yield server.listRoutineRelatedRecords(req);
|
|
40
|
+
if (!res)
|
|
41
41
|
return;
|
|
42
|
-
const relatedRecords = (_b = (_a =
|
|
42
|
+
const relatedRecords = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.RelatedRecords) !== null && _b !== void 0 ? _b : [];
|
|
43
43
|
if (relatedRecords.length === 0) {
|
|
44
44
|
logger.log(`๐
${t('domain_list_empty').d('No related domains found')}`);
|
|
45
45
|
return;
|
|
@@ -0,0 +1,118 @@
|
|
|
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 fs from 'fs-extra';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import Template from '../../libs/templates/index.js';
|
|
13
|
+
import { getProjectConfig } from '../../utils/fileUtils/index.js';
|
|
14
|
+
import logger from '../../libs/logger.js';
|
|
15
|
+
import { execSync } from 'child_process';
|
|
16
|
+
import t from '../../i18n/index.js';
|
|
17
|
+
import chalk from 'chalk';
|
|
18
|
+
import inquirer from 'inquirer';
|
|
19
|
+
import { getDirName } from '../../utils/fileUtils/base.js';
|
|
20
|
+
export const getTemplateInstances = (templateHubPath) => {
|
|
21
|
+
return fs
|
|
22
|
+
.readdirSync(templateHubPath)
|
|
23
|
+
.filter((item) => {
|
|
24
|
+
const itemPath = path.join(templateHubPath, item);
|
|
25
|
+
return (fs.statSync(itemPath).isDirectory() &&
|
|
26
|
+
!['.git', 'node_modules', 'lib'].includes(item));
|
|
27
|
+
})
|
|
28
|
+
.map((item) => {
|
|
29
|
+
var _a;
|
|
30
|
+
const projectPath = path.join(templateHubPath, item);
|
|
31
|
+
const projectConfig = getProjectConfig(projectPath);
|
|
32
|
+
const templateName = (_a = projectConfig === null || projectConfig === void 0 ? void 0 : projectConfig.name) !== null && _a !== void 0 ? _a : '';
|
|
33
|
+
return new Template(projectPath, templateName);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
export const transferTemplatesToSelectItem = (configs, templateInstanceList, lang) => {
|
|
37
|
+
if (!configs)
|
|
38
|
+
return [];
|
|
39
|
+
return configs.map((config) => {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
const title = config.Title_EN;
|
|
42
|
+
const value = (_b = (_a = templateInstanceList.find((template) => {
|
|
43
|
+
return title === template.title;
|
|
44
|
+
})) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : '';
|
|
45
|
+
const children = transferTemplatesToSelectItem(config.children, templateInstanceList, lang);
|
|
46
|
+
return {
|
|
47
|
+
label: lang === 'en' ? config.Title_EN : config.Title_ZH,
|
|
48
|
+
value: value,
|
|
49
|
+
children
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
export const preInstallDependencies = (targetPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
const packageJsonPath = path.join(targetPath, 'package.json');
|
|
55
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
56
|
+
logger.log(t('init_install_dependence').d('โ๏ธ Installing dependencies...'));
|
|
57
|
+
execSync('npm install', { stdio: 'inherit', cwd: targetPath });
|
|
58
|
+
logger.success(t('init_install_dependencies_success').d('Dependencies installed successfully.'));
|
|
59
|
+
// Read and parse package.json to check for build script
|
|
60
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
61
|
+
if (packageJson.scripts && packageJson.scripts.build) {
|
|
62
|
+
logger.log(t('init_build_project').d('โ๏ธ Building project...'));
|
|
63
|
+
execSync('npm run build', { stdio: 'inherit', cwd: targetPath });
|
|
64
|
+
logger.success(t('init_build_project_success').d('Project built successfully.'));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
logger.log(t('no_build_script').d('No build script found in package.json, skipping build step.'));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
export function checkAndUpdatePackage(packageName) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
try {
|
|
74
|
+
// ่ทๅๅฝๅๅฎ่ฃ
็็ๆฌ
|
|
75
|
+
const __dirname = getDirName(import.meta.url);
|
|
76
|
+
console.log(__dirname);
|
|
77
|
+
const packageJsonPath = path.join(__dirname, '../../../');
|
|
78
|
+
const versionInfo = execSync(`npm list ${packageName}`, {
|
|
79
|
+
cwd: packageJsonPath
|
|
80
|
+
}).toString();
|
|
81
|
+
const match = versionInfo.match(new RegExp(`(${packageName})@([0-9.]+)`));
|
|
82
|
+
const currentVersion = match ? match[2] : '';
|
|
83
|
+
// ่ทๅๆๆฐ็ๆฌ
|
|
84
|
+
const latestVersion = execSync(`npm view ${packageName} version`, {
|
|
85
|
+
cwd: packageJsonPath
|
|
86
|
+
})
|
|
87
|
+
.toString()
|
|
88
|
+
.trim();
|
|
89
|
+
if (currentVersion !== latestVersion) {
|
|
90
|
+
logger.log(t('display_current_esa_template_version').d(`Current esa-template version:`) +
|
|
91
|
+
chalk.green(currentVersion) +
|
|
92
|
+
' ' +
|
|
93
|
+
t('display_latest_esa_template_version').d(`Latest esa-template version:`) +
|
|
94
|
+
chalk.green(latestVersion));
|
|
95
|
+
const { isUpdate } = yield inquirer.prompt({
|
|
96
|
+
type: 'confirm',
|
|
97
|
+
name: 'isUpdate',
|
|
98
|
+
message: t('is_update_to_latest_version').d('Do you want to update templates to latest version?')
|
|
99
|
+
});
|
|
100
|
+
if (isUpdate) {
|
|
101
|
+
execSync(`rm -rf node_modules/${packageName} &&rm -rf package-lock.json &&npm install ${packageName}@latest`, {
|
|
102
|
+
cwd: packageJsonPath
|
|
103
|
+
});
|
|
104
|
+
logger.log(t('updated_esa_template_to_latest_version', { packageName }).d(`${packageName} updated successfully`));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
logger.log(t('esa_template_is_latest_version', { packageName }).d(`${packageName} is latest.`));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
console.log(error);
|
|
113
|
+
if (error instanceof Error) {
|
|
114
|
+
logger.error('ๆฃๆตๅๆดๆฐๅ
ๆถๅ็้่ฏฏ๏ผ่ทณ่ฟๆดๆฐๆจก็');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|