@datatruck/cli 0.21.1 → 0.22.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.
@@ -1,7 +1,6 @@
1
1
  import type { ConfigType } from "../Config/Config";
2
2
  import { PackageConfigType } from "../Config/PackageConfig";
3
3
  import { RepositoryConfigType } from "../Config/RepositoryConfig";
4
- import { AppError } from "../Error/AppError";
5
4
  import { SnapshotType } from "../Repository/RepositoryAbstract";
6
5
  import { BackupSessionManager } from "../SessionManager/BackupSessionManager";
7
6
  import { TaskAbstract } from "../Task/TaskAbstract";
@@ -35,7 +34,7 @@ export declare class BackupAction<TRequired extends boolean = true> {
35
34
  error: boolean;
36
35
  tmpDirs: string[];
37
36
  }>;
38
- protected getError(pkg: PackageConfigType): AppError | null;
37
+ protected getError(pkg: PackageConfigType): Error | undefined;
39
38
  protected splitRepositories(repositoryNames: string[]): {
40
39
  repoNames: string[];
41
40
  mirrors: {
@@ -45,6 +44,6 @@ export declare class BackupAction<TRequired extends boolean = true> {
45
44
  };
46
45
  exec(session: BackupSessionManager): Promise<{
47
46
  total: number;
48
- errors: number;
47
+ errors: Error[];
49
48
  }>;
50
49
  }
@@ -67,7 +67,7 @@ class BackupAction {
67
67
  const key = `${pkg.name}`;
68
68
  let error;
69
69
  if (this.taskErrors[key]?.length) {
70
- error = new AppError_1.AppError("Previous task failed");
70
+ error = AppError_1.AppError.create("Previous task failed", this.taskErrors[key]);
71
71
  }
72
72
  else {
73
73
  try {
@@ -110,7 +110,7 @@ class BackupAction {
110
110
  let error;
111
111
  let repoInstance;
112
112
  if (this.taskErrors[pkg.name]?.length) {
113
- error = new AppError_1.AppError("Task failed");
113
+ error = AppError_1.AppError.create("Task failed", this.taskErrors[pkg.name]);
114
114
  }
115
115
  else {
116
116
  try {
@@ -156,7 +156,7 @@ class BackupAction {
156
156
  let error;
157
157
  let repoInstance;
158
158
  if (this.taskErrors[pkg.name]?.length) {
159
- error = new AppError_1.AppError("Task failed");
159
+ error = AppError_1.AppError.create("Task failed", this.taskErrors[pkg.name]);
160
160
  }
161
161
  else {
162
162
  try {
@@ -190,20 +190,16 @@ class BackupAction {
190
190
  };
191
191
  }
192
192
  getError(pkg) {
193
- const taskErrors = this.taskErrors[pkg.name]?.length;
194
- const repoErrors = this.repoErrors[pkg.name]?.length;
195
- if (taskErrors && repoErrors) {
196
- return new AppError_1.AppError("Task and repository failed");
197
- }
198
- else if (taskErrors && !repoErrors) {
199
- return new AppError_1.AppError("Task failed");
200
- }
201
- else if (!taskErrors && repoErrors) {
202
- return new AppError_1.AppError("Repository failed");
203
- }
204
- else {
205
- return null;
206
- }
193
+ const taskErrors = this.taskErrors[pkg.name] || [];
194
+ const repoErrors = this.repoErrors[pkg.name] || [];
195
+ const errors = [...taskErrors, ...repoErrors];
196
+ if (!errors.length)
197
+ return;
198
+ return AppError_1.AppError.create(taskErrors.length && repoErrors.length
199
+ ? "Task and repository failed"
200
+ : taskErrors.length && !repoErrors.length
201
+ ? "Task failed"
202
+ : "Repository failed", errors);
207
203
  }
208
204
  splitRepositories(repositoryNames) {
209
205
  const mirrorRepoMap = {};
@@ -230,7 +226,7 @@ class BackupAction {
230
226
  }
231
227
  async exec(session) {
232
228
  const [snapshot, packages] = await this.init(session);
233
- let errors = 0;
229
+ const errors = [];
234
230
  for (const pkg of packages) {
235
231
  const id = session.findId({
236
232
  packageName: pkg.name,
@@ -272,7 +268,7 @@ class BackupAction {
272
268
  }
273
269
  const error = this.getError(pkg);
274
270
  if (error)
275
- errors++;
271
+ errors.push(error);
276
272
  await session.end({
277
273
  id: id,
278
274
  error: error?.message,
@@ -283,7 +279,7 @@ class BackupAction {
283
279
  });
284
280
  return {
285
281
  total: packages.length,
286
- errors: errors,
282
+ errors,
287
283
  };
288
284
  }
289
285
  }
@@ -39,6 +39,8 @@ export declare class RestoreAction<TRequired extends boolean = true> {
39
39
  tmpDirs: string[];
40
40
  }>;
41
41
  protected getError(pkg: PackageConfigType): AppError | null;
42
- exec(session: RestoreSessionManager): Promise<boolean>;
42
+ exec(session: RestoreSessionManager): Promise<{
43
+ errors: Error[];
44
+ }>;
43
45
  }
44
46
  export {};
@@ -100,10 +100,10 @@ class RestoreAction {
100
100
  });
101
101
  let error;
102
102
  if (this.repoErrors[pkg.name]?.length) {
103
- error = new AppError_1.AppError("Repository failed");
103
+ error = AppError_1.AppError.create("Repository failed", this.repoErrors[pkg.name]);
104
104
  }
105
105
  else if (this.taskErrors[pkg.name]?.length) {
106
- error = new AppError_1.AppError("Previous task failed");
106
+ error = AppError_1.AppError.create("Previous task failed", this.taskErrors[pkg.name]);
107
107
  }
108
108
  else {
109
109
  try {
@@ -222,7 +222,7 @@ class RestoreAction {
222
222
  });
223
223
  const snapshotAndConfigs = this.assocConfigs(packages, snapshots);
224
224
  await this.init(session, this.options.snapshotId, snapshotAndConfigs);
225
- let sessionErrors = 0;
225
+ const errors = [];
226
226
  for (const [snapshot, pkg] of snapshotAndConfigs) {
227
227
  (0, assert_1.ok)(pkg);
228
228
  const repo = (0, config_1.findRepositoryOrFail)(this.config, snapshot.repositoryName);
@@ -257,10 +257,10 @@ class RestoreAction {
257
257
  error: error?.message,
258
258
  });
259
259
  if (error)
260
- sessionErrors++;
260
+ errors.push(error);
261
261
  }
262
262
  await session.endDrivers();
263
- return !sessionErrors;
263
+ return { errors };
264
264
  }
265
265
  }
266
266
  exports.RestoreAction = RestoreAction;
@@ -12,5 +12,5 @@ export type BackupCommandOptionsType<TResolved = false> = {
12
12
  };
13
13
  export declare class BackupCommand extends CommandAbstract<BackupCommandOptionsType<false>, BackupCommandOptionsType<true>> {
14
14
  onOptions(): import("../utils/cli").OptionsType<BackupCommandOptionsType<false>, BackupCommandOptionsType<true>>;
15
- onExec(): Promise<0 | 1>;
15
+ onExec(): Promise<1 | 0>;
16
16
  }
@@ -74,7 +74,7 @@ class BackupCommand extends CommandAbstract_1.CommandAbstract {
74
74
  progressInterval: this.globalOptions.progressInterval,
75
75
  });
76
76
  const result = await backup.exec(sessionManager);
77
- if (result.errors) {
77
+ if (result.errors.length) {
78
78
  return 1;
79
79
  }
80
80
  else if (!result.total) {
@@ -12,5 +12,5 @@ export type RestoreCommandOptionsType<TResolved = false> = {
12
12
  };
13
13
  export declare class RestoreCommand extends CommandAbstract<RestoreCommandOptionsType<false>, RestoreCommandOptionsType<true>> {
14
14
  onOptions(): import("../utils/cli").OptionsType<RestoreCommandOptionsType<false>, RestoreCommandOptionsType<true>>;
15
- onExec(): Promise<0 | 1>;
15
+ onExec(): Promise<1 | 0>;
16
16
  }
@@ -74,7 +74,7 @@ class RestoreCommand extends CommandAbstract_1.CommandAbstract {
74
74
  progressInterval: this.globalOptions.progressInterval,
75
75
  });
76
76
  const result = await restore.exec(sessionManager);
77
- return result ? 0 : 1;
77
+ return result.errors.length ? 1 : 0;
78
78
  }
79
79
  }
80
80
  exports.RestoreCommand = RestoreCommand;
@@ -1,3 +1,4 @@
1
1
  export declare class AppError extends Error {
2
2
  constructor(message: string);
3
+ static create(message: string, errors: Error[]): Error;
3
4
  }
package/Error/AppError.js CHANGED
@@ -6,5 +6,13 @@ class AppError extends Error {
6
6
  super(message);
7
7
  this.name = AppError.name;
8
8
  }
9
+ static create(message, errors) {
10
+ if (errors.length === 1) {
11
+ return errors[0];
12
+ }
13
+ else {
14
+ return new AggregateError(errors, message);
15
+ }
16
+ }
9
17
  }
10
18
  exports.AppError = AppError;
@@ -38,7 +38,7 @@ exports.definitions = {
38
38
  [DefinitionEnum_1.DefinitionEnum.resticPackageRepository]: ResticRepository_1.resticPackageRepositoryDefinition,
39
39
  [DefinitionEnum_1.DefinitionEnum.gitTask]: GitTask_1.gitTaskDefinition,
40
40
  [DefinitionEnum_1.DefinitionEnum.scriptTask]: ScriptTask_1.scriptTaskDefinition,
41
- [DefinitionEnum_1.DefinitionEnum.sqlDumpTask]: SqlDumpTaskAbstract_1.sqlDumpTaskDefinition,
41
+ [DefinitionEnum_1.DefinitionEnum.sqlDumpTask]: (0, SqlDumpTaskAbstract_1.sqlDumpTaskDefinition)(),
42
42
  [DefinitionEnum_1.DefinitionEnum.mariadbTask]: MariadbTask_1.mariadbTaskDefinition,
43
43
  [DefinitionEnum_1.DefinitionEnum.mssqlTask]: MssqlTask_1.mssqlTaskDefinition,
44
44
  [DefinitionEnum_1.DefinitionEnum.mysqlDumpTask]: MysqlDumpTask_1.mysqlDumpTaskDefinition,
@@ -196,7 +196,7 @@ class DatatruckRepository extends RepositoryAbstract_1.RepositoryAbstract {
196
196
  if (entry.dirent.isDirectory() &&
197
197
  !(await (0, fs_1.isEmptyDir)((0, path_1.join)(sourcePath, entry.path))))
198
198
  return false;
199
- let packIndex = configPacks.findIndex((pack) => (0, string_1.checkPath)(entry.path, pack.include, pack.exclude));
199
+ let packIndex = configPacks.findIndex((pack) => (0, string_1.match)(entry.path, pack.include, pack.exclude));
200
200
  if (packIndex === -1)
201
201
  packIndex = defaultsPackIndex;
202
202
  const pack = packs[packIndex];
@@ -1,17 +1,15 @@
1
- import { SqlDumpTaskAbstract, SqlDumpTaskConfigType, TargetDatabaseType } from "./SqlDumpTaskAbstract";
2
- import { JSONSchema7 } from "json-schema";
1
+ import { SqlDumpTaskConfigType } from "./SqlDumpTaskAbstract";
2
+ import { BackupDataType, RestoreDataType, TaskAbstract } from "./TaskAbstract";
3
3
  export declare const mysqlDumpTaskName = "mysql-dump";
4
- export type MysqlDumpTaskConfigType = {} & SqlDumpTaskConfigType;
5
- export declare const mysqlDumpTaskDefinition: JSONSchema7;
6
- export declare class MysqlDumpTask extends SqlDumpTaskAbstract<MysqlDumpTaskConfigType> {
7
- buildConnectionArgs(database?: string): Promise<string[]>;
8
- onDatabaseIsEmpty(name: string): Promise<boolean>;
9
- onCreateDatabase(database: TargetDatabaseType): Promise<void>;
10
- onExecQuery(query: string): Promise<import("../utils/process").ExecResultType>;
11
- onFetchTableNames(database: string): Promise<string[]>;
12
- onExportTables(tableNames: string[], output: string, onProgress: (progress: {
13
- totalBytes: number;
14
- }) => void): Promise<void>;
15
- onExportStoredPrograms(output: string): Promise<void>;
16
- onImport(path: string, database: string): Promise<void>;
4
+ export type MysqlDumpTaskConfigType = {
5
+ /**
6
+ * @default "sql"
7
+ */
8
+ dataFormat?: "csv" | "sql";
9
+ csvSharedPath?: string;
10
+ } & SqlDumpTaskConfigType;
11
+ export declare const mysqlDumpTaskDefinition: import("json-schema").JSONSchema7;
12
+ export declare class MysqlDumpTask extends TaskAbstract<MysqlDumpTaskConfigType> {
13
+ onBackup(data: BackupDataType): Promise<void>;
14
+ onRestore(data: RestoreDataType): Promise<void>;
17
15
  }
@@ -2,165 +2,223 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MysqlDumpTask = exports.mysqlDumpTaskDefinition = exports.mysqlDumpTaskName = void 0;
4
4
  const AppError_1 = require("../Error/AppError");
5
- const DefinitionEnum_1 = require("../JsonSchema/DefinitionEnum");
5
+ const cli_1 = require("../utils/cli");
6
+ const config_1 = require("../utils/datatruck/config");
6
7
  const fs_1 = require("../utils/fs");
7
- const process_1 = require("../utils/process");
8
+ const math_1 = require("../utils/math");
9
+ const mysql_1 = require("../utils/mysql");
10
+ const string_1 = require("../utils/string");
8
11
  const SqlDumpTaskAbstract_1 = require("./SqlDumpTaskAbstract");
9
- const fs_2 = require("fs");
10
- const fs_3 = require("fs");
12
+ const TaskAbstract_1 = require("./TaskAbstract");
13
+ const assert_1 = require("assert");
14
+ const promises_1 = require("fs/promises");
15
+ const path_1 = require("path");
11
16
  exports.mysqlDumpTaskName = "mysql-dump";
12
- exports.mysqlDumpTaskDefinition = {
13
- allOf: [(0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.sqlDumpTask)],
17
+ exports.mysqlDumpTaskDefinition = (0, SqlDumpTaskAbstract_1.sqlDumpTaskDefinition)({
18
+ dataFormat: { enum: ["csv", "sql"] },
19
+ csvSharedPath: { type: "string" },
20
+ });
21
+ const suffix = {
22
+ database: ".database.sql",
23
+ stored: ".stored-programs.sql",
24
+ table: ".table.sql",
25
+ tableData: ".table-data.csv",
26
+ tableSchema: ".table-schema.sql",
14
27
  };
15
- class MysqlDumpTask extends SqlDumpTaskAbstract_1.SqlDumpTaskAbstract {
16
- async buildConnectionArgs(database) {
17
- const password = await this.fetchPassword();
18
- return [
19
- `--host=${this.config.hostname}`,
20
- ...(this.config.port ? [`--port=${this.config.port}`] : []),
21
- `--user=${this.config.username}`,
22
- `--password=${password ?? ""}`,
23
- ...(database ? [database] : []),
24
- ];
25
- }
26
- async onDatabaseIsEmpty(name) {
27
- const [total] = await this.fetchValues(`
28
- SELECT
29
- COUNT(*) AS total
30
- FROM
31
- information_schema.tables
32
- WHERE
33
- table_schema = '${name}'
34
- `);
35
- return Number(total) ? false : true;
36
- }
37
- async onCreateDatabase(database) {
38
- const query = `
39
- CREATE DATABASE IF NOT EXISTS \`${database.name}\`
40
- CHARACTER SET ${database.charset ?? "utf8"}
41
- COLLATE ${database.charset ?? "utf8_general_ci"}
42
- `;
43
- await this.onExecQuery(query);
44
- }
45
- async onExecQuery(query) {
46
- return await (0, process_1.exec)("mysql", [
47
- ...(await this.buildConnectionArgs()),
48
- "-e",
49
- query.replace(/\s{1,}/g, " "),
50
- "-N",
51
- ], undefined, {
52
- log: this.verbose,
53
- stderr: {
54
- toExitCode: true,
55
- },
56
- stdout: {
57
- save: true,
58
- },
28
+ class MysqlDumpTask extends TaskAbstract_1.TaskAbstract {
29
+ async onBackup(data) {
30
+ const sql = (0, mysql_1.createMysqlCli)({
31
+ ...this.config,
32
+ verbose: data.options.verbose,
59
33
  });
34
+ const tableNames = await sql.fetchTableNames(this.config.database, this.config.includeTables, this.config.excludeTables);
35
+ const outputPath = data.package.path;
36
+ (0, assert_1.ok)(typeof outputPath === "string");
37
+ const dataFormat = this.config.dataFormat ?? "sql";
38
+ await (0, promises_1.mkdir)(outputPath, { recursive: true });
39
+ const sharedDir = dataFormat === "csv"
40
+ ? await sql.initSharedDir(this.config.csvSharedPath)
41
+ : undefined;
42
+ if (this.config.oneFileByTable || sharedDir) {
43
+ let current = 0;
44
+ for (const tableName of tableNames) {
45
+ await data.onProgress({
46
+ relative: {
47
+ description: "Exporting",
48
+ payload: tableName,
49
+ },
50
+ absolute: {
51
+ total: tableNames.length,
52
+ current,
53
+ percent: (0, math_1.progressPercent)(tableNames.length, current),
54
+ },
55
+ });
56
+ if (sharedDir) {
57
+ const tableSharedPath = (0, path_1.join)(sharedDir, `tmp-dtt-backup-${data.snapshot.id.slice(0, 8)}-${tableName}`);
58
+ if (data.options.verbose)
59
+ (0, cli_1.logExec)("mkdir", [tableSharedPath]);
60
+ await (0, promises_1.mkdir)(tableSharedPath, { recursive: true });
61
+ try {
62
+ await sql.csvDump({
63
+ sharedPath: tableSharedPath,
64
+ items: [tableName],
65
+ database: this.config.database,
66
+ });
67
+ const files = await (0, promises_1.readdir)(tableSharedPath);
68
+ const schemaFile = `${tableName}.sql`;
69
+ const dataFile = `${tableName}.txt`;
70
+ const successCsvDump = files.length === 2 &&
71
+ files.every((file) => file === schemaFile || file === dataFile);
72
+ if (!successCsvDump)
73
+ throw new AppError_1.AppError(`Invalid csv dump files: ${files.join(", ")}`);
74
+ await (0, promises_1.rename)((0, path_1.join)(tableSharedPath, schemaFile), (0, path_1.join)(outputPath, `${tableName}${suffix.tableSchema}`));
75
+ await (0, promises_1.rename)((0, path_1.join)(tableSharedPath, dataFile), (0, path_1.join)(outputPath, `${tableName}${suffix.tableData}`));
76
+ }
77
+ finally {
78
+ await (0, promises_1.rm)(tableSharedPath, { recursive: true });
79
+ }
80
+ }
81
+ else {
82
+ const outPath = (0, path_1.join)(outputPath, `${tableName}${suffix.table}`);
83
+ await sql.dump({
84
+ output: outPath,
85
+ items: [tableName],
86
+ database: this.config.database,
87
+ onProgress(progress) {
88
+ data.onProgress({
89
+ relative: {
90
+ description: "Exporting",
91
+ payload: tableName,
92
+ current: progress.totalBytes,
93
+ format: "size",
94
+ },
95
+ absolute: {
96
+ total: tableNames.length,
97
+ current,
98
+ percent: (0, math_1.progressPercent)(tableNames.length, current),
99
+ },
100
+ });
101
+ },
102
+ });
103
+ }
104
+ current++;
105
+ }
106
+ }
107
+ else {
108
+ await data.onProgress({
109
+ relative: { description: "Exporting" },
110
+ });
111
+ await sql.dump({
112
+ output: (0, path_1.join)(outputPath, `${this.config.database}${suffix.database}`),
113
+ items: tableNames,
114
+ database: this.config.database,
115
+ onProgress: (progress) => data.onProgress({
116
+ absolute: {
117
+ description: "Exporting in single file",
118
+ current: progress.totalBytes,
119
+ format: "size",
120
+ },
121
+ }),
122
+ });
123
+ }
124
+ if (this.config.storedPrograms ?? true) {
125
+ await data.onProgress({
126
+ relative: { description: "Exporting stored programs" },
127
+ });
128
+ await sql.dump({
129
+ database: this.config.database,
130
+ output: (0, path_1.join)(outputPath, `${this.config.database}${suffix.stored}`),
131
+ onlyStoredPrograms: true,
132
+ });
133
+ }
60
134
  }
61
- async onFetchTableNames(database) {
62
- return await this.fetchValues(`
63
- SELECT
64
- table_name
65
- FROM
66
- information_schema.tables
67
- WHERE
68
- table_schema = '${database}'
69
- ORDER BY
70
- table_name
71
- `);
72
- }
73
- async onExportTables(tableNames, output, onProgress) {
74
- const stream = (0, fs_2.createWriteStream)(output);
75
- await Promise.all([
76
- new Promise((resolve, reject) => {
77
- stream.on("close", resolve);
78
- stream.on("error", reject);
79
- }),
80
- await (0, process_1.exec)("mysqldump", [
81
- ...(await this.buildConnectionArgs(this.config.database)),
82
- "--lock-tables=false",
83
- "--skip-add-drop-table=false",
84
- ...tableNames,
85
- ], null, {
86
- pipe: {
87
- stream,
88
- onWriteProgress: onProgress,
89
- },
90
- log: {
91
- exec: this.verbose,
92
- stderr: this.verbose,
93
- allToStderr: true,
94
- },
95
- stderr: {
96
- toExitCode: true,
97
- },
98
- }),
99
- ]);
100
- const headerContents = await (0, fs_1.readPartialFile)(output, [0, 100]);
101
- const footerContents = await (0, fs_1.readPartialFile)(output, [-100]);
102
- const successHeader = headerContents.split(/\r?\n/).some((line) => {
103
- const firstLine = line.trim().toLowerCase();
104
- return (firstLine.startsWith("-- mysql dump") ||
105
- firstLine.startsWith("-- mariadb dump"));
135
+ async onRestore(data) {
136
+ const sql = (0, mysql_1.createMysqlCli)({
137
+ ...this.config,
138
+ verbose: data.options.verbose,
106
139
  });
107
- if (!successHeader)
108
- throw new AppError_1.AppError("No start line found");
109
- const successFooter = footerContents
110
- .split(/\r?\n/)
111
- .some((line) => line.trim().toLowerCase().startsWith("-- dump completed"));
112
- if (!successFooter)
113
- throw new AppError_1.AppError("No end line found (incomplete backup)");
114
- }
115
- async onExportStoredPrograms(output) {
116
- const stream = (0, fs_2.createWriteStream)(output);
117
- await Promise.all([
118
- new Promise((resolve, reject) => {
119
- stream.on("close", resolve);
120
- stream.on("error", reject);
121
- }),
122
- await (0, process_1.exec)("mysqldump", [
123
- ...(await this.buildConnectionArgs(this.config.database)),
124
- "--lock-tables=false",
125
- "--routines",
126
- "--events",
127
- "--skip-triggers",
128
- "--no-create-info",
129
- "--no-data",
130
- "--no-create-db",
131
- "--skip-opt",
132
- ], null, {
133
- pipe: { stream: stream },
134
- log: {
135
- exec: this.verbose,
136
- stderr: this.verbose,
137
- allToStderr: true,
140
+ const restorePath = data.package.restorePath;
141
+ (0, assert_1.ok)(typeof restorePath === "string");
142
+ const params = {
143
+ packageName: data.package.name,
144
+ snapshotId: data.options.snapshotId,
145
+ snapshotDate: data.snapshot.date,
146
+ action: "restore",
147
+ database: undefined,
148
+ };
149
+ const database = {
150
+ name: (0, config_1.resolveDatabaseName)(this.config.database, params),
151
+ };
152
+ if (this.config.targetDatabase)
153
+ database.name = (0, config_1.resolveDatabaseName)(this.config.targetDatabase.name, {
154
+ ...params,
155
+ database: database.name,
156
+ });
157
+ const suffixes = Object.values(suffix);
158
+ const files = (await (0, fs_1.readDir)(restorePath)).filter((f) => (0, string_1.endsWith)(f, suffixes));
159
+ // Database check
160
+ if (files.some((f) => f.endsWith(suffix.database)) &&
161
+ !(await sql.isDatabaseEmpty(database.name)))
162
+ throw new AppError_1.AppError(`Target database is not empty: ${database.name}`);
163
+ // Table check
164
+ const restoreTables = [
165
+ ...new Set(...files
166
+ .filter((f) => (0, string_1.endsWith)(f, [suffix.table, suffix.tableSchema, suffix.tableData]))
167
+ .map((f) => f.split(".")[0])),
168
+ ];
169
+ const serverTables = await sql.fetchTableNames(database.name);
170
+ const errorTables = restoreTables.filter((v) => serverTables.includes(v));
171
+ if (errorTables.length)
172
+ throw new AppError_1.AppError(`Target table already exists: ${errorTables.join(", ")}`);
173
+ // Data check
174
+ const dataFiles = files.filter((f) => f.endsWith(suffix.tableData));
175
+ const sharedDir = dataFiles.length
176
+ ? await sql.initSharedDir(this.config.csvSharedPath)
177
+ : undefined;
178
+ await sql.createDatabase(database);
179
+ if (data.options.verbose)
180
+ (0, cli_1.logExec)("readdir", [restorePath]);
181
+ let current = 0;
182
+ for (const file of files.filter((f) => !f.endsWith(suffix.tableData))) {
183
+ const path = (0, path_1.join)(restorePath, file);
184
+ data.onProgress({
185
+ relative: {
186
+ description: "Importing",
187
+ payload: file,
138
188
  },
139
- stderr: {
140
- toExitCode: true,
189
+ absolute: {
190
+ total: files.length,
191
+ current: current,
192
+ percent: (0, math_1.progressPercent)(files.length, current),
141
193
  },
142
- }),
143
- ]);
144
- }
145
- async onImport(path, database) {
146
- await (0, process_1.exec)("mysql", await this.buildConnectionArgs(database), null, {
147
- pipe: {
148
- stream: (0, fs_3.createReadStream)(path),
149
- onReadProgress: (data) => {
150
- if (this.verbose)
151
- (0, process_1.logExecStdout)({
152
- data: JSON.stringify(data),
153
- colorize: true,
154
- stderr: true,
155
- lineSalt: true,
156
- });
194
+ });
195
+ await sql.importFile(path, database.name);
196
+ current++;
197
+ }
198
+ for (const file of dataFiles) {
199
+ const filePath = (0, path_1.join)(restorePath, file);
200
+ const tableName = file.slice(0, suffix.tableData.length * -1);
201
+ data.onProgress({
202
+ relative: {
203
+ description: "Importing",
204
+ payload: file,
157
205
  },
158
- },
159
- log: this.verbose,
160
- stderr: {
161
- toExitCode: true,
162
- },
163
- });
206
+ absolute: {
207
+ total: files.length,
208
+ current: current,
209
+ percent: (0, math_1.progressPercent)(files.length, current),
210
+ },
211
+ });
212
+ const sharedFilePath = (0, path_1.join)(sharedDir, `tmp-dtt-restore-${data.snapshot.id.slice(0, 8)}-${tableName}.data.csv`);
213
+ try {
214
+ await (0, promises_1.rename)(filePath, sharedFilePath);
215
+ await sql.importCsvFile(sharedFilePath, database.name, tableName);
216
+ }
217
+ finally {
218
+ await (0, promises_1.rm)(sharedFilePath);
219
+ }
220
+ current++;
221
+ }
164
222
  }
165
223
  }
166
224
  exports.MysqlDumpTask = MysqlDumpTask;
@@ -1,6 +1,6 @@
1
1
  import { exec } from "../utils/process";
2
2
  import { BackupDataType, RestoreDataType, TaskAbstract } from "./TaskAbstract";
3
- import { JSONSchema7 } from "json-schema";
3
+ import { JSONSchema7, JSONSchema7Definition } from "json-schema";
4
4
  export type TargetDatabaseType = {
5
5
  name: string;
6
6
  charset?: string;
@@ -14,13 +14,16 @@ export type SqlDumpTaskConfigType = {
14
14
  port?: number;
15
15
  database: string;
16
16
  username: string;
17
+ /**
18
+ * @default true
19
+ */
17
20
  storedPrograms?: boolean;
18
21
  targetDatabase?: TargetDatabaseType;
19
22
  includeTables?: string[];
20
23
  excludeTables?: string[];
21
24
  oneFileByTable?: boolean;
22
25
  };
23
- export declare const sqlDumpTaskDefinition: JSONSchema7;
26
+ export declare const sqlDumpTaskDefinition: (props?: Record<string, JSONSchema7Definition>) => JSONSchema7;
24
27
  export declare abstract class SqlDumpTaskAbstract<TConfig extends SqlDumpTaskConfigType> extends TaskAbstract<TConfig> {
25
28
  protected verbose?: boolean;
26
29
  fetchPassword(): Promise<string | null>;
@@ -12,11 +12,12 @@ const assert_1 = require("assert");
12
12
  const promises_1 = require("fs/promises");
13
13
  const micromatch_1 = require("micromatch");
14
14
  const path_1 = require("path");
15
- exports.sqlDumpTaskDefinition = {
15
+ const sqlDumpTaskDefinition = (props = {}) => ({
16
16
  type: "object",
17
17
  required: ["password", "hostname", "username", "database"],
18
18
  additionalProperties: false,
19
19
  properties: {
20
+ ...props,
20
21
  password: {
21
22
  anyOf: [
22
23
  {
@@ -50,7 +51,8 @@ exports.sqlDumpTaskDefinition = {
50
51
  excludeTables: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.stringListUtil),
51
52
  oneFileByTable: { type: "boolean" },
52
53
  },
53
- };
54
+ });
55
+ exports.sqlDumpTaskDefinition = sqlDumpTaskDefinition;
54
56
  function serializeSqlFile(input) {
55
57
  if (input.database && input.table) {
56
58
  return `${input.database}.${input.table}.table.sql`;
@@ -880,11 +880,85 @@
880
880
  }
881
881
  },
882
882
  "mysql-dump-task": {
883
- "allOf": [
884
- {
885
- "$ref": "#/definitions/sqldump-task"
883
+ "type": "object",
884
+ "required": [
885
+ "password",
886
+ "hostname",
887
+ "username",
888
+ "database"
889
+ ],
890
+ "additionalProperties": false,
891
+ "properties": {
892
+ "dataFormat": {
893
+ "enum": [
894
+ "csv",
895
+ "sql"
896
+ ]
897
+ },
898
+ "csvSharedPath": {
899
+ "type": "string"
900
+ },
901
+ "password": {
902
+ "anyOf": [
903
+ {
904
+ "type": "string"
905
+ },
906
+ {
907
+ "type": "object",
908
+ "additionalProperties": false,
909
+ "required": [
910
+ "path"
911
+ ],
912
+ "properties": {
913
+ "path": {
914
+ "type": "string"
915
+ }
916
+ }
917
+ }
918
+ ]
919
+ },
920
+ "hostname": {
921
+ "type": "string"
922
+ },
923
+ "port": {
924
+ "type": "integer"
925
+ },
926
+ "username": {
927
+ "type": "string"
928
+ },
929
+ "database": {
930
+ "type": "string"
931
+ },
932
+ "targetDatabase": {
933
+ "type": "object",
934
+ "required": [
935
+ "name"
936
+ ],
937
+ "properties": {
938
+ "name": {
939
+ "type": "string"
940
+ },
941
+ "charset": {
942
+ "type": "string"
943
+ },
944
+ "collate": {
945
+ "type": "string"
946
+ }
947
+ }
948
+ },
949
+ "storedPrograms": {
950
+ "type": "boolean"
951
+ },
952
+ "includeTables": {
953
+ "$ref": "#/definitions/stringlist-util"
954
+ },
955
+ "excludeTables": {
956
+ "$ref": "#/definitions/stringlist-util"
957
+ },
958
+ "oneFileByTable": {
959
+ "type": "boolean"
886
960
  }
887
- ]
961
+ }
888
962
  },
889
963
  "postgresql-dump-task": {
890
964
  "allOf": [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datatruck/cli",
3
- "version": "0.21.1",
3
+ "version": "0.22.0",
4
4
  "dependencies": {
5
5
  "ajv": "^8.12.0",
6
6
  "async": "^3.2.4",
package/utils/cli.js CHANGED
@@ -65,7 +65,9 @@ function logExec(command, argv = [], env, logToStderr) {
65
65
  .join(" ")
66
66
  : "";
67
67
  const text = `+ ${envText ? envText + " " : ""}${chalk_1.default.yellow(`${command} ${argv.join(" ")}`)}`;
68
- logToStderr ? process.stderr.write(`${text}\n`) : console.info(text);
68
+ logToStderr /* && process.env.VITEST !== "true"*/
69
+ ? process.stderr.write(`${text}\n`)
70
+ : console.info(text);
69
71
  }
70
72
  exports.logExec = logExec;
71
73
  function resultColumn(error, state) {
@@ -15,7 +15,7 @@ type ResolvePackagePathParamsType = ResolvePackageParamsType & {
15
15
  path: string | undefined;
16
16
  };
17
17
  export declare function resolvePackagePath(value: string, params: ResolvePackagePathParamsType): string;
18
- type ResolveDatabaseNameParamsType = ResolvePackageParamsType & {
18
+ export type ResolveDatabaseNameParamsType = ResolvePackageParamsType & {
19
19
  packageName: string;
20
20
  database: string | undefined;
21
21
  };
package/utils/fs.d.ts CHANGED
@@ -119,4 +119,5 @@ export declare function createWriteStreamPool(options: {
119
119
  end(): Promise<void>;
120
120
  };
121
121
  export declare function countFileLines(path: string): Promise<number>;
122
+ export declare function fetchData<T>(input: T, onPath?: (input: Exclude<T, string>) => string | undefined): Promise<string | null>;
122
123
  export {};
package/utils/fs.js CHANGED
@@ -3,7 +3,7 @@ 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.countFileLines = exports.createWriteStreamPool = exports.createFileScanner = exports.cpy = exports.isNotFoundError = exports.updateFileStats = exports.copyFileWithStreams = exports.waitForClose = exports.writeGitIgnoreList = exports.fastglobToGitIgnore = exports.forEachFile = exports.readDir = exports.readPartialFile = exports.mkTmpDir = exports.fastFolderSizeAsync = exports.tmpDir = exports.rmTmpDir = exports.isTmpDir = exports.sessionTmpDir = exports.parentTmpDir = exports.findFile = exports.parsePackageFile = exports.parseFile = exports.parseFileExtensions = exports.writeJSONFile = exports.existsFile = exports.existsDir = exports.safeStat = exports.ensureEmptyDir = exports.mkdirIfNotExists = exports.isLocalDir = exports.isEmptyDir = exports.isWSLSystem = void 0;
6
+ exports.fetchData = exports.countFileLines = exports.createWriteStreamPool = exports.createFileScanner = exports.cpy = exports.isNotFoundError = exports.updateFileStats = exports.copyFileWithStreams = exports.waitForClose = exports.writeGitIgnoreList = exports.fastglobToGitIgnore = exports.forEachFile = exports.readDir = exports.readPartialFile = exports.mkTmpDir = exports.fastFolderSizeAsync = exports.tmpDir = exports.rmTmpDir = exports.isTmpDir = exports.sessionTmpDir = exports.parentTmpDir = exports.findFile = exports.parsePackageFile = exports.parseFile = exports.parseFileExtensions = exports.writeJSONFile = exports.existsFile = exports.existsDir = exports.safeStat = exports.ensureEmptyDir = exports.mkdirIfNotExists = exports.isLocalDir = exports.isEmptyDir = exports.isWSLSystem = void 0;
7
7
  const globalData_1 = __importDefault(require("../globalData"));
8
8
  const math_1 = require("./math");
9
9
  const path_1 = require("./path");
@@ -43,9 +43,8 @@ function isLocalDir(path) {
43
43
  }
44
44
  exports.isLocalDir = isLocalDir;
45
45
  async function mkdirIfNotExists(path) {
46
- await (0, promises_1.mkdir)(path, {
47
- recursive: true,
48
- });
46
+ if (!(await existsDir(path)))
47
+ await (0, promises_1.mkdir)(path, { recursive: true });
49
48
  return path;
50
49
  }
51
50
  exports.mkdirIfNotExists = mkdirIfNotExists;
@@ -536,3 +535,12 @@ function countFileLines(path) {
536
535
  });
537
536
  }
538
537
  exports.countFileLines = countFileLines;
538
+ async function fetchData(input, onPath) {
539
+ if (typeof input === "string")
540
+ return input;
541
+ const path = onPath?.(input);
542
+ if (typeof path === "string")
543
+ return (await (0, promises_1.readFile)(path)).toString();
544
+ return null;
545
+ }
546
+ exports.fetchData = fetchData;
@@ -0,0 +1,39 @@
1
+ export type MysqlCliOptions = {
2
+ password: string | {
3
+ path: string;
4
+ };
5
+ hostname: string;
6
+ port?: number;
7
+ username: string;
8
+ verbose?: boolean;
9
+ };
10
+ export declare function createMysqlCli(options: MysqlCliOptions): {
11
+ options: MysqlCliOptions;
12
+ initSharedDir: (sharedDir?: string) => Promise<string>;
13
+ args: () => Promise<string[]>;
14
+ run: (query: string, database?: string) => Promise<import("./process").ExecResultType>;
15
+ fetchAll: (query: string, database?: string) => Promise<string[][]>;
16
+ dump: (input: {
17
+ output: string;
18
+ database: string;
19
+ items?: string[] | undefined;
20
+ onlyStoredPrograms?: boolean | undefined;
21
+ onProgress?: ((data: {
22
+ totalBytes: number;
23
+ }) => void) | undefined;
24
+ }) => Promise<[void, import("./process").ExecResultType]>;
25
+ fetchTableNames: (database: string, include?: string[], exclude?: string[]) => Promise<string[]>;
26
+ importFile: (path: string, database: string) => Promise<import("./process").ExecResultType>;
27
+ isDatabaseEmpty: (database: string) => Promise<boolean>;
28
+ createDatabase: (database: {
29
+ name: string;
30
+ charset?: string;
31
+ }) => Promise<void>;
32
+ csvDump: (input: {
33
+ database: string;
34
+ sharedPath: string;
35
+ items?: string[];
36
+ }) => Promise<void>;
37
+ importCsvFile: (path: string, database: string, table: string) => Promise<import("./process").ExecResultType>;
38
+ fetchVariable: (name: string) => Promise<string | undefined>;
39
+ };
package/utils/mysql.js ADDED
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createMysqlCli = void 0;
4
+ const AppError_1 = require("../Error/AppError");
5
+ const fs_1 = require("./fs");
6
+ const process_1 = require("./process");
7
+ const string_1 = require("./string");
8
+ const crypto_1 = require("crypto");
9
+ const fs_2 = require("fs");
10
+ const promises_1 = require("fs/promises");
11
+ const os_1 = require("os");
12
+ const path_1 = require("path");
13
+ function createMysqlCli(options) {
14
+ async function args() {
15
+ const password = await (0, fs_1.fetchData)(options.password, (p) => p.path);
16
+ return [
17
+ `--host=${options.hostname}`,
18
+ ...(options.port ? [`--port=${options.port}`] : []),
19
+ `--user=${options.username}`,
20
+ `--password=${password ?? ""}`,
21
+ ];
22
+ }
23
+ async function run(query, database) {
24
+ return await (0, process_1.exec)("mysql", [
25
+ ...(await args()),
26
+ ...(database ? [database] : []),
27
+ "-e",
28
+ query.replace(/\s{1,}/g, " "),
29
+ "-N",
30
+ "--silent",
31
+ ], undefined, {
32
+ log: options.verbose,
33
+ stderr: { toExitCode: true },
34
+ stdout: { save: true },
35
+ });
36
+ }
37
+ async function fetchAll(query, database) {
38
+ return (0, string_1.splitLines)((await run(query, database)).stdout).map((line) => line.split("\t"));
39
+ }
40
+ async function fetchTableNames(database, include, exclude) {
41
+ return (await fetchAll(`
42
+ SELECT
43
+ table_name
44
+ FROM
45
+ information_schema.tables
46
+ WHERE
47
+ table_schema = '${database}'
48
+ ORDER BY
49
+ table_name
50
+ `))
51
+ .map((r) => r[0])
52
+ .filter((0, string_1.createMatchFilter)(include, exclude));
53
+ }
54
+ async function dump(input) {
55
+ const stream = (0, fs_2.createWriteStream)(input.output);
56
+ return await Promise.all([
57
+ new Promise((resolve, reject) => {
58
+ stream.on("close", resolve);
59
+ stream.on("error", reject);
60
+ }),
61
+ await (0, process_1.exec)("mysqldump", [
62
+ ...(await args()),
63
+ input.database,
64
+ "--lock-tables=false",
65
+ "--skip-add-drop-table=false",
66
+ ...(input.onlyStoredPrograms
67
+ ? [
68
+ "--routines",
69
+ "--events",
70
+ "--skip-triggers",
71
+ "--no-create-info",
72
+ "--no-data",
73
+ "--no-create-db",
74
+ "--skip-opt",
75
+ ]
76
+ : []),
77
+ ...(input.items || []),
78
+ ], null, {
79
+ stderr: { toExitCode: true },
80
+ pipe: {
81
+ stream,
82
+ onWriteProgress: input.onProgress,
83
+ },
84
+ log: {
85
+ exec: options.verbose,
86
+ stderr: options.verbose,
87
+ allToStderr: true,
88
+ },
89
+ }),
90
+ ]);
91
+ }
92
+ async function csvDump(input) {
93
+ await (0, process_1.exec)("mysqldump", [
94
+ ...(await args()),
95
+ input.database,
96
+ "--lock-tables=false",
97
+ "--skip-add-drop-table=false",
98
+ "-T",
99
+ input.sharedPath,
100
+ ...(input.items || []),
101
+ ], null, {
102
+ stderr: { toExitCode: true },
103
+ log: {
104
+ exec: options.verbose,
105
+ stderr: options.verbose,
106
+ allToStderr: true,
107
+ },
108
+ });
109
+ }
110
+ async function importFile(path, database) {
111
+ return await (0, process_1.exec)("mysql", [
112
+ `--init-command=SET ${[
113
+ "autocommit=0",
114
+ "unique_checks=0",
115
+ "foreign_key_checks=0",
116
+ ].join(",")};`,
117
+ ...(await args()),
118
+ database,
119
+ ], null, {
120
+ pipe: {
121
+ stream: (0, fs_2.createReadStream)(path),
122
+ onReadProgress: (data) => {
123
+ if (options.verbose)
124
+ (0, process_1.logExecStdout)({
125
+ data: JSON.stringify(data),
126
+ colorize: true,
127
+ stderr: true,
128
+ lineSalt: true,
129
+ });
130
+ },
131
+ },
132
+ stderr: { toExitCode: true },
133
+ log: options.verbose,
134
+ });
135
+ }
136
+ async function importCsvFile(path, database, table) {
137
+ return run(`
138
+ LOAD DATA LOCAL INFILE '${path.replaceAll("\\", "/")}'
139
+ INTO TABLE ${table}
140
+ FIELDS TERMINATED BY ','
141
+ ENCLOSED BY '"'
142
+ LINES TERMINATED BY '\\n'`, database);
143
+ }
144
+ async function isDatabaseEmpty(database) {
145
+ const [total] = await fetchAll(`
146
+ SELECT
147
+ COUNT(*) AS total
148
+ FROM
149
+ information_schema.tables
150
+ WHERE
151
+ table_schema = '${database}'
152
+ `);
153
+ return Number(total) ? false : true;
154
+ }
155
+ async function createDatabase(database) {
156
+ await run(`
157
+ CREATE DATABASE IF NOT EXISTS \`${database.name}\`
158
+ CHARACTER SET ${database.charset ?? "utf8"}
159
+ COLLATE ${database.charset ?? "utf8_general_ci"}
160
+ `);
161
+ }
162
+ async function fetchVariable(name) {
163
+ const stdout = (0, string_1.undefIfEmpty)((await run(`SHOW VARIABLES LIKE "${name}"`)).stdout.trim());
164
+ return stdout ? (0, string_1.undefIfEmpty)(stdout.slice(name.length).trim()) : undefined;
165
+ }
166
+ async function initSharedDir(sharedDir) {
167
+ const secure_file_priv = await fetchVariable("secure_file_priv");
168
+ if (secure_file_priv?.toUpperCase() === "NULL")
169
+ throw new AppError_1.AppError("'secure_file_priv' is null in MySQL Server");
170
+ const dir = sharedDir ??
171
+ secure_file_priv ??
172
+ (await fetchVariable("tmpdir")) ??
173
+ (0, os_1.tmpdir)();
174
+ await checkSharedDir(dir);
175
+ return dir;
176
+ }
177
+ async function checkSharedDir(dir) {
178
+ const id = (0, crypto_1.randomBytes)(8).toString("hex");
179
+ const outFile = (0, path_1.join)(dir, `dtt_test_${id}`);
180
+ const outFileVar = JSON.stringify(outFile.replaceAll("\\", "/"));
181
+ try {
182
+ await (0, fs_1.mkdirIfNotExists)(dir);
183
+ await run(`SELECT 1 INTO OUTFILE ${outFileVar}`);
184
+ const exists = await (0, fs_1.existsFile)(outFile);
185
+ if (!exists)
186
+ throw new AppError_1.AppError(`MySQL shared dir is not reached: ${dir}`);
187
+ }
188
+ finally {
189
+ try {
190
+ await (0, promises_1.rm)(outFile);
191
+ }
192
+ catch (e) { }
193
+ }
194
+ }
195
+ return {
196
+ options,
197
+ initSharedDir,
198
+ args,
199
+ run,
200
+ fetchAll,
201
+ dump,
202
+ fetchTableNames,
203
+ importFile,
204
+ isDatabaseEmpty,
205
+ createDatabase,
206
+ csvDump,
207
+ importCsvFile,
208
+ fetchVariable,
209
+ };
210
+ }
211
+ exports.createMysqlCli = createMysqlCli;
package/utils/string.d.ts CHANGED
@@ -13,6 +13,10 @@ export type UriType = {
13
13
  export declare function formatUri(input: UriType, hidePassword?: boolean): string;
14
14
  export declare function formatSeconds(seconds: number): string;
15
15
  export declare function makePathPatterns(values: string[] | undefined): string[] | undefined;
16
- export declare function checkPath(path: string, include: string[], exclude?: string[]): boolean;
16
+ export declare function match(path: string, include?: string[], exclude?: string[]): boolean;
17
+ export declare function endsWith(input: string, patterns: string[]): boolean;
18
+ export declare function createMatchFilter(include?: string[], exclude?: string[]): (input: string) => boolean;
17
19
  export declare function checkMatch(subject: string | undefined, patterns: string[]): boolean;
18
20
  export declare function formatDateTime(datetime: string): string;
21
+ export declare function splitLines(input: string, satinize?: boolean): string[];
22
+ export declare function undefIfEmpty(input: string): string | undefined;
package/utils/string.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatDateTime = exports.checkMatch = exports.checkPath = exports.makePathPatterns = exports.formatSeconds = exports.formatUri = exports.parseStringList = exports.render = exports.snakeCase = exports.serialize = void 0;
3
+ exports.undefIfEmpty = exports.splitLines = exports.formatDateTime = exports.checkMatch = exports.createMatchFilter = exports.endsWith = exports.match = exports.makePathPatterns = exports.formatSeconds = exports.formatUri = exports.parseStringList = exports.render = exports.snakeCase = exports.serialize = void 0;
4
4
  const AppError_1 = require("../Error/AppError");
5
5
  const micromatch_1 = require("micromatch");
6
6
  function serialize(message, data) {
@@ -91,13 +91,19 @@ function makePathPatterns(values) {
91
91
  });
92
92
  }
93
93
  exports.makePathPatterns = makePathPatterns;
94
- function checkPath(path, include, exclude) {
95
- return ((0, micromatch_1.isMatch)(path, include, {
96
- dot: true,
97
- }) &&
94
+ function match(path, include, exclude) {
95
+ return ((!include || (0, micromatch_1.isMatch)(path, include, { dot: true })) &&
98
96
  (!exclude || !(0, micromatch_1.isMatch)(path, exclude, { dot: true })));
99
97
  }
100
- exports.checkPath = checkPath;
98
+ exports.match = match;
99
+ function endsWith(input, patterns) {
100
+ return patterns.some((pattern) => input.endsWith(pattern));
101
+ }
102
+ exports.endsWith = endsWith;
103
+ function createMatchFilter(include, exclude) {
104
+ return (input) => match(input, include, exclude);
105
+ }
106
+ exports.createMatchFilter = createMatchFilter;
101
107
  function checkMatch(subject, patterns) {
102
108
  if (!subject?.length)
103
109
  subject = "<empty>";
@@ -114,3 +120,19 @@ function formatDateTime(datetime) {
114
120
  return result;
115
121
  }
116
122
  exports.formatDateTime = formatDateTime;
123
+ function splitLines(input, satinize = true) {
124
+ const lines = input.split(/\r?\n/);
125
+ return satinize
126
+ ? input.split(/\r?\n/).reduce((result, value) => {
127
+ value = value.trim();
128
+ if (value.length)
129
+ result.push(value);
130
+ return result;
131
+ }, [])
132
+ : lines;
133
+ }
134
+ exports.splitLines = splitLines;
135
+ function undefIfEmpty(input) {
136
+ return input.length ? input : undefined;
137
+ }
138
+ exports.undefIfEmpty = undefIfEmpty;