xhs-mp-compiler-cli 1.1.1 → 1.1.2

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 (34) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +2 -2
  2. package/dist/bin/xhs-mp-cli-dev.js +3 -3
  3. package/dist/compilerCP.js +3 -3
  4. package/dist/config/constant.config.d.ts +20 -0
  5. package/dist/config/constant.config.js +25 -5
  6. package/dist/config/dir.config.d.ts +3 -0
  7. package/dist/config/dir.config.js +16 -0
  8. package/dist/index.d.ts +84 -1
  9. package/dist/index.js +452 -3
  10. package/dist/packs/mp-pack/index.js +1 -1
  11. package/dist/{dev-server → packs/webpack/dev-server}/index.d.ts +1 -1
  12. package/dist/{dev-server → packs/webpack/dev-server}/index.js +1 -1
  13. package/dist/packs/webpack/index.d.ts +1 -1
  14. package/dist/packs/webpack/index.js +1 -1
  15. package/dist/presets/common.js +13 -11
  16. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.d.ts +2 -2
  17. package/dist/presets/configs/minigame/assets/index.d.ts +1 -0
  18. package/dist/presets/configs/miniprogram/render/index.js +6 -5
  19. package/dist/presets/configs/miniprogram/service/index.js +7 -7
  20. package/dist/presets/index.d.ts +1 -1
  21. package/dist/utils/file.d.ts +2 -0
  22. package/dist/utils/file.js +67 -0
  23. package/dist/utils/project.d.ts +27 -0
  24. package/dist/utils/project.js +164 -0
  25. package/dist/utils/utils.d.ts +2 -0
  26. package/dist/utils/utils.js +13 -0
  27. package/dist/utils/zip.d.ts +28 -0
  28. package/dist/utils/zip.js +287 -0
  29. package/package.json +11 -5
  30. /package/dist/{dev-server → packs/webpack/dev-server}/lib/ensurePort.d.ts +0 -0
  31. /package/dist/{dev-server → packs/webpack/dev-server}/lib/ensurePort.js +0 -0
  32. /package/dist/{dev-server → packs/webpack/dev-server}/lib/openBrowser.d.ts +0 -0
  33. /package/dist/{dev-server → packs/webpack/dev-server}/lib/openBrowser.js +0 -0
  34. /package/dist/{dev-server → packs/webpack/dev-server}/lib/openChrome.applescript +0 -0
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.getFolderSize = exports.fileSize = void 0;
39
+ const fs = __importStar(require("fs"));
40
+ const globby_1 = __importDefault(require("globby"));
41
+ const fileSize = (size) => {
42
+ if (!size)
43
+ return '';
44
+ const base = 1024;
45
+ if (size < base) {
46
+ return `${size}B`;
47
+ }
48
+ if (size < Math.pow(base, 2)) {
49
+ return `${(size / base).toFixed(2)}KB`;
50
+ }
51
+ if (size < Math.pow(base, 3)) {
52
+ return `${(size / Math.pow(base, 2)).toFixed(2)}MB`;
53
+ }
54
+ if (size < Math.pow(base, 4)) {
55
+ return `${(size / Math.pow(base, 3)).toFixed(2)}GB`;
56
+ }
57
+ return `${(size / Math.pow(base, 4)).toFixed(2)}T`;
58
+ };
59
+ exports.fileSize = fileSize;
60
+ const getFolderSize = (folder) => __awaiter(void 0, void 0, void 0, function* () {
61
+ const _folder = folder.map(f => f.replace(/\\/g, '/')); // 修复windows的路径
62
+ const folders = yield (0, globby_1.default)(_folder);
63
+ // eslint-disable-next-line no-return-assign
64
+ const totalSize = folders.reduce((pre, cur) => (pre += fs.statSync(cur).size), 0);
65
+ return totalSize;
66
+ });
67
+ exports.getFolderSize = getFolderSize;
@@ -0,0 +1,27 @@
1
+ export interface IZipFactory {
2
+ outputDir: string;
3
+ ignore: string[];
4
+ prefix: string;
5
+ inputDir: string;
6
+ type: "v0" | "v1" | "v2";
7
+ appendFile?: string[];
8
+ appendBuffer?: {
9
+ buffer: Buffer;
10
+ name: string;
11
+ }[];
12
+ meta: {
13
+ root: string;
14
+ };
15
+ }
16
+ export declare const checkSubPackagesLegality: ({ appJsonContent, rootPath, distDir }: {
17
+ appJsonContent: any;
18
+ rootPath: string;
19
+ distDir: string;
20
+ }) => Promise<any[]>;
21
+ export declare const zipFactory: ({ outputDir, ignore, prefix, inputDir, type, appendFile, appendBuffer, meta, }: IZipFactory) => Promise<{
22
+ type: "v2" | "v1" | "v0";
23
+ zipPath: string;
24
+ zipSize: number;
25
+ root: string;
26
+ originSize: number;
27
+ }>;
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.zipFactory = exports.checkSubPackagesLegality = void 0;
36
+ const path = __importStar(require("path"));
37
+ const fs = __importStar(require("fs-extra"));
38
+ const zip_1 = require("./zip");
39
+ const file_1 = require("./file");
40
+ const ignoreFiles = ['!node_modules'];
41
+ const _getFolderSize = (rootPath) => __awaiter(void 0, void 0, void 0, function* () { return yield (0, file_1.getFolderSize)([rootPath, ...ignoreFiles]); });
42
+ const getCheckSizeFlag = (rootPath) => {
43
+ const projectConfig = path.resolve(rootPath, 'project.config.json');
44
+ const content = fs.readJSONSync(projectConfig);
45
+ return content.skipCheckSize;
46
+ };
47
+ // const SUB_PACKAGE_MAX_SIZE = 2 * 1024 * 1024; // 2M
48
+ // const TOTAL_PACKAGE_MAX_SIZE = 20 * 1024 * 1024; // 20M
49
+ /* 整个小程序体积不能超过 20M。 */
50
+ const checkProjectSize = (rootPath, distDir) => __awaiter(void 0, void 0, void 0, function* () {
51
+ const totalSize = yield _getFolderSize(distDir);
52
+ /* 微盟先暂时跳过整包体积校验 */
53
+ if (getCheckSizeFlag(rootPath) === true) {
54
+ return totalSize;
55
+ }
56
+ /* 跳过体积校验 */
57
+ // if (totalSize > TOTAL_PACKAGE_MAX_SIZE) {
58
+ // throw new Error(`源码包大小 ${fileSize(totalSize)} 超过 20MB 上限 `);
59
+ // }
60
+ return totalSize;
61
+ });
62
+ /* 校验subPagee的root */
63
+ const checkSubPackages = (_a) => __awaiter(void 0, [_a], void 0, function* ({ subPackages, distDir }) {
64
+ let subSize = 0;
65
+ const sizeInfo = [];
66
+ for (let i = 0; i < subPackages.length; i++) {
67
+ const item = subPackages[i];
68
+ const fullPath = path.resolve(distDir, item.root);
69
+ const size = yield _getFolderSize(fullPath);
70
+ /* 跳过体积校验 */
71
+ // if (size > SUB_PACKAGE_MAX_SIZE) {
72
+ // throw new Error(`分包 ${item.root} 大小 ${fileSize(size)} 超出 2MB 上限`);
73
+ // }
74
+ const info = {
75
+ originSize: size,
76
+ root: item.root
77
+ };
78
+ sizeInfo.push(info);
79
+ subSize += size;
80
+ }
81
+ return { subSize, sizeInfo };
82
+ });
83
+ /* 主包体积:项目移除分包之后的体积 */
84
+ const checkMainPackages = ({ totalSize, subPkgSize }) => {
85
+ const mainSize = totalSize - subPkgSize;
86
+ /* 跳过体积校验 */
87
+ // if (mainSize > SUB_PACKAGE_MAX_SIZE) {
88
+ // throw new Error(`主包大小 ${fileSize(mainSize)} 超出 2MB 上限`);
89
+ // }
90
+ const sizeInfo = {
91
+ originSize: mainSize,
92
+ root: '/'
93
+ };
94
+ return { mainSize, sizeInfo };
95
+ };
96
+ /* 校验preloadRule的预加载的包 */
97
+ const checkPreloadRule = (_b) => __awaiter(void 0, [_b], void 0, function* ({ preloadRule, subPackages, distDir }) {
98
+ for (const ruleKey in preloadRule) {
99
+ let useSize = 0;
100
+ const rulePackages = preloadRule[ruleKey];
101
+ const packages = rulePackages.packages;
102
+ for (let i = 0; i < packages.length; i++) {
103
+ const pkg = packages[i];
104
+ if (pkg === "__APP__") {
105
+ return;
106
+ }
107
+ const _pathIten = subPackages.find((v) => pkg === v.name || pkg === v.root);
108
+ if (!_pathIten)
109
+ return;
110
+ const fullPath = path.resolve(distDir, _pathIten.root);
111
+ const size = yield _getFolderSize(fullPath);
112
+ useSize += size;
113
+ /* 跳过体积校验 */
114
+ // if (useSize > SUB_PACKAGE_MAX_SIZE) {
115
+ // throw new Error(
116
+ // `预加载 ${ruleKey} 包大小 ${fileSize(useSize)} 超出 2MB 上限`
117
+ // );
118
+ // }
119
+ }
120
+ }
121
+ });
122
+ /* 校验子包体积 */
123
+ const checkSubPackagesLegality = (_c) => __awaiter(void 0, [_c], void 0, function* ({ appJsonContent, rootPath, distDir }) {
124
+ const subPackages = appJsonContent.subPackages || appJsonContent.subpackages;
125
+ const preloadRule = appJsonContent.preloadRule || {};
126
+ const totalSize = yield checkProjectSize(rootPath, distDir);
127
+ if (!subPackages || subPackages.length === 0) {
128
+ /* 未开启分包 */
129
+ /* 微盟先暂时跳过整包体积校验 */
130
+ if (getCheckSizeFlag(rootPath) === true) {
131
+ return [{ originSize: totalSize, root: '' }];
132
+ }
133
+ checkMainPackages({ totalSize, subPkgSize: 0 }); // 校验主包
134
+ return [{ originSize: totalSize, root: '' }];
135
+ }
136
+ const { subSize, sizeInfo } = yield checkSubPackages({ subPackages, distDir });
137
+ const { sizeInfo: mainSizeInfo } = checkMainPackages({ totalSize, subPkgSize: subSize });
138
+ yield checkPreloadRule({ preloadRule, subPackages, distDir });
139
+ return [mainSizeInfo, ...sizeInfo];
140
+ });
141
+ exports.checkSubPackagesLegality = checkSubPackagesLegality;
142
+ /* zip包的工厂函数 */
143
+ const zipFactory = (_d) => __awaiter(void 0, [_d], void 0, function* ({ outputDir, ignore, prefix, inputDir, type, appendFile = [], appendBuffer = [], meta, }) {
144
+ const zip = new zip_1.Zip({ outputDir });
145
+ /* 添加文件 */
146
+ appendFile.forEach((file) => {
147
+ zip.archive.file(file, {
148
+ name: path.basename(file),
149
+ prefix,
150
+ });
151
+ });
152
+ /* 添加文件buffer */
153
+ appendBuffer.forEach((item) => {
154
+ zip.archive.append(item.buffer, {
155
+ name: item.name,
156
+ prefix,
157
+ });
158
+ });
159
+ zip.archive.glob("**/*", { cwd: inputDir, ignore }, { prefix });
160
+ yield zip.finalize();
161
+ const stat = fs.statSync(outputDir);
162
+ return { type, zipPath: outputDir, zipSize: stat.size, root: meta.root, originSize: 0 };
163
+ });
164
+ exports.zipFactory = zipFactory;
@@ -1,3 +1,5 @@
1
+ export declare function getMd5(str: any): any;
2
+ export declare function normalizePath(str: any): any;
1
3
  declare const _default: {
2
4
  hasFile: (filepath: any) => boolean;
3
5
  getTsconfigPath: (config: any) => string | null;
@@ -3,9 +3,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.normalizePath = exports.getMd5 = void 0;
6
7
  // @ts-nocheck
7
8
  const path_1 = __importDefault(require("path"));
8
9
  const fs_1 = __importDefault(require("fs"));
10
+ const crypto = require('crypto');
11
+ function getMd5(str) {
12
+ const hash = crypto.createHash('md5');
13
+ hash.update(str);
14
+ const md5 = hash.digest('hex');
15
+ return md5;
16
+ }
17
+ exports.getMd5 = getMd5;
18
+ function normalizePath(str) {
19
+ return str.replace(/\\/g, '/');
20
+ }
21
+ exports.normalizePath = normalizePath;
9
22
  const hasFile = filepath => {
10
23
  try {
11
24
  const stat = fs_1.default.statSync(filepath);
@@ -0,0 +1,28 @@
1
+ import type { Archiver } from 'archiver';
2
+ import * as fse from 'fs-extra';
3
+ interface IOptions {
4
+ outputDir: string;
5
+ }
6
+ export declare class Zip {
7
+ options: IOptions;
8
+ archive: Archiver;
9
+ output: fse.WriteStream;
10
+ constructor(options: IOptions);
11
+ initOutput(): void;
12
+ finalize(): Promise<any>;
13
+ }
14
+ /**
15
+ * 解压 zip 文件 yauzl
16
+ * @param zipPath string
17
+ * @param outputDir string
18
+ * @param options unzipOptions
19
+ */
20
+ export declare function unzip(zipPath: string, outputDir: string): Promise<void>;
21
+ /**
22
+ * 解压 zip 文件
23
+ * @param zipPath string
24
+ * @param outputDir string
25
+ */
26
+ export declare function unzipMac(zipPath: string, outputDir: string): Promise<unknown>;
27
+ export declare const genZip: (input: string, output: string, appId?: string) => Promise<void>;
28
+ export {};
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
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;
55
+ }
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 });
60
+ 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);
90
+ }
91
+ catch (error) {
92
+ canonicalDestDir = path.resolve(destDir);
93
+ }
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}`);
97
+ }
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
117
+ });
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
+ });
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);
176
+ }
177
+ if (!mode) {
178
+ mode = 0o755;
179
+ }
180
+ }
181
+ else {
182
+ if (this.opts.defaultFileMode) {
183
+ mode = parseInt(this.opts.defaultFileMode, 10);
184
+ }
185
+ if (!mode) {
186
+ mode = 0o644;
187
+ }
188
+ }
189
+ }
190
+ return mode;
191
+ }
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();
200
+ }
201
+ initOutput() {
202
+ const { outputDir } = this.options;
203
+ this.output = fse.createWriteStream(outputDir);
204
+ this.archive.pipe(this.output);
205
+ }
206
+ finalize() {
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ return this.archive.finalize();
209
+ });
210
+ }
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* () {
240
+ 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
+ }
253
+ });
254
+ proc.stderr.on('data', chunk => {
255
+ console.error(chunk.toString());
256
+ });
257
+ proc.stdout.on('data', chunk => {
258
+ // console.log(chunk.toString());
259
+ });
260
+ });
261
+ });
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
+ });
286
+ });
287
+ exports.genZip = genZip;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -20,6 +20,11 @@
20
20
  "author": "zyu1 <fuzhu@xiaohongshu.com>",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
+ "archiver": "^7.0.1",
24
+ "debug": "^4.3.4",
25
+ "globby": "^11.1.0",
26
+ "query-string": "^8.1.0",
27
+ "yauzl": "^2.10.0",
23
28
  "@babel/core": "^7.12.10",
24
29
  "@babel/generator": "^7.12.10",
25
30
  "@babel/parser": "^7.12.10",
@@ -77,10 +82,11 @@
77
82
  "webpack-bundle-analyzer": "^4.4.0",
78
83
  "webpack-chain": "^6.5.1",
79
84
  "webpack-sources": "^3.2.2",
80
- "xhs-mp-compiler-ml-loader": "1.1.0",
81
- "xhs-mp-compiler-utils": "1.1.0",
82
- "xhs-mp-pack": "^1.1.0",
83
- "xhs-mp-project": "^1.1.0"
85
+ "xhs-mp-compiler-ml-loader": "1.1.1",
86
+ "xhs-mp-compiler-utils": "1.1.1",
87
+ "xhs-mp-pack": "^1.1.1",
88
+ "xhs-mp-project": "^1.1.1",
89
+ "xhs-mp-utils": "^1.1.1"
84
90
  },
85
91
  "devDependencies": {
86
92
  "@types/babel__generator": "7.6.3",