esa-cli 0.0.1-beta.8 → 0.0.2-beta.0
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/README.md +4 -6
- package/bin/enter.cjs +0 -1
- package/dist/README.md +4 -6
- package/dist/bin/enter.cjs +0 -1
- package/dist/commands/commit/index.js +2 -2
- package/dist/commands/commit/prodBuild.js +1 -1
- package/dist/commands/deploy/index.js +4 -2
- package/dist/commands/deployments/list.js +2 -1
- package/dist/commands/dev/build.js +57 -0
- package/dist/commands/dev/doProcess.js +10 -2
- package/dist/commands/dev/ew2/devEntry.js +9 -0
- package/dist/commands/dev/ew2/devPack.js +185 -0
- package/dist/commands/dev/ew2/mock/cache.js +32 -0
- package/dist/commands/dev/ew2/mock/kv.js +45 -0
- package/dist/commands/dev/ew2/server.js +234 -0
- package/dist/commands/dev/index.js +113 -50
- package/dist/commands/dev/{config → mockWorker}/devEntry.js +3 -2
- package/dist/commands/dev/{devPack.js → mockWorker/devPack.js} +10 -7
- package/dist/commands/dev/{server.js → mockWorker/server.js} +43 -36
- package/dist/commands/init/index.js +119 -47
- package/dist/commands/login/index.js +2 -2
- package/dist/commands/logout.js +0 -4
- package/dist/commands/route/list.js +2 -1
- package/dist/commands/routine/list.js +6 -4
- package/dist/commands/utils.js +2 -1
- package/dist/components/mutiLevelSelect.js +69 -0
- package/dist/i18n/index.js +4 -1
- package/dist/i18n/locales.json +897 -781
- package/dist/index.js +10 -3
- package/dist/libs/api.js +155 -0
- package/dist/libs/logger.js +42 -31
- package/dist/libs/service.js +178 -0
- package/dist/package.json +14 -7
- package/dist/utils/checkDevPort.js +19 -14
- package/dist/utils/checkOS.js +32 -0
- package/dist/utils/checkVersion.js +1 -1
- package/dist/utils/fileMd5.js +18 -0
- package/dist/utils/fileUtils/base.js +31 -0
- package/dist/utils/fileUtils/index.js +5 -33
- package/dist/utils/install/installEw2.sh +33 -0
- package/dist/utils/{installRuntime.js → installDeno.js} +16 -16
- package/dist/utils/installEw2.js +127 -0
- package/dist/utils/readJson.js +10 -0
- package/dist/utils/stepsRunner.js +33 -0
- package/dist/zh_CN.md +4 -5
- package/package.json +14 -7
- package/dist/commands/dev/config/devBuild.js +0 -26
- package/dist/libs/request.js +0 -98
- /package/dist/commands/dev/{config → mockWorker}/mock/cache.js +0 -0
- /package/dist/commands/dev/{config → mockWorker}/mock/kv.js +0 -0
|
@@ -0,0 +1,234 @@
|
|
|
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 * as http from 'http';
|
|
11
|
+
import spawn from 'cross-spawn';
|
|
12
|
+
import fetch from 'node-fetch';
|
|
13
|
+
import logger from '../../../libs/logger.js';
|
|
14
|
+
import { getRoot } from '../../../utils/fileUtils/base.js';
|
|
15
|
+
import { EW2BinPath } from '../../../utils/installEw2.js';
|
|
16
|
+
import { HttpProxyAgent } from 'http-proxy-agent';
|
|
17
|
+
import chalk from 'chalk';
|
|
18
|
+
import t from '../../../i18n/index.js';
|
|
19
|
+
import sleep from '../../../utils/sleep.js';
|
|
20
|
+
const getColorForStatusCode = (statusCode, message) => {
|
|
21
|
+
if (statusCode >= 100 && statusCode < 200) {
|
|
22
|
+
return chalk.blue(`${statusCode} ${message}`);
|
|
23
|
+
}
|
|
24
|
+
else if (statusCode >= 200 && statusCode < 300) {
|
|
25
|
+
return chalk.green(`${statusCode} ${message}`);
|
|
26
|
+
}
|
|
27
|
+
else if (statusCode >= 300 && statusCode < 400) {
|
|
28
|
+
return chalk.yellow(`${statusCode} ${message}`);
|
|
29
|
+
}
|
|
30
|
+
else if (statusCode >= 400 && statusCode < 500) {
|
|
31
|
+
return chalk.red(`${statusCode} ${message}`);
|
|
32
|
+
}
|
|
33
|
+
else if (statusCode >= 500) {
|
|
34
|
+
return chalk.magenta(chalk.bold(`${statusCode} ${message}`));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
return `${statusCode} ${message}`;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
class Ew2Server {
|
|
41
|
+
constructor(props) {
|
|
42
|
+
this.worker = null;
|
|
43
|
+
this.startingWorker = false;
|
|
44
|
+
this.workerStartTimeout = undefined;
|
|
45
|
+
this.server = null;
|
|
46
|
+
this.restarting = false;
|
|
47
|
+
this.port = 18080;
|
|
48
|
+
// @ts-ignore
|
|
49
|
+
if (global.port)
|
|
50
|
+
this.port = global.port;
|
|
51
|
+
if (props.port)
|
|
52
|
+
this.port = props.port;
|
|
53
|
+
if (props.onClose)
|
|
54
|
+
this.onClose = props.onClose;
|
|
55
|
+
}
|
|
56
|
+
start() {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
this.startingWorker = true;
|
|
59
|
+
const result = yield this.openEdgeWorker();
|
|
60
|
+
if (!result) {
|
|
61
|
+
throw new Error('Worker start failed');
|
|
62
|
+
}
|
|
63
|
+
this.createServer();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
openEdgeWorker() {
|
|
67
|
+
if (this.worker) {
|
|
68
|
+
return Promise.resolve();
|
|
69
|
+
}
|
|
70
|
+
const root = getRoot();
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
const id = global.id || '';
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
var _a, _b, _c;
|
|
75
|
+
this.worker = spawn(EW2BinPath, [
|
|
76
|
+
'--config_file',
|
|
77
|
+
`${root}/.dev/config-${id}.toml`,
|
|
78
|
+
'--log_stdout',
|
|
79
|
+
'-v'
|
|
80
|
+
], {
|
|
81
|
+
stdio: ['pipe', 'pipe', 'pipe']
|
|
82
|
+
});
|
|
83
|
+
this.workerStartTimeout = setTimeout(() => {
|
|
84
|
+
reject(new Error(t('dev_worker_timeout').d('Worker start timeout')));
|
|
85
|
+
this.worker && this.worker.kill();
|
|
86
|
+
}, 60000);
|
|
87
|
+
const sendToRuntime = () => {
|
|
88
|
+
return new Promise((resolveStart) => {
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
const ew2Port = global.ew2Port;
|
|
91
|
+
const options = {
|
|
92
|
+
hostname: '127.0.0.1',
|
|
93
|
+
port: ew2Port,
|
|
94
|
+
method: 'GET'
|
|
95
|
+
};
|
|
96
|
+
const req = http.get(options, (res) => {
|
|
97
|
+
resolveStart(res.statusCode);
|
|
98
|
+
});
|
|
99
|
+
req.on('error', (err) => {
|
|
100
|
+
resolveStart(null);
|
|
101
|
+
});
|
|
102
|
+
req.end();
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const checkRuntimeStart = () => __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
while (this.startingWorker) {
|
|
107
|
+
const [result] = yield Promise.all([sendToRuntime(), sleep(500)]);
|
|
108
|
+
if (result) {
|
|
109
|
+
this.startingWorker = false;
|
|
110
|
+
this.clearTimeout();
|
|
111
|
+
resolve(result);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
checkRuntimeStart();
|
|
116
|
+
(_a = this.worker.stdout) === null || _a === void 0 ? void 0 : _a.setEncoding('utf8');
|
|
117
|
+
(_b = this.worker.stdout) === null || _b === void 0 ? void 0 : _b.on('data', this.stdoutHandler.bind(this));
|
|
118
|
+
(_c = this.worker.stderr) === null || _c === void 0 ? void 0 : _c.on('data', this.stderrHandler.bind(this));
|
|
119
|
+
this.worker.on('close', this.closeHandler.bind(this));
|
|
120
|
+
this.worker.on('error', this.errorHandler.bind(this));
|
|
121
|
+
process.on('SIGTERM', () => {
|
|
122
|
+
this.worker && this.worker.kill();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
clearTimeout() {
|
|
127
|
+
clearTimeout(this.workerStartTimeout);
|
|
128
|
+
}
|
|
129
|
+
createServer() {
|
|
130
|
+
this.server = http.createServer((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
var _a;
|
|
132
|
+
try {
|
|
133
|
+
const host = req.headers.host;
|
|
134
|
+
const url = req.url;
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
const ew2Port = global.ew2Port;
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
const localUpstream = global.localUpstream;
|
|
139
|
+
const workerRes = yield fetch(`http://${localUpstream ? localUpstream : host}${url}`, {
|
|
140
|
+
method: 'GET',
|
|
141
|
+
headers: {
|
|
142
|
+
'x-er-context': 'eyJzaXRlX2lkIjogIjYyMjcxODQ0NjgwNjA4IiwgInNpdGVfbmFtZSI6ICJjb21wdXRlbHguYWxpY2RuLXRlc3QuY29tIiwgInNpdGVfcmVjb3JkIjogIm1vY2hlbi1uY2RuLmNvbXB1dGVseC5hbGljZG4tdGVzdC5jb20iLCAiYWxpdWlkIjogIjEzMjI0OTI2ODY2NjU2MDgiLCAic2NoZW1lIjoiaHR0cCIsICAiaW1hZ2VfZW5hYmxlIjogdHJ1ZX0=',
|
|
143
|
+
'x-er-id': 'a.bA'
|
|
144
|
+
},
|
|
145
|
+
agent: new HttpProxyAgent(`http://127.0.0.1:${ew2Port}`)
|
|
146
|
+
});
|
|
147
|
+
const workerHeaders = Object.fromEntries(workerRes.headers.entries());
|
|
148
|
+
// 解决 gzip 兼容性问题,防止net::ERR_CONTENT_DECODING_FAILED
|
|
149
|
+
workerHeaders['content-encoding'] = 'identity';
|
|
150
|
+
if (workerRes.body) {
|
|
151
|
+
if ((_a = workerRes.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('text/')) {
|
|
152
|
+
const text = yield workerRes.text();
|
|
153
|
+
// 出现换行符之类会导致 content-length 不一致
|
|
154
|
+
workerHeaders['content-length'] =
|
|
155
|
+
Buffer.byteLength(text).toString();
|
|
156
|
+
res.writeHead(workerRes.status, workerHeaders);
|
|
157
|
+
res.end(text);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
res.writeHead(workerRes.status, workerHeaders);
|
|
161
|
+
workerRes.body.pipe(res);
|
|
162
|
+
}
|
|
163
|
+
logger.log(`[ESA Dev] ${req.method} ${url} ${getColorForStatusCode(workerRes.status, workerRes.statusText)}`);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
167
|
+
res.end('EW2 return null');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
console.log(err);
|
|
172
|
+
}
|
|
173
|
+
}));
|
|
174
|
+
this.server.listen(this.port, () => {
|
|
175
|
+
logger.log(`listening on port ${this.port}`);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
stdoutHandler(chunk) {
|
|
179
|
+
logger.log(`${chalk.bgGreen('[Worker]')} ${chunk.toString().trim()}`);
|
|
180
|
+
}
|
|
181
|
+
stderrHandler(chunk) {
|
|
182
|
+
logger.subError(`${chalk.bgGreen('[Worker Error]')} ${chunk.toString().trim()}`);
|
|
183
|
+
}
|
|
184
|
+
errorHandler(error) {
|
|
185
|
+
logger.error(error.message ? error.message : error);
|
|
186
|
+
if (error.code && error.code === 'EACCES') {
|
|
187
|
+
logger.pathEacces(EW2BinPath);
|
|
188
|
+
}
|
|
189
|
+
this.stop();
|
|
190
|
+
}
|
|
191
|
+
closeHandler(code, signal) {
|
|
192
|
+
if (this.restarting) {
|
|
193
|
+
this.restarting = false;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
this.stop().then(() => {
|
|
197
|
+
logger.log(t('dev_server_closed').d('Worker server closed'));
|
|
198
|
+
logger.info('Worker server closed');
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
global.port = undefined;
|
|
201
|
+
this.onClose && this.onClose();
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
runCommand(command) {
|
|
205
|
+
var _a, _b;
|
|
206
|
+
(_b = (_a = this.worker) === null || _a === void 0 ? void 0 : _a.stdin) === null || _b === void 0 ? void 0 : _b.write(command);
|
|
207
|
+
}
|
|
208
|
+
stop() {
|
|
209
|
+
return new Promise((resolve) => {
|
|
210
|
+
var _a;
|
|
211
|
+
if (!this.worker) {
|
|
212
|
+
resolve(false);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const onExit = (code, signal) => {
|
|
216
|
+
this.worker = null;
|
|
217
|
+
resolve(true);
|
|
218
|
+
};
|
|
219
|
+
this.worker.on('exit', onExit);
|
|
220
|
+
this.worker.kill('SIGTERM');
|
|
221
|
+
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close();
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
restart() {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
this.restarting = true;
|
|
227
|
+
yield this.stop();
|
|
228
|
+
this.start();
|
|
229
|
+
logger.log(t('dev_server_restart').d('Worker server restarted'));
|
|
230
|
+
logger.info('Worker server restarted');
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
export default Ew2Server;
|
|
@@ -8,28 +8,46 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { exec } from 'child_process';
|
|
11
|
+
import { isIP } from 'net';
|
|
11
12
|
import chokidar from 'chokidar';
|
|
12
13
|
import doProcess from './doProcess.js';
|
|
13
|
-
import { getProjectConfig,
|
|
14
|
+
import { getProjectConfig, generateConfigFile, getDevConf } from '../../utils/fileUtils/index.js';
|
|
15
|
+
import { getRoot } from '../../utils/fileUtils/base.js';
|
|
14
16
|
import SelectItems from '../../components/selectInput.js';
|
|
15
17
|
import logger from '../../libs/logger.js';
|
|
16
|
-
import
|
|
17
|
-
import
|
|
18
|
-
import
|
|
18
|
+
import MockServer from './mockWorker/server.js';
|
|
19
|
+
import mockPack from './mockWorker/devPack.js';
|
|
20
|
+
import Ew2Server from './ew2/server.js';
|
|
21
|
+
import ew2Pack from './ew2/devPack.js';
|
|
22
|
+
import { preCheckDeno } from '../../utils/installDeno.js';
|
|
23
|
+
import { preCheckEw2 } from '../../utils/installEw2.js';
|
|
19
24
|
import debounce from '../../utils/debounce.js';
|
|
20
25
|
import t from '../../i18n/index.js';
|
|
21
26
|
import checkAndInputPort from '../../utils/checkDevPort.js';
|
|
27
|
+
import { checkOS, Platforms } from '../../utils/checkOS.js';
|
|
22
28
|
let yargsIns;
|
|
29
|
+
const OS = checkOS();
|
|
30
|
+
const useEw2 = OS === Platforms.AppleArm || Platforms.AppleIntel || Platforms.LinuxX86;
|
|
23
31
|
const dev = {
|
|
24
32
|
command: 'dev [entry]',
|
|
25
33
|
describe: `💻 ${t('dev_describe').d('Start a local server for developing your routine')}`,
|
|
26
34
|
builder: (yargs) => {
|
|
27
|
-
yargsIns = yargs
|
|
28
|
-
return yargs
|
|
35
|
+
yargsIns = yargs
|
|
29
36
|
.positional('entry', {
|
|
30
37
|
describe: t('dev_entry_describe').d('Entry file of the Routine'),
|
|
31
38
|
type: 'string',
|
|
32
39
|
demandOption: false
|
|
40
|
+
})
|
|
41
|
+
.option('p', {
|
|
42
|
+
alias: 'port',
|
|
43
|
+
describe: t('dev_port_describe').d('Port to listen on'),
|
|
44
|
+
type: 'number'
|
|
45
|
+
})
|
|
46
|
+
.option('m', {
|
|
47
|
+
alias: 'minify',
|
|
48
|
+
describe: t('dev_option_minify').d('Minify code during development'),
|
|
49
|
+
type: 'boolean',
|
|
50
|
+
default: false
|
|
33
51
|
})
|
|
34
52
|
.option('refresh-command', {
|
|
35
53
|
describe: t('dev_refresh_command_describe').d('Provide a command to be executed before the auto-refresh on save'),
|
|
@@ -39,46 +57,47 @@ const dev = {
|
|
|
39
57
|
describe: t('dev_option_local_upstream').d('Host to act as origin in development'),
|
|
40
58
|
type: 'string'
|
|
41
59
|
})
|
|
42
|
-
.option('
|
|
43
|
-
|
|
44
|
-
describe: t('dev_option_minify').d('Minify code during development'),
|
|
60
|
+
.option('debug', {
|
|
61
|
+
describe: t('dev_option_debugger').d('Output debug logs'),
|
|
45
62
|
type: 'boolean',
|
|
46
63
|
default: false
|
|
47
|
-
})
|
|
48
|
-
.option('inspect-port', {
|
|
49
|
-
describe: t('dev_inspect_port_describe').d('Chrome inspect devTool port'),
|
|
50
|
-
type: 'number'
|
|
51
|
-
})
|
|
52
|
-
.option('p', {
|
|
53
|
-
alias: 'port',
|
|
54
|
-
describe: t('dev_port_describe').d('Port to listen on'),
|
|
55
|
-
type: 'number'
|
|
56
64
|
});
|
|
65
|
+
if (!useEw2) {
|
|
66
|
+
yargsIns.option('inspect-port', {
|
|
67
|
+
describe: t('dev_inspect_port_describe').d('Chrome inspect devTool port'),
|
|
68
|
+
type: 'number'
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return yargsIns;
|
|
57
72
|
},
|
|
58
73
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
74
|
let { port, inspectPort } = argv;
|
|
60
|
-
let userFileRepacking = false; //
|
|
61
|
-
const { entry, minify, refreshCommand, localUpstream, help } = argv;
|
|
75
|
+
let userFileRepacking = false; // Indicates that user code repacking does not change the .dev file
|
|
76
|
+
const { entry, minify, refreshCommand, localUpstream, help, debug } = argv;
|
|
62
77
|
if (yargsIns && help) {
|
|
63
78
|
yargsIns.showHelp('log');
|
|
64
|
-
|
|
79
|
+
process.exit(0);
|
|
65
80
|
}
|
|
81
|
+
if (debug) {
|
|
82
|
+
logger.setLogLevel('debug');
|
|
83
|
+
}
|
|
84
|
+
// Get options and set global variables
|
|
66
85
|
const projectConfig = getProjectConfig();
|
|
67
86
|
if (!projectConfig) {
|
|
68
|
-
if (entry) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
catch (_) {
|
|
73
|
-
logger.notInProject();
|
|
74
|
-
process.exit(1);
|
|
75
|
-
}
|
|
87
|
+
if (!entry) {
|
|
88
|
+
logger.notInProject();
|
|
89
|
+
process.exit(1);
|
|
76
90
|
}
|
|
77
|
-
|
|
78
|
-
|
|
91
|
+
try {
|
|
92
|
+
// If no config is found and an entry is provided, create a new one.
|
|
93
|
+
yield selectToCreateConf(entry);
|
|
94
|
+
}
|
|
95
|
+
catch (_) {
|
|
96
|
+
logger.notInProject();
|
|
97
|
+
process.exit(1);
|
|
79
98
|
}
|
|
80
99
|
}
|
|
81
|
-
|
|
100
|
+
if (entry) {
|
|
82
101
|
// @ts-ignore
|
|
83
102
|
global.entry = entry;
|
|
84
103
|
}
|
|
@@ -94,9 +113,18 @@ const dev = {
|
|
|
94
113
|
}
|
|
95
114
|
// @ts-ignore
|
|
96
115
|
global.port = port;
|
|
116
|
+
logger.debug(`Entered port: ${port}`);
|
|
97
117
|
}
|
|
98
118
|
if (inspectPort) {
|
|
99
|
-
|
|
119
|
+
if (Array.isArray(inspectPort)) {
|
|
120
|
+
inspectPort = Number(inspectPort[0]);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
inspectPort = Number(inspectPort);
|
|
124
|
+
if (isNaN(inspectPort)) {
|
|
125
|
+
logger.warn(t('dev_import_port_invalid').d('Invalid port entered, default port will be used.'));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
100
128
|
// @ts-ignore
|
|
101
129
|
global.inspectPort = inspectPort;
|
|
102
130
|
}
|
|
@@ -104,33 +132,63 @@ const dev = {
|
|
|
104
132
|
// @ts-ignore
|
|
105
133
|
global.minify = minify;
|
|
106
134
|
}
|
|
107
|
-
if (minify) {
|
|
108
|
-
// @ts-ignore
|
|
109
|
-
global.minify = minify;
|
|
110
|
-
}
|
|
111
135
|
if (localUpstream) {
|
|
136
|
+
const url = localUpstream;
|
|
112
137
|
// @ts-ignore
|
|
113
138
|
global.localUpstream = localUpstream;
|
|
139
|
+
try {
|
|
140
|
+
const hostname = new URL(url).hostname;
|
|
141
|
+
if (isIP(hostname)) {
|
|
142
|
+
logger.error(t('dev_ip_not_allowed', { url }).d(`Direct access to IP addresses is not allowed when setting local-upstream: ${url}`));
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
logger.error(t('dev_url_invalid', { url }).d(`Invalid URL: ${url}. Please enter a valid URL.`));
|
|
148
|
+
process.exit(1);
|
|
149
|
+
}
|
|
114
150
|
}
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
151
|
+
const checkFunc = useEw2 ? preCheckEw2 : preCheckDeno;
|
|
152
|
+
const checkResult = yield checkFunc();
|
|
153
|
+
if (!checkResult) {
|
|
154
|
+
process.exit(1);
|
|
118
155
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
156
|
+
if (useEw2) {
|
|
157
|
+
const speEw2Port = getDevConf('port', 'dev', 18080);
|
|
158
|
+
const result = yield checkAndInputPort(speEw2Port);
|
|
159
|
+
// @ts-ignore
|
|
160
|
+
global.port = result.port;
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
const speDenoPort = getDevConf('port', 'dev', 18080);
|
|
164
|
+
const speInspectPort = getDevConf('inspectPort', 'dev', 9229);
|
|
122
165
|
const result = yield checkAndInputPort(speDenoPort, speInspectPort);
|
|
123
166
|
// @ts-ignore
|
|
124
|
-
global.port = result.
|
|
167
|
+
global.port = result.port;
|
|
125
168
|
// @ts-ignore
|
|
126
169
|
global.inspectPort = result.inspectPort;
|
|
127
170
|
}
|
|
171
|
+
const devPack = useEw2 ? ew2Pack : mockPack;
|
|
172
|
+
try {
|
|
173
|
+
yield devPack();
|
|
174
|
+
}
|
|
128
175
|
catch (err) {
|
|
129
176
|
process.exit(1);
|
|
130
177
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
178
|
+
let worker;
|
|
179
|
+
if (useEw2) {
|
|
180
|
+
worker = new Ew2Server({ onClose: onWorkerClosed });
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
worker = new MockServer({ command: checkResult });
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
yield worker.start();
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
console.log('Track err', err);
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
134
192
|
const ignored = (path) => {
|
|
135
193
|
return /(^|[\/\\])\.(?!dev($|[\/\\]))/.test(path);
|
|
136
194
|
};
|
|
@@ -147,7 +205,8 @@ const dev = {
|
|
|
147
205
|
worker.restart();
|
|
148
206
|
return;
|
|
149
207
|
}
|
|
150
|
-
logger.info(
|
|
208
|
+
logger.info('Dev repack');
|
|
209
|
+
logger.log(`${t('dev_repacking').d('Detected local file changes, re-packaging')}...`);
|
|
151
210
|
if (refreshCommand) {
|
|
152
211
|
try {
|
|
153
212
|
yield execRefreshCommand(refreshCommand);
|
|
@@ -158,9 +217,12 @@ const dev = {
|
|
|
158
217
|
yield devPack();
|
|
159
218
|
yield worker.restart();
|
|
160
219
|
}), 500));
|
|
161
|
-
|
|
220
|
+
var { devElement, exit } = doProcess(worker);
|
|
162
221
|
const { waitUntilExit } = devElement;
|
|
163
222
|
yield waitUntilExit();
|
|
223
|
+
function onWorkerClosed() {
|
|
224
|
+
exit && exit();
|
|
225
|
+
}
|
|
164
226
|
watcher.close();
|
|
165
227
|
})
|
|
166
228
|
};
|
|
@@ -215,7 +277,8 @@ function execRefreshCommand(cmd) {
|
|
|
215
277
|
}
|
|
216
278
|
function selectToCreateConf(entry) {
|
|
217
279
|
return new Promise((resolve, reject) => {
|
|
218
|
-
logger.info(
|
|
280
|
+
logger.info('Configuration file not found');
|
|
281
|
+
logger.log(t('dev_create_config').d('Configuration file not found. Would you like to create one?'));
|
|
219
282
|
SelectItems({
|
|
220
283
|
items: [
|
|
221
284
|
{ label: 'Yes', value: 'yes' },
|
|
@@ -41,6 +41,7 @@ const dev = async () => {
|
|
|
41
41
|
handler: async (request) => {
|
|
42
42
|
const url = new URL(request.url);
|
|
43
43
|
let nextRequest = request;
|
|
44
|
+
// Replace the request url with the local upstream url
|
|
44
45
|
if (config.localUpstream) {
|
|
45
46
|
const nextUrl = `${config.localUpstream}${url.pathname}${url.search}${url.hash}`;
|
|
46
47
|
nextRequest = new Request(nextUrl, request);
|
|
@@ -49,13 +50,13 @@ const dev = async () => {
|
|
|
49
50
|
const res = await worker.fetch(nextRequest);
|
|
50
51
|
const status = res.status;
|
|
51
52
|
console.log(
|
|
52
|
-
`[
|
|
53
|
+
`[ESA Dev] ${request.method} ${url.pathname} ${getColorForStatusCode(status, res.statusText)}`
|
|
53
54
|
);
|
|
54
55
|
return res;
|
|
55
56
|
} catch (err) {
|
|
56
57
|
console.error(err);
|
|
57
58
|
console.log(
|
|
58
|
-
`[
|
|
59
|
+
`[ESA Dev] ${request.method} ${url.pathname} ${getColorForStatusCode(500, 'Internal Server Error')}`
|
|
59
60
|
);
|
|
60
61
|
return new Response('Internal Server Error', {
|
|
61
62
|
status: 500
|
|
@@ -9,19 +9,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import path from 'path';
|
|
11
11
|
import fs from 'fs';
|
|
12
|
-
import logger from '
|
|
13
|
-
import devBuild from '
|
|
14
|
-
import t from '
|
|
15
|
-
import { getDevConf
|
|
16
|
-
import {
|
|
12
|
+
import logger from '../../../libs/logger.js';
|
|
13
|
+
import devBuild from '../build.js';
|
|
14
|
+
import t from '../../../i18n/index.js';
|
|
15
|
+
import { getDevConf } from '../../../utils/fileUtils/index.js';
|
|
16
|
+
import { getRoot, getDirName } from '../../../utils/fileUtils/base.js';
|
|
17
|
+
import { checkPort } from '../../../utils/checkDevPort.js';
|
|
17
18
|
const generateEntry = (id, projectEntry, userRoot) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
19
|
const __dirname = getDirName(import.meta.url);
|
|
19
20
|
const devDir = path.resolve(userRoot, '.dev');
|
|
20
21
|
const devEntry = path.resolve(devDir, `devEntry-${id}.js`);
|
|
21
|
-
const devEntryTemp = path.resolve(__dirname, './
|
|
22
|
+
const devEntryTemp = path.resolve(__dirname, './devEntry.js');
|
|
22
23
|
const devEntryTempFile = fs.readFileSync(devEntryTemp, 'utf-8');
|
|
23
24
|
const mockDevDir = path.resolve(userRoot, '.dev/mock');
|
|
24
|
-
const mockDir = path.resolve(__dirname, './
|
|
25
|
+
const mockDir = path.resolve(__dirname, './mock');
|
|
25
26
|
if (!fs.existsSync(devDir)) {
|
|
26
27
|
yield fs.promises.mkdir(devDir);
|
|
27
28
|
}
|
|
@@ -39,10 +40,12 @@ const generateEntry = (id, projectEntry, userRoot) => __awaiter(void 0, void 0,
|
|
|
39
40
|
const prepare = (configPath, entry, port, localUpstream, userRoot) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
41
|
const options = {};
|
|
41
42
|
const currentOptions = { entry, port, localUpstream };
|
|
43
|
+
// 支持同时跑多个 deno
|
|
42
44
|
const id = new Date().getTime().toString();
|
|
43
45
|
// @ts-ignore
|
|
44
46
|
global.id = id;
|
|
45
47
|
yield generateEntry(id, entry, userRoot);
|
|
48
|
+
// 给每一次 dev 的配置项,在一个文件中通过 id 区分
|
|
46
49
|
if (fs.existsSync(configPath)) {
|
|
47
50
|
const currentConfig = fs
|
|
48
51
|
.readFileSync(configPath, 'utf-8')
|