esa-cli 0.0.5 → 1.0.1

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.
Files changed (85) hide show
  1. package/README.md +34 -145
  2. package/dist/commands/commit/index.js +49 -104
  3. package/dist/commands/commit/prodBuild.js +2 -3
  4. package/dist/commands/common/constant.js +4 -4
  5. package/dist/commands/common/utils.js +419 -0
  6. package/dist/commands/config.js +2 -2
  7. package/dist/commands/deploy/helper.js +43 -49
  8. package/dist/commands/deploy/index.js +51 -174
  9. package/dist/commands/deployments/delete.js +32 -22
  10. package/dist/commands/deployments/index.js +4 -4
  11. package/dist/commands/deployments/list.js +21 -34
  12. package/dist/commands/dev/build.js +3 -3
  13. package/dist/commands/dev/doProcess.js +5 -5
  14. package/dist/commands/dev/ew2/cacheService.js +33 -0
  15. package/dist/commands/dev/ew2/devEntry.js +2 -1
  16. package/dist/commands/dev/ew2/devPack.js +31 -20
  17. package/dist/commands/dev/ew2/kvService.js +50 -0
  18. package/dist/commands/dev/ew2/mock/cache.js +99 -15
  19. package/dist/commands/dev/ew2/mock/kv.js +142 -21
  20. package/dist/commands/dev/ew2/server.js +163 -28
  21. package/dist/commands/dev/index.js +17 -18
  22. package/dist/commands/dev/mockWorker/devPack.js +19 -10
  23. package/dist/commands/dev/mockWorker/server.js +7 -6
  24. package/dist/commands/domain/add.js +4 -4
  25. package/dist/commands/domain/delete.js +3 -3
  26. package/dist/commands/domain/index.js +4 -4
  27. package/dist/commands/domain/list.js +7 -7
  28. package/dist/commands/init/helper.js +654 -21
  29. package/dist/commands/init/index.js +88 -152
  30. package/dist/commands/init/snippets/nextjs/next.config.mjs +6 -0
  31. package/dist/commands/init/snippets/nextjs/next.config.ts +7 -0
  32. package/dist/commands/init/snippets/react-router/react-router.config.ts +7 -0
  33. package/dist/commands/init/template.jsonc +84 -0
  34. package/dist/commands/init/types.js +1 -0
  35. package/dist/commands/lang.js +2 -2
  36. package/dist/commands/login/index.js +74 -34
  37. package/dist/commands/logout.js +6 -6
  38. package/dist/commands/route/add.js +101 -43
  39. package/dist/commands/route/delete.js +6 -6
  40. package/dist/commands/route/helper.js +9 -10
  41. package/dist/commands/route/index.js +4 -4
  42. package/dist/commands/route/list.js +4 -4
  43. package/dist/commands/routine/delete.js +9 -8
  44. package/dist/commands/routine/index.js +6 -5
  45. package/dist/commands/routine/list.js +45 -39
  46. package/dist/commands/site/index.js +3 -3
  47. package/dist/commands/site/list.js +6 -7
  48. package/dist/commands/utils.js +61 -25
  49. package/dist/components/descriptionInput.js +1 -1
  50. package/dist/components/filterSelector.js +1 -1
  51. package/dist/components/mutiLevelSelect.js +19 -20
  52. package/dist/components/mutiSelectTable.js +1 -1
  53. package/dist/components/routeBuilder.js +68 -0
  54. package/dist/components/selectInput.js +2 -3
  55. package/dist/components/selectItem.js +1 -1
  56. package/dist/docs/Commands_en.md +164 -117
  57. package/dist/docs/Commands_zh_CN.md +155 -107
  58. package/dist/docs/Config_en.md +70 -0
  59. package/dist/docs/Config_zh_CN.md +68 -0
  60. package/dist/i18n/index.js +2 -2
  61. package/dist/i18n/locales.json +418 -82
  62. package/dist/index.js +28 -13
  63. package/dist/libs/api.js +3 -6
  64. package/dist/libs/apiService.js +201 -70
  65. package/dist/libs/git/index.js +86 -9
  66. package/dist/libs/interface.js +0 -1
  67. package/dist/libs/logger.js +162 -10
  68. package/dist/libs/service.js +2 -2
  69. package/dist/libs/templates/index.js +1 -1
  70. package/dist/utils/checkAssetsExist.js +80 -0
  71. package/dist/utils/checkDevPort.js +5 -19
  72. package/dist/utils/checkEntryFileExist.js +10 -0
  73. package/dist/utils/checkIsRoutineCreated.js +27 -21
  74. package/dist/utils/checkVersion.js +119 -1
  75. package/dist/utils/command.js +149 -0
  76. package/dist/utils/compress.js +142 -0
  77. package/dist/utils/download.js +8 -8
  78. package/dist/utils/fileMd5.js +1 -1
  79. package/dist/utils/fileUtils/index.js +136 -45
  80. package/dist/utils/installDeno.js +4 -4
  81. package/dist/utils/installEw2.js +9 -9
  82. package/dist/utils/openInBrowser.js +1 -1
  83. package/dist/utils/prompt.js +97 -0
  84. package/package.json +23 -12
  85. package/zh_CN.md +31 -150
package/README.md CHANGED
@@ -1,173 +1,62 @@
1
- # ESA CLI
1
+ # Install/Update ESA CLI
2
2
 
3
- A CLI for operating Alibaba Cloud ESA EdgeRoutine (Edge Functions). It supports quick creation of edge functions, local debugging, version publishing and deployment, and trigger management.
3
+ ESA CLI is a command-line tool for building with Alibaba Cloud ESA Functions and Pages.
4
4
 
5
- English | [简体中文](./zh_CN.md)
5
+ <p>
6
+ <a href="https://discord.gg/xygV6MYx">
7
+ <img alt="Discord CN" src="https://img.shields.io/badge/Discord-中文-5865F2?logo=discord&logoColor=white" />
8
+ </a>
9
+ <a href="https://discord.gg/YeFg4yUA" style="margin-left:8px;">
10
+ <img alt="Discord EN" src="https://img.shields.io/badge/Discord-English-5865F2?logo=discord&logoColor=white" />
11
+ </a>
12
+ </p>
6
13
 
7
- #### Reference
14
+ ## Install ESA CLI
8
15
 
9
- - [Edge Security Acceleration (ESA)](https://www.aliyun.com/product/esa)
10
- - [What is EdgeRoutine?](https://help.aliyun.com/document_detail/2710021.html)
16
+ ### Prerequisites
11
17
 
12
- > **Note**: **In version 0.0.2 and above, the local development mode of the ESA CLI has been replaced with the same runtime as the ESA edge functions, ensuring that its actual behavior now matches the live environment. We invite you to experience it.**
13
-
14
- ESA CLI is in public beta. If you encounter any issues or have any suggestions while using it, please feel free to submit an issue or a pull request.We are actively working to improve this tool and welcome any feedback. Thank you for your understanding and support!
18
+ - Node.js: 18.x or higher (supports 18.x, 20.x, 22.x)
19
+ - OS: macOS (x86, Apple Silicon), Linux
20
+ - Recommended to use a Node version manager like Volta or nvm to avoid permission issues and easily switch Node.js versions.
15
21
 
16
22
  ## Installation
17
23
 
18
- Install and run the CLI using npm:
24
+ To ensure consistent collaboration across your team, we recommend installing `esa-cli` as a dev dependency in your project. This helps everyone use the same version.
19
25
 
20
26
  ```bash
21
- $ npm install esa-cli -g # Install globally
22
- $ esa -v # Check the version
27
+ npm i -D esa-cli@latest
23
28
  ```
24
29
 
25
- ## Usage
26
-
27
- ### 1. Initialize Routine Project
30
+ Alternatively, install globally to use the `esa-cli` command system-wide:
28
31
 
29
32
  ```bash
30
- & esa init
33
+ npm i -g esa-cli@latest
31
34
  ```
32
35
 
33
- The initialization command has a complete process guide. Follow the prompts to enter the project name and choose a template.
36
+ > [!TIP]
37
+ > When `esa-cli` is not previously installed, `npx` will fetch and run the latest version from the registry.
34
38
 
35
- ### 2. Start a local server for developing your routine
39
+ ## Check your ESA CLI version
36
40
 
37
- Local dev is the core feature of the CLI, offering a more convenient debugging experience compared to the Alibaba Cloud ESA console.
41
+ **To check your ESA CLI version, run:**
38
42
 
39
43
  ```bash
40
- & cd <Project Name>
41
- & esa dev [options]
42
- ```
43
-
44
- #### Write Code
45
-
46
- In an EdgeRoutine project, the entry file is `src/index.js`, and the basic structure is as follows:
47
-
48
- ```javascript
49
- const html = `<!DOCTYPE html>
50
- <body>
51
- <h1>Hello World!</h1>
52
- </body>`;
53
-
54
- async function handleRequest(request) {
55
- return new Response(html, {
56
- headers: {
57
- 'content-type': 'text/html;charset=UTF-8'
58
- }
59
- });
60
- }
61
-
62
- export default {
63
- async fetch(event) {
64
- return event.respondWith(handleRequest(event));
65
- }
66
- };
44
+ npx esa-cli --version
45
+ # or
46
+ npx esa-cli -v
67
47
  ```
68
48
 
69
- For more EdgeRoutine APIs and syntax, please refer to the [API Reference](https://help.aliyun.com/document_detail/2710024.html)
70
-
71
- #### Local Debugging
72
-
73
- After executing `esa dev`, the entry file will be automatically packaged and a local debugging service will be started. The interface looks like this:
49
+ ## Update ESA CLI
74
50
 
75
- ![dev png](https://github.com/aliyun/alibabacloud-esa-cli/blob/master/docs/dev.png)
76
-
77
- - Press `b` to open the debugging page in the browser.
78
- - Press `c` to clear the panel.
79
- - Press `x` to exit debugging.
80
- - You can use `esa dev --port <port>` to temporarily specify the port or use `esa config -l` to set the port by project configuration.
81
-
82
- ### 3. Log in to Alibaba Cloud Account
83
-
84
- You need to log in to your Alibaba Cloud account to perform remote management operations.
85
-
86
- First, visit the [Alibaba Cloud RAM Console](https://ram.console.aliyun.com/manage/ak) to get your AccessKey ID and AccessKey Secret, then execute `esa login` and follow the prompts to enter them.
51
+ **To update ESA CLI to the latest version, run:**
87
52
 
88
53
  ```bash
89
- & esa login
90
- & esa logout
91
- ```
92
-
93
- ### 4. Create a Version
94
-
95
- After local debugging is complete, you need to create a code version for deployment.
96
-
97
- ```bash
98
- & esa commit # Create a Version
99
- ```
100
-
101
- ### 5. Deploy to Environment & Manage Versions and Deployments
102
-
103
- After the code version is created, it needs to be deployed to edge nodes.
104
-
105
- Use the `esa deployments [script]` command to manage versions and deployments.
106
-
107
- ```bash
108
- & esa deploy # Deploy versions to the target environment
109
- & esa deployments list # View deployments
110
- & esa deployments delete <versionId> # Delete a version
111
- ```
112
-
113
- _Note: Deployed versions cannot be deleted._
114
-
115
- ### 6. Manage Triggers
116
-
117
- Once deployed to the nodes, you can configure triggers to access your edge functions. There are two types of triggers:
118
-
119
- - Domain: After you associate the routine with a domain name, you can use the domain name to access the routine.
120
- - Route: After you add a route for requested URLs, the routine is called from the edge to respond to the request.
121
-
122
- ```bash
123
- & esa domain list
124
- & esa domain add <domainName>
125
- & esa domain delete <domainName>
126
-
127
- & esa route list
128
- & esa route add [route] [site]
129
- & esa route delete <route>
130
- ```
131
-
132
- ### 7. Manage Functions
133
-
134
- You can view and delete Routine functions through the CLI.
135
-
136
- ```bash
137
- & esa routine list
138
- & esa routine delete <routineName>
139
- ```
140
-
141
- ## Commands
142
-
143
- see [Commands](./docs/Commands_en.md)
144
-
145
- ## Config files
146
-
147
- ### Global config file
148
-
149
- ```toml
150
- endpoint = "" # ESA API Endpoint
151
- lang = "zh_CN" # language
152
-
153
- [auth]
154
- accessKeyId = "" # AccessKey ID
155
- accessKeySecret = "" # AccessKey Secret
156
- ```
157
-
158
- ### Project config file
159
-
160
- ```toml
161
- name = "Hello World" # Project name
162
- description = "Hello World" # Project description
163
- entry = "src/index.js" # Entry file
164
- codeVersions = [ ] # Code version
165
-
166
- [dev]
167
- port = 18080 # Debug port
168
- localUpstream = '' # When debugging locally, the upstream source site will replace the current origin when returning to the source
54
+ npm i -D esa-cli@latest
169
55
  ```
170
56
 
171
- ## LICENSE
57
+ ## Related Documentation
172
58
 
173
- [The MIT License](./LICENSE)
59
+ - [esa-cli command](./docs/Commands_en.md)
60
+ - [ESA Configuration Guide](./docs/Config_en.md)
61
+ - [Alibaba Cloud ESA Documentation](https://www.alibabacloud.com/help/en/edge-security-acceleration/esa/user-guide/what-is-er/)
62
+ - [Functions and Pages API Reference](https://www.alibabacloud.com/help/en/edge-security-acceleration/esa/user-guide/api-documentation/)
@@ -7,16 +7,16 @@ 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 { getProjectConfig, readEdgeRoutineFile } from '../../utils/fileUtils/index.js';
11
- import { checkDirectory, checkIsLoginSuccess } from '../utils.js';
12
- import { descriptionInput } from '../../components/descriptionInput.js';
13
- import { ApiService } from '../../libs/apiService.js';
14
- import prodBuild from './prodBuild.js';
15
- import logger from '../../libs/logger.js';
10
+ import { exit } from 'process';
11
+ import { intro, outro } from '@clack/prompts';
12
+ import chalk from 'chalk';
16
13
  import t from '../../i18n/index.js';
14
+ import logger from '../../libs/logger.js';
15
+ import promptParameter from '../../utils/prompt.js';
16
+ import { validateAndInitializeProject, generateCodeVersion } from '../common/utils.js';
17
17
  const commit = {
18
18
  command: 'commit [entry]',
19
- describe: `📥 ${t('commit_describe').d('Commit your code, save as a new version')}`,
19
+ describe: `📦 ${t('commit_describe').d('Commit your code, save as a new version')}`,
20
20
  builder: (yargs) => {
21
21
  return yargs
22
22
  .option('minify', {
@@ -25,116 +25,61 @@ const commit = {
25
25
  type: 'boolean',
26
26
  default: false
27
27
  })
28
- .positional('entry', {
29
- describe: t('dev_entry_describe').d('Entry file of the Routine'),
30
- type: 'string',
31
- demandOption: false
28
+ .option('assets', {
29
+ alias: 'a',
30
+ describe: t('commit_option_assets').d('Assets directory'),
31
+ type: 'string'
32
+ })
33
+ .option('description', {
34
+ alias: 'd',
35
+ describe: t('commit_option_description').d('Description for Functions& Pages/version (skip interactive input)'),
36
+ type: 'string'
37
+ })
38
+ .option('name', {
39
+ alias: 'n',
40
+ describe: t('commit_option_name').d('Functions& Pages name'),
41
+ type: 'string'
32
42
  });
33
43
  },
34
44
  handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
35
- handleCommit(argv);
45
+ yield handleCommit(argv);
46
+ exit();
36
47
  })
37
48
  };
38
49
  export default commit;
39
50
  export function handleCommit(argv) {
40
51
  return __awaiter(this, void 0, void 0, function* () {
41
- if (!checkDirectory())
42
- return;
43
- const projectConfig = getProjectConfig();
44
- if (!projectConfig)
45
- return logger.notInProject();
46
- if (!(yield checkIsLoginSuccess()))
52
+ var _a, _b;
53
+ intro(`Commit an application with ESA`);
54
+ const projectInfo = yield validateAndInitializeProject(argv === null || argv === void 0 ? void 0 : argv.name);
55
+ if (!projectInfo)
47
56
  return;
48
- yield prodBuild(!!argv.minify, argv === null || argv === void 0 ? void 0 : argv.entry);
49
- try {
50
- const server = yield ApiService.getInstance();
51
- const req = { Name: projectConfig.name };
52
- const response = yield server.getRoutine(req, false);
53
- let action = 'Creating';
54
- let description;
55
- if (!response) {
56
- logger.log(`🙅 ${t('commit_er_not_exist').d('No routine found, creating a new one')}`);
57
- description = yield descriptionInput(`🖊️ ${t('commit_er_description').d('Enter a description for the routine')}:`, false);
58
- }
59
- else {
60
- logger.log(`🔄 ${t('commit_er_exist').d('Routine exists, updating the code')}`);
61
- description = yield descriptionInput(`🖊️ ${t('commit_version_description').d('Enter a description for the version')}:`, false);
62
- action = 'Updating';
63
- }
64
- const code = readEdgeRoutineFile() || '';
65
- const edgeRoutine = {
66
- name: projectConfig.name,
67
- code,
68
- description
69
- };
70
- if (action === 'Creating') {
71
- yield createEdgeRoutine(edgeRoutine);
72
- }
73
- else {
74
- if (!(yield uploadEdgeRoutineCode(edgeRoutine)))
75
- return;
76
- yield releaseOfficialVersion(edgeRoutine);
77
- }
78
- }
79
- catch (error) {
80
- logger.error(`${t('common_error_occurred').d('An error occurred:')} ${error}`);
57
+ const { projectName } = projectInfo;
58
+ let description;
59
+ if (argv.description) {
60
+ description = argv.description;
81
61
  }
82
- });
83
- }
84
- export function createEdgeRoutine(edgeRoutine) {
85
- return __awaiter(this, void 0, void 0, function* () {
86
- try {
87
- const server = yield ApiService.getInstance();
88
- const res = yield server.createRoutine(edgeRoutine);
89
- const createResult = (res === null || res === void 0 ? void 0 : res.data.Status) === 'OK';
90
- if (!createResult) {
91
- logger.error(t('commit_create_er_fail').d('An error occurred while trying to create a routine'));
92
- return false;
93
- }
94
- logger.success(t('commit_create_er_success').d('Routine created successfully.'));
95
- return yield uploadEdgeRoutineCode(edgeRoutine);
96
- }
97
- catch (error) {
98
- logger.error(`${t('common_error_occurred').d('An error occurred:')} ${error}`);
99
- return false;
100
- }
101
- });
102
- }
103
- export function uploadEdgeRoutineCode(edgeRoutine) {
104
- return __awaiter(this, void 0, void 0, function* () {
105
- try {
106
- const server = yield ApiService.getInstance();
107
- const uploadResult = yield server.getRoutineStagingCodeUploadInfo(edgeRoutine);
108
- if (!uploadResult) {
109
- logger.error(t('commit_upload_fail').d('An error occurred while trying to upload your code'));
110
- process.exit(0);
111
- }
112
- logger.success(t('commit_upload_success').d('Code uploaded successfully.'));
113
- return true;
114
- }
115
- catch (error) {
116
- logger.error('123');
117
- logger.error(`${t('common_error_occurred').d('An error occurred:')} ${error}`);
118
- process.exit(0);
62
+ else {
63
+ description = (yield promptParameter({
64
+ type: 'text',
65
+ question: t('commit_version_description').d('Enter a description for the version'),
66
+ label: 'Description',
67
+ defaultValue: ''
68
+ }));
119
69
  }
120
- });
121
- }
122
- export function releaseOfficialVersion(edgeRoutine) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- var _a;
125
- const param = {
126
- Name: edgeRoutine.name,
127
- CodeDescription: (_a = edgeRoutine.description) !== null && _a !== void 0 ? _a : ''
128
- };
129
- const server = yield ApiService.getInstance();
130
- const commitResult = yield server.commitRoutineStagingCode(param);
131
- if (commitResult) {
132
- logger.success(t('commit_success').d('Code version committed successfully.'));
133
- return true;
70
+ logger.startSubStep('Generating code version');
71
+ const res = yield generateCodeVersion(projectName, description, argv === null || argv === void 0 ? void 0 : argv.entry, argv === null || argv === void 0 ? void 0 : argv.assets, argv === null || argv === void 0 ? void 0 : argv.minify);
72
+ const { isSuccess } = res || {};
73
+ if (!isSuccess) {
74
+ logger.endSubStep('Generate version failed');
75
+ exit(1);
134
76
  }
135
- else {
136
- logger.error(t('commit_fail').d('An error occurred while trying to commit your code.'));
77
+ const codeVersion = (_b = (_a = res === null || res === void 0 ? void 0 : res.res) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.CodeVersion;
78
+ if (!codeVersion) {
79
+ logger.endSubStep('Missing CodeVersion in response');
137
80
  return false;
138
81
  }
82
+ logger.endSubStep(`Version generated: ${codeVersion}`);
83
+ outro(`Code version ${chalk.bold(codeVersion)} generated successfully`);
139
84
  });
140
85
  }
@@ -7,15 +7,14 @@ 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 fs from 'fs';
11
+ import path from 'path';
10
12
  import esbuild from 'esbuild';
11
13
  import { lessLoader } from 'esbuild-plugin-less';
12
- import path from 'path';
13
- import fs from 'fs';
14
14
  import { getRoot } from '../../utils/fileUtils/base.js';
15
15
  import { NODE_EXTERNALS } from '../common/constant.js';
16
16
  const userRoot = getRoot();
17
17
  const entry = path.resolve(userRoot, 'src/index.js');
18
- const outfile = path.resolve(userRoot, '.dev/pub.js');
19
18
  export default function () {
20
19
  return __awaiter(this, arguments, void 0, function* (minify = false, customEntry, outputPath) {
21
20
  const entryPoint = customEntry
@@ -2,21 +2,21 @@ import t from '../../i18n/index.js';
2
2
  export const getSummary = (routineName) => {
3
3
  return [
4
4
  {
5
- title: t('summery_cd').d('Enter your routine project folder'),
5
+ title: t('summery_cd').d('Enter your project folder'),
6
6
  command: `💡 cd ${routineName}`
7
7
  },
8
8
  {
9
9
  title: t('summery_dev').d('Start a local development server for your project'),
10
- command: '💡 esa dev'
10
+ command: '💡 esa-cli dev'
11
11
  },
12
12
  {
13
13
  title: t('summery_commit').d('Save a new version of code'),
14
- command: '💡 esa commit'
14
+ command: '💡 esa-cli commit'
15
15
  },
16
16
  // Use Deploy or Release?
17
17
  {
18
18
  title: t('summery_deploy').d('Deploy your project to different environments'),
19
- command: '💡 esa deploy'
19
+ command: '💡 esa-cli deploy'
20
20
  }
21
21
  ];
22
22
  };