xhs-mp-compiler-cli 2.0.0-beta.3 → 2.0.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.
Files changed (94) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +48 -34
  2. package/dist/bin/xhs-mp-cli-dev.js +44 -44
  3. package/dist/bin/xhs-mp-cli.js +22 -12
  4. package/dist/compiler.d.ts +21 -28
  5. package/dist/compiler.js +327 -327
  6. package/dist/compilerImpl.d.ts +19 -0
  7. package/dist/compilerImpl.js +173 -0
  8. package/dist/config/constant.config.d.ts +24 -0
  9. package/dist/config/constant.config.js +82 -30
  10. package/dist/config/dir.config.js +23 -13
  11. package/dist/index.d.ts +9 -9
  12. package/dist/index.js +454 -408
  13. package/dist/packs/index.d.ts +4 -4
  14. package/dist/packs/index.js +38 -30
  15. package/dist/packs/mp-pack/index.js +38 -30
  16. package/dist/packs/webpack/dev-server/index.js +61 -50
  17. package/dist/packs/webpack/dev-server/lib/ensurePort.js +33 -23
  18. package/dist/packs/webpack/dev-server/lib/openBrowser.js +28 -18
  19. package/dist/packs/webpack/index.d.ts +1 -1
  20. package/dist/packs/webpack/index.js +99 -90
  21. package/dist/packs/webpack/webpack.d.ts +2 -1
  22. package/dist/packs/webpack/webpack.js +81 -63
  23. package/dist/presets/babel.d.ts +0 -1
  24. package/dist/presets/babel.js +64 -58
  25. package/dist/presets/common.js +42 -45
  26. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +50 -40
  27. package/dist/presets/configs/minigame/assets/index.js +62 -52
  28. package/dist/presets/configs/minigame/service/index.js +49 -30
  29. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +33 -23
  30. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +2 -2
  31. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -60
  32. package/dist/presets/configs/minigame/service/serviceEntryPlugin.d.ts +3 -3
  33. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +39 -29
  34. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +120 -113
  35. package/dist/presets/configs/miniprogram/assets/index.js +45 -43
  36. package/dist/presets/configs/miniprogram/render/component.js +51 -41
  37. package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
  38. package/dist/presets/configs/miniprogram/render/index.js +85 -68
  39. package/dist/presets/configs/miniprogram/render/render.js +53 -43
  40. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.js +84 -49
  41. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  42. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.js +164 -126
  43. package/dist/presets/configs/miniprogram/service/index.js +82 -65
  44. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.js +60 -50
  45. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +93 -55
  46. package/dist/presets/index.js +100 -90
  47. package/dist/presets/loaders/mg-entry-loader.js +32 -22
  48. package/dist/presets/loaders/mini-style-loader.js +79 -55
  49. package/dist/presets/loaders/mp-entry-loader.js +149 -140
  50. package/dist/presets/loaders/wxss-loader.js +73 -63
  51. package/dist/presets/plugins/InjectorPlugin.js +23 -13
  52. package/dist/presets/plugins/SourceMapDevToolPlugin.d.ts +1 -0
  53. package/dist/presets/plugins/SourceMapDevToolPlugin.js +23 -0
  54. package/dist/presets/prod.js +88 -78
  55. package/dist/sharedFs.d.ts +2 -0
  56. package/dist/sharedFs.js +24 -0
  57. package/dist/types/index.d.ts +11 -37
  58. package/dist/types/index.js +23 -13
  59. package/dist/utils/{apm/index.d.ts → apm.d.ts} +5 -3
  60. package/dist/utils/apm.js +245 -0
  61. package/dist/utils/cache.d.ts +28 -0
  62. package/dist/utils/cache.js +49 -0
  63. package/dist/utils/common.js +42 -32
  64. package/dist/utils/css.d.ts +1 -1
  65. package/dist/utils/css.js +43 -35
  66. package/dist/utils/depenedHelper.js +95 -85
  67. package/dist/utils/file.js +39 -29
  68. package/dist/utils/loader-utils.js +63 -53
  69. package/dist/utils/postcssRpx2Vw.js +43 -33
  70. package/dist/utils/project.d.ts +3 -2
  71. package/dist/utils/project.js +129 -119
  72. package/dist/utils/projectConfig.js +24 -15
  73. package/dist/utils/utils.js +51 -42
  74. package/dist/utils/workerPool.d.ts +29 -0
  75. package/dist/utils/workerPool.js +125 -0
  76. package/dist/utils/zip.js +239 -229
  77. package/package.json +33 -14
  78. package/dist/compilerCP.d.ts +0 -1
  79. package/dist/compilerCP.js +0 -175
  80. package/dist/presets/loaders/sjs-loader/index.d.ts +0 -12
  81. package/dist/presets/loaders/sjs-loader/index.js +0 -605
  82. package/dist/presets/loaders/sjs-loader/scope.d.ts +0 -36
  83. package/dist/presets/loaders/sjs-loader/scope.js +0 -122
  84. package/dist/presets/loaders/sjs-loader/tranform.d.ts +0 -2
  85. package/dist/presets/loaders/sjs-loader/tranform.js +0 -31
  86. package/dist/utils/apm/index.js +0 -198
  87. package/dist/utils/asyncWebpack.d.ts +0 -3
  88. package/dist/utils/asyncWebpack.js +0 -38
  89. package/dist/utils/const.d.ts +0 -27
  90. package/dist/utils/const.js +0 -47
  91. package/dist/utils/getSuffixName.d.ts +0 -3
  92. package/dist/utils/getSuffixName.js +0 -18
  93. package/dist/utils/tagTransformMap.d.ts +0 -36
  94. package/dist/utils/tagTransformMap.js +0 -39
@@ -0,0 +1,125 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ // 获取当前设备的 CPU 线程数目,作为 numberOfThreads 的默认值。
13
+ const { length: cpusLength } = require('os').cpus();
14
+ const { Worker } = require('worker_threads');
15
+ class WorkerPool {
16
+ constructor(workerPath, options = {}, numberOfThreads = cpusLength) {
17
+ this._workersById = {};
18
+ this._activeWorkersById = {};
19
+ if (numberOfThreads < 1) {
20
+ throw new Error('Number of threads should be greater or equal than 1!');
21
+ }
22
+ this.workerPath = workerPath;
23
+ this.numberOfThreads = numberOfThreads;
24
+ // 任务队列
25
+ this._queue = [];
26
+ // Worker 索引
27
+ this._workersById = {};
28
+ // Worker 激活状态索引
29
+ this._activeWorkersById = {};
30
+ // 创建 Workers
31
+ for (let i = 0; i < this.numberOfThreads; i++) {
32
+ const worker = new Worker(workerPath, options);
33
+ this._workersById[i] = worker;
34
+ // 将这些 Worker 设置为未激活状态
35
+ this._activeWorkersById[i] = false;
36
+ }
37
+ }
38
+ /**
39
+ * 检查空闲的 Worker
40
+ */
41
+ getInactiveWorkerId() {
42
+ for (let i = 0; i < this.numberOfThreads; i++) {
43
+ if (!this._activeWorkersById[i])
44
+ return i;
45
+ }
46
+ return -1;
47
+ }
48
+ /**
49
+ * 调用 Worker 执行,目的是在指定的 Worker 里执行指定的任务
50
+ */
51
+ runWorker(workerId, taskObj) {
52
+ const worker = this._workersById[workerId];
53
+ // 当任务执行完毕后执行
54
+ const doAfterTaskIsFinished = () => {
55
+ // 去除所有的 Listener,不然一次次添加不同的 Listener 会内存溢出(OOM)
56
+ worker.removeAllListeners('message');
57
+ worker.removeAllListeners('error');
58
+ // 将这个 Worker 设为未激活状态
59
+ this._activeWorkersById[workerId] = false;
60
+ if (this._queue.length) {
61
+ // 任务队列非空,使用该 Worker 执行任务队列中第一个任务
62
+ this.runWorker(workerId, this._queue.shift());
63
+ }
64
+ };
65
+ // 将这个 Worker 设置为激活状态
66
+ this._activeWorkersById[workerId] = true;
67
+ // 设置两个回调,用于 Worker 的监听器
68
+ const messageCallback = result => {
69
+ taskObj.cb(null, result);
70
+ doAfterTaskIsFinished();
71
+ };
72
+ const errorCallback = error => {
73
+ taskObj.cb(error);
74
+ doAfterTaskIsFinished();
75
+ };
76
+ // 为 Worker 添加 'message' 和 'error' 两个 Listener
77
+ worker.once('message', messageCallback);
78
+ worker.once('error', errorCallback);
79
+ // 将数据传给 Worker 供其获取和执行
80
+ worker.postMessage(taskObj.data);
81
+ }
82
+ /**
83
+ * 运行线程
84
+ */
85
+ run(data) {
86
+ // Promise 是个好东西
87
+ return new Promise((resolve, reject) => {
88
+ // 调用 getInactiveWorkerId() 获取一个空闲的 Worker
89
+ const availableWorkerId = this.getInactiveWorkerId();
90
+ const taskObj = {
91
+ data,
92
+ cb: (error, result) => {
93
+ // 虽然 Workers 需要使用 Listener 和 Callback,但这不能阻止我们使用 Promise,对吧?
94
+ // 不,你不能 util.promisify(taskObj) 。人不能,至少不应该。
95
+ if (error)
96
+ reject(error);
97
+ return resolve(result);
98
+ }
99
+ };
100
+ if (availableWorkerId === -1) {
101
+ // 当前没有空闲的 Workers 了,把任务丢进队列里,这样一旦有 Workers 空闲时就会开始执行。
102
+ this._queue.push(taskObj);
103
+ return null;
104
+ }
105
+ // 有一个空闲的 Worker,用它执行任务
106
+ this.runWorker(availableWorkerId, taskObj);
107
+ });
108
+ }
109
+ /**
110
+ * 销毁
111
+ */
112
+ destroy(force = false) {
113
+ for (let i = 0; i < this.numberOfThreads; i++) {
114
+ if (this._activeWorkersById[i] && !force) {
115
+ // 通常情况下,不应该在还有 Worker 在执行的时候就销毁它,这一定是什么地方出了问题,所以还是抛个 Error 比较好
116
+ // 不过保留一个 force 参数,总有人用得到的
117
+ throw new Error(`The worker ${i} is still runing!`);
118
+ }
119
+ // 销毁这个 Worker
120
+ this._workersById[i].terminate();
121
+ }
122
+ }
123
+ }
124
+ exports.default = WorkerPool;
125
+ });
package/dist/utils/zip.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
2
  if (k2 === undefined) k2 = k;
4
3
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -31,257 +30,268 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
30
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
31
  });
33
32
  };
34
- Object.defineProperty(exports, "__esModule", { value: true });
35
- exports.genZip = exports.unzipMac = exports.unzip = exports.Zip = void 0;
36
- const child_process_1 = require("child_process");
37
- const fs_1 = require("fs");
38
- const fse = __importStar(require("fs-extra"));
39
- const debug = require('debug')('zip');
40
- const path = require('path');
41
- const { promisify } = require('util');
42
- const stream = require('stream');
43
- const archiver = require('archiver');
44
- const yauzl = require('yauzl');
45
- const openZip = promisify(yauzl.open);
46
- const pipeline = promisify(stream.pipeline);
47
- class Extractor {
48
- constructor(zipPath, opts) {
49
- this.zipPath = '';
50
- this.opts = null;
51
- this.zipfile = null;
52
- this.canceled = false;
53
- this.zipPath = zipPath;
54
- this.opts = opts;
33
+ (function (factory) {
34
+ if (typeof module === "object" && typeof module.exports === "object") {
35
+ var v = factory(require, exports);
36
+ if (v !== undefined) module.exports = v;
55
37
  }
56
- extract() {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- debug('opening', this.zipPath, 'with opts', this.opts);
59
- this.zipfile = yield openZip(this.zipPath, { lazyEntries: true });
38
+ else if (typeof define === "function" && define.amd) {
39
+ define(["require", "exports", "child_process", "fs", "fs-extra"], factory);
40
+ }
41
+ })(function (require, exports) {
42
+ "use strict";
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.genZip = exports.Zip = void 0;
45
+ exports.unzip = unzip;
46
+ exports.unzipMac = unzipMac;
47
+ const child_process_1 = require("child_process");
48
+ const fs_1 = require("fs");
49
+ const fse = __importStar(require("fs-extra"));
50
+ const debug = require('debug')('zip');
51
+ const path = require('path');
52
+ const { promisify } = require('util');
53
+ const stream = require('stream');
54
+ const archiver = require('archiver');
55
+ const yauzl = require('yauzl');
56
+ const openZip = promisify(yauzl.open);
57
+ const pipeline = promisify(stream.pipeline);
58
+ class Extractor {
59
+ constructor(zipPath, opts) {
60
+ this.zipPath = '';
61
+ this.opts = null;
62
+ this.zipfile = null;
60
63
  this.canceled = false;
61
- return new Promise((resolve, reject) => {
62
- this.zipfile.on('error', err => {
63
- this.canceled = true;
64
- reject(err);
65
- });
66
- this.zipfile.readEntry();
67
- this.zipfile.on('close', () => {
68
- if (!this.canceled) {
69
- debug('zip extraction complete');
70
- resolve();
71
- }
72
- });
73
- this.zipfile.on('entry', (entry) => __awaiter(this, void 0, void 0, function* () {
74
- /* istanbul ignore if */
75
- if (this.canceled) {
76
- debug('skipping entry', entry.fileName, { cancelled: this.canceled });
77
- return;
78
- }
79
- debug('zipfile entry', entry.fileName);
80
- if (entry.fileName.startsWith('__MACOSX/')) {
81
- this.zipfile.readEntry();
82
- return;
83
- }
84
- const destDir = path.dirname(path.join(this.opts.dir, entry.fileName));
85
- try {
86
- yield fs_1.promises.mkdir(destDir, { recursive: true });
87
- let canonicalDestDir = '';
88
- try {
89
- canonicalDestDir = yield fs_1.promises.realpath(destDir);
64
+ this.zipPath = zipPath;
65
+ this.opts = opts;
66
+ }
67
+ extract() {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ debug('opening', this.zipPath, 'with opts', this.opts);
70
+ this.zipfile = yield openZip(this.zipPath, { lazyEntries: true });
71
+ this.canceled = false;
72
+ return new Promise((resolve, reject) => {
73
+ this.zipfile.on('error', err => {
74
+ this.canceled = true;
75
+ reject(err);
76
+ });
77
+ this.zipfile.readEntry();
78
+ this.zipfile.on('close', () => {
79
+ if (!this.canceled) {
80
+ debug('zip extraction complete');
81
+ resolve();
90
82
  }
91
- catch (error) {
92
- canonicalDestDir = path.resolve(destDir);
83
+ });
84
+ this.zipfile.on('entry', (entry) => __awaiter(this, void 0, void 0, function* () {
85
+ /* istanbul ignore if */
86
+ if (this.canceled) {
87
+ debug('skipping entry', entry.fileName, { cancelled: this.canceled });
88
+ return;
93
89
  }
94
- const relativeDestDir = path.relative(this.opts.dir, canonicalDestDir);
95
- if (relativeDestDir.split(path.sep).includes('..')) {
96
- throw new Error(`Out of bound path "${canonicalDestDir}" found while processing file ${entry.fileName}`);
90
+ debug('zipfile entry', entry.fileName);
91
+ if (entry.fileName.startsWith('__MACOSX/')) {
92
+ this.zipfile.readEntry();
93
+ return;
97
94
  }
98
- yield this.extractEntry(entry);
99
- debug('finished processing', entry.fileName);
100
- this.zipfile.readEntry();
101
- }
102
- catch (err) {
103
- this.canceled = true;
104
- this.zipfile.close();
105
- reject(err);
106
- }
107
- }));
108
- });
109
- });
110
- }
111
- extractEntry(entry) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- /* istanbul ignore if */
114
- if (this.canceled) {
115
- debug('skipping entry extraction', entry.fileName, {
116
- cancelled: this.canceled
95
+ const destDir = path.dirname(path.join(this.opts.dir, entry.fileName));
96
+ try {
97
+ yield fs_1.promises.mkdir(destDir, { recursive: true });
98
+ let canonicalDestDir = '';
99
+ try {
100
+ canonicalDestDir = yield fs_1.promises.realpath(destDir);
101
+ }
102
+ catch (error) {
103
+ canonicalDestDir = path.resolve(destDir);
104
+ }
105
+ const relativeDestDir = path.relative(this.opts.dir, canonicalDestDir);
106
+ if (relativeDestDir.split(path.sep).includes('..')) {
107
+ throw new Error(`Out of bound path "${canonicalDestDir}" found while processing file ${entry.fileName}`);
108
+ }
109
+ yield this.extractEntry(entry);
110
+ debug('finished processing', entry.fileName);
111
+ this.zipfile.readEntry();
112
+ }
113
+ catch (err) {
114
+ this.canceled = true;
115
+ this.zipfile.close();
116
+ reject(err);
117
+ }
118
+ }));
117
119
  });
118
- return;
119
- }
120
- if (this.opts.onEntry) {
121
- this.opts.onEntry(entry, this.zipfile);
122
- }
123
- const dest = path.join(this.opts.dir, entry.fileName);
124
- // convert external file attr int into a fs stat mode int
125
- const mode = (entry.externalFileAttributes >> 16) & 0xffff;
126
- // check if it's a symlink or dir (using stat mode constants)
127
- const IFMT = 61440;
128
- const IFDIR = 16384;
129
- const IFLNK = 40960;
130
- const symlink = (mode & IFMT) === IFLNK;
131
- let isDir = (mode & IFMT) === IFDIR;
132
- // Failsafe, borrowed from jsZip
133
- if (!isDir && entry.fileName.endsWith('/')) {
134
- isDir = true;
135
- }
136
- // check for windows weird way of specifying a directory
137
- // https://github.com/maxogden/extract-zip/issues/13#issuecomment-154494566
138
- const madeBy = entry.versionMadeBy >> 8;
139
- if (!isDir)
140
- isDir = madeBy === 0 && entry.externalFileAttributes === 16;
141
- debug('extracting entry', {
142
- filename: entry.fileName,
143
- isDir,
144
- isSymlink: symlink
145
120
  });
146
- const procMode = this.getExtractedMode(mode, isDir) & 0o777;
147
- // always ensure folders are created
148
- const destDir = isDir ? dest : path.dirname(dest);
149
- const mkdirOptions = { recursive: true };
150
- if (isDir) {
151
- mkdirOptions.mode = procMode;
152
- }
153
- debug('mkdir', Object.assign({ dir: destDir }, mkdirOptions));
154
- yield fs_1.promises.mkdir(destDir, mkdirOptions);
155
- if (isDir)
156
- return;
157
- debug('opening read stream', dest);
158
- const readStream = yield promisify(this.zipfile.openReadStream.bind(this.zipfile))(entry);
159
- if (symlink) {
160
- // const link = await getStream(readStream)
161
- // debug('creating symlink', link, dest)
162
- // await fs.symlink(link, dest)
163
- }
164
- else {
165
- yield pipeline(readStream, (0, fs_1.createWriteStream)(dest, { mode: procMode }));
166
- }
167
- });
168
- }
169
- getExtractedMode(entryMode, isDir) {
170
- let mode = entryMode;
171
- // Set defaults, if necessary
172
- if (mode === 0) {
173
- if (isDir) {
174
- if (this.opts.defaultDirMode) {
175
- mode = parseInt(this.opts.defaultDirMode, 10);
121
+ }
122
+ extractEntry(entry) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ /* istanbul ignore if */
125
+ if (this.canceled) {
126
+ debug('skipping entry extraction', entry.fileName, {
127
+ cancelled: this.canceled
128
+ });
129
+ return;
176
130
  }
177
- if (!mode) {
178
- mode = 0o755;
131
+ if (this.opts.onEntry) {
132
+ this.opts.onEntry(entry, this.zipfile);
179
133
  }
180
- }
181
- else {
182
- if (this.opts.defaultFileMode) {
183
- mode = parseInt(this.opts.defaultFileMode, 10);
134
+ const dest = path.join(this.opts.dir, entry.fileName);
135
+ // convert external file attr int into a fs stat mode int
136
+ const mode = (entry.externalFileAttributes >> 16) & 0xffff;
137
+ // check if it's a symlink or dir (using stat mode constants)
138
+ const IFMT = 61440;
139
+ const IFDIR = 16384;
140
+ const IFLNK = 40960;
141
+ const symlink = (mode & IFMT) === IFLNK;
142
+ let isDir = (mode & IFMT) === IFDIR;
143
+ // Failsafe, borrowed from jsZip
144
+ if (!isDir && entry.fileName.endsWith('/')) {
145
+ isDir = true;
146
+ }
147
+ // check for windows weird way of specifying a directory
148
+ // https://github.com/maxogden/extract-zip/issues/13#issuecomment-154494566
149
+ const madeBy = entry.versionMadeBy >> 8;
150
+ if (!isDir)
151
+ isDir = madeBy === 0 && entry.externalFileAttributes === 16;
152
+ debug('extracting entry', {
153
+ filename: entry.fileName,
154
+ isDir,
155
+ isSymlink: symlink
156
+ });
157
+ const procMode = this.getExtractedMode(mode, isDir) & 0o777;
158
+ // always ensure folders are created
159
+ const destDir = isDir ? dest : path.dirname(dest);
160
+ const mkdirOptions = { recursive: true };
161
+ if (isDir) {
162
+ mkdirOptions.mode = procMode;
163
+ }
164
+ debug('mkdir', Object.assign({ dir: destDir }, mkdirOptions));
165
+ yield fs_1.promises.mkdir(destDir, mkdirOptions);
166
+ if (isDir)
167
+ return;
168
+ debug('opening read stream', dest);
169
+ const readStream = yield promisify(this.zipfile.openReadStream.bind(this.zipfile))(entry);
170
+ if (symlink) {
171
+ // const link = await getStream(readStream)
172
+ // debug('creating symlink', link, dest)
173
+ // await fs.symlink(link, dest)
174
+ }
175
+ else {
176
+ yield pipeline(readStream, (0, fs_1.createWriteStream)(dest, { mode: procMode }));
184
177
  }
185
- if (!mode) {
186
- mode = 0o644;
178
+ });
179
+ }
180
+ getExtractedMode(entryMode, isDir) {
181
+ let mode = entryMode;
182
+ // Set defaults, if necessary
183
+ if (mode === 0) {
184
+ if (isDir) {
185
+ if (this.opts.defaultDirMode) {
186
+ mode = parseInt(this.opts.defaultDirMode, 10);
187
+ }
188
+ if (!mode) {
189
+ mode = 0o755;
190
+ }
191
+ }
192
+ else {
193
+ if (this.opts.defaultFileMode) {
194
+ mode = parseInt(this.opts.defaultFileMode, 10);
195
+ }
196
+ if (!mode) {
197
+ mode = 0o644;
198
+ }
187
199
  }
188
200
  }
201
+ return mode;
189
202
  }
190
- return mode;
191
203
  }
192
- }
193
- class Zip {
194
- constructor(options) {
195
- this.options = options;
196
- this.archive = archiver('zip', {
197
- zlib: { level: 9 } // Sets the compression level.
198
- });
199
- this.initOutput();
204
+ class Zip {
205
+ constructor(options) {
206
+ this.options = options;
207
+ this.archive = archiver('zip', {
208
+ zlib: { level: 9 } // Sets the compression level.
209
+ });
210
+ this.initOutput();
211
+ }
212
+ initOutput() {
213
+ const { outputDir } = this.options;
214
+ this.output = fse.createWriteStream(outputDir);
215
+ this.archive.pipe(this.output);
216
+ }
217
+ finalize() {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ return this.archive.finalize();
220
+ });
221
+ }
200
222
  }
201
- initOutput() {
202
- const { outputDir } = this.options;
203
- this.output = fse.createWriteStream(outputDir);
204
- this.archive.pipe(this.output);
223
+ exports.Zip = Zip;
224
+ /**
225
+ * 解压 zip 文件 yauzl
226
+ * @param zipPath string
227
+ * @param outputDir string
228
+ * @param options unzipOptions
229
+ */
230
+ function unzip(zipPath, outputDir) {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ debug('creating target directory', outputDir);
233
+ if (!path.isAbsolute(outputDir)) {
234
+ throw new Error('Target directory is expected to be absolute');
235
+ }
236
+ yield fs_1.promises.mkdir(outputDir, { recursive: true });
237
+ const _outputDir = yield fs_1.promises.realpath(outputDir);
238
+ return new Extractor(zipPath, {
239
+ dir: _outputDir
240
+ }).extract();
241
+ });
205
242
  }
206
- finalize() {
243
+ /**
244
+ * 解压 zip 文件
245
+ * @param zipPath string
246
+ * @param outputDir string
247
+ */
248
+ function unzipMac(zipPath, outputDir) {
207
249
  return __awaiter(this, void 0, void 0, function* () {
208
- return this.archive.finalize();
250
+ return new Promise((resolve, reject) => {
251
+ const proc = (0, child_process_1.spawn)('/usr/bin/unzip', [zipPath, '-d', outputDir]);
252
+ const output = '';
253
+ proc.on('error', err => {
254
+ reject(err);
255
+ });
256
+ proc.on('exit', (code) => {
257
+ if (code > 0) {
258
+ reject(new Error(`Exited with code ${code}`));
259
+ }
260
+ else {
261
+ resolve(output);
262
+ }
263
+ });
264
+ proc.stderr.on('data', chunk => {
265
+ console.error(chunk.toString());
266
+ });
267
+ proc.stdout.on('data', chunk => {
268
+ // console.log(chunk.toString());
269
+ });
270
+ });
209
271
  });
210
272
  }
211
- }
212
- exports.Zip = Zip;
213
- /**
214
- * 解压 zip 文件 yauzl
215
- * @param zipPath string
216
- * @param outputDir string
217
- * @param options unzipOptions
218
- */
219
- function unzip(zipPath, outputDir) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- debug('creating target directory', outputDir);
222
- if (!path.isAbsolute(outputDir)) {
223
- throw new Error('Target directory is expected to be absolute');
224
- }
225
- yield fs_1.promises.mkdir(outputDir, { recursive: true });
226
- const _outputDir = yield fs_1.promises.realpath(outputDir);
227
- return new Extractor(zipPath, {
228
- dir: _outputDir
229
- }).extract();
230
- });
231
- }
232
- exports.unzip = unzip;
233
- /**
234
- * 解压 zip 文件
235
- * @param zipPath string
236
- * @param outputDir string
237
- */
238
- function unzipMac(zipPath, outputDir) {
239
- return __awaiter(this, void 0, void 0, function* () {
273
+ const genZip = (input, output, appId) => __awaiter(void 0, void 0, void 0, function* () {
240
274
  return new Promise((resolve, reject) => {
241
- const proc = (0, child_process_1.spawn)('/usr/bin/unzip', [zipPath, '-d', outputDir]);
242
- const output = '';
243
- proc.on('error', err => {
244
- reject(err);
245
- });
246
- proc.on('exit', (code) => {
247
- if (code > 0) {
248
- reject(new Error(`Exited with code ${code}`));
249
- }
250
- else {
251
- resolve(output);
252
- }
275
+ const outputStream = (0, fs_1.createWriteStream)(output);
276
+ const archive = archiver('zip', {
277
+ zlib: { level: 9 } // Sets the compression level.
253
278
  });
254
- proc.stderr.on('data', chunk => {
255
- console.error(chunk.toString());
279
+ outputStream.on('close', () => {
280
+ resolve();
256
281
  });
257
- proc.stdout.on('data', chunk => {
258
- // console.log(chunk.toString());
282
+ archive.on('error', error => {
283
+ console.error('archive error:', error);
284
+ reject(error);
259
285
  });
286
+ archive.pipe(outputStream);
287
+ if (appId) {
288
+ archive.directory(input, appId);
289
+ }
290
+ else {
291
+ archive.directory(input, './');
292
+ }
293
+ archive.finalize();
260
294
  });
261
295
  });
262
- }
263
- exports.unzipMac = unzipMac;
264
- const genZip = (input, output, appId) => __awaiter(void 0, void 0, void 0, function* () {
265
- return new Promise((resolve, reject) => {
266
- const outputStream = (0, fs_1.createWriteStream)(output);
267
- const archive = archiver('zip', {
268
- zlib: { level: 9 } // Sets the compression level.
269
- });
270
- outputStream.on('close', () => {
271
- resolve();
272
- });
273
- archive.on('error', error => {
274
- console.error('archive error:', error);
275
- reject(error);
276
- });
277
- archive.pipe(outputStream);
278
- if (appId) {
279
- archive.directory(input, appId);
280
- }
281
- else {
282
- archive.directory(input, './');
283
- }
284
- archive.finalize();
285
- });
296
+ exports.genZip = genZip;
286
297
  });
287
- exports.genZip = genZip;