xjs-common 9.0.2 → 9.1.0-alpha.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.
@@ -34,6 +34,13 @@ export declare namespace UFile {
34
34
  * @returns exportable file path.
35
35
  */
36
36
  function reserveFilePath(dir: MaybeArray<string>, fname: string): string;
37
+ /**
38
+ * decompress zip file. this depends on os enviroment due to using the os command.
39
+ * currently this supports only windows (installed `tar` or `unzip` in gnu compiler) and linux systems (installed `unzip`).
40
+ * @param zipPath zip file to be unzipped.
41
+ * @param destDir directory that the decompress files export to.
42
+ */
43
+ function unzip(zipPath: MaybeArray<string>, destDir?: MaybeArray<string>): void;
37
44
  function joinPath(...p: MaybeArray<string>[]): string;
38
45
  }
39
46
  export {};
@@ -24,6 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.UFile = void 0;
27
+ const child_process_1 = require("child_process");
27
28
  const fs = __importStar(require("fs"));
28
29
  const path = __importStar(require("path"));
29
30
  const u_type_1 = require("./u-type");
@@ -55,7 +56,7 @@ var UFile;
55
56
  }
56
57
  UFile.rm = rm;
57
58
  function exists(p) {
58
- return fs.existsSync(joinPath(p));
59
+ return !!p && fs.existsSync(joinPath(p));
59
60
  }
60
61
  UFile.exists = exists;
61
62
  /**
@@ -113,6 +114,51 @@ var UFile;
113
114
  return dest;
114
115
  }
115
116
  UFile.reserveFilePath = reserveFilePath;
117
+ /**
118
+ * decompress zip file. this depends on os enviroment due to using the os command.
119
+ * currently this supports only windows (installed `tar` or `unzip` in gnu compiler) and linux systems (installed `unzip`).
120
+ * @param zipPath zip file to be unzipped.
121
+ * @param destDir directory that the decompress files export to.
122
+ */
123
+ function unzip(zipPath, destDir) {
124
+ if (!exists(zipPath))
125
+ throw new xjs_err_1.XjsErr(s_errCode, "There is no file on the zip path.");
126
+ if (!!destDir && !exists(destDir))
127
+ throw new xjs_err_1.XjsErr(s_errCode, "The destination directory is not found.");
128
+ let cmd = "unzip", options = null, availableCmd = true;
129
+ if (destDir)
130
+ options = `-d "${destDir}"`;
131
+ const check = () => { try {
132
+ (0, child_process_1.execSync)(`${cmd} --help`, { stdio: "ignore" });
133
+ }
134
+ catch {
135
+ availableCmd = false;
136
+ } };
137
+ check();
138
+ if (process.platform === "win32") {
139
+ if (!availableCmd) {
140
+ cmd = "tar";
141
+ options = "-xf";
142
+ availableCmd = true;
143
+ if (destDir)
144
+ options = `-C "${destDir}" ${options}`;
145
+ check();
146
+ }
147
+ }
148
+ else if (process.platform === "linux") {
149
+ }
150
+ else
151
+ throw new xjs_err_1.XjsErr(s_errCode, "The os running on is not supported for xjs unzip.");
152
+ if (!availableCmd)
153
+ throw new xjs_err_1.XjsErr(s_errCode, `"${cmd}" command is not installed.`);
154
+ try {
155
+ (0, child_process_1.execSync)([cmd, options, `"${zipPath}"`].filter(e => e).join(" "), { stdio: "ignore" });
156
+ }
157
+ catch (e) {
158
+ throw new xjs_err_1.XjsErr(s_errCode, "Something went wrong at unzip.", e);
159
+ }
160
+ }
161
+ UFile.unzip = unzip;
116
162
  function joinPath(...p) {
117
163
  return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
118
164
  }
@@ -1,4 +1,5 @@
1
1
  export declare class XjsErr extends Error {
2
2
  code: number;
3
- constructor(code: number, msg: string);
3
+ origin?: any;
4
+ constructor(code: number, msg: string, origin?: any);
4
5
  }
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.XjsErr = void 0;
4
4
  class XjsErr extends Error {
5
5
  code;
6
- constructor(code, msg) {
6
+ origin;
7
+ constructor(code, msg, origin) {
7
8
  super(`[XJS] ${msg}`);
8
9
  this.code = code;
10
+ this.origin = origin;
9
11
  }
10
12
  }
11
13
  exports.XjsErr = XjsErr;
@@ -27,6 +27,7 @@ exports.HttpResolverContext = exports.s_clientMode = void 0;
27
27
  const tls = __importStar(require("tls"));
28
28
  const zlib = __importStar(require("zlib"));
29
29
  const fs = __importStar(require("fs"));
30
+ const path = __importStar(require("path"));
30
31
  const url_1 = require("url");
31
32
  const https_1 = require("https");
32
33
  const http_1 = require("http");
@@ -214,8 +215,9 @@ class HttpResolverContext {
214
215
  const dest = this.resolveDownloadPath(rc.downloadPath, res.headers["content-disposition"]);
215
216
  const stream = fs.createWriteStream(dest);
216
217
  res.pipe(stream);
217
- stream.on("finish", () => stream.close());
218
- resolve({ headers: res.headers });
218
+ stream.on("finish", () => { stream.close(); resolve({ headers: res.headers }); });
219
+ stream.on("error", reject);
220
+ return;
219
221
  }
220
222
  catch (e) {
221
223
  if (e instanceof xjs_err_1.XjsErr)
@@ -261,9 +263,7 @@ class HttpResolverContext {
261
263
  if (opPath) {
262
264
  const st = u_file_1.UFile.status(opPath);
263
265
  if (!st || st.isFile()) {
264
- const pathArray = opPath.split("/");
265
- pathArray.pop();
266
- if (!u_file_1.UFile.exists(pathArray))
266
+ if (!u_file_1.UFile.exists(path.dirname(opPath)))
267
267
  throw new xjs_err_1.XjsErr(s_errCode, "Directory of the download file was not found.");
268
268
  return opPath;
269
269
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "9.0.2",
3
+ "version": "9.1.0-alpha.2",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,4 +28,4 @@
28
28
  "ts-node": "^10.9.1",
29
29
  "typescript": "^4.9.5"
30
30
  }
31
- }
31
+ }