@datatruck/cli 0.0.1

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 (154) hide show
  1. package/lib/Action/BackupAction.d.ts +32 -0
  2. package/lib/Action/BackupAction.js +201 -0
  3. package/lib/Action/BackupSessionsAction.d.ts +13 -0
  4. package/lib/Action/BackupSessionsAction.js +16 -0
  5. package/lib/Action/CleanCacheAction.d.ts +9 -0
  6. package/lib/Action/CleanCacheAction.js +18 -0
  7. package/lib/Action/ConfigAction.d.ts +14 -0
  8. package/lib/Action/ConfigAction.js +64 -0
  9. package/lib/Action/InitAction.d.ts +18 -0
  10. package/lib/Action/InitAction.js +39 -0
  11. package/lib/Action/PruneAction.d.ts +37 -0
  12. package/lib/Action/PruneAction.js +80 -0
  13. package/lib/Action/RestoreAction.d.ts +36 -0
  14. package/lib/Action/RestoreAction.js +246 -0
  15. package/lib/Action/RestoreSessionsAction.d.ts +13 -0
  16. package/lib/Action/RestoreSessionsAction.js +16 -0
  17. package/lib/Action/SnapshotsAction.d.ts +30 -0
  18. package/lib/Action/SnapshotsAction.js +35 -0
  19. package/lib/Command/BackupCommand.d.ts +15 -0
  20. package/lib/Command/BackupCommand.js +83 -0
  21. package/lib/Command/BackupSessionsCommand.d.ts +12 -0
  22. package/lib/Command/BackupSessionsCommand.js +88 -0
  23. package/lib/Command/CleanCacheCommand.d.ts +6 -0
  24. package/lib/Command/CleanCacheCommand.js +20 -0
  25. package/lib/Command/CommandAbstract.d.ts +19 -0
  26. package/lib/Command/CommandAbstract.js +14 -0
  27. package/lib/Command/ConfigCommand.d.ts +17 -0
  28. package/lib/Command/ConfigCommand.js +61 -0
  29. package/lib/Command/InitCommand.d.ts +13 -0
  30. package/lib/Command/InitCommand.js +67 -0
  31. package/lib/Command/PruneCommand.d.ts +27 -0
  32. package/lib/Command/PruneCommand.js +160 -0
  33. package/lib/Command/RestoreCommand.d.ts +14 -0
  34. package/lib/Command/RestoreCommand.js +71 -0
  35. package/lib/Command/RestoreSessionsCommand.d.ts +12 -0
  36. package/lib/Command/RestoreSessionsCommand.js +87 -0
  37. package/lib/Command/SnapshotsCommand.d.ts +25 -0
  38. package/lib/Command/SnapshotsCommand.js +128 -0
  39. package/lib/Config/Config.d.ts +8 -0
  40. package/lib/Config/Config.js +19 -0
  41. package/lib/Config/PackageConfig.d.ts +27 -0
  42. package/lib/Config/PackageConfig.js +69 -0
  43. package/lib/Config/PackageRepositoryConfig.d.ts +17 -0
  44. package/lib/Config/PackageRepositoryConfig.js +37 -0
  45. package/lib/Config/PrunePolicyConfig.d.ts +4 -0
  46. package/lib/Config/PrunePolicyConfig.js +28 -0
  47. package/lib/Config/RepositoryConfig.d.ts +18 -0
  48. package/lib/Config/RepositoryConfig.js +37 -0
  49. package/lib/Config/TaskConfig.d.ts +23 -0
  50. package/lib/Config/TaskConfig.js +39 -0
  51. package/lib/Decorator/EntityDecorator.d.ts +11 -0
  52. package/lib/Decorator/EntityDecorator.js +17 -0
  53. package/lib/Entity/BackupSessionEntity.d.ts +6 -0
  54. package/lib/Entity/BackupSessionEntity.js +22 -0
  55. package/lib/Entity/BackupSessionRepositoryEntity.d.ts +6 -0
  56. package/lib/Entity/BackupSessionRepositoryEntity.js +22 -0
  57. package/lib/Entity/BackupSessionTaskEntity.d.ts +5 -0
  58. package/lib/Entity/BackupSessionTaskEntity.js +22 -0
  59. package/lib/Entity/CrudEntityAbstract.d.ts +5 -0
  60. package/lib/Entity/CrudEntityAbstract.js +6 -0
  61. package/lib/Entity/RestoreSessionEntity.d.ts +5 -0
  62. package/lib/Entity/RestoreSessionEntity.js +22 -0
  63. package/lib/Entity/RestoreSessionRepositoryEntity.d.ts +6 -0
  64. package/lib/Entity/RestoreSessionRepositoryEntity.js +22 -0
  65. package/lib/Entity/RestoreSessionTaskEntity.d.ts +5 -0
  66. package/lib/Entity/RestoreSessionTaskEntity.js +22 -0
  67. package/lib/Entity/StateEntityAbstract.d.ts +12 -0
  68. package/lib/Entity/StateEntityAbstract.js +7 -0
  69. package/lib/Error/AppError.d.ts +2 -0
  70. package/lib/Error/AppError.js +6 -0
  71. package/lib/Factory/CommandFactory.d.ts +42 -0
  72. package/lib/Factory/CommandFactory.js +79 -0
  73. package/lib/Factory/EntityFactory.d.ts +6 -0
  74. package/lib/Factory/EntityFactory.js +40 -0
  75. package/lib/Factory/RepositoryFactory.d.ts +3 -0
  76. package/lib/Factory/RepositoryFactory.js +23 -0
  77. package/lib/Factory/TaskFactory.d.ts +3 -0
  78. package/lib/Factory/TaskFactory.js +30 -0
  79. package/lib/JsonSchema/DefinitionEnum.d.ts +25 -0
  80. package/lib/JsonSchema/DefinitionEnum.js +32 -0
  81. package/lib/JsonSchema/JsonSchema.d.ts +4 -0
  82. package/lib/JsonSchema/JsonSchema.js +50 -0
  83. package/lib/Repository/GitRepository.d.ts +29 -0
  84. package/lib/Repository/GitRepository.js +237 -0
  85. package/lib/Repository/LocalRepository.d.ts +51 -0
  86. package/lib/Repository/LocalRepository.js +358 -0
  87. package/lib/Repository/RepositoryAbstract.d.ts +77 -0
  88. package/lib/Repository/RepositoryAbstract.js +19 -0
  89. package/lib/Repository/ResticRepository.d.ts +36 -0
  90. package/lib/Repository/ResticRepository.js +230 -0
  91. package/lib/SessionDriver/ConsoleSessionDriver.d.ts +37 -0
  92. package/lib/SessionDriver/ConsoleSessionDriver.js +178 -0
  93. package/lib/SessionDriver/SessionDriverAbstract.d.ts +78 -0
  94. package/lib/SessionDriver/SessionDriverAbstract.js +27 -0
  95. package/lib/SessionDriver/SqliteSessionDriver.d.ts +20 -0
  96. package/lib/SessionDriver/SqliteSessionDriver.js +169 -0
  97. package/lib/SessionManager/BackupSessionManager.d.ts +44 -0
  98. package/lib/SessionManager/BackupSessionManager.js +206 -0
  99. package/lib/SessionManager/RestoreSessionManager.d.ts +44 -0
  100. package/lib/SessionManager/RestoreSessionManager.js +206 -0
  101. package/lib/Task/GitTask.d.ts +35 -0
  102. package/lib/Task/GitTask.js +248 -0
  103. package/lib/Task/MariadbTask.d.ts +25 -0
  104. package/lib/Task/MariadbTask.js +139 -0
  105. package/lib/Task/MssqlTask.d.ts +22 -0
  106. package/lib/Task/MssqlTask.js +109 -0
  107. package/lib/Task/MysqlDumpTask.d.ts +14 -0
  108. package/lib/Task/MysqlDumpTask.js +129 -0
  109. package/lib/Task/PostgresqlDumpTask.d.ts +14 -0
  110. package/lib/Task/PostgresqlDumpTask.js +101 -0
  111. package/lib/Task/SqlDumpTaskAbstract.d.ts +36 -0
  112. package/lib/Task/SqlDumpTaskAbstract.js +146 -0
  113. package/lib/Task/TaskAbstract.d.ts +37 -0
  114. package/lib/Task/TaskAbstract.js +17 -0
  115. package/lib/bin.d.ts +2 -0
  116. package/lib/bin.js +5 -0
  117. package/lib/cli.d.ts +4 -0
  118. package/lib/cli.js +110 -0
  119. package/lib/index.d.ts +0 -0
  120. package/lib/index.js +1 -0
  121. package/lib/util/DataFormat.d.ts +24 -0
  122. package/lib/util/DataFormat.js +50 -0
  123. package/lib/util/GitUtil.d.ts +38 -0
  124. package/lib/util/GitUtil.js +105 -0
  125. package/lib/util/ObjectVault.d.ts +13 -0
  126. package/lib/util/ObjectVault.js +31 -0
  127. package/lib/util/ResticUtil.d.ts +92 -0
  128. package/lib/util/ResticUtil.js +144 -0
  129. package/lib/util/cli-util.d.ts +27 -0
  130. package/lib/util/cli-util.js +118 -0
  131. package/lib/util/datatruck/config-util.d.ts +55 -0
  132. package/lib/util/datatruck/config-util.js +93 -0
  133. package/lib/util/datatruck/paths-util.d.ts +5 -0
  134. package/lib/util/datatruck/paths-util.js +22 -0
  135. package/lib/util/datatruck/snapshot-util.d.ts +4 -0
  136. package/lib/util/datatruck/snapshot-util.js +31 -0
  137. package/lib/util/date-util.d.ts +12 -0
  138. package/lib/util/date-util.js +74 -0
  139. package/lib/util/entity-util.d.ts +4 -0
  140. package/lib/util/entity-util.js +10 -0
  141. package/lib/util/fs-util.d.ts +43 -0
  142. package/lib/util/fs-util.js +278 -0
  143. package/lib/util/math-util.d.ts +1 -0
  144. package/lib/util/math-util.js +7 -0
  145. package/lib/util/object-util.d.ts +7 -0
  146. package/lib/util/object-util.js +58 -0
  147. package/lib/util/process-util.d.ts +50 -0
  148. package/lib/util/process-util.js +181 -0
  149. package/lib/util/string-util.d.ts +17 -0
  150. package/lib/util/string-util.js +77 -0
  151. package/lib/util/zip-util.d.ts +52 -0
  152. package/lib/util/zip-util.js +135 -0
  153. package/migrations/001-initial.sql +122 -0
  154. package/package.json +62 -0
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.params = exports.dbNameParams = exports.pkgRestorePathParams = exports.pkgPathParams = exports.resolvePackages = exports.resolvePackage = exports.resolveDatabaseName = exports.resolvePackagePath = exports.filterPackages = exports.findRepositoryOrFail = void 0;
4
+ const AppError_1 = require("../../Error/AppError");
5
+ const fs_util_1 = require("../fs-util");
6
+ const string_util_1 = require("../string-util");
7
+ const micromatch_1 = require("micromatch");
8
+ function findRepositoryOrFail(config, repositoryName) {
9
+ const repo = config.repositories.find((v) => v.name === repositoryName);
10
+ if (!repo)
11
+ throw new AppError_1.AppError(`Repository '${repositoryName}' not found`);
12
+ return repo;
13
+ }
14
+ exports.findRepositoryOrFail = findRepositoryOrFail;
15
+ function filterPackages(config, options) {
16
+ const packagePatterns = (0, string_util_1.makePathPatterns)(options.packageNames);
17
+ return config.packages
18
+ .map((pkg) => {
19
+ pkg = Object.assign({}, pkg);
20
+ pkg.repositoryNames = (pkg.repositoryNames ?? []).filter((name) => {
21
+ const repo = findRepositoryOrFail(config, name);
22
+ return ((!options.repositoryNames ||
23
+ options.repositoryNames.includes(name)) &&
24
+ (!options.repositoryTypes ||
25
+ options.repositoryTypes.includes(repo.type)));
26
+ });
27
+ return pkg;
28
+ })
29
+ .filter((pkg) => {
30
+ return ((typeof pkg.enabled !== "boolean" || pkg.enabled) &&
31
+ !!pkg.repositoryNames?.length &&
32
+ (!packagePatterns || (0, micromatch_1.isMatch)(pkg.name, packagePatterns)));
33
+ });
34
+ }
35
+ exports.filterPackages = filterPackages;
36
+ function resolvePackagePath(value, params) {
37
+ return (0, string_util_1.render)(value, {
38
+ ...params,
39
+ ...{
40
+ temp: (0, fs_util_1.tmpDir)("pkg"),
41
+ },
42
+ });
43
+ }
44
+ exports.resolvePackagePath = resolvePackagePath;
45
+ function resolveDatabaseName(value, params) {
46
+ return (0, string_util_1.render)(value, params);
47
+ }
48
+ exports.resolveDatabaseName = resolveDatabaseName;
49
+ function resolvePackage(pkg, params) {
50
+ pkg = Object.assign({}, pkg);
51
+ const pkgParams = {
52
+ ...params,
53
+ packageName: pkg.name,
54
+ path: undefined,
55
+ };
56
+ if (pkg.path)
57
+ pkg.path = resolvePackagePath(pkg.path, pkgParams);
58
+ if (pkg.restorePath)
59
+ pkg.restorePath = resolvePackagePath(pkg.restorePath, {
60
+ ...pkgParams,
61
+ path: pkg.path,
62
+ });
63
+ return pkg;
64
+ }
65
+ exports.resolvePackage = resolvePackage;
66
+ function resolvePackages(packages, params) {
67
+ return packages.map((pkg) => resolvePackage(pkg, params));
68
+ }
69
+ exports.resolvePackages = resolvePackages;
70
+ exports.pkgPathParams = {
71
+ action: "{action}",
72
+ packageName: "{packageName}",
73
+ snapshotId: "{snapshotId}",
74
+ temp: "{temp}",
75
+ };
76
+ exports.pkgRestorePathParams = {
77
+ action: "{action}",
78
+ packageName: "{packageName}",
79
+ path: "{path}",
80
+ snapshotId: "{snapshotId}",
81
+ temp: "{temp}",
82
+ };
83
+ exports.dbNameParams = {
84
+ action: "{action}",
85
+ packageName: "{packageName}",
86
+ snapshotId: "{snapshotId}",
87
+ database: "{database}",
88
+ };
89
+ exports.params = {
90
+ pkgPath: exports.pkgPathParams,
91
+ pkgRestorePath: exports.pkgRestorePathParams,
92
+ dbName: exports.dbNameParams,
93
+ };
@@ -0,0 +1,5 @@
1
+ import { PathsObjectType } from "../../Config/PackageConfig";
2
+ export declare function parsePaths(values: (string | PathsObjectType)[], options: {
3
+ cwd?: string;
4
+ verbose?: boolean;
5
+ }): Promise<string[]>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parsePaths = void 0;
4
+ const process_util_1 = require("../process-util");
5
+ async function parsePaths(values, options) {
6
+ let paths = [];
7
+ for (const value of values) {
8
+ if (typeof value === "string") {
9
+ paths.push(value);
10
+ }
11
+ else if (value.type === "spawn") {
12
+ const spawnResult = await (0, process_util_1.exec)(value.command, value.args, { cwd: options.cwd }, { log: options.verbose, stderr: { save: true }, stdout: { save: true } });
13
+ const spawnFiles = [spawnResult.stderr, spawnResult.stdout].flatMap((text) => text
14
+ .split(/\r?\n/)
15
+ .map((v) => v.trim())
16
+ .filter((v) => !!v.length));
17
+ paths.push(...spawnFiles);
18
+ }
19
+ }
20
+ return paths;
21
+ }
22
+ exports.parsePaths = parsePaths;
@@ -0,0 +1,4 @@
1
+ import { SnapshotGroupByType } from "../../Action/SnapshotsAction";
2
+ import { SnapshotResultType } from "../../Repository/RepositoryAbstract";
3
+ import { FilterByLastOptionsType } from "../date-util";
4
+ export declare function groupAndFilter<TSnapshot extends SnapshotResultType>(snapshots: TSnapshot[], groupByKey?: SnapshotGroupByType[], filter?: FilterByLastOptionsType | ((groupedSnapshots: TSnapshot[]) => FilterByLastOptionsType), reasons?: Record<number, string[]>): TSnapshot[];
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupAndFilter = void 0;
4
+ const date_util_1 = require("../date-util");
5
+ const object_util_1 = require("../object-util");
6
+ function groupAndFilter(snapshots, groupByKey, filter, reasons) {
7
+ const grouped = groupByKey?.length
8
+ ? (0, object_util_1.groupBy)(snapshots, groupByKey)
9
+ : { "": snapshots };
10
+ const result = [];
11
+ for (const key in grouped) {
12
+ if (filter) {
13
+ const groupReasons = reasons
14
+ ? {}
15
+ : undefined;
16
+ result.push(...(0, date_util_1.filterByLast)(grouped[key], typeof filter === "function" ? filter(grouped[key]) : filter, groupReasons));
17
+ if (groupReasons && reasons) {
18
+ for (const groupItemIndex in groupReasons) {
19
+ const snapshot = grouped[key][groupItemIndex];
20
+ const snapshotIndex = snapshots.indexOf(snapshot);
21
+ reasons[snapshotIndex] = groupReasons[groupItemIndex];
22
+ }
23
+ }
24
+ }
25
+ else {
26
+ result.push(...grouped[key]);
27
+ }
28
+ }
29
+ return snapshots.filter((snapshot) => result.includes(snapshot));
30
+ }
31
+ exports.groupAndFilter = groupAndFilter;
@@ -0,0 +1,12 @@
1
+ export declare type FilterByLastOptionsType = {
2
+ last?: number;
3
+ lastMinutely?: number;
4
+ lastHourly?: number;
5
+ lastDaily?: number;
6
+ lastWeekly?: number;
7
+ lastMonthly?: number;
8
+ lastYearly?: number;
9
+ };
10
+ export declare function filterByLast<TItem extends {
11
+ date: string;
12
+ }>(items: TItem[], options: FilterByLastOptionsType, reasons?: Record<number, string[]>): TItem[];
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.filterByLast = void 0;
7
+ const dayjs_1 = __importDefault(require("dayjs"));
8
+ const advancedFormat_1 = __importDefault(require("dayjs/plugin/advancedFormat"));
9
+ const isoWeek_1 = __importDefault(require("dayjs/plugin/isoWeek"));
10
+ dayjs_1.default.extend(isoWeek_1.default);
11
+ dayjs_1.default.extend(advancedFormat_1.default);
12
+ function filterByLast(items, options, reasons) {
13
+ const filters = {
14
+ last: { handler: (_, i) => i.toString(), value: options.last },
15
+ lastMinutely: {
16
+ handler: (d) => d.format("YYYYMMDDHHmm"),
17
+ value: options.lastMinutely,
18
+ },
19
+ lastHourly: {
20
+ handler: (d) => d.format("YYYYMMDDHH"),
21
+ value: options.lastHourly,
22
+ },
23
+ lastDaily: {
24
+ handler: (d) => d.format("YYYYMMDD"),
25
+ value: options.lastDaily,
26
+ },
27
+ lastMonthly: {
28
+ handler: (d) => d.format("YYYYMM"),
29
+ value: options.lastMonthly,
30
+ },
31
+ lastWeekly: {
32
+ handler: (d) => d.format("YYYYWW"),
33
+ value: options.lastWeekly,
34
+ },
35
+ lastYearly: { handler: (d) => d.format("YYYY"), value: options.lastYearly },
36
+ };
37
+ let someFilter = false;
38
+ for (const key in filters) {
39
+ const object = filters[key];
40
+ if (object?.value) {
41
+ someFilter = true;
42
+ }
43
+ }
44
+ if (!someFilter)
45
+ return items;
46
+ const validItems = items
47
+ .slice(0)
48
+ .sort((a, b) => b.date.localeCompare(a.date))
49
+ .filter((item, index) => {
50
+ const date = (0, dayjs_1.default)(item.date);
51
+ const itemIndex = items.indexOf(item);
52
+ let success = false;
53
+ for (const key in filters) {
54
+ const object = filters[key];
55
+ if (object?.value) {
56
+ const value = object.handler(date, index);
57
+ if (value != object.last) {
58
+ success = true;
59
+ if (reasons) {
60
+ if (!reasons[itemIndex])
61
+ reasons[itemIndex] = [];
62
+ if (!reasons[itemIndex].includes(key))
63
+ reasons[itemIndex].push(key);
64
+ }
65
+ object.last = value;
66
+ object.value--;
67
+ }
68
+ }
69
+ }
70
+ return success;
71
+ });
72
+ return items.filter((item) => validItems.includes(item));
73
+ }
74
+ exports.filterByLast = filterByLast;
@@ -0,0 +1,4 @@
1
+ export declare function makeTableSelector<T>(tableName: string): {
2
+ (name?: keyof T | undefined): string;
3
+ toString: any;
4
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.makeTableSelector = void 0;
4
+ function makeTableSelector(tableName) {
5
+ const q = (value) => `\`${value}\``;
6
+ const cb = (name) => `${q(tableName)}${name ? `.${q(name)}` : ""}`;
7
+ cb.toString = cb;
8
+ return cb;
9
+ }
10
+ exports.makeTableSelector = makeTableSelector;
@@ -0,0 +1,43 @@
1
+ /// <reference types="node" />
2
+ export declare function isLocalDir(path: string): boolean;
3
+ export declare function isDirEmpty(path: string): Promise<boolean>;
4
+ export declare function mkdirIfNotExists(path: string): Promise<string>;
5
+ export declare function ensureEmptyDir(path: string): Promise<void>;
6
+ export declare function existsDir(path: string): Promise<boolean>;
7
+ export declare function writeJSONFile<T = any>(path: string, json: T): Promise<void>;
8
+ export declare function readdirIfExists(path: string): Promise<string[]>;
9
+ export declare const parseFileExtensions: string[];
10
+ export declare function parseFile(path: string, jsKey?: string): Promise<any>;
11
+ export declare function parsePackageFile(): {
12
+ name: string;
13
+ version: string;
14
+ description: string;
15
+ };
16
+ export declare function findFile(sourcePath: string, baseName: string, extensions: string[], errorMessage?: string): Promise<string>;
17
+ export declare function existsFile(path: string): Promise<boolean>;
18
+ export declare function parentTmpDir(): string;
19
+ export declare function sessionTmpDir(): string;
20
+ export declare function tmpDir(prefix: string, id?: string): string;
21
+ export declare function mkTmpDir(prefix: string, id?: string): Promise<string>;
22
+ export declare function readPartialFile(path: string, positions: [number, number?]): Promise<string>;
23
+ export declare function checkFile(path: string): Promise<boolean>;
24
+ export declare function checkDir(path: string): Promise<boolean>;
25
+ export declare function forEachFile(dirPath: string, cb: (path: string, dir: boolean) => void, includeDir?: boolean): Promise<void>;
26
+ export declare function writePathLists(options: {
27
+ paths: NodeJS.ReadableStream | string[];
28
+ packs?: {
29
+ include: string[];
30
+ exclude?: string[];
31
+ multiple?: boolean;
32
+ }[];
33
+ }): Promise<{
34
+ path: string;
35
+ includedPackPaths: string[];
36
+ excludedPackPaths: string[];
37
+ total: {
38
+ all: number;
39
+ path: number;
40
+ packsPaths: number[];
41
+ multipleStats: Record<string, number>;
42
+ };
43
+ }>;
@@ -0,0 +1,278 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writePathLists = exports.forEachFile = exports.checkDir = exports.checkFile = exports.readPartialFile = exports.mkTmpDir = exports.tmpDir = 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 = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const fs_1 = require("fs");
6
+ const fs_2 = require("fs");
7
+ const fs_extra_1 = require("fs-extra");
8
+ const promises_1 = require("fs/promises");
9
+ const micromatch_1 = require("micromatch");
10
+ const os_1 = require("os");
11
+ const path_1 = require("path");
12
+ const path_2 = require("path");
13
+ function isLocalDir(path) {
14
+ return /^[\/\.]|([A-Z]:)/i.test(path);
15
+ }
16
+ exports.isLocalDir = isLocalDir;
17
+ async function isDirEmpty(path) {
18
+ const files = await (0, promises_1.readdir)(path);
19
+ return !files.length;
20
+ }
21
+ exports.isDirEmpty = isDirEmpty;
22
+ async function mkdirIfNotExists(path) {
23
+ try {
24
+ await (0, fs_extra_1.mkdir)(path, {
25
+ recursive: true,
26
+ });
27
+ }
28
+ catch (e) { }
29
+ return path;
30
+ }
31
+ exports.mkdirIfNotExists = mkdirIfNotExists;
32
+ async function ensureEmptyDir(path) {
33
+ if (!(await isDirEmpty(path)))
34
+ throw new Error(`Dir is not empty: ${path}`);
35
+ }
36
+ exports.ensureEmptyDir = ensureEmptyDir;
37
+ async function existsDir(path) {
38
+ try {
39
+ const info = await (0, promises_1.stat)(path);
40
+ return info.isDirectory();
41
+ }
42
+ catch (e) {
43
+ return false;
44
+ }
45
+ }
46
+ exports.existsDir = existsDir;
47
+ async function writeJSONFile(path, json) {
48
+ await (0, promises_1.writeFile)(path, JSON.stringify(json));
49
+ }
50
+ exports.writeJSONFile = writeJSONFile;
51
+ async function readdirIfExists(path) {
52
+ if (!(await existsDir(path)))
53
+ return [];
54
+ return await (0, promises_1.readdir)(path);
55
+ }
56
+ exports.readdirIfExists = readdirIfExists;
57
+ exports.parseFileExtensions = ["json", "js", "ts", "yaml", "yml"];
58
+ async function parseFile(path, jsKey) {
59
+ if (!(0, path_2.isAbsolute)(path))
60
+ path = (0, path_1.join)(process.cwd(), path);
61
+ if (path.endsWith(".ts"))
62
+ require("ts-node").register();
63
+ if (path.endsWith(".yaml") || path.endsWith("yml")) {
64
+ const contents = await (0, promises_1.readFile)(path);
65
+ return require("yaml").parse(contents.toString());
66
+ }
67
+ else if (path.endsWith(".json")) {
68
+ return require(path);
69
+ }
70
+ else {
71
+ const object = require(path);
72
+ const value = jsKey ? object[jsKey] : object;
73
+ return typeof value === "function" ? await value() : value;
74
+ }
75
+ }
76
+ exports.parseFile = parseFile;
77
+ function parsePackageFile() {
78
+ return require(`${__dirname}/../../package.json`);
79
+ }
80
+ exports.parsePackageFile = parsePackageFile;
81
+ async function findFile(sourcePath, baseName, extensions, errorMessage = "Path not found") {
82
+ const info = await (0, promises_1.stat)(sourcePath);
83
+ let path;
84
+ if (info.isDirectory()) {
85
+ for (const ext of extensions) {
86
+ const extPath = (0, path_1.join)(sourcePath, baseName) + "." + ext;
87
+ if (await existsFile(extPath)) {
88
+ path = extPath;
89
+ break;
90
+ }
91
+ }
92
+ }
93
+ else {
94
+ path = sourcePath;
95
+ }
96
+ if (typeof path !== "string")
97
+ throw new Error(errorMessage);
98
+ return path;
99
+ }
100
+ exports.findFile = findFile;
101
+ async function existsFile(path) {
102
+ try {
103
+ const info = await (0, promises_1.stat)(path);
104
+ return info.isFile();
105
+ }
106
+ catch (e) {
107
+ return false;
108
+ }
109
+ }
110
+ exports.existsFile = existsFile;
111
+ function parentTmpDir() {
112
+ const tmpDir = (0, os_1.tmpdir)();
113
+ return (0, path_1.join)(tmpDir, "datatruck");
114
+ }
115
+ exports.parentTmpDir = parentTmpDir;
116
+ function sessionTmpDir() {
117
+ return (0, path_1.join)(parentTmpDir(), process.pid.toString());
118
+ }
119
+ exports.sessionTmpDir = sessionTmpDir;
120
+ function tmpDir(prefix, id) {
121
+ if (!id)
122
+ id = (0, crypto_1.randomBytes)(8).toString("hex");
123
+ return (0, path_1.join)(sessionTmpDir(), `${prefix}-${id}`);
124
+ }
125
+ exports.tmpDir = tmpDir;
126
+ async function mkTmpDir(prefix, id) {
127
+ const path = tmpDir(prefix, id);
128
+ await (0, fs_extra_1.mkdir)(path, { recursive: true });
129
+ return path;
130
+ }
131
+ exports.mkTmpDir = mkTmpDir;
132
+ async function readPartialFile(path, positions) {
133
+ let result = "";
134
+ const statResult = await (0, promises_1.stat)(path);
135
+ const size = statResult.size;
136
+ let [start, end] = positions;
137
+ if (start < 0)
138
+ start = size + start;
139
+ if (end && end < 0)
140
+ end = size + end;
141
+ if (typeof start === "number")
142
+ start = Math.min(Math.max(start, 0), size);
143
+ if (typeof end === "number")
144
+ end = Math.min(Math.max(end, 0), size);
145
+ return new Promise((resolve, reject) => {
146
+ const reader = (0, fs_1.createReadStream)(path, {
147
+ start: start,
148
+ end: end,
149
+ });
150
+ reader
151
+ .on("error", reject)
152
+ .on("data", (chunk) => {
153
+ result += chunk.toString();
154
+ })
155
+ .on("close", () => resolve(result));
156
+ });
157
+ }
158
+ exports.readPartialFile = readPartialFile;
159
+ async function checkFile(path) {
160
+ try {
161
+ return (await (0, promises_1.stat)(path)).isFile();
162
+ }
163
+ catch (e) {
164
+ return false;
165
+ }
166
+ }
167
+ exports.checkFile = checkFile;
168
+ async function checkDir(path) {
169
+ try {
170
+ return (await (0, promises_1.stat)(path)).isDirectory();
171
+ }
172
+ catch (e) {
173
+ return false;
174
+ }
175
+ }
176
+ exports.checkDir = checkDir;
177
+ async function forEachFile(dirPath, cb, includeDir) {
178
+ const files = await (0, promises_1.readdir)(dirPath);
179
+ for (const file of files) {
180
+ const filePath = (0, path_1.join)(dirPath, file);
181
+ if ((await (0, promises_1.stat)(filePath)).isDirectory()) {
182
+ if (includeDir)
183
+ cb(filePath, true);
184
+ await forEachFile(filePath, cb, includeDir);
185
+ }
186
+ else {
187
+ cb(filePath, false);
188
+ }
189
+ }
190
+ }
191
+ exports.forEachFile = forEachFile;
192
+ async function writePathLists(options) {
193
+ const tempDir = await mkTmpDir("path-lists");
194
+ const includedPaths = [];
195
+ const excludedPaths = [];
196
+ const included = [];
197
+ const excluded = [];
198
+ const multipleStats = {};
199
+ const total = new Array((options.packs?.length || 0) + 1).fill(0);
200
+ await Promise.all([
201
+ ...new Array((options.packs?.length || 0) + 1).fill(null).map((_, index) => new Promise((resolve, reject) => {
202
+ const path = (0, path_1.join)(tempDir, `${index}-included.txt`);
203
+ const stream = (0, fs_2.createWriteStream)(path);
204
+ includedPaths.push(path);
205
+ included.push(stream);
206
+ stream.on("close", resolve);
207
+ stream.on("error", reject);
208
+ })),
209
+ ...new Array(options.packs?.length || 0).fill(null).map((_, index) => new Promise((resolve, reject) => {
210
+ const path = (0, path_1.join)(tempDir, `${index}-excluded.txt`);
211
+ const stream = (0, fs_2.createWriteStream)(path);
212
+ excludedPaths.push(path);
213
+ excluded.push(stream);
214
+ stream.on("close", resolve);
215
+ stream.on("error", reject);
216
+ })),
217
+ new Promise(async (resolve) => {
218
+ const packDirectories = [];
219
+ for await (const value of options.paths) {
220
+ const entry = value.toString();
221
+ const isDir = entry.endsWith("/");
222
+ const matchEntry = isDir ? entry.slice(0, -1) : entry;
223
+ let packIndex = 1;
224
+ let matches = false;
225
+ for (const pack of options.packs || []) {
226
+ if ((0, micromatch_1.isMatch)(matchEntry, pack.include) &&
227
+ (!pack.exclude || !(0, micromatch_1.isMatch)(matchEntry, pack.exclude))) {
228
+ if (isDir)
229
+ packDirectories.push([packIndex - 1, entry]);
230
+ included[packIndex].write(`${entry}\n`);
231
+ if (!isDir)
232
+ total[packIndex]++;
233
+ matches = true;
234
+ break;
235
+ }
236
+ packIndex++;
237
+ }
238
+ if (!matches) {
239
+ const packDir = packDirectories.find(([, p]) => entry.startsWith(p));
240
+ if (packDir) {
241
+ const [i, v] = packDir;
242
+ const multipleExclude = options.packs?.[i].exclude;
243
+ if (multipleExclude && (0, micromatch_1.isMatch)(matchEntry, multipleExclude)) {
244
+ included[0].write(`${entry}\n`);
245
+ excluded[i].write(`${entry}\n`);
246
+ }
247
+ else {
248
+ if (!multipleStats[v])
249
+ multipleStats[v] = 0;
250
+ multipleStats[v]++;
251
+ }
252
+ }
253
+ else {
254
+ included[0].write(`${entry}\n`);
255
+ }
256
+ if (!isDir)
257
+ total[0]++;
258
+ }
259
+ }
260
+ for (const stream of [...included, ...excluded]) {
261
+ stream.end();
262
+ }
263
+ resolve();
264
+ }),
265
+ ]);
266
+ return {
267
+ path: includedPaths[0],
268
+ includedPackPaths: includedPaths.slice(1),
269
+ excludedPackPaths: excludedPaths,
270
+ total: {
271
+ all: total.reduce((p, v) => p + v, 0),
272
+ path: total[0],
273
+ packsPaths: total.slice(1),
274
+ multipleStats,
275
+ },
276
+ };
277
+ }
278
+ exports.writePathLists = writePathLists;
@@ -0,0 +1 @@
1
+ export declare function progressPercent(total: number, current: number): number;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.progressPercent = void 0;
4
+ function progressPercent(total, current) {
5
+ return Number(((current / total) * 100).toFixed(2));
6
+ }
7
+ exports.progressPercent = progressPercent;
@@ -0,0 +1,7 @@
1
+ export declare function merge<T extends Record<string, unknown>>(target: T, ...sources: Record<string, unknown>[]): T;
2
+ export declare function push<T>(map: Record<string, T[]>, key: string, object: T): void;
3
+ export declare function getErrorProperties(error: Error): Record<string, string>;
4
+ declare type GroupByKeyParamType<TItem> = ((item: TItem) => string[] | string) | (keyof TItem)[] | keyof TItem;
5
+ export declare function groupBy<TItem>(items: TItem[], keyOrCb: GroupByKeyParamType<TItem>): Record<string, TItem[]>;
6
+ export declare function groupBy<TItem>(items: TItem[], keyOrCb: GroupByKeyParamType<TItem>, single: true): Record<string, TItem>;
7
+ export {};
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupBy = exports.getErrorProperties = exports.push = exports.merge = void 0;
4
+ function merge(target, ...sources) {
5
+ const isObject = (o) => typeof o === "object" && o !== null;
6
+ for (const source of sources)
7
+ for (const key in source) {
8
+ const a = source[key];
9
+ const b = target[key];
10
+ target[key] =
11
+ isObject(b) && isObject(a) ? merge(b, a) : source[key];
12
+ }
13
+ return target;
14
+ }
15
+ exports.merge = merge;
16
+ function push(map, key, object) {
17
+ if (!map[key])
18
+ map[key] = [];
19
+ map[key].push(object);
20
+ }
21
+ exports.push = push;
22
+ function getErrorProperties(error) {
23
+ const alt = {};
24
+ for (const key of Object.getOwnPropertyNames(error)) {
25
+ alt[key] = error[key];
26
+ }
27
+ return alt;
28
+ }
29
+ exports.getErrorProperties = getErrorProperties;
30
+ function groupBy(items, keyOrCb, single) {
31
+ const keyCb = typeof keyOrCb === "function"
32
+ ? keyOrCb
33
+ : Array.isArray(keyOrCb)
34
+ ? (item) => keyOrCb.map((key) => item[key])
35
+ : (item) => item[keyOrCb];
36
+ const stringify = (keys) => typeof keys === "string"
37
+ ? keys
38
+ : keys.length == 1
39
+ ? keys[0]
40
+ : JSON.stringify(keys);
41
+ if (single) {
42
+ return items.reduce((result, item) => {
43
+ const resultKey = stringify(keyCb(item));
44
+ result[resultKey] = item;
45
+ return result;
46
+ }, {});
47
+ }
48
+ else {
49
+ return items.reduce((result, item) => {
50
+ const resultKey = stringify(keyCb(item));
51
+ if (!result[resultKey])
52
+ result[resultKey] = [];
53
+ result[resultKey].push(item);
54
+ return result;
55
+ }, {});
56
+ }
57
+ }
58
+ exports.groupBy = groupBy;