@giteeteam/apps-cli 0.1.16 → 0.1.17

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 (2) hide show
  1. package/dist/index.js +140 -18
  2. package/package.json +4 -2
package/dist/index.js CHANGED
@@ -9,11 +9,13 @@ var archiver = require('archiver');
9
9
  var ora = require('ora');
10
10
  var yaml = require('yaml');
11
11
  var inquirer = require('inquirer');
12
- var simpleGit = require('simple-git');
13
12
  var uuid = require('uuid');
13
+ var simpleGit = require('simple-git');
14
14
  var chalk = require('chalk');
15
15
  var readline = require('readline');
16
16
  var execa = require('execa');
17
+ var child_process = require('child_process');
18
+ var chokidar = require('chokidar');
17
19
 
18
20
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
21
 
@@ -25,8 +27,10 @@ var inquirer__default = /*#__PURE__*/_interopDefaultLegacy(inquirer);
25
27
  var simpleGit__default = /*#__PURE__*/_interopDefaultLegacy(simpleGit);
26
28
  var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);
27
29
  var execa__default = /*#__PURE__*/_interopDefaultLegacy(execa);
30
+ var child_process__default = /*#__PURE__*/_interopDefaultLegacy(child_process);
31
+ var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
28
32
 
29
- var version = "0.1.16";
33
+ var version = "0.1.17";
30
34
 
31
35
  const packageApp = async ({ outputDir, manifest, copyFiles }) => {
32
36
  const { resources } = manifest;
@@ -86,14 +90,9 @@ const archiveApp = async (archiveDir, pkgName) => {
86
90
  };
87
91
 
88
92
  const bundleApp = async (opts) => {
89
- var _a, _b;
90
93
  const { manifest, mode, outputDir, inputDir } = opts;
91
- const fileNames = new Set();
92
- (_b = (_a = manifest.modules) === null || _a === void 0 ? void 0 : _a.function) === null || _b === void 0 ? void 0 : _b.forEach(({ handler }) => {
93
- const filename = handler.split('.')[0];
94
- fileNames.add(filename);
95
- });
96
- const bundlePromise = [...fileNames].map(async (fileName) => {
94
+ const functionFiles = manifest.getFunctionFiles();
95
+ const bundlePromise = functionFiles.map(async (fileName) => {
97
96
  await appsBundler.bundle({
98
97
  inputDir,
99
98
  fileName,
@@ -117,16 +116,66 @@ const execActionWithSpinner = async (message, action) => {
117
116
  spinner.succeed(message.succeed);
118
117
  };
119
118
 
120
- const getManifestObject = async () => {
119
+ class Manifest {
120
+ constructor(manifestStr) {
121
+ this.manifest = yaml.parse(manifestStr);
122
+ }
123
+ get resources() {
124
+ return this.manifest.resources;
125
+ }
126
+ get app() {
127
+ return this.manifest.app;
128
+ }
129
+ /**
130
+ * 根据key获取执行的function名称
131
+ * @param key
132
+ * @returns
133
+ */
134
+ getFunctionByKey(key) {
135
+ var _a, _b, _c, _d, _e;
136
+ const { modules } = this.manifest;
137
+ for (const k in modules) {
138
+ if (k === 'function') {
139
+ continue;
140
+ }
141
+ else if (k === 'importer') {
142
+ // importer扩展点需要判断function和validate
143
+ if (key === ((_a = modules[k]) === null || _a === void 0 ? void 0 : _a.function) || key === ((_b = modules[k]) === null || _b === void 0 ? void 0 : _b.validate)) {
144
+ return (_c = modules === null || modules === void 0 ? void 0 : modules.function) === null || _c === void 0 ? void 0 : _c.find(v => v.key === key);
145
+ }
146
+ }
147
+ else if (Array.isArray(modules[k])) {
148
+ const targetModule = (_d = modules[k]) === null || _d === void 0 ? void 0 : _d.find(v => v.key === key);
149
+ if (targetModule) {
150
+ return (_e = modules === null || modules === void 0 ? void 0 : modules.function) === null || _e === void 0 ? void 0 : _e.find(v => v.key === targetModule.function);
151
+ }
152
+ }
153
+ }
154
+ }
155
+ /**
156
+ * 获取function下的文件列表
157
+ * @returns
158
+ */
159
+ getFunctionFiles() {
160
+ var _a, _b;
161
+ const fileNames = new Set();
162
+ (_b = (_a = this.manifest.modules) === null || _a === void 0 ? void 0 : _a.function) === null || _b === void 0 ? void 0 : _b.forEach(({ handler }) => {
163
+ const filename = handler.split('.')[0];
164
+ fileNames.add(filename);
165
+ });
166
+ return [...fileNames];
167
+ }
168
+ }
169
+
170
+ const getManifest = async () => {
121
171
  const manifestFile = path__default["default"].resolve('./manifest.yml');
122
172
  const strBuffer = await fse__default["default"].readFile(manifestFile);
123
- const obj = yaml.parse(strBuffer.toString());
124
- return obj;
173
+ return new Manifest(strBuffer.toString());
125
174
  };
126
175
 
127
- var run$1 = async ({ outputDir, inputDir, prod, copyFiles, zip }) => {
128
- const manifest = await getManifestObject();
129
- const { app: { version, key }, } = manifest;
176
+ var run$2 = async ({ outputDir, inputDir, prod, copyFiles, zip }) => {
177
+ const manifest = await getManifest();
178
+ const { version, key } = manifest.app;
130
179
  if (!key) {
131
180
  console.error('manifest app key is required');
132
181
  return;
@@ -163,7 +212,7 @@ var run$1 = async ({ outputDir, inputDir, prod, copyFiles, zip }) => {
163
212
  const defaultCopyFiles = ['manifest.yml'];
164
213
  var build = async (options) => {
165
214
  const { input = 'src', output = 'dist', prod, copy = [], zip } = options;
166
- await run$1({
215
+ await run$2({
167
216
  outputDir: path__default["default"].resolve(output),
168
217
  inputDir: path__default["default"].resolve(input),
169
218
  prod: !!prod,
@@ -276,7 +325,7 @@ const dependenciesInstall = async (projectPath) => {
276
325
  succeed: 'Install dependencies success!',
277
326
  }, () => command.executeCommand('yarn', ['install'], projectPath));
278
327
  };
279
- var run = async (options) => {
328
+ var run$1 = async (options) => {
280
329
  const { cwd, packageName } = options;
281
330
  const projectPath = path__default["default"].join(cwd, packageName);
282
331
  if (await fse__default["default"].pathExists(projectPath)) {
@@ -324,12 +373,84 @@ var create = async (packageName) => {
324
373
  console.error('Please input your project name');
325
374
  return;
326
375
  }
327
- await run({
376
+ await run$1({
328
377
  cwd,
329
378
  packageName,
330
379
  });
331
380
  };
332
381
 
382
+ const containerName = 'cli-dev-apps-runtime-server';
383
+ class DockerService {
384
+ constructor(imageName) {
385
+ this.imageName = imageName;
386
+ }
387
+ async downloadImage() {
388
+ await this.execPromise(`docker pull ${this.imageName}`);
389
+ }
390
+ async runContainer() {
391
+ const command = `docker run -d -p 8455:8455 -e PORT=8455 --name ${containerName} ${this.imageName}`;
392
+ await this.execPromise(command);
393
+ }
394
+ async removeContainer() {
395
+ await this.execPromise(`docker rm -f ${containerName}`);
396
+ }
397
+ execPromise(cmd) {
398
+ return new Promise(resolve => {
399
+ child_process__default["default"].exec(cmd, (err, stdout, stderr) => {
400
+ resolve({
401
+ err: err,
402
+ stdout: stdout,
403
+ stderr: stderr,
404
+ });
405
+ });
406
+ });
407
+ }
408
+ }
409
+
410
+ const watchFiles = async (options) => {
411
+ const { inputDir, outputDir } = options;
412
+ const manifest = await getManifest();
413
+ const functionFiles = manifest.getFunctionFiles();
414
+ for (const fileName of functionFiles) {
415
+ const filePath = path__default["default"].join(inputDir, fileName);
416
+ const watcher = chokidar__default["default"].watch(filePath);
417
+ watcher.on('change', () => {
418
+ appsBundler.bundle({
419
+ inputDir,
420
+ outputDir,
421
+ fileName,
422
+ mode: appsBundler.MODE.DEV,
423
+ });
424
+ });
425
+ }
426
+ };
427
+
428
+ var run = async (options) => {
429
+ const { imageName, inputDir, outputDir } = options;
430
+ const dockerService = new DockerService(imageName);
431
+ await dockerService.downloadImage();
432
+ await dockerService.runContainer();
433
+ process.on('SIGINT', () => {
434
+ console.log('process,SIGINT');
435
+ dockerService.removeContainer();
436
+ process.exit();
437
+ });
438
+ await watchFiles({ inputDir, outputDir });
439
+ };
440
+
441
+ const image = {
442
+ repository: 'registry.baidubce.com/proxima/proxima-apps-runtime-server',
443
+ tag: '2.5.0-alpha-41',
444
+ };
445
+ var dev = async (options) => {
446
+ const { input = 'src', output = 'dist' } = options;
447
+ await run({
448
+ imageName: `${image.repository}:${image.tag}`,
449
+ outputDir: path__default["default"].resolve(output),
450
+ inputDir: path__default["default"].resolve(input),
451
+ });
452
+ };
453
+
333
454
  const program = new commander.Command();
334
455
  program.version(version).description('Giteeteam Apps Cli');
335
456
  program
@@ -341,4 +462,5 @@ program
341
462
  .option('-o --output [dir]', 'output dir')
342
463
  .action(build);
343
464
  program.command('create [name]').action(create);
465
+ program.command('dev').action(dev);
344
466
  program.parse(process.argv);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@giteeteam/apps-cli",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "description": "Giteeteam Apps cli",
5
5
  "keywords": [
6
6
  "typescript",
@@ -39,8 +39,10 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@giteeteam/apps-bundler": "^0.1.7",
42
+ "@giteeteam/apps-manifest": "^0.0.1",
42
43
  "archiver": "^5.3.1",
43
44
  "chalk": "^4.1.2",
45
+ "chokidar": "^3.5.3",
44
46
  "commander": "^9.3.0",
45
47
  "execa": "^5.1.1",
46
48
  "fs-extra": "^10.1.0",
@@ -56,5 +58,5 @@
56
58
  "@types/inquirer": "^8.2.1",
57
59
  "@types/uuid": "^8.3.4"
58
60
  },
59
- "gitHead": "ed0a34bda70ee112ec04caf0ee75247c912b0d5b"
61
+ "gitHead": "d8d5a8bac2ee65541cd015e3f2bb574caecf39fb"
60
62
  }