exifreader 4.46.0 → 4.48.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/README.md +100 -34
- package/bin/TagFilterPlugin.js +23 -14
- package/bin/analyze.js +345 -0
- package/bin/build.js +13 -2
- package/bin/cli.js +449 -23
- package/bin/custom-build-marker.js +78 -0
- package/bin/unshare-file.js +29 -0
- package/bin/verify-build.js +147 -0
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/package.json +3 -3
- package/src/canon-tags.js +4 -2
- package/src/composite.js +94 -47
- package/src/exif-reader.js +81 -16
- package/src/file-loaders.js +204 -30
- package/src/image-header-iso-bmff-iloc.js +5 -2
- package/src/image-header-iso-bmff.js +41 -10
- package/src/image-header-jpeg.js +9 -2
- package/src/image-header-png.js +33 -21
- package/src/iptc-tags.js +46 -11
- package/src/load-auto.js +6 -3
- package/src/loadview-pipeline.js +111 -61
- package/src/mpf-tags.js +10 -3
- package/src/pentax-tags.js +49 -23
- package/src/photoshop-tag-names.js +6 -2
- package/src/photoshop-tags.js +15 -22
- package/src/png-text-tags.js +237 -77
- package/src/tag-decoder.js +10 -10
- package/src/tag-filter-config.js +2 -0
- package/src/tag-filter.js +2 -0
- package/src/tag-names-0th-ifd.js +13 -1
- package/src/tags-helpers.js +220 -44
- package/src/tags.js +22 -4
- package/src/types.js +0 -5
- package/src/utils.js +260 -32
- package/src/xml.js +19 -2
- package/src/xmp-element-depth.js +226 -0
- package/src/xmp-namespaces.js +22 -7
- package/src/xmp-tags.js +285 -165
package/bin/cli.js
CHANGED
|
@@ -8,10 +8,13 @@
|
|
|
8
8
|
|
|
9
9
|
const path = require('path');
|
|
10
10
|
const fs = require('fs');
|
|
11
|
+
const {readMarker, markerMatches} = require('./custom-build-marker');
|
|
12
|
+
const {collectImagePaths, loadFullParser, resolveDomParser, analyzeImages, deriveConfig, writeConfigToPackageJson} = require('./analyze');
|
|
13
|
+
const {verifyImages, loadCustomParser} = require('./verify-build');
|
|
11
14
|
|
|
12
15
|
const EXIFREADER_ROOT_DIR = path.join(__dirname, '..');
|
|
13
16
|
|
|
14
|
-
module.exports = {parseArgs, run, checkLocalInstall};
|
|
17
|
+
module.exports = {parseArgs, run, checkLocalInstall, readConfigFile};
|
|
15
18
|
|
|
16
19
|
// build.js is required lazily (only when run) so importing this file in unit
|
|
17
20
|
// tests does not pull its integration-tested build logic into coverage.
|
|
@@ -21,8 +24,14 @@ if (require.main === module) {
|
|
|
21
24
|
env: process.env,
|
|
22
25
|
version: () => require(path.join(EXIFREADER_ROOT_DIR, 'package.json')).version,
|
|
23
26
|
resolveConfig: (directory) => require('./findDependentConfig').forCli(directory),
|
|
27
|
+
readConfigFile,
|
|
28
|
+
readMarker,
|
|
24
29
|
checkLocalInstall,
|
|
25
30
|
build: (options) => require('./build').runBuild(options),
|
|
31
|
+
fs,
|
|
32
|
+
loadFullParser,
|
|
33
|
+
loadCustomParser,
|
|
34
|
+
resolveDomParser,
|
|
26
35
|
log: console.log,
|
|
27
36
|
error: console.error,
|
|
28
37
|
exit: (code) => process.exit(code)
|
|
@@ -45,8 +54,11 @@ function run(argv, deps) {
|
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
if (args.command === 'build') {
|
|
48
|
-
runBuildCommand(deps);
|
|
49
|
-
|
|
57
|
+
return runBuildCommand(args, deps);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (args.command === 'analyze') {
|
|
61
|
+
return runAnalyzeCommand(args, deps);
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
if (args.command) {
|
|
@@ -59,54 +71,447 @@ function run(argv, deps) {
|
|
|
59
71
|
function parseArgs(argv) {
|
|
60
72
|
const args = argv.slice(2);
|
|
61
73
|
return {
|
|
62
|
-
command: args
|
|
74
|
+
command: positionals(args)[0],
|
|
63
75
|
help: args.includes('-h') || args.includes('--help'),
|
|
64
|
-
version: args.includes('-v') || args.includes('--version')
|
|
76
|
+
version: args.includes('-v') || args.includes('--version'),
|
|
77
|
+
check: args.includes('--check'),
|
|
78
|
+
ifNeeded: args.includes('--if-needed'),
|
|
79
|
+
config: configPath(args),
|
|
80
|
+
write: args.includes('--write'),
|
|
81
|
+
auto: args.includes('--auto'),
|
|
82
|
+
verify: args.includes('--verify'),
|
|
83
|
+
paths: positionals(args).slice(1)
|
|
65
84
|
};
|
|
66
85
|
}
|
|
67
86
|
|
|
87
|
+
function positionals(args) {
|
|
88
|
+
return args.filter((arg, index) => !arg.startsWith('-') && args[index - 1] !== '--config');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Returns undefined when --config is absent and '' when it has no value.
|
|
92
|
+
function configPath(args) {
|
|
93
|
+
const index = args.findIndex((arg) => arg === '--config' || arg.startsWith('--config='));
|
|
94
|
+
if (index === -1) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
if (args[index] !== '--config') {
|
|
98
|
+
return args[index].slice('--config='.length);
|
|
99
|
+
}
|
|
100
|
+
const next = args[index + 1];
|
|
101
|
+
return next !== undefined && !next.startsWith('-') ? next : '';
|
|
102
|
+
}
|
|
103
|
+
|
|
68
104
|
function helpText() {
|
|
69
|
-
return 'Usage: npx exifreader <command
|
|
105
|
+
return 'Usage: npx exifreader <command> [options]\n'
|
|
70
106
|
+ '\n'
|
|
71
107
|
+ 'Commands:\n'
|
|
72
|
-
+ ' build
|
|
73
|
-
+ '
|
|
108
|
+
+ ' build Rebuild dist/exif-reader.js using the "exifreader" custom build\n'
|
|
109
|
+
+ ' configuration (include/exclude) from --config, the\n'
|
|
110
|
+
+ ' EXIFREADER_CUSTOM_BUILD env var, or your project\'s package.json.\n'
|
|
111
|
+
+ ' analyze <paths...> Read the given image files and directories with the full parser\n'
|
|
112
|
+
+ ' and print the include configuration that reads everything found\n'
|
|
113
|
+
+ ' in them.\n'
|
|
114
|
+
+ '\n'
|
|
115
|
+
+ 'Options for build:\n'
|
|
116
|
+
+ ' --check Exit with an error if the installed bundle was not built from\n'
|
|
117
|
+
+ ' the current configuration and exifreader version. Never builds.\n'
|
|
118
|
+
+ ' --if-needed Build only if the installed bundle is not already up to date.\n'
|
|
119
|
+
+ ' --config <path> Read the configuration from a JSON file holding the\n'
|
|
120
|
+
+ ' include/exclude object.\n'
|
|
121
|
+
+ ' --auto <paths...> Build the configuration that analyze prints for these images\n'
|
|
122
|
+
+ ' instead of reading one. The configuration is not saved.\n'
|
|
123
|
+
+ ' --verify <paths...>\n'
|
|
124
|
+
+ ' After building, read these images with the full parser and\n'
|
|
125
|
+
+ ' with the installed bundle, and fail on any difference.\n'
|
|
126
|
+
+ '\n'
|
|
127
|
+
+ 'Options for analyze:\n'
|
|
128
|
+
+ ' --write Put the configuration in your project\'s package.json, replacing\n'
|
|
129
|
+
+ ' its include or exclude section.\n'
|
|
74
130
|
+ '\n'
|
|
75
131
|
+ 'Options:\n'
|
|
76
|
-
+ ' -h, --help
|
|
77
|
-
+ ' -v, --version
|
|
132
|
+
+ ' -h, --help Show this help.\n'
|
|
133
|
+
+ ' -v, --version Show the exifreader version.\n'
|
|
78
134
|
+ '\n'
|
|
79
135
|
+ 'Run this after installing exifreader and adding an "exifreader" section to your\n'
|
|
80
136
|
+ 'package.json. It replaces the deprecated automatic postinstall rebuild.';
|
|
81
137
|
}
|
|
82
138
|
|
|
83
|
-
function runBuildCommand(deps) {
|
|
139
|
+
function runBuildCommand(args, deps) {
|
|
84
140
|
const cwd = deps.cwd();
|
|
85
141
|
const install = deps.checkLocalInstall(cwd);
|
|
86
142
|
if (!install.ok) {
|
|
87
|
-
deps
|
|
88
|
-
|
|
89
|
-
return;
|
|
143
|
+
fail(deps, install.message);
|
|
144
|
+
return undefined;
|
|
90
145
|
}
|
|
91
146
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
147
|
+
if (args.check && args.ifNeeded) {
|
|
148
|
+
fail(deps, '--check and --if-needed cannot be used together. Use --check to only verify the '
|
|
149
|
+
+ 'installed bundle, or --if-needed to build it when it is out of date.');
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const sampleError = sampleOptionError(args);
|
|
154
|
+
if (sampleError) {
|
|
155
|
+
fail(deps, sampleError);
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let imagePaths;
|
|
160
|
+
if (args.auto || args.verify) {
|
|
161
|
+
imagePaths = collectImages(args.paths, cwd, deps);
|
|
162
|
+
if (!imagePaths) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (args.auto) {
|
|
168
|
+
return runAutoBuild(args, cwd, imagePaths, install, deps);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const resolved = resolveBuildConfig(args, cwd, deps);
|
|
172
|
+
if (resolved.error) {
|
|
173
|
+
fail(deps, resolved.error);
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
const config = resolved.config;
|
|
177
|
+
|
|
178
|
+
if (args.check) {
|
|
179
|
+
runCheck(config, install, deps);
|
|
180
|
+
return undefined;
|
|
97
181
|
}
|
|
98
182
|
|
|
99
|
-
const config = deps.resolveConfig(cwd);
|
|
100
183
|
if (!config) {
|
|
101
|
-
deps
|
|
102
|
-
|
|
184
|
+
fail(deps, noConfigMessage());
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
buildUnlessUpToDate(config, args, install, deps);
|
|
189
|
+
|
|
190
|
+
if (args.verify) {
|
|
191
|
+
return failOnRejection(verifyBuild(imagePaths, cwd, install, deps), deps);
|
|
192
|
+
}
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function fail(deps, message) {
|
|
197
|
+
deps.error(message);
|
|
198
|
+
deps.exit(1);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function sampleOptionError(args) {
|
|
202
|
+
if (args.write) {
|
|
203
|
+
return '--write is an option of analyze, not build. To save the configuration, run '
|
|
204
|
+
+ '"npx exifreader analyze <paths...> --write", then "npx exifreader build".';
|
|
205
|
+
}
|
|
206
|
+
if (args.auto && args.config !== undefined) {
|
|
207
|
+
return '--auto and --config cannot be used together. --auto builds the configuration it derives '
|
|
208
|
+
+ 'from the images, so leave out --config.';
|
|
209
|
+
}
|
|
210
|
+
if (args.auto && args.check) {
|
|
211
|
+
return '--auto and --check cannot be used together. --check never builds, and --auto does not '
|
|
212
|
+
+ 'save the configuration it derives, so there is nothing for --check to compare against.';
|
|
213
|
+
}
|
|
214
|
+
if (args.verify && args.check) {
|
|
215
|
+
return '--verify and --check cannot be used together. To compare the installed bundle with the '
|
|
216
|
+
+ 'full parser without rebuilding an up-to-date one, use --verify with --if-needed.';
|
|
217
|
+
}
|
|
218
|
+
if ((args.auto || args.verify) && args.paths.length === 0) {
|
|
219
|
+
const flag = args.auto ? '--auto' : '--verify';
|
|
220
|
+
return `${flag} needs the image files or directories to read, for example `
|
|
221
|
+
+ `"npx exifreader build ${flag} ./samples".`;
|
|
222
|
+
}
|
|
223
|
+
if (!args.auto && !args.verify && args.paths.length > 0) {
|
|
224
|
+
return `Unexpected argument: ${args.paths.join(' ')}. Image paths are only read with --auto or `
|
|
225
|
+
+ '--verify, for example "npx exifreader build --auto ./samples".';
|
|
226
|
+
}
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function collectImages(paths, cwd, deps) {
|
|
231
|
+
let collected;
|
|
232
|
+
try {
|
|
233
|
+
collected = collectImagePaths(paths.map((imagePath) => path.resolve(cwd, imagePath)), deps.fs);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
fail(deps, error.message);
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
reportSkipped(collected.skipped, deps);
|
|
239
|
+
if (collected.paths.length === 0) {
|
|
240
|
+
fail(deps, `No files were found in ${paths.join(', ')}.`);
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
return collected.paths;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function reportSkipped(skipped, deps) {
|
|
247
|
+
for (const entry of skipped) {
|
|
248
|
+
deps.error(`Skipped ${entry.path}: ${entry.message}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function runAutoBuild(args, cwd, imagePaths, install, deps) {
|
|
253
|
+
return failOnRejection(analyzeSample(imagePaths, cwd, deps).then((analysis) => {
|
|
254
|
+
if (!analysis) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
buildUnlessUpToDate(analysis.config, args, install, deps);
|
|
258
|
+
return args.verify ? verifyBuild(analysis.imagePaths, cwd, install, deps) : undefined;
|
|
259
|
+
}), deps);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function failOnRejection(promise, deps) {
|
|
263
|
+
return promise.catch((error) => fail(deps, error.message));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Prints the derived configuration on stdout and the summary on stderr, and
|
|
267
|
+
// resolves to the configuration and the images it was derived from. Resolves
|
|
268
|
+
// to undefined after failing when no image could be read.
|
|
269
|
+
async function analyzeSample(imagePaths, cwd, deps) {
|
|
270
|
+
const domParser = deps.resolveDomParser(cwd);
|
|
271
|
+
if (!domParser) {
|
|
272
|
+
deps.error('@xmldom/xmldom was not found, so the XMP content of the images was not read. '
|
|
273
|
+
+ 'XMP is still included in the configuration when an image has it.');
|
|
274
|
+
}
|
|
275
|
+
const parser = await deps.loadFullParser();
|
|
276
|
+
const {results, skipped} = await withoutConsoleWarnings(!domParser, () => analyzeImages(imagePaths, {fs: deps.fs, parser, domParser}));
|
|
277
|
+
reportSkipped(skipped, deps);
|
|
278
|
+
if (results.length === 0) {
|
|
279
|
+
fail(deps, 'None of the given files could be read as an image, so there is nothing to analyze.');
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const derived = deriveConfig(results.map((result) => result.tags));
|
|
284
|
+
deps.log(JSON.stringify(derived.config, null, 2));
|
|
285
|
+
deps.error(`Analyzed ${countOf(results.length, 'image')} (${derived.fileTypes.join(', ')}).`);
|
|
286
|
+
for (const warning of derived.warnings) {
|
|
287
|
+
deps.error(warning);
|
|
288
|
+
}
|
|
289
|
+
return {config: derived.config, imagePaths: results.map((result) => result.path)};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Without a DOM parser, ExifReader warns on the console for every image with
|
|
293
|
+
// XMP. The CLI prints one line about it instead.
|
|
294
|
+
async function withoutConsoleWarnings(silence, action) {
|
|
295
|
+
if (!silence) {
|
|
296
|
+
return action();
|
|
297
|
+
}
|
|
298
|
+
const originalWarn = console.warn;
|
|
299
|
+
console.warn = ignoreWarning;
|
|
300
|
+
try {
|
|
301
|
+
return await action();
|
|
302
|
+
} finally {
|
|
303
|
+
console.warn = originalWarn;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function ignoreWarning() {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function countOf(count, noun) {
|
|
312
|
+
return `${count} ${noun}${count === 1 ? '' : 's'}`;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function buildUnlessUpToDate(config, args, install, deps) {
|
|
316
|
+
if (args.ifNeeded && bundleState(config, install, deps) === 'up to date') {
|
|
317
|
+
deps.log(upToDateMessage(install.distPath));
|
|
103
318
|
return;
|
|
104
319
|
}
|
|
105
320
|
|
|
321
|
+
if (install.shared) {
|
|
322
|
+
deps.error(sharedInstallMessage(install.distPath));
|
|
323
|
+
}
|
|
106
324
|
deps.log(`Building a custom exifreader bundle at ${install.distPath}`);
|
|
107
325
|
deps.build({config});
|
|
108
326
|
}
|
|
109
327
|
|
|
328
|
+
async function verifyBuild(imagePaths, cwd, install, deps) {
|
|
329
|
+
const domParser = deps.resolveDomParser(cwd);
|
|
330
|
+
if (!domParser) {
|
|
331
|
+
deps.error('@xmldom/xmldom was not found, so XMP was compared at group level only.');
|
|
332
|
+
}
|
|
333
|
+
const fullParser = await deps.loadFullParser();
|
|
334
|
+
const customParser = deps.loadCustomParser(install.distPath);
|
|
335
|
+
const {results, skipped} = await withoutConsoleWarnings(
|
|
336
|
+
!domParser,
|
|
337
|
+
() => verifyImages(imagePaths, {fs: deps.fs, fullParser, customParser, domParser})
|
|
338
|
+
);
|
|
339
|
+
reportSkipped(skipped, deps);
|
|
340
|
+
if (results.length === 0) {
|
|
341
|
+
fail(deps, 'None of the given files could be read as an image, so nothing was verified.');
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const differing = results.filter((result) => result.differences.length > 0);
|
|
346
|
+
for (const result of differing) {
|
|
347
|
+
deps.error(result.path);
|
|
348
|
+
for (const difference of result.differences) {
|
|
349
|
+
deps.error(` ${difference}`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (differing.length > 0) {
|
|
353
|
+
fail(deps, `${differing.length} of ${countOf(results.length, 'image')} gave a different result with the `
|
|
354
|
+
+ 'custom build than with the full parser.');
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
deps.log(`${results.length === 1 ? 'The image' : `All ${results.length} images`} gave the same result with the `
|
|
358
|
+
+ 'custom build as with the full parser.');
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function runAnalyzeCommand(args, deps) {
|
|
362
|
+
const cwd = deps.cwd();
|
|
363
|
+
if (args.paths.length === 0) {
|
|
364
|
+
fail(deps, 'analyze needs the image files or directories to read, for example '
|
|
365
|
+
+ '"npx exifreader analyze ./samples".');
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
let packageDir;
|
|
370
|
+
if (args.write) {
|
|
371
|
+
packageDir = nearestPackageDir(cwd, deps.fs.existsSync);
|
|
372
|
+
if (!packageDir) {
|
|
373
|
+
fail(deps, `No package.json found in ${cwd} or any directory above it, so there is nowhere to `
|
|
374
|
+
+ 'write the configuration. Run the command from your project, or leave out --write.');
|
|
375
|
+
return undefined;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const imagePaths = collectImages(args.paths, cwd, deps);
|
|
380
|
+
if (!imagePaths) {
|
|
381
|
+
return undefined;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return failOnRejection(analyzeSample(imagePaths, cwd, deps).then((analysis) => {
|
|
385
|
+
if (!analysis) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (packageDir) {
|
|
389
|
+
writeToPackageJson(path.join(packageDir, 'package.json'), analysis.config.include, deps);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
deps.error('To use it, set it as the "exifreader" section of your package.json and run '
|
|
393
|
+
+ '"npx exifreader build", or save it to a file and run "npx exifreader build --config <file>". '
|
|
394
|
+
+ 'Run this command again with --write to update package.json for you.');
|
|
395
|
+
}), deps);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function writeToPackageJson(filePath, include, deps) {
|
|
399
|
+
let outcome;
|
|
400
|
+
try {
|
|
401
|
+
outcome = writeConfigToPackageJson(filePath, include, deps.fs);
|
|
402
|
+
} catch (error) {
|
|
403
|
+
fail(deps, `Could not write the configuration to ${filePath}: ${error.message}`);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (outcome.created) {
|
|
407
|
+
deps.error(`Added an "exifreader" section with this configuration to ${filePath}.`);
|
|
408
|
+
} else if (outcome.replaced) {
|
|
409
|
+
deps.error(`Replaced ${JSON.stringify(outcome.replaced)} in the "exifreader" section of ${filePath}.`);
|
|
410
|
+
} else {
|
|
411
|
+
deps.error(`Added this configuration to the "exifreader" section of ${filePath}.`);
|
|
412
|
+
}
|
|
413
|
+
deps.error('Run "npx exifreader build" to build it.');
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function resolveBuildConfig(args, cwd, deps) {
|
|
417
|
+
if (args.config !== undefined) {
|
|
418
|
+
return configFromFile(args.config, cwd, deps);
|
|
419
|
+
}
|
|
420
|
+
if (deps.env.EXIFREADER_CUSTOM_BUILD) {
|
|
421
|
+
return configFromEnv(deps.env.EXIFREADER_CUSTOM_BUILD);
|
|
422
|
+
}
|
|
423
|
+
return {config: deps.resolveConfig(cwd)};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function configFromFile(configArg, cwd, deps) {
|
|
427
|
+
if (configArg === '') {
|
|
428
|
+
return {error: '--config needs a path to a JSON file with the custom build configuration, '
|
|
429
|
+
+ 'for example "npx exifreader build --config exifreader.json".'};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const filePath = path.resolve(cwd, configArg);
|
|
433
|
+
let config;
|
|
434
|
+
try {
|
|
435
|
+
config = deps.readConfigFile(filePath);
|
|
436
|
+
} catch (error) {
|
|
437
|
+
return {error: `Could not read the custom build configuration file ${filePath}: ${error.message}`};
|
|
438
|
+
}
|
|
439
|
+
return checkedConfig(config, `The custom build configuration file ${filePath}`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Read a JSON file, ignoring a leading UTF-8 byte order mark.
|
|
444
|
+
*
|
|
445
|
+
* @param {string} filePath Absolute path of the file.
|
|
446
|
+
* @returns {*} The parsed JSON value. Throws when the file cannot be read or is
|
|
447
|
+
* not valid JSON.
|
|
448
|
+
*/
|
|
449
|
+
function readConfigFile(filePath) {
|
|
450
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, ''));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function checkedConfig(config, source) {
|
|
454
|
+
if (config && (config.include || config.exclude)) {
|
|
455
|
+
return {config};
|
|
456
|
+
}
|
|
457
|
+
return {error: `${source} must hold a JSON object with an "include" or "exclude" section, `
|
|
458
|
+
+ 'for example {"include": {"jpeg": true}}.'};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function configFromEnv(value) {
|
|
462
|
+
let config;
|
|
463
|
+
try {
|
|
464
|
+
config = JSON.parse(value);
|
|
465
|
+
} catch (error) {
|
|
466
|
+
return {error: `EXIFREADER_CUSTOM_BUILD is not valid JSON: ${error.message}. Set it to the custom `
|
|
467
|
+
+ 'build configuration object, for example {"include": {"jpeg": true}}, or unset it.'};
|
|
468
|
+
}
|
|
469
|
+
return checkedConfig(config, 'EXIFREADER_CUSTOM_BUILD');
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function runCheck(config, install, deps) {
|
|
473
|
+
if (!config) {
|
|
474
|
+
deps.log('No ExifReader custom build configuration found, so there is nothing to check.');
|
|
475
|
+
deps.exit(0);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const state = bundleState(config, install, deps);
|
|
480
|
+
if (state === 'up to date') {
|
|
481
|
+
deps.log(upToDateMessage(install.distPath));
|
|
482
|
+
deps.exit(0);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
fail(deps, state === 'stock' ? stockBundleMessage(install.distPath) : staleBundleMessage(install.distPath));
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function bundleState(config, install, deps) {
|
|
489
|
+
const marker = deps.readMarker(path.dirname(install.distPath));
|
|
490
|
+
if (markerMatches(marker, config, deps.version())) {
|
|
491
|
+
return 'up to date';
|
|
492
|
+
}
|
|
493
|
+
return marker === undefined ? 'stock' : 'stale';
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function upToDateMessage(distPath) {
|
|
497
|
+
return `The custom exifreader bundle at ${distPath} is up to date.`;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function stockBundleMessage(distPath) {
|
|
501
|
+
return `The exifreader bundle at ${distPath} is the stock full build, not a custom build. `
|
|
502
|
+
+ 'Run "npx exifreader build" to build it from your configuration.';
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function staleBundleMessage(distPath) {
|
|
506
|
+
return `The custom exifreader bundle at ${distPath} was built from a different configuration or `
|
|
507
|
+
+ 'exifreader version. Run "npx exifreader build" to rebuild it.';
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function sharedInstallMessage(distPath) {
|
|
511
|
+
return `Warning: ${distPath} is shared by every workspace package that uses this exifreader copy, `
|
|
512
|
+
+ 'so they all get the same bundle and the last build wins.';
|
|
513
|
+
}
|
|
514
|
+
|
|
110
515
|
function noConfigMessage() {
|
|
111
516
|
return 'No ExifReader custom build configuration found. Add an "exifreader" section with '
|
|
112
517
|
+ '"include" or "exclude" to your project\'s package.json, then run "npx exifreader build" '
|
|
@@ -118,6 +523,7 @@ function checkLocalInstall(cwd, options) {
|
|
|
118
523
|
const resolve = options.resolve || defaultResolve;
|
|
119
524
|
const realpath = options.realpath || fs.realpathSync;
|
|
120
525
|
const rootDir = options.rootDir || EXIFREADER_ROOT_DIR;
|
|
526
|
+
const exists = options.exists || fs.existsSync;
|
|
121
527
|
const isPnp = options.isPnp !== undefined ? options.isPnp : !!process.versions.pnp;
|
|
122
528
|
|
|
123
529
|
let resolvedDir;
|
|
@@ -133,7 +539,11 @@ function checkLocalInstall(cwd, options) {
|
|
|
133
539
|
return {ok: false, message: mismatchMessage(installed)};
|
|
134
540
|
}
|
|
135
541
|
|
|
136
|
-
return {
|
|
542
|
+
return {
|
|
543
|
+
ok: true,
|
|
544
|
+
distPath: path.join(installed, 'dist', 'exif-reader.js'),
|
|
545
|
+
shared: isSharedInstall(cwd, installed, exists, realpath)
|
|
546
|
+
};
|
|
137
547
|
}
|
|
138
548
|
|
|
139
549
|
function defaultResolve(cwd) {
|
|
@@ -163,3 +573,19 @@ function mismatchMessage(installedDir) {
|
|
|
163
573
|
+ `(installed at ${installedDir}). This usually means you ran "npx exifreader@<version>" with `
|
|
164
574
|
+ 'a version other than the installed one. Run "npx exifreader build" without a version pin.';
|
|
165
575
|
}
|
|
576
|
+
|
|
577
|
+
function isSharedInstall(cwd, installed, exists, realpath) {
|
|
578
|
+
const packageDir = nearestPackageDir(cwd, exists);
|
|
579
|
+
if (!packageDir) {
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
return !installed.startsWith(safeRealpath(realpath, path.join(packageDir, 'node_modules')) + path.sep);
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
function nearestPackageDir(directory, exists) {
|
|
586
|
+
if (exists(path.join(directory, 'package.json'))) {
|
|
587
|
+
return directory;
|
|
588
|
+
}
|
|
589
|
+
const parent = path.dirname(directory);
|
|
590
|
+
return parent === directory ? undefined : nearestPackageDir(parent, exists);
|
|
591
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
|
+
|
|
5
|
+
const crypto = require('crypto');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
const MARKER_FILE_NAME = '.exifreader-custom-build.json';
|
|
10
|
+
|
|
11
|
+
module.exports = {MARKER_FILE_NAME, configHash, readMarker, writeMarker, removeMarker, markerMatches};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Hash a custom build configuration so that key order does not matter.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} config The JSON-derived `exifreader` config object.
|
|
17
|
+
* @returns {string} The sha256 hex digest of the config.
|
|
18
|
+
*/
|
|
19
|
+
function configHash(config) {
|
|
20
|
+
return crypto.createHash('sha256').update(stableStringify(config)).digest('hex');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function stableStringify(value) {
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
return '[' + value.map(stableStringify).join(',') + ']';
|
|
26
|
+
}
|
|
27
|
+
if (value && typeof value === 'object') {
|
|
28
|
+
return '{' + Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(',') + '}';
|
|
29
|
+
}
|
|
30
|
+
return JSON.stringify(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Read the custom build marker in a dist directory.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} distDir The package's dist directory.
|
|
37
|
+
* @returns {{configHash: string, version: string}|undefined} The parsed marker,
|
|
38
|
+
* or undefined when the file is missing or is not valid JSON.
|
|
39
|
+
*/
|
|
40
|
+
function readMarker(distDir) {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(fs.readFileSync(path.join(distDir, MARKER_FILE_NAME), 'utf8'));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Record which config and exifreader version a custom bundle was built from.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} distDir The package's dist directory.
|
|
52
|
+
* @param {object} config The `exifreader` config object that was built.
|
|
53
|
+
* @param {string} version The exifreader version that was built.
|
|
54
|
+
*/
|
|
55
|
+
function writeMarker(distDir, config, version) {
|
|
56
|
+
fs.writeFileSync(path.join(distDir, MARKER_FILE_NAME), JSON.stringify({configHash: configHash(config), version}));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Remove the custom build marker from a dist directory, if there is one.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} distDir The package's dist directory.
|
|
63
|
+
*/
|
|
64
|
+
function removeMarker(distDir) {
|
|
65
|
+
fs.rmSync(path.join(distDir, MARKER_FILE_NAME), {force: true});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Tell whether a marker was written for this config and exifreader version.
|
|
70
|
+
*
|
|
71
|
+
* @param {{configHash: string, version: string}|undefined} marker The result of readMarker.
|
|
72
|
+
* @param {object} config The `exifreader` config object to compare with.
|
|
73
|
+
* @param {string} version The exifreader version to compare with.
|
|
74
|
+
* @returns {boolean} True only when both the config hash and the version match.
|
|
75
|
+
*/
|
|
76
|
+
function markerMatches(marker, config, version) {
|
|
77
|
+
return !!marker && marker.configHash === configHash(config) && marker.version === version;
|
|
78
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
|
|
7
|
+
module.exports = {unshareFile};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Give a hardlinked file its own inode with the same content, so that writing
|
|
11
|
+
* to it no longer changes the other links. A file with one link, or a missing
|
|
12
|
+
* file, is left alone.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} filePath The file to unshare.
|
|
15
|
+
*/
|
|
16
|
+
function unshareFile(filePath) {
|
|
17
|
+
// Package managers can hardlink package files from a shared store, and webpack writes in place.
|
|
18
|
+
if (!fs.existsSync(filePath) || fs.statSync(filePath).nlink <= 1) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const tmpPath = `${filePath}.${process.pid}.tmp`;
|
|
22
|
+
try {
|
|
23
|
+
fs.copyFileSync(filePath, tmpPath);
|
|
24
|
+
fs.renameSync(tmpPath, filePath);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
fs.rmSync(tmpPath, {force: true});
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|