esa-cli 0.0.2-beta.10 → 0.0.2-beta.12
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 +34 -24
- package/dist/commands/deploy/helper.js +10 -26
- package/dist/commands/deploy/index.js +49 -60
- package/dist/commands/deployments/delete.js +1 -16
- package/dist/commands/deployments/index.js +1 -1
- package/dist/commands/deployments/list.js +8 -15
- 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 +5 -3
- package/dist/commands/dev/ew2/kvService.js +27 -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 +150 -18
- package/dist/commands/dev/index.js +2 -3
- package/dist/commands/domain/add.js +1 -1
- package/dist/commands/domain/delete.js +4 -4
- package/dist/commands/domain/index.js +1 -1
- package/dist/commands/domain/list.js +3 -3
- package/dist/commands/init/helper.js +28 -4
- package/dist/commands/init/index.js +78 -14
- package/dist/commands/login/index.js +49 -3
- package/dist/commands/logout.js +1 -1
- package/dist/commands/route/add.js +50 -52
- package/dist/commands/route/delete.js +29 -23
- package/dist/commands/route/helper.js +124 -0
- package/dist/commands/route/index.js +1 -1
- package/dist/commands/route/list.js +53 -14
- package/dist/commands/routine/index.js +1 -1
- package/dist/commands/routine/list.js +4 -5
- package/dist/commands/site/index.js +1 -1
- package/dist/commands/utils.js +5 -5
- package/dist/docs/Commands_en.md +27 -13
- package/dist/docs/Commands_zh_CN.md +14 -0
- package/dist/docs/Dev_en.md +0 -0
- package/dist/docs/Dev_zh_CN.md +0 -0
- package/dist/i18n/locales.json +102 -10
- package/dist/index.js +6 -1
- package/dist/libs/api.js +32 -9
- package/dist/libs/apiService.js +88 -78
- package/dist/libs/interface.js +0 -1
- package/dist/utils/checkIsRoutineCreated.js +0 -16
- package/package.json +3 -4
|
@@ -15,8 +15,8 @@ import logger from '../../libs/logger.js';
|
|
|
15
15
|
import { execSync } from 'child_process';
|
|
16
16
|
import t from '../../i18n/index.js';
|
|
17
17
|
import chalk from 'chalk';
|
|
18
|
-
import { getDirName } from '../../utils/fileUtils/base.js';
|
|
19
18
|
import inquirer from 'inquirer';
|
|
19
|
+
import { getDirName } from '../../utils/fileUtils/base.js';
|
|
20
20
|
export const getTemplateInstances = (templateHubPath) => {
|
|
21
21
|
return fs
|
|
22
22
|
.readdirSync(templateHubPath)
|
|
@@ -74,11 +74,28 @@ export function checkAndUpdatePackage(packageName) {
|
|
|
74
74
|
// 获取当前安装的版本
|
|
75
75
|
const __dirname = getDirName(import.meta.url);
|
|
76
76
|
const packageJsonPath = path.join(__dirname, '../../../');
|
|
77
|
-
|
|
77
|
+
let versionInfo;
|
|
78
|
+
try {
|
|
79
|
+
versionInfo = execSync(`npm list ${packageName}`, {
|
|
80
|
+
cwd: packageJsonPath
|
|
81
|
+
}).toString();
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
execSync(`rm -rf node_modules/${packageName}`, {
|
|
85
|
+
cwd: packageJsonPath
|
|
86
|
+
});
|
|
87
|
+
execSync(`npm install ${packageName}@latest`, {
|
|
88
|
+
cwd: packageJsonPath,
|
|
89
|
+
stdio: 'inherit'
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
78
93
|
const match = versionInfo.match(new RegExp(`(${packageName})@([0-9.]+)`));
|
|
79
94
|
const currentVersion = match ? match[2] : '';
|
|
80
95
|
// 获取最新版本
|
|
81
|
-
const latestVersion = execSync(`npm view ${packageName} version
|
|
96
|
+
const latestVersion = execSync(`npm view ${packageName} version`, {
|
|
97
|
+
cwd: packageJsonPath
|
|
98
|
+
})
|
|
82
99
|
.toString()
|
|
83
100
|
.trim();
|
|
84
101
|
if (currentVersion !== latestVersion) {
|
|
@@ -93,9 +110,16 @@ export function checkAndUpdatePackage(packageName) {
|
|
|
93
110
|
message: t('is_update_to_latest_version').d('Do you want to update templates to latest version?')
|
|
94
111
|
});
|
|
95
112
|
if (isUpdate) {
|
|
96
|
-
execSync(`rm -rf node_modules/${packageName}
|
|
113
|
+
execSync(`rm -rf node_modules/${packageName}`, {
|
|
114
|
+
cwd: packageJsonPath
|
|
115
|
+
});
|
|
116
|
+
execSync(`rm -rf package-lock.json`, {
|
|
97
117
|
cwd: packageJsonPath
|
|
98
118
|
});
|
|
119
|
+
execSync(`npm install ${packageName}@latest`, {
|
|
120
|
+
cwd: packageJsonPath,
|
|
121
|
+
stdio: 'inherit'
|
|
122
|
+
});
|
|
99
123
|
logger.log(t('updated_esa_template_to_latest_version', { packageName }).d(`${packageName} updated successfully`));
|
|
100
124
|
}
|
|
101
125
|
}
|
|
@@ -24,13 +24,29 @@ import { checkIsLoginSuccess } from '../utils.js';
|
|
|
24
24
|
import chalk from 'chalk';
|
|
25
25
|
import { checkAndUpdatePackage, getTemplateInstances, preInstallDependencies, transferTemplatesToSelectItem } from './helper.js';
|
|
26
26
|
const init = {
|
|
27
|
-
command: 'init',
|
|
27
|
+
command: 'init [name]',
|
|
28
28
|
describe: `📥 ${t('init_describe').d('Initialize a routine with a template')}`,
|
|
29
29
|
builder: (yargs) => {
|
|
30
|
-
return yargs
|
|
30
|
+
return yargs
|
|
31
|
+
.positional('name', {
|
|
32
|
+
describe: t('init_project_name').d('Project name'),
|
|
33
|
+
type: 'string'
|
|
34
|
+
})
|
|
35
|
+
.option('template', {
|
|
36
|
+
alias: 't',
|
|
37
|
+
describe: t('init_template_name').d('Template name to use'),
|
|
38
|
+
type: 'string'
|
|
39
|
+
})
|
|
40
|
+
.option('config', {
|
|
31
41
|
alias: 'c',
|
|
32
42
|
describe: t('init_config_file').d('Generate a config file for your project'),
|
|
33
43
|
type: 'boolean'
|
|
44
|
+
})
|
|
45
|
+
.option('skip', {
|
|
46
|
+
alias: 's',
|
|
47
|
+
describe: t('init_skip').d('Skip the project git and deployment initialization'),
|
|
48
|
+
type: 'boolean',
|
|
49
|
+
default: false
|
|
34
50
|
});
|
|
35
51
|
},
|
|
36
52
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -39,13 +55,35 @@ const init = {
|
|
|
39
55
|
})
|
|
40
56
|
};
|
|
41
57
|
export default init;
|
|
58
|
+
export function findTemplatePathByName(templateName) {
|
|
59
|
+
const templateInstanceList = getTemplateInstances(templateHubPath);
|
|
60
|
+
const templateConfig = getTemplatesConfig();
|
|
61
|
+
// find template recursively
|
|
62
|
+
function findTemplateRecursive(configs) {
|
|
63
|
+
for (const config of configs) {
|
|
64
|
+
const title = config.Title_EN;
|
|
65
|
+
if (title === templateName) {
|
|
66
|
+
const template = templateInstanceList.find((template) => {
|
|
67
|
+
return config.Title_EN === template.title;
|
|
68
|
+
});
|
|
69
|
+
return (template === null || template === void 0 ? void 0 : template.path) || null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return findTemplateRecursive(templateConfig);
|
|
75
|
+
}
|
|
76
|
+
export function validateProjectName(name) {
|
|
77
|
+
const regex = /^[a-z0-9-]{2,}$/;
|
|
78
|
+
return regex.test(name);
|
|
79
|
+
}
|
|
42
80
|
export function promptProjectName() {
|
|
43
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
82
|
const { name } = yield inquirer.prompt([
|
|
45
83
|
{
|
|
46
84
|
type: 'input',
|
|
47
85
|
name: 'name',
|
|
48
|
-
message:
|
|
86
|
+
message: `${t('init_input_name').d('Enter the name of edgeRoutine:')}`,
|
|
49
87
|
validate: (input) => {
|
|
50
88
|
const regex = /^[a-z0-9-]{2,}$/;
|
|
51
89
|
if (!regex.test(input)) {
|
|
@@ -151,12 +189,34 @@ export function handleInit(argv) {
|
|
|
151
189
|
if (config === undefined) {
|
|
152
190
|
yield generateConfigFile(String(config));
|
|
153
191
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
192
|
+
// Handle project name parameter
|
|
193
|
+
let name = argv.name;
|
|
194
|
+
if (!name) {
|
|
195
|
+
name = yield promptProjectName();
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
if (!validateProjectName(name)) {
|
|
199
|
+
logger.error(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.'));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
// Handle template name parameter
|
|
204
|
+
let selectedTemplatePath = null;
|
|
205
|
+
if (argv.template) {
|
|
206
|
+
const templateName = argv.template;
|
|
207
|
+
selectedTemplatePath = findTemplatePathByName(templateName);
|
|
208
|
+
if (!selectedTemplatePath) {
|
|
209
|
+
logger.error(t('init_template_not_found').d(`Template "${templateName}" not found. Please check the template name and try again.`));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
const templateItems = prepareTemplateItems();
|
|
215
|
+
// Select a template
|
|
216
|
+
selectedTemplatePath = yield selectTemplate(templateItems);
|
|
217
|
+
if (!selectedTemplatePath) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
160
220
|
}
|
|
161
221
|
// Initialize project files and configuration
|
|
162
222
|
const project = yield initializeProject(selectedTemplatePath, name);
|
|
@@ -164,11 +224,15 @@ export function handleInit(argv) {
|
|
|
164
224
|
return;
|
|
165
225
|
}
|
|
166
226
|
const { template, targetPath } = project;
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
227
|
+
if (!argv.skip) {
|
|
228
|
+
// Handle Git initialization
|
|
229
|
+
yield handleGitInitialization(targetPath);
|
|
230
|
+
}
|
|
231
|
+
if (!argv.skip) {
|
|
232
|
+
// Handle deployment
|
|
233
|
+
const projectConfig = getProjectConfig(targetPath);
|
|
234
|
+
yield handleDeployment(targetPath, projectConfig);
|
|
235
|
+
}
|
|
172
236
|
template.printSummary();
|
|
173
237
|
return;
|
|
174
238
|
});
|
|
@@ -16,15 +16,35 @@ import logger from '../../libs/logger.js';
|
|
|
16
16
|
const login = {
|
|
17
17
|
command: 'login',
|
|
18
18
|
describe: `🔑 ${t('login_describe').d('Login to the server')}`,
|
|
19
|
-
builder: {
|
|
19
|
+
builder: (yargs) => {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
return yargs
|
|
22
|
+
.option('access-key-id', {
|
|
23
|
+
alias: 'ak',
|
|
24
|
+
describe: (_a = t('login_option_access_key_id')) === null || _a === void 0 ? void 0 : _a.d('AccessKey ID'),
|
|
25
|
+
type: 'string'
|
|
26
|
+
})
|
|
27
|
+
.option('access-key-secret', {
|
|
28
|
+
alias: 'sk',
|
|
29
|
+
describe: (_b = t('login_option_access_key_secret')) === null || _b === void 0 ? void 0 : _b.d('AccessKey Secret'),
|
|
30
|
+
type: 'string'
|
|
31
|
+
});
|
|
32
|
+
},
|
|
20
33
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
-
handleLogin();
|
|
34
|
+
handleLogin(argv);
|
|
22
35
|
})
|
|
23
36
|
};
|
|
24
37
|
export default login;
|
|
25
|
-
export function handleLogin() {
|
|
38
|
+
export function handleLogin(argv) {
|
|
26
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
27
40
|
generateDefaultConfig();
|
|
41
|
+
// Prioritize command line parameters
|
|
42
|
+
const accessKeyId = argv === null || argv === void 0 ? void 0 : argv['access-key-id'];
|
|
43
|
+
const accessKeySecret = argv === null || argv === void 0 ? void 0 : argv['access-key-secret'];
|
|
44
|
+
if (accessKeyId && accessKeySecret) {
|
|
45
|
+
yield handleLoginWithAKSK(accessKeyId, accessKeySecret);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
28
48
|
const cliConfig = getCliConfig();
|
|
29
49
|
if (!cliConfig)
|
|
30
50
|
return;
|
|
@@ -64,6 +84,32 @@ export function handleLogin() {
|
|
|
64
84
|
}
|
|
65
85
|
});
|
|
66
86
|
}
|
|
87
|
+
function handleLoginWithAKSK(accessKeyId, accessKeySecret) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
let apiConfig = getApiConfig();
|
|
90
|
+
apiConfig.auth = {
|
|
91
|
+
accessKeyId,
|
|
92
|
+
accessKeySecret
|
|
93
|
+
};
|
|
94
|
+
try {
|
|
95
|
+
yield updateCliConfigFile({
|
|
96
|
+
auth: apiConfig.auth
|
|
97
|
+
});
|
|
98
|
+
const service = yield ApiService.getInstance();
|
|
99
|
+
service.updateConfig(apiConfig);
|
|
100
|
+
const loginStatus = yield service.checkLogin();
|
|
101
|
+
if (loginStatus.success) {
|
|
102
|
+
logger.success(t('login_success').d('Login success!'));
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
logger.error(loginStatus.message || 'Login failed');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
logger.error(t('login_failed').d('An error occurred while trying to log in.'));
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
67
113
|
export function getUserInputAuthInfo() {
|
|
68
114
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
115
|
const styledUrl = chalk.underline.blue('https://ram.console.aliyun.com/manage/ak');
|
package/dist/commands/logout.js
CHANGED
|
@@ -12,7 +12,7 @@ import logger from '../libs/logger.js';
|
|
|
12
12
|
import t from '../i18n/index.js';
|
|
13
13
|
const logout = {
|
|
14
14
|
command: 'logout',
|
|
15
|
-
describe:
|
|
15
|
+
describe: `🚪 ${t('logout_describe').d('Logout')}`,
|
|
16
16
|
builder: (yargs) => {
|
|
17
17
|
return yargs;
|
|
18
18
|
},
|
|
@@ -7,18 +7,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import chalk from 'chalk';
|
|
11
10
|
import logger from '../../libs/logger.js';
|
|
12
|
-
import { checkDirectory, checkIsLoginSuccess
|
|
11
|
+
import { checkDirectory, checkIsLoginSuccess } from '../utils.js';
|
|
13
12
|
import { getProjectConfig } from '../../utils/fileUtils/index.js';
|
|
14
13
|
import { ApiService } from '../../libs/apiService.js';
|
|
15
14
|
import t from '../../i18n/index.js';
|
|
16
|
-
import { descriptionInput } from '../../components/descriptionInput.js';
|
|
17
|
-
import { promptFilterSelector } from '../../components/filterSelector.js';
|
|
18
15
|
import { validRoutine } from '../../utils/checkIsRoutineCreated.js';
|
|
16
|
+
import inquirer from 'inquirer';
|
|
17
|
+
import { transferRouteToRuleString } from './helper.js';
|
|
19
18
|
const addRoute = {
|
|
20
19
|
command: 'add [route] [site]',
|
|
21
|
-
describe:
|
|
20
|
+
describe: `🚄 ${t('route_add_describe').d('Bind a Route to a routine')}`,
|
|
22
21
|
builder: (yargs) => {
|
|
23
22
|
return yargs.fail((msg, err, yargsIns) => {
|
|
24
23
|
if (err)
|
|
@@ -36,7 +35,6 @@ const addRoute = {
|
|
|
36
35
|
};
|
|
37
36
|
export function handlerAddRoute(argv) {
|
|
38
37
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
var _a, _b, _c;
|
|
40
38
|
if (!checkDirectory()) {
|
|
41
39
|
return;
|
|
42
40
|
}
|
|
@@ -47,8 +45,6 @@ export function handlerAddRoute(argv) {
|
|
|
47
45
|
if (!isSuccess)
|
|
48
46
|
return;
|
|
49
47
|
yield validRoutine(projectConfig.name);
|
|
50
|
-
// input route and site
|
|
51
|
-
const { route, site } = argv;
|
|
52
48
|
const listSitesReq = {
|
|
53
49
|
SiteSearchType: 'fuzzy',
|
|
54
50
|
Status: 'active',
|
|
@@ -58,57 +54,59 @@ export function handlerAddRoute(argv) {
|
|
|
58
54
|
const server = yield ApiService.getInstance();
|
|
59
55
|
const ListSitesRes = yield server.listSites(listSitesReq);
|
|
60
56
|
const siteList = ((ListSitesRes === null || ListSitesRes === void 0 ? void 0 : ListSitesRes.data.Sites) || []).map((i) => ({
|
|
61
|
-
|
|
57
|
+
name: i.SiteName,
|
|
62
58
|
value: i.SiteId
|
|
63
59
|
}));
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
logger.success(t('route_add_success').d('Add route success!'));
|
|
60
|
+
const { routeName } = yield inquirer.prompt([
|
|
61
|
+
{
|
|
62
|
+
type: 'input',
|
|
63
|
+
name: 'routeName',
|
|
64
|
+
message: t('create_route_route_name').d('Enter a Route Name (Aliases):'),
|
|
65
|
+
validate: (input) => {
|
|
66
|
+
if (!input) {
|
|
67
|
+
return t('route_name_input_required').d('Route name is required');
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
76
71
|
}
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
]);
|
|
73
|
+
const { routeSite } = yield inquirer.prompt([
|
|
74
|
+
{
|
|
75
|
+
type: 'list',
|
|
76
|
+
name: 'routeSite',
|
|
77
|
+
message: t('create_route_site').d('Select a site that is active in your account:'),
|
|
78
|
+
choices: siteList
|
|
79
79
|
}
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return logger.error(t('route_site_not_match').d('The route does not correspond to the domain.'));
|
|
93
|
-
}
|
|
94
|
-
if (domain.value !== '') {
|
|
95
|
-
const req = {
|
|
96
|
-
Name: projectConfig.name,
|
|
97
|
-
SiteId: Number(domain.value),
|
|
98
|
-
SiteName: domain.label,
|
|
99
|
-
Route: inputRoute
|
|
100
|
-
};
|
|
101
|
-
const res = yield server.createRoutineRelatedRoute(req);
|
|
102
|
-
const addSuccess = ((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.Status) === 'OK';
|
|
103
|
-
if (addSuccess) {
|
|
104
|
-
logger.success(t('route_add_success').d('Add route success!'));
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
logger.error(t('route_add_fail').d('Add route fail!'));
|
|
80
|
+
]);
|
|
81
|
+
const { inputRoute } = yield inquirer.prompt([
|
|
82
|
+
{
|
|
83
|
+
type: 'input',
|
|
84
|
+
name: 'inputRoute',
|
|
85
|
+
message: t('create_route_route').d('Enter a Route:'),
|
|
86
|
+
validate: (input) => {
|
|
87
|
+
if (!input) {
|
|
88
|
+
return t('route_input_required').d('Route is required');
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
108
92
|
}
|
|
93
|
+
]);
|
|
94
|
+
const rule = transferRouteToRuleString(inputRoute);
|
|
95
|
+
const req = {
|
|
96
|
+
RoutineName: projectConfig.name,
|
|
97
|
+
RouteName: routeName,
|
|
98
|
+
SiteId: routeSite,
|
|
99
|
+
RouteEnable: 'on',
|
|
100
|
+
Bypass: 'off',
|
|
101
|
+
Rule: rule
|
|
102
|
+
};
|
|
103
|
+
const res = yield server.createRoutineRoute(req);
|
|
104
|
+
const addSuccess = (res === null || res === void 0 ? void 0 : res.code) === 200;
|
|
105
|
+
if (addSuccess) {
|
|
106
|
+
logger.success(t('route_add_success').d('Add route success!'));
|
|
109
107
|
}
|
|
110
108
|
else {
|
|
111
|
-
logger.error(t('
|
|
109
|
+
logger.error(t('route_add_fail').d('Add route fail!'));
|
|
112
110
|
}
|
|
113
111
|
});
|
|
114
112
|
}
|
|
@@ -9,18 +9,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { getProjectConfig } from '../../utils/fileUtils/index.js';
|
|
11
11
|
import { checkDirectory, checkIsLoginSuccess } from '../utils.js';
|
|
12
|
-
import { ApiService } from '../../libs/apiService.js';
|
|
13
12
|
import logger from '../../libs/logger.js';
|
|
14
13
|
import t from '../../i18n/index.js';
|
|
15
14
|
import { validRoutine } from '../../utils/checkIsRoutineCreated.js';
|
|
15
|
+
import api from '../../libs/api.js';
|
|
16
16
|
const deleteRoute = {
|
|
17
17
|
command: 'delete <route>',
|
|
18
18
|
describe: `🗑 ${t('route_delete_describe').d('Delete a related route')}`,
|
|
19
19
|
builder: (yargs) => {
|
|
20
|
-
return yargs
|
|
20
|
+
return yargs
|
|
21
|
+
.positional('route', {
|
|
21
22
|
describe: t('route_delete_positional_describe').d('The name of the routes to delete'),
|
|
22
23
|
type: 'string',
|
|
23
24
|
demandOption: true
|
|
25
|
+
})
|
|
26
|
+
.fail((msg, err, yargsIns) => {
|
|
27
|
+
console.log(msg, err);
|
|
28
|
+
if (err)
|
|
29
|
+
throw err;
|
|
30
|
+
if (msg) {
|
|
31
|
+
yargsIns.showHelp('log');
|
|
32
|
+
}
|
|
33
|
+
process.exit(1);
|
|
24
34
|
});
|
|
25
35
|
},
|
|
26
36
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -30,7 +40,7 @@ const deleteRoute = {
|
|
|
30
40
|
export default deleteRoute;
|
|
31
41
|
export function handleDeleteRoute(argv) {
|
|
32
42
|
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
var _a
|
|
43
|
+
var _a;
|
|
34
44
|
if (!checkDirectory()) {
|
|
35
45
|
return;
|
|
36
46
|
}
|
|
@@ -41,29 +51,25 @@ export function handleDeleteRoute(argv) {
|
|
|
41
51
|
if (!isSuccess)
|
|
42
52
|
return;
|
|
43
53
|
yield validRoutine(projectConfig.name);
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
if (matchedSite === undefined) {
|
|
55
|
-
logger.error(t('route_not_exist').d('Route not exist!'));
|
|
54
|
+
const req = {
|
|
55
|
+
routineName: projectConfig.name
|
|
56
|
+
};
|
|
57
|
+
const res = yield api.listRoutineRoutes(req);
|
|
58
|
+
const configs = ((_a = res.body) === null || _a === void 0 ? void 0 : _a.configs) || [];
|
|
59
|
+
const deleteRouteName = argv.routeName;
|
|
60
|
+
const matchedRoute = configs.find((config) => config.routeName === deleteRouteName);
|
|
61
|
+
if (!matchedRoute) {
|
|
62
|
+
logger.error(t('no_route_found').d('No route found! Please check the route name.'));
|
|
56
63
|
return;
|
|
57
64
|
}
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
RouteId: matchedSite.RouteId
|
|
65
|
+
const siteId = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.siteId;
|
|
66
|
+
const configId = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.configId;
|
|
67
|
+
const deleteRouteReq = {
|
|
68
|
+
siteId: siteId,
|
|
69
|
+
configId: configId
|
|
64
70
|
};
|
|
65
|
-
const
|
|
66
|
-
const isDeleteSuccess =
|
|
71
|
+
const deleteRouteRes = yield api.deleteRoutineRoute(deleteRouteReq);
|
|
72
|
+
const isDeleteSuccess = deleteRouteRes.statusCode === 200;
|
|
67
73
|
if (isDeleteSuccess) {
|
|
68
74
|
logger.success(t('route_delete_success').d('Delete route success!'));
|
|
69
75
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/* 操作符号枚举 */
|
|
2
|
+
export var OPERATOR_ENUM;
|
|
3
|
+
(function (OPERATOR_ENUM) {
|
|
4
|
+
OPERATOR_ENUM["Eq"] = "eq";
|
|
5
|
+
OPERATOR_ENUM["Ne"] = "ne";
|
|
6
|
+
OPERATOR_ENUM["Contains"] = "contains";
|
|
7
|
+
OPERATOR_ENUM["Negate_Contains"] = "negate_contains";
|
|
8
|
+
OPERATOR_ENUM["StartsWith"] = "starts_with";
|
|
9
|
+
OPERATOR_ENUM["Negate_StartsWith"] = "negate_starts_with";
|
|
10
|
+
OPERATOR_ENUM["EndsWith"] = "ends_with";
|
|
11
|
+
OPERATOR_ENUM["Negate_EndsWith"] = "negate_ends_with";
|
|
12
|
+
OPERATOR_ENUM["Matches"] = "matches";
|
|
13
|
+
OPERATOR_ENUM["Negate_Matches"] = "negate_matches";
|
|
14
|
+
OPERATOR_ENUM["In"] = "in";
|
|
15
|
+
OPERATOR_ENUM["Negate_In"] = "negate_in";
|
|
16
|
+
OPERATOR_ENUM["Gt"] = "gt";
|
|
17
|
+
OPERATOR_ENUM["Lt"] = "lt";
|
|
18
|
+
OPERATOR_ENUM["Ge"] = "ge";
|
|
19
|
+
OPERATOR_ENUM["Le"] = "le";
|
|
20
|
+
OPERATOR_ENUM["InList"] = "in_list";
|
|
21
|
+
OPERATOR_ENUM["Negate_InList"] = "negate_in_list";
|
|
22
|
+
})(OPERATOR_ENUM || (OPERATOR_ENUM = {}));
|
|
23
|
+
const RuleMatchTypeHost = 'http.host';
|
|
24
|
+
const RuleMatchTypeUriPath = 'http.request.uri.path';
|
|
25
|
+
const RuleMatchOperatorEq = OPERATOR_ENUM.Eq;
|
|
26
|
+
const RuleMatchOperatorStartsWith = OPERATOR_ENUM.StartsWith;
|
|
27
|
+
const RuleMatchOperatorEndsWith = OPERATOR_ENUM.EndsWith;
|
|
28
|
+
export const transferRouteToRuleString = (routePath) => {
|
|
29
|
+
if (!routePath) {
|
|
30
|
+
return '';
|
|
31
|
+
}
|
|
32
|
+
const index = routePath.indexOf('/');
|
|
33
|
+
let host = '';
|
|
34
|
+
let uriPath = '';
|
|
35
|
+
if (index < 0) {
|
|
36
|
+
host = routePath;
|
|
37
|
+
uriPath = '/';
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
host = routePath.substring(0, index);
|
|
41
|
+
uriPath = routePath.substring(index);
|
|
42
|
+
}
|
|
43
|
+
let hostOperator = RuleMatchOperatorEq;
|
|
44
|
+
if (host.startsWith('*')) {
|
|
45
|
+
hostOperator = RuleMatchOperatorEndsWith;
|
|
46
|
+
host = host.replace(/\*/g, '');
|
|
47
|
+
}
|
|
48
|
+
let uriPathOperator = RuleMatchOperatorEq;
|
|
49
|
+
if (uriPath.endsWith('*')) {
|
|
50
|
+
uriPathOperator = RuleMatchOperatorStartsWith;
|
|
51
|
+
uriPath = uriPath.replace(/\*$/, '');
|
|
52
|
+
}
|
|
53
|
+
let ruleStr = '';
|
|
54
|
+
if (hostOperator === RuleMatchOperatorEq) {
|
|
55
|
+
if (uriPathOperator === RuleMatchOperatorEq) {
|
|
56
|
+
ruleStr = `(${RuleMatchTypeHost} ${hostOperator} "${host}" and ${RuleMatchTypeUriPath} ${uriPathOperator} "${uriPath}")`;
|
|
57
|
+
}
|
|
58
|
+
else if (uriPathOperator === RuleMatchOperatorStartsWith) {
|
|
59
|
+
ruleStr = `(${RuleMatchTypeHost} ${hostOperator} "${host}" and ${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath}, "${uriPath}"))`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (hostOperator === RuleMatchOperatorEndsWith) {
|
|
63
|
+
if (uriPathOperator === RuleMatchOperatorEq) {
|
|
64
|
+
ruleStr = `(${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost}, "${host}") and ${RuleMatchTypeUriPath} ${uriPathOperator} "${uriPath}")`;
|
|
65
|
+
}
|
|
66
|
+
else if (uriPathOperator === RuleMatchOperatorStartsWith) {
|
|
67
|
+
ruleStr = `(${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost}, "${host}") and ${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath}, "${uriPath}"))`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return ruleStr;
|
|
71
|
+
};
|
|
72
|
+
export const transferRuleStringToRoute = (ruleStr) => {
|
|
73
|
+
if (!ruleStr) {
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
76
|
+
// 去掉外层括号并按 " and " 分割
|
|
77
|
+
const cleanedRule = ruleStr.replace(/^\(|\)$/g, '');
|
|
78
|
+
const parts = cleanedRule.split(' and ');
|
|
79
|
+
if (parts.length !== 2) {
|
|
80
|
+
return '';
|
|
81
|
+
}
|
|
82
|
+
let host = '';
|
|
83
|
+
let uriPath = '';
|
|
84
|
+
// 处理host部分
|
|
85
|
+
const hostPart = parts[0].trim();
|
|
86
|
+
if (hostPart.startsWith(`${RuleMatchOperatorEndsWith}(${RuleMatchTypeHost},`)) {
|
|
87
|
+
// host匹配eq时的逻辑
|
|
88
|
+
// ends_with(http.host, "value")
|
|
89
|
+
const match = hostPart.match(/ends_with\(http\.host,\s*"([^"]+)"\)/);
|
|
90
|
+
if (match) {
|
|
91
|
+
host = `*${match[1]}`; // 加前缀 *
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else if (hostPart.startsWith(`${RuleMatchTypeHost} ${RuleMatchOperatorEq}`)) {
|
|
95
|
+
// host匹配eq时的逻辑
|
|
96
|
+
// http.host eq "value"
|
|
97
|
+
const match = hostPart.match(/http\.host eq "([^"]+)"/);
|
|
98
|
+
if (match) {
|
|
99
|
+
host = match[1];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// 处理uriPath 部分
|
|
103
|
+
const uriPathPart = parts[1].trim();
|
|
104
|
+
if (uriPathPart.startsWith(`${RuleMatchOperatorStartsWith}(${RuleMatchTypeUriPath},`)) {
|
|
105
|
+
// uriPath匹配startsWith时的逻辑
|
|
106
|
+
// starts_with(http.request.uri.path, "value")
|
|
107
|
+
const match = uriPathPart.match(/starts_with\(http\.request\.uri\.path,\s*"([^"]+)"\)/);
|
|
108
|
+
if (match) {
|
|
109
|
+
uriPath = `${match[1]}*`; // 加后缀 *
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else if (uriPathPart.startsWith(`${RuleMatchTypeUriPath} ${RuleMatchOperatorEq}`)) {
|
|
113
|
+
// uriPath匹配eq时的逻辑
|
|
114
|
+
// http.request.uri.path eq "value"
|
|
115
|
+
const match = uriPathPart.match(/http\.request\.uri\.path eq "([^"]+)"/);
|
|
116
|
+
if (match) {
|
|
117
|
+
uriPath = match[1];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!host || !uriPath) {
|
|
121
|
+
return '';
|
|
122
|
+
}
|
|
123
|
+
return `${host}${uriPath}`;
|
|
124
|
+
};
|
|
@@ -5,7 +5,7 @@ import t from '../../i18n/index.js';
|
|
|
5
5
|
let yargsIns;
|
|
6
6
|
const routeCommand = {
|
|
7
7
|
command: 'route [script]',
|
|
8
|
-
describe:
|
|
8
|
+
describe: `🚄 ${t('route_describe').d('Manage the routes bound to your routine')}`,
|
|
9
9
|
builder: (yargs) => {
|
|
10
10
|
yargsIns = yargs;
|
|
11
11
|
return yargs
|