@datatruck/cli 0.17.1 → 0.18.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 (104) hide show
  1. package/Action/BackupAction.d.ts +1 -1
  2. package/Action/BackupAction.js +14 -14
  3. package/Action/BackupSessionsAction.d.ts +1 -1
  4. package/Action/CleanCacheAction.d.ts +1 -1
  5. package/Action/CleanCacheAction.js +3 -3
  6. package/Action/ConfigAction.d.ts +1 -1
  7. package/Action/ConfigAction.js +5 -5
  8. package/Action/InitAction.d.ts +1 -1
  9. package/Action/InitAction.js +2 -2
  10. package/Action/PruneAction.d.ts +1 -1
  11. package/Action/PruneAction.js +4 -4
  12. package/Action/RestoreAction.d.ts +1 -1
  13. package/Action/RestoreAction.js +18 -18
  14. package/Action/RestoreSessionsAction.d.ts +1 -1
  15. package/Action/SnapshotsAction.d.ts +1 -1
  16. package/Action/SnapshotsAction.js +4 -4
  17. package/Command/BackupCommand.d.ts +2 -2
  18. package/Command/BackupCommand.js +6 -6
  19. package/Command/BackupSessionsCommand.d.ts +2 -2
  20. package/Command/BackupSessionsCommand.js +9 -9
  21. package/Command/CleanCacheCommand.d.ts +1 -1
  22. package/Command/CleanCacheCommand.js +2 -2
  23. package/Command/CommandAbstract.d.ts +3 -3
  24. package/Command/CommandAbstract.js +2 -2
  25. package/Command/ConfigCommand.d.ts +2 -2
  26. package/Command/ConfigCommand.js +8 -8
  27. package/Command/InitCommand.d.ts +2 -2
  28. package/Command/InitCommand.js +9 -9
  29. package/Command/PruneCommand.d.ts +2 -2
  30. package/Command/PruneCommand.js +10 -10
  31. package/Command/RestoreCommand.d.ts +2 -2
  32. package/Command/RestoreCommand.js +6 -6
  33. package/Command/RestoreSessionsCommand.d.ts +2 -2
  34. package/Command/RestoreSessionsCommand.js +9 -9
  35. package/Command/SnapshotsCommand.d.ts +2 -2
  36. package/Command/SnapshotsCommand.js +9 -9
  37. package/Entity/StateEntityAbstract.d.ts +1 -1
  38. package/Repository/DatatruckRepository.d.ts +1 -2
  39. package/Repository/DatatruckRepository.js +56 -118
  40. package/Repository/GitRepository.js +23 -23
  41. package/Repository/RepositoryAbstract.d.ts +1 -1
  42. package/Repository/RepositoryAbstract.js +2 -2
  43. package/Repository/ResticRepository.d.ts +1 -1
  44. package/Repository/ResticRepository.js +27 -27
  45. package/SessionDriver/ConsoleSessionDriver.d.ts +1 -1
  46. package/SessionDriver/ConsoleSessionDriver.js +10 -10
  47. package/SessionDriver/SqliteSessionDriver.js +12 -12
  48. package/SessionManager/BackupSessionManager.d.ts +2 -2
  49. package/SessionManager/BackupSessionManager.js +1 -1
  50. package/SessionManager/RestoreSessionManager.d.ts +2 -2
  51. package/SessionManager/RestoreSessionManager.js +1 -1
  52. package/Task/GitTask.js +21 -21
  53. package/Task/MariadbTask.d.ts +11 -0
  54. package/Task/MariadbTask.js +240 -58
  55. package/Task/MssqlTask.js +8 -8
  56. package/Task/MysqlDumpTask.d.ts +1 -1
  57. package/Task/MysqlDumpTask.js +13 -13
  58. package/Task/PostgresqlDumpTask.d.ts +1 -1
  59. package/Task/PostgresqlDumpTask.js +4 -4
  60. package/Task/ScriptTask.js +7 -7
  61. package/Task/SqlDumpTaskAbstract.d.ts +1 -1
  62. package/Task/SqlDumpTaskAbstract.js +12 -12
  63. package/Task/TaskAbstract.d.ts +1 -1
  64. package/Task/TaskAbstract.js +2 -2
  65. package/cli.js +10 -10
  66. package/config.schema.json +44 -0
  67. package/package.json +3 -3
  68. package/{util → utils}/DataFormat.d.ts +0 -0
  69. package/{util → utils}/DataFormat.js +0 -0
  70. package/{util/GitUtil.d.ts → utils/Git.d.ts} +8 -8
  71. package/{util/GitUtil.js → utils/Git.js} +8 -8
  72. package/{util → utils}/ObjectVault.d.ts +0 -0
  73. package/{util → utils}/ObjectVault.js +0 -0
  74. package/{util/ResticUtil.d.ts → utils/Restic.d.ts} +3 -3
  75. package/{util/ResticUtil.js → utils/Restic.js} +9 -9
  76. package/{util/cli-util.d.ts → utils/cli.d.ts} +0 -0
  77. package/{util/cli-util.js → utils/cli.js} +0 -0
  78. package/{util/datatruck/config-util.d.ts → utils/datatruck/config.d.ts} +1 -1
  79. package/{util/datatruck/config-util.js → utils/datatruck/config.js} +10 -10
  80. package/{util/datatruck/paths-util.d.ts → utils/datatruck/paths.d.ts} +0 -0
  81. package/{util/datatruck/paths-util.js → utils/datatruck/paths.js} +2 -2
  82. package/{util/datatruck/snapshot-util.d.ts → utils/datatruck/snapshot.d.ts} +1 -1
  83. package/{util/datatruck/snapshot-util.js → utils/datatruck/snapshot.js} +4 -4
  84. package/{util/date-util.d.ts → utils/date.d.ts} +0 -0
  85. package/{util/date-util.js → utils/date.js} +2 -2
  86. package/{util/entity-util.d.ts → utils/entity.d.ts} +0 -0
  87. package/{util/entity-util.js → utils/entity.js} +0 -0
  88. package/{util/fs-util.d.ts → utils/fs.d.ts} +23 -2
  89. package/{util/fs-util.js → utils/fs.js} +83 -24
  90. package/{util/math-util.d.ts → utils/math.d.ts} +0 -0
  91. package/{util/math-util.js → utils/math.js} +0 -0
  92. package/{util/object-util.d.ts → utils/object.d.ts} +0 -0
  93. package/{util/object-util.js → utils/object.js} +0 -0
  94. package/{util/path-util.d.ts → utils/path.d.ts} +0 -0
  95. package/{util/path-util.js → utils/path.js} +0 -0
  96. package/utils/process.d.ts +107 -0
  97. package/{util/process-util.js → utils/process.js} +168 -28
  98. package/{util → utils}/progress.d.ts +0 -0
  99. package/{util → utils}/progress.js +0 -0
  100. package/{util/string-util.d.ts → utils/string.d.ts} +0 -0
  101. package/{util/string-util.js → utils/string.js} +0 -0
  102. package/{util/zip-util.d.ts → utils/zip.d.ts} +0 -0
  103. package/{util/zip-util.js → utils/zip.js} +5 -5
  104. package/util/process-util.d.ts +0 -55
@@ -3,9 +3,10 @@ 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.cpy = exports.isNotFoundError = exports.updateFileStats = exports.copyFileWithStreams = exports.waitForClose = exports.writeGitIgnoreList = exports.fastglobToGitIgnore = exports.forEachFile = exports.readDir = exports.checkDir = exports.checkFile = exports.readPartialFile = exports.mkTmpDir = exports.fastFolderSizeAsync = exports.tmpDir = exports.rmTmpDir = exports.isTmpDir = exports.sessionTmpDir = exports.parentTmpDir = exports.existsFile = exports.findFile = exports.parsePackageFile = exports.parseFile = exports.parseFileExtensions = exports.readdirIfExists = exports.writeJSONFile = exports.existsDir = exports.ensureEmptyDir = exports.mkdirIfNotExists = exports.isDirEmpty = exports.isLocalDir = exports.pathIterator = exports.applyPermissions = exports.isEmptyDir = exports.isWSLSystem = void 0;
6
+ exports.createFileScanner = exports.cpy = exports.isNotFoundError = exports.updateFileStats = exports.copyFileWithStreams = exports.waitForClose = exports.writeGitIgnoreList = exports.fastglobToGitIgnore = exports.forEachFile = exports.readDir = exports.checkDir = exports.checkFile = exports.readPartialFile = exports.mkTmpDir = exports.fastFolderSizeAsync = exports.tmpDir = exports.rmTmpDir = exports.isTmpDir = exports.sessionTmpDir = exports.parentTmpDir = exports.existsFile = exports.findFile = exports.parsePackageFile = exports.parseFile = exports.parseFileExtensions = exports.readdirIfExists = exports.writeJSONFile = exports.existsDir = exports.ensureEmptyDir = exports.mkdirIfNotExists = exports.isDirEmpty = exports.isLocalDir = exports.pathIterator = exports.applyPermissions = exports.isEmptyDir = exports.isWSLSystem = void 0;
7
7
  const globalData_1 = __importDefault(require("../globalData"));
8
- const path_util_1 = require("./path-util");
8
+ const math_1 = require("./math");
9
+ const path_1 = require("./path");
9
10
  const async_1 = require("async");
10
11
  const crypto_1 = require("crypto");
11
12
  const fast_folder_size_1 = __importDefault(require("fast-folder-size"));
@@ -14,8 +15,8 @@ const fs_1 = require("fs");
14
15
  const fs_2 = require("fs");
15
16
  const promises_1 = require("fs/promises");
16
17
  const os_1 = require("os");
17
- const path_1 = require("path");
18
18
  const path_2 = require("path");
19
+ const path_3 = require("path");
19
20
  const readline_1 = require("readline");
20
21
  const util_1 = require("util");
21
22
  exports.isWSLSystem = (0, os_1.release)().includes("microsoft-standard-WSL");
@@ -40,7 +41,7 @@ async function applyPermissions(baseDir, permissionsPath) {
40
41
  });
41
42
  for await (const line of singleReader) {
42
43
  const [rpath, rawUid, rawGui, rawMode] = line.split(":");
43
- const path = (0, path_1.join)(baseDir, rpath);
44
+ const path = (0, path_2.join)(baseDir, rpath);
44
45
  if (!path.startsWith(baseDir)) {
45
46
  throw new Error(`Entry path is out of the base dir: (${path}, ${baseDir})`);
46
47
  }
@@ -66,12 +67,9 @@ async function isDirEmpty(path) {
66
67
  }
67
68
  exports.isDirEmpty = isDirEmpty;
68
69
  async function mkdirIfNotExists(path) {
69
- try {
70
- await (0, promises_1.mkdir)(path, {
71
- recursive: true,
72
- });
73
- }
74
- catch (e) { }
70
+ await (0, promises_1.mkdir)(path, {
71
+ recursive: true,
72
+ });
75
73
  return path;
76
74
  }
77
75
  exports.mkdirIfNotExists = mkdirIfNotExists;
@@ -100,8 +98,8 @@ async function readdirIfExists(path) {
100
98
  exports.readdirIfExists = readdirIfExists;
101
99
  exports.parseFileExtensions = ["json", "js", "ts", "yaml", "yml"];
102
100
  async function parseFile(path, jsKey) {
103
- if (!(0, path_2.isAbsolute)(path))
104
- path = (0, path_1.join)(process.cwd(), path);
101
+ if (!(0, path_3.isAbsolute)(path))
102
+ path = (0, path_2.join)(process.cwd(), path);
105
103
  if (path.endsWith(".ts"))
106
104
  require("ts-node").register();
107
105
  if (path.endsWith(".yaml") || path.endsWith("yml")) {
@@ -119,7 +117,7 @@ async function parseFile(path, jsKey) {
119
117
  }
120
118
  exports.parseFile = parseFile;
121
119
  function parsePackageFile() {
122
- return require(`${path_util_1.rootPath}/package.json`);
120
+ return require(`${path_1.rootPath}/package.json`);
123
121
  }
124
122
  exports.parsePackageFile = parsePackageFile;
125
123
  async function findFile(sourcePath, baseName, extensions, errorMessage = "Path not found") {
@@ -127,7 +125,7 @@ async function findFile(sourcePath, baseName, extensions, errorMessage = "Path n
127
125
  let path;
128
126
  if (info.isDirectory()) {
129
127
  for (const ext of extensions) {
130
- const extPath = (0, path_1.join)(sourcePath, baseName) + "." + ext;
128
+ const extPath = (0, path_2.join)(sourcePath, baseName) + "." + ext;
131
129
  if (await existsFile(extPath)) {
132
130
  path = extPath;
133
131
  break;
@@ -153,11 +151,11 @@ async function existsFile(path) {
153
151
  }
154
152
  exports.existsFile = existsFile;
155
153
  function parentTmpDir() {
156
- return (0, path_1.join)(globalData_1.default.tempDir, "datatruck-temp");
154
+ return (0, path_2.join)(globalData_1.default.tempDir, "datatruck-temp");
157
155
  }
158
156
  exports.parentTmpDir = parentTmpDir;
159
157
  function sessionTmpDir() {
160
- return (0, path_1.join)(parentTmpDir(), process.pid.toString());
158
+ return (0, path_2.join)(parentTmpDir(), process.pid.toString());
161
159
  }
162
160
  exports.sessionTmpDir = sessionTmpDir;
163
161
  function isTmpDir(path) {
@@ -181,7 +179,7 @@ exports.rmTmpDir = rmTmpDir;
181
179
  function tmpDir(prefix, id) {
182
180
  if (!id)
183
181
  id = (0, crypto_1.randomUUID)().slice(0, 8);
184
- return (0, path_1.join)(sessionTmpDir(), `${prefix}-${id}`);
182
+ return (0, path_2.join)(sessionTmpDir(), `${prefix}-${id}`);
185
183
  }
186
184
  exports.tmpDir = tmpDir;
187
185
  async function fastFolderSizeAsync(path) {
@@ -261,7 +259,7 @@ exports.readDir = readDir;
261
259
  async function forEachFile(dirPath, cb, includeDir) {
262
260
  const files = await readDir(dirPath);
263
261
  for (const file of files) {
264
- const filePath = (0, path_1.join)(dirPath, file);
262
+ const filePath = (0, path_2.join)(dirPath, file);
265
263
  if ((await (0, promises_1.stat)(filePath)).isDirectory()) {
266
264
  if (includeDir)
267
265
  cb(filePath, true);
@@ -284,12 +282,12 @@ function fastglobToGitIgnore(patterns, baseDir) {
284
282
  exports.fastglobToGitIgnore = fastglobToGitIgnore;
285
283
  async function writeGitIgnoreList(options) {
286
284
  const { outDir } = options;
287
- const path = (0, path_1.join)(outDir, `.gitignore`);
285
+ const path = (0, path_2.join)(outDir, `.gitignore`);
288
286
  const stream = (0, fs_2.createWriteStream)(path);
289
287
  const dirs = new Set();
290
288
  stream.write("*\n");
291
289
  for await (const value of options.paths) {
292
- const dir = (0, path_1.dirname)(value.toString());
290
+ const dir = (0, path_2.dirname)(value.toString());
293
291
  if (dir !== ".") {
294
292
  let parentPath;
295
293
  for (const value of dir.split("/")) {
@@ -368,9 +366,9 @@ async function cpy(options) {
368
366
  const task = async (rawEntryPath, basePath) => {
369
367
  [rawEntryPath] = rawEntryPath.split(":");
370
368
  const isDir = rawEntryPath.endsWith("/");
371
- const entryPath = (0, path_1.normalize)(rawEntryPath);
372
- const entrySourcePath = (0, path_1.resolve)((0, path_1.join)(basePath, rawEntryPath));
373
- const entryTargetPath = (0, path_1.resolve)((0, path_1.join)(options.targetPath, rawEntryPath));
369
+ const entryPath = (0, path_2.normalize)(rawEntryPath);
370
+ const entrySourcePath = (0, path_2.resolve)((0, path_2.join)(basePath, rawEntryPath));
371
+ const entryTargetPath = (0, path_2.resolve)((0, path_2.join)(options.targetPath, rawEntryPath));
374
372
  const onPathResult = await options?.onPath?.({
375
373
  isDir,
376
374
  entryPath,
@@ -385,7 +383,7 @@ async function cpy(options) {
385
383
  await makeRecursiveDir(entryTargetPath);
386
384
  }
387
385
  else {
388
- const dir = (0, path_1.dirname)(entryTargetPath);
386
+ const dir = (0, path_2.dirname)(entryTargetPath);
389
387
  await makeRecursiveDir(dir);
390
388
  await options.onProgress?.({
391
389
  current: stats.files,
@@ -458,3 +456,64 @@ async function cpy(options) {
458
456
  return stats;
459
457
  }
460
458
  exports.cpy = cpy;
459
+ async function createFileScanner(options) {
460
+ const object = {
461
+ total: 0,
462
+ current: 0,
463
+ progress: async (description, data) => {
464
+ await options.onProgress({
465
+ relative: {
466
+ description,
467
+ payload: data.path,
468
+ percent: data.percent,
469
+ },
470
+ absolute: {
471
+ total: object.total,
472
+ current: object.current + data.current,
473
+ percent: (0, math_1.progressPercent)(object.total, object.current + data.current),
474
+ },
475
+ });
476
+ if (data.type === "end") {
477
+ object.current += data.current;
478
+ }
479
+ },
480
+ updateProgress: async (end) => {
481
+ const currentTime = performance.now();
482
+ const diff = currentTime - lastTime;
483
+ if (end || diff > 1000) {
484
+ await options.onProgress({
485
+ relative: {
486
+ description: end ? "Scanned files" : "Scanning files",
487
+ payload: object.total.toString(),
488
+ },
489
+ });
490
+ lastTime = currentTime;
491
+ }
492
+ },
493
+ start: async (cb) => {
494
+ for await (const entry of pathIterator(stream)) {
495
+ if (!options.disableCounting)
496
+ object.total++;
497
+ await object.updateProgress();
498
+ if (cb)
499
+ await cb(entry);
500
+ }
501
+ if (!options.disableEndProgress)
502
+ await object.updateProgress(true);
503
+ },
504
+ };
505
+ await options.onProgress({
506
+ relative: {
507
+ description: "Scanning files",
508
+ },
509
+ });
510
+ const stream = fast_glob_1.default.stream(options.glob.include, {
511
+ dot: true,
512
+ markDirectories: true,
513
+ stats: true,
514
+ ...options.glob,
515
+ });
516
+ let lastTime = performance.now();
517
+ return object;
518
+ }
519
+ exports.createFileScanner = createFileScanner;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,107 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import { SpawnOptions, ChildProcess, ChildProcessByStdio } from "child_process";
5
+ import { ReadStream, WriteStream } from "fs";
6
+ import { Readable, Writable } from "stream";
7
+ export type ExecLogSettingsType = {
8
+ colorize?: boolean;
9
+ exec?: boolean;
10
+ stdout?: boolean;
11
+ stderr?: boolean;
12
+ allToStderr?: boolean;
13
+ envNames?: string[];
14
+ };
15
+ export interface ExecSettingsInterface {
16
+ exec?: boolean;
17
+ pipe?: {
18
+ stream: WriteStream;
19
+ onWriteProgress?: (data: {
20
+ totalBytes: number;
21
+ }) => void;
22
+ } | {
23
+ stream: ReadStream;
24
+ onReadProgress?: (data: {
25
+ totalBytes: number;
26
+ currentBytes: number;
27
+ progress: number;
28
+ }) => void;
29
+ } | {
30
+ stream: Readable;
31
+ };
32
+ log?: ExecLogSettingsType | boolean;
33
+ onSpawn?: (p: ChildProcess) => any;
34
+ stdout?: {
35
+ save?: boolean;
36
+ parseLines?: boolean;
37
+ onData?: (data: string) => void;
38
+ };
39
+ stderr?: {
40
+ save?: boolean;
41
+ onData?: (data: string) => void;
42
+ toExitCode?: boolean;
43
+ };
44
+ onExitCodeError?: (data: ExecResultType, error: Error) => Error | false;
45
+ }
46
+ export declare function logExecStdout(input: {
47
+ data: string;
48
+ colorize?: boolean;
49
+ stderr?: boolean;
50
+ lineSalt?: boolean;
51
+ }): void;
52
+ export declare function logExecStderr(data: string, colorize?: boolean): void;
53
+ export type ExecResultType = {
54
+ stdout: string;
55
+ stderr: string;
56
+ exitCode: number;
57
+ };
58
+ export type ParseStreamDataOptions<S extends boolean = boolean> = {
59
+ save?: S;
60
+ parseLines?: boolean;
61
+ log?: LogProcessOptions | boolean;
62
+ onData?: (data: string) => void;
63
+ };
64
+ export declare function parseStreamData<S extends boolean>(stream: Readable, options?: ParseStreamDataOptions<S>): Promise<S extends true ? string : undefined>;
65
+ type OnExitCode = OnExitCodeValue | ((code: number) => OnExitCodeValue | void | undefined);
66
+ type OnExitCodeValue = Error | string | number | boolean;
67
+ export declare function waitForClose<O extends boolean, E extends boolean>(p: ChildProcess, options?: {
68
+ strict?: boolean;
69
+ stdout?: O;
70
+ stderr?: E;
71
+ onExitCode?: OnExitCode;
72
+ }): Promise<{
73
+ exitCode: number;
74
+ } & (O extends true ? {
75
+ stdout: string;
76
+ } : {}) & (E extends true ? {
77
+ stderr: string;
78
+ } : {})>;
79
+ export type LogProcessOptions = {
80
+ envNames?: string[];
81
+ env?: Record<string, any>;
82
+ pipe?: Readable | Writable;
83
+ toStderr?: boolean;
84
+ colorize?: boolean;
85
+ };
86
+ export declare function logProcessExec(command: string, argv: any[], options: LogProcessOptions): Promise<void>;
87
+ export type ProcessOptions<O1 extends boolean, O2 extends boolean> = {
88
+ $stdout?: Omit<ParseStreamDataOptions<O1>, "log">;
89
+ $stderr?: Omit<ParseStreamDataOptions<O2>, "log">;
90
+ $onExitCode?: OnExitCode;
91
+ $log?: boolean | {
92
+ exec?: boolean | LogProcessOptions;
93
+ stdout?: boolean | LogProcessOptions;
94
+ stderr?: boolean | LogProcessOptions;
95
+ };
96
+ };
97
+ export declare function createProcess<O1 extends boolean, O2 extends boolean>(command: string, argv?: (string | number)[], options?: SpawnOptions & ProcessOptions<O1, O2>): ChildProcessByStdio<Writable, Readable, Readable> & PromiseLike<{
98
+ exitCode: number;
99
+ } & (O1 extends true ? {
100
+ stdout: string;
101
+ } : {}) & (O2 extends true ? {
102
+ stderr: string;
103
+ } : {})>;
104
+ export declare function exec(command: string, argv?: string[], options?: SpawnOptions | null, settings?: ExecSettingsInterface): Promise<ExecResultType>;
105
+ type EventNameType = "exit" | "SIGINT" | "SIGUSR1" | "SIGUSR2" | "SIGTERM" | "uncaughtException";
106
+ export declare function onExit(cb: (eventName: EventNameType, ...args: any[]) => void): void;
107
+ export {};
@@ -3,15 +3,16 @@ 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.onExit = exports.exec = exports.logExecStderr = exports.logExecStdout = void 0;
7
- const cli_util_1 = require("./cli-util");
8
- const fs_util_1 = require("./fs-util");
9
- const math_util_1 = require("./math-util");
6
+ exports.onExit = exports.exec = exports.createProcess = exports.logProcessExec = exports.waitForClose = exports.parseStreamData = exports.logExecStderr = exports.logExecStdout = void 0;
7
+ const cli_1 = require("./cli");
8
+ const fs_1 = require("./fs");
9
+ const math_1 = require("./math");
10
10
  const chalk_1 = __importDefault(require("chalk"));
11
11
  const child_process_1 = require("child_process");
12
- const fs_1 = require("fs");
12
+ const fs_2 = require("fs");
13
13
  const promises_1 = require("fs/promises");
14
14
  const readline_1 = require("readline");
15
+ const stream_1 = require("stream");
15
16
  function logExecStdout(input) {
16
17
  let text = input.colorize ? chalk_1.default.grey(input.data) : input.data;
17
18
  if (input.lineSalt)
@@ -23,6 +24,152 @@ function logExecStderr(data, colorize) {
23
24
  process.stdout.write(colorize ? chalk_1.default.red(data) : data);
24
25
  }
25
26
  exports.logExecStderr = logExecStderr;
27
+ function parseStreamData(stream, options = {}) {
28
+ const log = options.log === true ? {} : options.log;
29
+ let result;
30
+ if (options.save)
31
+ result = "";
32
+ return new Promise((resolve, reject) => {
33
+ const lines = options.parseLines;
34
+ const onData = (data) => {
35
+ if (options.onData)
36
+ options.onData(data.toString());
37
+ if (log)
38
+ logExecStdout({
39
+ data: lines ? `${data}\n` : data.toString(),
40
+ stderr: log.toStderr,
41
+ colorize: log.colorize,
42
+ });
43
+ if (options?.save)
44
+ result += data.toString();
45
+ };
46
+ if (lines) {
47
+ const rl = (0, readline_1.createInterface)({
48
+ input: stream,
49
+ });
50
+ rl.on("line", onData).on("close", () => resolve(result));
51
+ }
52
+ else {
53
+ stream
54
+ .on("data", onData)
55
+ .on("error", reject)
56
+ .once("close", () => resolve(result));
57
+ }
58
+ });
59
+ }
60
+ exports.parseStreamData = parseStreamData;
61
+ function waitForClose(p, options = {}) {
62
+ return new Promise((resolve, reject) => {
63
+ let result = {
64
+ exitCode: 1,
65
+ };
66
+ if (options.stdout) {
67
+ result.stdout = "";
68
+ p.stdout.on("data", (data) => (result.stdout += data.toString()));
69
+ }
70
+ p.once("error", reject).once("close", (exitCode) => {
71
+ if (exitCode) {
72
+ let onExitCode = options.onExitCode ?? true;
73
+ if (typeof onExitCode === "function") {
74
+ onExitCode = onExitCode(exitCode);
75
+ }
76
+ if (typeof onExitCode === "string") {
77
+ reject(new Error(onExitCode));
78
+ }
79
+ else if (typeof onExitCode === "number") {
80
+ reject(new Error(`Exit code: ${onExitCode}`));
81
+ }
82
+ else if (onExitCode instanceof Error) {
83
+ reject(onExitCode);
84
+ }
85
+ else if (onExitCode === false) {
86
+ resolve({ ...result, exitCode: exitCode });
87
+ }
88
+ else {
89
+ reject(new Error(`Exit code: ${exitCode}`));
90
+ }
91
+ }
92
+ else {
93
+ resolve({ ...result, exitCode: exitCode });
94
+ }
95
+ });
96
+ });
97
+ }
98
+ exports.waitForClose = waitForClose;
99
+ async function logProcessExec(command, argv, options) {
100
+ const logEnv = options.envNames?.reduce((env, key) => {
101
+ const value = options?.env?.[key];
102
+ if (typeof value !== "undefined")
103
+ env[key] = value;
104
+ return env;
105
+ }, {});
106
+ (0, cli_1.logExec)(command, options.pipe
107
+ ? [
108
+ ...argv,
109
+ options.pipe instanceof stream_1.Readable ? "<" : ">",
110
+ "path" in options.pipe ? String(options.pipe.path) : "[stream]",
111
+ ]
112
+ : argv, logEnv, options.toStderr);
113
+ }
114
+ exports.logProcessExec = logProcessExec;
115
+ function createProcess(command, argv = [], options = {}) {
116
+ const $log = options.$log === true
117
+ ? { exec: {}, stdout: {}, stderr: {} }
118
+ : options.$log || {};
119
+ if ($log.exec)
120
+ logProcessExec(command, argv, $log.exec === true ? {} : $log.exec);
121
+ if (typeof options.cwd === "string") {
122
+ let isDir = false;
123
+ try {
124
+ isDir = (0, fs_2.statSync)(options.cwd).isDirectory();
125
+ }
126
+ catch (error) { }
127
+ if (!isDir)
128
+ throw new Error(`Current working directory does not exist: ${options.cwd}`);
129
+ }
130
+ const handler = (0, child_process_1.spawn)(command, argv.map((v) => (typeof v === "number" ? v.toString() : v)), options ?? {});
131
+ const { $stdout, $stderr, $onExitCode } = options;
132
+ async function exec() {
133
+ const [stdout, stderr, result] = await Promise.all([
134
+ (!!$log.stdout || !!$stdout) &&
135
+ parseStreamData(handler.stdout, {
136
+ log: $log.stdout,
137
+ ...$stdout,
138
+ }),
139
+ (!!$log.stderr || !!$stderr) &&
140
+ parseStreamData(handler.stderr, {
141
+ log: $log.stderr,
142
+ ...$stderr,
143
+ }),
144
+ waitForClose(handler, {
145
+ onExitCode: $onExitCode,
146
+ }),
147
+ ]);
148
+ const endResult = {
149
+ exitCode: result.exitCode,
150
+ };
151
+ if (typeof stdout === "string")
152
+ endResult.stdout = stdout;
153
+ if (typeof stderr === "string")
154
+ endResult.stderr = stderr;
155
+ return endResult;
156
+ }
157
+ const promise = {
158
+ [Symbol.toStringTag]: "process",
159
+ then: function (onfulfilled, onrejected) {
160
+ return exec().then(onfulfilled, onrejected);
161
+ },
162
+ catch: function (onrejected) {
163
+ return exec().catch(onrejected);
164
+ },
165
+ finally: function (onfinally) {
166
+ return exec().finally(onfinally);
167
+ },
168
+ };
169
+ Object.assign(handler, promise);
170
+ return handler;
171
+ }
172
+ exports.createProcess = createProcess;
26
173
  async function exec(command, argv = [], options = null, settings = {}) {
27
174
  const pipe = settings.pipe;
28
175
  let log = {};
@@ -34,23 +181,16 @@ async function exec(command, argv = [], options = null, settings = {}) {
34
181
  }
35
182
  return new Promise(async (resolve, reject) => {
36
183
  if (log.exec) {
37
- const logEnv = log.envNames?.reduce((env, key) => {
38
- const value = options?.env?.[key];
39
- if (typeof value !== "undefined")
40
- env[key] = value;
41
- return env;
42
- }, {});
43
- (0, cli_util_1.logExec)(command, pipe
44
- ? [
45
- ...argv,
46
- pipe.stream instanceof fs_1.ReadStream ? "<" : ">",
47
- String(pipe.stream.path),
48
- ]
49
- : argv, logEnv, log.allToStderr);
184
+ logProcessExec(command, argv, {
185
+ env: options?.env,
186
+ envNames: log.envNames,
187
+ pipe: pipe?.stream,
188
+ toStderr: log.allToStderr,
189
+ });
50
190
  }
51
- if (typeof options?.cwd === "string" && !(await (0, fs_util_1.checkDir)(options.cwd)))
52
- return reject(new Error(`Current working directory does not exist: ${options.cwd}`));
53
- if (pipe?.onReadProgress && pipe.stream instanceof fs_1.ReadStream) {
191
+ if (typeof options?.cwd === "string" && !(await (0, fs_1.checkDir)(options.cwd)))
192
+ throw new Error(`Current working directory does not exist: ${options.cwd}`);
193
+ if (pipe?.stream instanceof fs_2.ReadStream && "onReadProgress" in pipe) {
54
194
  const fileInfo = await (0, promises_1.stat)(pipe.stream.path);
55
195
  const totalBytes = fileInfo.size;
56
196
  let currentBytes = 0;
@@ -59,12 +199,12 @@ async function exec(command, argv = [], options = null, settings = {}) {
59
199
  pipe.onReadProgress?.({
60
200
  totalBytes: totalBytes,
61
201
  currentBytes: currentBytes,
62
- progress: (0, math_util_1.progressPercent)(totalBytes, currentBytes),
202
+ progress: (0, math_1.progressPercent)(totalBytes, currentBytes),
63
203
  });
64
204
  });
65
205
  }
66
206
  const p = (0, child_process_1.spawn)(command, argv, options ?? {});
67
- settings.onSpawn?.(p);
207
+ await settings.onSpawn?.(p);
68
208
  let spawnError;
69
209
  const spawnData = {
70
210
  stdout: "",
@@ -72,7 +212,7 @@ async function exec(command, argv = [], options = null, settings = {}) {
72
212
  exitCode: 0,
73
213
  };
74
214
  let finishListeners = 1;
75
- if (pipe?.stream instanceof fs_1.WriteStream)
215
+ if (pipe?.stream instanceof fs_2.WriteStream)
76
216
  finishListeners++;
77
217
  if (settings.stdout?.parseLines)
78
218
  finishListeners++;
@@ -116,12 +256,12 @@ async function exec(command, argv = [], options = null, settings = {}) {
116
256
  streamError = error;
117
257
  tryFinish();
118
258
  });
119
- if (pipe.stream instanceof fs_1.WriteStream) {
259
+ if (pipe.stream instanceof fs_2.WriteStream) {
120
260
  if (!p.stdout)
121
261
  throw new Error(`stdout is not defined`);
122
262
  if (!p.stderr)
123
263
  throw new Error(`stderr is not defined`);
124
- if (pipe.onWriteProgress) {
264
+ if ("onWriteProgress" in pipe && pipe.onWriteProgress) {
125
265
  let totalBytes = 0;
126
266
  p.stdout.on("data", (chunk) => {
127
267
  totalBytes += chunk.length;
@@ -136,7 +276,7 @@ async function exec(command, argv = [], options = null, settings = {}) {
136
276
  p.stderr.pipe(pipe.stream, { end: false });
137
277
  p.on("close", tryFinish);
138
278
  }
139
- else if (pipe.stream instanceof fs_1.ReadStream) {
279
+ else if (pipe.stream instanceof stream_1.Readable) {
140
280
  if (!p.stdin)
141
281
  throw new Error(`stdin is not defined`);
142
282
  pipe.stream.pipe(p.stdin);
@@ -187,7 +327,7 @@ async function exec(command, argv = [], options = null, settings = {}) {
187
327
  }
188
328
  p.on("error", (error) => (spawnError = error)).on("close", (exitCode) => {
189
329
  spawnData.exitCode = exitCode ?? 0;
190
- if (pipe?.stream instanceof fs_1.WriteStream)
330
+ if (pipe?.stream instanceof fs_2.WriteStream)
191
331
  pipe.stream.end();
192
332
  tryFinish();
193
333
  });
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.unzip = exports.zip = exports.listZip = exports.checkSSEOption = exports.buildArguments = void 0;
4
- const process_util_1 = require("./process-util");
4
+ const process_1 = require("./process");
5
5
  const path_1 = require("path");
6
6
  function buildArguments(filters) {
7
7
  const args = [];
@@ -32,7 +32,7 @@ function buildArguments(filters) {
32
32
  exports.buildArguments = buildArguments;
33
33
  let checkSSEOptionResult;
34
34
  async function checkSSEOption(command = "7z") {
35
- const result = await (0, process_util_1.exec)(command, [], {}, {
35
+ const result = await (0, process_1.exec)(command, [], {}, {
36
36
  stdout: {
37
37
  save: true,
38
38
  },
@@ -68,7 +68,7 @@ function parseListZipLine(line, buffer) {
68
68
  }
69
69
  async function listZip(data) {
70
70
  const buffer = {};
71
- await (0, process_util_1.exec)(data.command ?? "7z", ["l", data.path, "-slt"], {}, {
71
+ await (0, process_1.exec)(data.command ?? "7z", ["l", data.path, "-slt"], {}, {
72
72
  log: {
73
73
  exec: data.verbose ?? false,
74
74
  stderr: data.verbose ?? false,
@@ -124,7 +124,7 @@ async function zip(data) {
124
124
  total: 0,
125
125
  type: "start",
126
126
  });
127
- await (0, process_util_1.exec)(data.command ?? "7z", [
127
+ await (0, process_1.exec)(data.command ?? "7z", [
128
128
  "a",
129
129
  // https://sourceforge.net/p/sevenzip/bugs/2099/,
130
130
  // https://github.com/mcmilk/7-Zip/commit/87ba6f01ba3c5b2ce3186bddfe3d7d880639193c#diff-779d6b1bfa6196b288478f78ca96c4d4c6d7ac6cf8be15a28a20dabc9137ca36L515
@@ -192,7 +192,7 @@ async function unzip(data) {
192
192
  percent: 0,
193
193
  type: "start",
194
194
  });
195
- const result = await (0, process_util_1.exec)(data.command ?? "7z", [
195
+ const result = await (0, process_1.exec)(data.command ?? "7z", [
196
196
  "x",
197
197
  "-bsp1",
198
198
  (0, path_1.normalize)(data.input),