generated from boneskull/boneskull-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-file.js
250 lines (238 loc) · 6.37 KB
/
sync-file.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const pluralize = require('pluralize');
const {defer, merge, from, iif, of} = require('rxjs');
const fs = require('fs-extra');
const {
catchError,
defaultIfEmpty,
concatMap,
throwIfEmpty,
filter,
reduce,
toArray,
share,
map,
mergeAll,
mergeMap,
mergeWith,
} = require('rxjs/operators');
const path = require('path');
const glob = require('globby');
const debug = require('debug')('sync-monorepo-packages:sync-file');
const {createFileCopyResult} = require('./model');
const {SyncMonorepoPackagesError} = require('./error');
const {
findLernaConfig,
findDirectoriesByGlobs,
findWorkspaces,
} = require('./find-package');
/**
* For dry-run mode, if a file were to be copied, but force is
* false, we should throw.
* @param {FileCopyResult} copyInfo
* @param {boolean} [force]
*/
function dryRunTestFile(copyInfo, force = false) {
return iif(
() => force,
of(copyInfo),
defer(() => from(fs.stat(copyInfo.to))).pipe(
map(() => {
/**
* @type {any}
*/
const err = new Error();
err.code = 'EEXIST';
throw err;
}),
catchError((err) => {
if (err.code === 'ENOENT') {
return of(copyInfo);
}
throw err;
})
)
);
}
/**
* Provide a summary of the file copies made
* @returns {OperatorFunction<Readonly<FileCopyResult>,Summary>}
*/
exports.summarizeFileCopies = () => (copyInfo$) => {
/**
* @returns {OperatorFunction<Readonly<FileCopyResult>,{totalCopies: number, allSources: Set<string>}>}
*/
const summary = () => (copyInfo$) =>
copyInfo$.pipe(
reduce(
({totalCopies, allSources}, {from}) => ({
totalCopies: totalCopies + 1,
allSources: allSources.add(from),
}),
{totalCopies: 0, allSources: new Set()}
),
filter(({totalCopies}) => totalCopies > 0)
);
copyInfo$ = copyInfo$.pipe(share());
const success$ = copyInfo$.pipe(
filter((copyInfo) => Boolean(copyInfo.success)),
summary(),
map(({totalCopies, allSources}) => ({
success: `Copied ${allSources.size} ${pluralize(
'file',
allSources.size
)} to ${totalCopies} ${pluralize('package', totalCopies)}`,
}))
);
const fail$ = copyInfo$.pipe(
filter((copyInfo) => Boolean(copyInfo.err)),
summary(),
map(({totalCopies, allSources}) => ({
fail: `Failed to copy ${allSources.size} ${pluralize(
'file',
allSources.size
)} to ${totalCopies} ${pluralize(
'package',
totalCopies
)}; use --verbose for details`,
}))
);
return merge(success$, fail$).pipe(
defaultIfEmpty(
/**
* @type {Summary}
*/
({noop: 'No files copied.'})
)
);
};
/**
* Synchronize source file(s) to packages
* @param {string[]} [files] - Source file(s)
* @param {Partial<SyncFileOptions>} [opts]
*/
exports.syncFile = (
files = [],
{
packages = [],
dryRun = false,
lerna: lernaJsonPath = '',
force = false,
cwd = process.cwd(),
} = {}
) => {
debug(
'syncFile called with force: %s, packages: %O, files: %O',
force,
packages,
files
);
const file$ = from(files).pipe(
throwIfEmpty(() => new SyncMonorepoPackagesError('No files to sync!')),
mergeMap((file) =>
from(glob(file)).pipe(
mergeAll(),
throwIfEmpty(
() =>
new SyncMonorepoPackagesError(
`Could not find any files matching glob "${file}"`
)
)
)
)
);
const packageDirs$ = iif(
() => Boolean(packages.length),
from(packages),
findLernaConfig({lernaJsonPath}).pipe(
filter(({lernaConfig}) => Boolean(lernaConfig.packages)),
mergeMap(({lernaConfig, lernaRoot: cwd}) =>
findDirectoriesByGlobs(lernaConfig.packages, {cwd})
),
mergeWith(
findWorkspaces(cwd).pipe(
mergeMap((workspaces) => findDirectoriesByGlobs(workspaces, {cwd}))
)
)
)
).pipe(
toArray(),
map((packageDirs) => ({cwd, packageDirs}))
);
return file$.pipe(
mergeMap((srcFilePath) =>
packageDirs$.pipe(
mergeMap(({packageDirs, cwd}) =>
// - we might not be at the package root
// - we don't know where the packages are relative to us
// - we don't know where we are relative to srcFilePath
// - to that end, we need to compute the destination relative to
// `cwd` (the variable) and also relative to our actual cwd.
// - display relative paths to the user for brevity
// (we can change this later)
packageDirs.map((packageDir) =>
createFileCopyResult(
srcFilePath,
path.relative(
process.cwd(),
path.join(
path.resolve(cwd, packageDir),
path.relative(path.resolve(process.cwd(), cwd), srcFilePath)
)
)
)
)
),
concatMap((copyInfo) => {
debug(
'attempting to copy %s to %s (overwrite: %s)',
copyInfo.from,
copyInfo.to,
force
);
return iif(
() => dryRun,
dryRunTestFile(copyInfo, force),
defer(() =>
from(fs.copy(copyInfo.from, copyInfo.to, {overwrite: force}))
).pipe(map(() => copyInfo))
).pipe(
map((copyInfo) => copyInfo.withSuccess()),
catchError((err) => {
if (err.code === 'EEXIST') {
return of(
copyInfo.withError(
new SyncMonorepoPackagesError(
`Refusing to overwrite existing file ${copyInfo.to}; use --force to overwrite`
)
)
);
}
throw err;
})
);
})
)
)
);
};
/**
* @template T,U
* @typedef {import('rxjs').OperatorFunction<T,U>} OperatorFunction
*/
/**
* @typedef {import('./model').FileCopyResult} FileCopyResult
*/
/**
* @typedef Summary
* @property {string} [fail] - Failure message
* @property {string} [success] - Success message
* @property {string} [noop] - No-op message
*/
/**
* @typedef SyncFileOptions
* @property {string} cwd
* @property {string} lerna
* @property {boolean} dryRun
* @property {string[]} packages
* @property {boolean} force
*/