generated from boneskull/boneskull-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
113 lines (101 loc) · 2.8 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const {inspect} = require('util');
/**
* Represents the result of a file-copy operation. Do not use directly; use {@linkcode createFileCopyResult} instead.
*/
class FileCopyResult {
/**
* @param {string} from - Source filepath
* @param {string} to - Destination filepath
* @param {FileCopyResultOpts} [opts] - Options
*/
constructor(from, to, {err, success} = {}) {
this.from = from;
this.to = to;
this.err = err;
this.success = success;
}
toString() {
return this.err
? `Could not synchronize file from ${this.from} to ${this.to}: ${this.err.message}`
: `Synchronized file ${this.from} to ${this.to}`;
}
/**
* Return a clone of this object, but add an Error
* @param {Error} err - Error
*/
withError(err) {
return Object.freeze(new FileCopyResult(this.from, this.to, {err}));
}
/**
* Return a clone of thios object, but mark as successfully copied
*/
withSuccess() {
return Object.freeze(
new FileCopyResult(this.from, this.to, {success: true})
);
}
}
/**
* Represents the result of a `package.json` modification
*/
class PkgChangeResult {
/**
*
* @param {string} pkgPath - Path to destination package.json
* @param {Operation[]} patch - JSON patch
* @param {PackageJson} pkg - Original package.json
* @param {PackageJson} [newPkg] - Updated package.json
*/
constructor(pkgPath, patch, pkg, newPkg) {
this.pkgPath = pkgPath;
this.patch = patch;
this.pkg = pkg;
this.newPkg = newPkg;
}
toString() {
return `${this.pkgPath} - Applied patch: ${inspect(this.patch, {
colors: true,
compact: true,
breakLength: Infinity,
})}`;
}
/**
*
* @param {PackageJson} newPkg
* @returns {Readonly<PkgChangeResult>}
*/
withNewPackage(newPkg) {
return Object.freeze(
new PkgChangeResult(this.pkgPath, [...this.patch], {...this.pkg}, newPkg)
);
}
}
/**
* Creates a {@linkcode FileCopyResult} object.
* @param {string} from
* @param {string} to
* @param {FileCopyResultOpts} [opts]
* @returns {Readonly<FileCopyResult>}
*/
exports.createFileCopyResult = (from, to, {err, success} = {}) =>
Object.freeze(new FileCopyResult(from, to, {err, success}));
/**
* Creates a {@linkcode PkgChangeResult} object.
* @param {string} pkgPath
* @param {Operation[]} patch
* @param {PackageJson} pkg
* @param {PackageJson} [newPkg]
*/
exports.createPkgChangeResult = (pkgPath, patch, pkg, newPkg) =>
Object.freeze(new PkgChangeResult(pkgPath, patch, pkg, newPkg));
/**
* @typedef {import('type-fest').PackageJson} PackageJson
* @typedef {import('rfc6902').Operation} Operation
*/
/**
* @typedef FileCopyResultOpts
* @property {Error} [err]
* @property {boolean} [success]
*/
exports.FileCopyResult = FileCopyResult;
exports.PkgChangeResult = PkgChangeResult;