@datatruck/cli 0.1.0 → 0.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @datatruck/cli
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`120460c`](https://github.com/swordev/datatruck/commit/120460c8824cef4184e43f571a4cc0798b899b66) Thanks [@juanrgm](https://github.com/juanrgm)! - Enable `include` option in restic repository
8
+
9
+ ### Patch Changes
10
+
11
+ - [`e30ede3`](https://github.com/swordev/datatruck/commit/e30ede371bc7ab3fc1cd47758fdac7a28e8e2705) Thanks [@juanrgm](https://github.com/juanrgm)! - Resolve `RESTIC_PASSWORD_FILE` path
12
+
13
+ * [`8539d28`](https://github.com/swordev/datatruck/commit/8539d285b2c51d700aa811cd772d573fa0d613eb) Thanks [@juanrgm](https://github.com/juanrgm)! - Allow empty backup in restic repository
14
+
3
15
  ## 0.1.0
4
16
 
5
17
  ### Minor Changes
@@ -1,13 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.ResticRepository = exports.resticPackageRepositoryDefinition = exports.resticRepositoryDefinition = exports.resticRepositoryName = void 0;
4
7
  const AppError_1 = require("../Error/AppError");
5
8
  const ResticUtil_1 = require("../util/ResticUtil");
9
+ const cli_util_1 = require("../util/cli-util");
6
10
  const paths_util_1 = require("../util/datatruck/paths-util");
7
11
  const fs_util_1 = require("../util/fs-util");
8
12
  const string_util_1 = require("../util/string-util");
9
13
  const RepositoryAbstract_1 = require("./RepositoryAbstract");
10
14
  const assert_1 = require("assert");
15
+ const fast_glob_1 = __importDefault(require("fast-glob"));
11
16
  const micromatch_1 = require("micromatch");
12
17
  const path_1 = require("path");
13
18
  exports.resticRepositoryName = "restic";
@@ -54,7 +59,7 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
54
59
  if (this.env)
55
60
  return this.env;
56
61
  return (this.env = {
57
- RESTIC_PASSWORD_FILE: this.config.passwordFile,
62
+ RESTIC_PASSWORD_FILE: (0, path_1.resolve)(this.config.passwordFile),
58
63
  RESTIC_REPOSITORY: await ResticUtil_1.ResticUtil.formatRepository(this.config.repository),
59
64
  });
60
65
  }
@@ -150,14 +155,28 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
150
155
  const pkg = data.package;
151
156
  const sourcePath = data.targetPath ?? data.package.path;
152
157
  (0, assert_1.ok)(sourcePath);
153
- const include = (await (0, paths_util_1.parsePaths)(pkg.include ?? [], {
158
+ const include = await (0, paths_util_1.parsePaths)(pkg.include ?? ["**"], {
154
159
  cwd: sourcePath,
155
160
  verbose: data.options.verbose,
156
- })).map(path_1.normalize);
157
- // https://github.com/restic/restic/issues/233
158
- // https://github.com/restic/restic/pull/2311
159
- if (include.length)
160
- throw new AppError_1.AppError(`Include is not supported`);
161
+ });
162
+ const exclude = pkg.exclude
163
+ ? await (0, paths_util_1.parsePaths)(pkg.exclude, {
164
+ cwd: sourcePath,
165
+ verbose: data.options.verbose,
166
+ })
167
+ : undefined;
168
+ const stream = fast_glob_1.default.stream(include, {
169
+ cwd: sourcePath,
170
+ ignore: exclude,
171
+ dot: true,
172
+ onlyFiles: true,
173
+ markDirectories: true,
174
+ });
175
+ if (data.options.verbose)
176
+ (0, cli_util_1.logExec)(`Writing paths lists`);
177
+ const gitignorePath = await (0, fs_util_1.writeGitIgnoreList)({
178
+ paths: stream,
179
+ });
161
180
  if (data.options.tags?.some((tag) => tag.startsWith(ResticRepository.refPrefix)))
162
181
  throw new AppError_1.AppError(`Tag prefix is not allowed`);
163
182
  const packageTag = ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.PACKAGE, data.package.name);
@@ -170,15 +189,13 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
170
189
  await restic.backup({
171
190
  cwd: sourcePath,
172
191
  paths: ["."],
192
+ allowEmptySnapshot: true,
193
+ excludeFile: [gitignorePath],
173
194
  parent: lastSnapshot?.id,
174
195
  // https://github.com/restic/restic/pull/3200
175
196
  ...((await restic.checkBackupSetPathSupport()) && {
176
197
  setPaths: [`/datatruck/${data.package.name}`],
177
198
  }),
178
- exclude: (await (0, paths_util_1.parsePaths)(pkg.exclude ?? [], {
179
- cwd: sourcePath,
180
- verbose: data.options.verbose,
181
- })).map(path_1.normalize),
182
199
  tags: [
183
200
  ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.ID, data.snapshot.id),
184
201
  ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.SHORT_ID, data.snapshot.id.slice(0, 8)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datatruck/cli",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "dependencies": {
5
5
  "ajv": "^8.11.0",
6
6
  "chalk": "^4.1.2",
@@ -1,4 +1,4 @@
1
- import { ExecSettingsInterface } from "./process-util";
1
+ import { ExecResultType, ExecSettingsInterface } from "./process-util";
2
2
  import { UriType } from "./string-util";
3
3
  export declare type RepositoryType = {
4
4
  name?: string;
@@ -43,7 +43,7 @@ export declare class ResticUtil {
43
43
  static formatRepository(input: RepositoryType, hidePassword?: boolean): Promise<string>;
44
44
  exec(args: string[], settings?: ExecSettingsInterface, options?: {
45
45
  cwd?: string;
46
- }): Promise<import("./process-util").ExecResultType>;
46
+ }): Promise<ExecResultType>;
47
47
  checkRepository(): Promise<boolean>;
48
48
  forget(options: {
49
49
  snapshotId?: string;
@@ -81,12 +81,14 @@ export declare class ResticUtil {
81
81
  paths: string[];
82
82
  setPaths?: string[];
83
83
  exclude?: string[];
84
+ excludeFile?: string[];
84
85
  parent?: string;
86
+ allowEmptySnapshot?: boolean;
85
87
  onStream?: (data: BackupStreamType) => Promise<void>;
86
- }): Promise<import("./process-util").ExecResultType>;
88
+ }): Promise<ExecResultType>;
87
89
  restore(options: {
88
90
  id: string;
89
91
  target: string;
90
92
  onStream?: (data: BackupStreamType) => Promise<void>;
91
- }): Promise<import("./process-util").ExecResultType>;
93
+ }): Promise<ExecResultType>;
92
94
  }
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ResticUtil = void 0;
4
+ const fs_util_1 = require("./fs-util");
4
5
  const process_util_1 = require("./process-util");
5
6
  const string_util_1 = require("./string-util");
6
7
  const fs_extra_1 = require("fs-extra");
8
+ const promises_1 = require("fs/promises");
7
9
  const path_1 = require("path");
8
10
  class ResticUtil {
9
11
  constructor(options) {
@@ -13,7 +15,7 @@ class ResticUtil {
13
15
  if (input.backend === "local") {
14
16
  if (typeof input.path !== "string")
15
17
  throw new Error(`Invalid path at "${input.name}" repository: ${input.path}`);
16
- return (0, path_1.normalize)(input.path);
18
+ return (0, path_1.resolve)(input.path);
17
19
  }
18
20
  if (input.passwordFile)
19
21
  input = {
@@ -36,7 +38,7 @@ class ResticUtil {
36
38
  stderr: true,
37
39
  colorize: true,
38
40
  allToStderr: true,
39
- envNames: ["RESTIC_REPOSITORY"],
41
+ envNames: ["RESTIC_REPOSITORY", "RESTIC_PASSWORD_FILE"],
40
42
  }
41
43
  : {},
42
44
  ...(settings ?? {}),
@@ -99,10 +101,11 @@ class ResticUtil {
99
101
  return result.stderr.includes("flag needs an argument");
100
102
  }
101
103
  async backup(options) {
102
- return await this.exec([
104
+ const exec = async () => await this.exec([
103
105
  "backup",
104
106
  "--json",
105
107
  ...(options.exclude?.flatMap((v) => ["-e", v]) ?? []),
108
+ ...(options.excludeFile?.flatMap((v) => ["--exclude-file", v]) ?? []),
106
109
  ...(options.tags?.flatMap((v) => ["--tag", v]) ?? []),
107
110
  ...(options.setPaths?.flatMap((v) => ["--set-path", v]) ?? []),
108
111
  ...(options.parent ? ["--parent", options.parent] : []),
@@ -123,6 +126,25 @@ class ResticUtil {
123
126
  }, {
124
127
  cwd: options.cwd,
125
128
  });
129
+ try {
130
+ return await exec();
131
+ }
132
+ catch (error) {
133
+ if (options.allowEmptySnapshot &&
134
+ error.message.includes("unable to save snapshot: snapshot is empty")) {
135
+ const emptyPath = await (0, fs_util_1.mkTmpDir)("empty");
136
+ await (0, promises_1.writeFile)(`${emptyPath}/.empty`, "");
137
+ return await this.backup({
138
+ ...options,
139
+ cwd: emptyPath,
140
+ allowEmptySnapshot: false,
141
+ paths: ["."],
142
+ exclude: [],
143
+ excludeFile: [],
144
+ });
145
+ }
146
+ throw error;
147
+ }
126
148
  }
127
149
  async restore(options) {
128
150
  return await this.exec(["restore", "--json", options.id, "--target", options.target], {
package/util/fs-util.d.ts CHANGED
@@ -23,6 +23,9 @@ export declare function readPartialFile(path: string, positions: [number, number
23
23
  export declare function checkFile(path: string): Promise<boolean>;
24
24
  export declare function checkDir(path: string): Promise<boolean>;
25
25
  export declare function forEachFile(dirPath: string, cb: (path: string, dir: boolean) => void, includeDir?: boolean): Promise<void>;
26
+ export declare function writeGitIgnoreList(options: {
27
+ paths: NodeJS.ReadableStream | string[];
28
+ }): Promise<string>;
26
29
  export declare function writePathLists(options: {
27
30
  paths: NodeJS.ReadableStream | string[];
28
31
  packs?: {
package/util/fs-util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
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;
3
+ exports.writePathLists = exports.writeGitIgnoreList = 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
4
  const path_util_1 = require("./path-util");
5
5
  const crypto_1 = require("crypto");
6
6
  const fs_1 = require("fs");
@@ -190,6 +190,40 @@ async function forEachFile(dirPath, cb, includeDir) {
190
190
  }
191
191
  }
192
192
  exports.forEachFile = forEachFile;
193
+ async function writeGitIgnoreList(options) {
194
+ const tempDir = await mkTmpDir("gitignore-list");
195
+ const path = (0, path_1.join)(tempDir, `.gitignore`);
196
+ const stream = (0, fs_2.createWriteStream)(path);
197
+ const dirs = new Set();
198
+ stream.write("*\n");
199
+ for await (const value of options.paths) {
200
+ const dir = (0, path_1.dirname)(value.toString());
201
+ if (dir !== ".") {
202
+ let parentPath;
203
+ for (const value of dir.split("/")) {
204
+ if (!parentPath) {
205
+ parentPath = `!${value}`;
206
+ }
207
+ else {
208
+ parentPath += `/${value}`;
209
+ }
210
+ if (!dirs.has(parentPath)) {
211
+ stream.write(`${parentPath}\n`);
212
+ stream.write(`${parentPath.slice(1)}/*\n`);
213
+ dirs.add(parentPath);
214
+ }
215
+ }
216
+ }
217
+ stream.write(`!${value}\n`);
218
+ }
219
+ await new Promise((resolve, reject) => {
220
+ stream.close();
221
+ stream.on("close", resolve);
222
+ stream.on("error", reject);
223
+ });
224
+ return path;
225
+ }
226
+ exports.writeGitIgnoreList = writeGitIgnoreList;
193
227
  async function writePathLists(options) {
194
228
  const tempDir = await mkTmpDir("path-lists");
195
229
  const includedPaths = [];