@tryghost/mg-fs-utils 0.11.4 → 0.11.6
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/lib/FileCache.js +36 -38
- package/lib/csv.js +5 -4
- package/lib/ghostJSON.js +2 -2
- package/package.json +8 -8
package/lib/FileCache.js
CHANGED
|
@@ -2,7 +2,9 @@ import path from 'node:path';
|
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import crypto from 'node:crypto';
|
|
4
4
|
import _ from 'lodash';
|
|
5
|
-
import
|
|
5
|
+
import {writeFileSync, readdirSync, rmdir, lstatSync, existsSync} from 'node:fs';
|
|
6
|
+
import {writeFile} from 'node:fs/promises';
|
|
7
|
+
import {outputJson, outputJsonSync, mkdirpSync, readJson, remove, outputFile} from 'fs-extra/esm';
|
|
6
8
|
import imageTransform from '@tryghost/image-transform';
|
|
7
9
|
import errors from '@tryghost/errors';
|
|
8
10
|
import transliterate from 'transliteration';
|
|
@@ -62,15 +64,15 @@ class FileCache {
|
|
|
62
64
|
get cacheDir() {
|
|
63
65
|
if (!this._cacheDir) {
|
|
64
66
|
this._cacheDir = path.join(this.tmpDirPath, basePath, this.cacheKey);
|
|
65
|
-
|
|
67
|
+
mkdirpSync(path.join(this.tmpDir));
|
|
66
68
|
|
|
67
69
|
// don't create the content directory when migrating members
|
|
68
70
|
if (this.options && this.options.contentDir) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
mkdirpSync(path.join(this.imageDir));
|
|
72
|
+
mkdirpSync(path.join(this.mediaDir));
|
|
73
|
+
mkdirpSync(path.join(this.filesDir));
|
|
72
74
|
} else {
|
|
73
|
-
|
|
75
|
+
mkdirpSync(path.join(this.zipDir));
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
78
|
return this._cacheDir;
|
|
@@ -222,9 +224,9 @@ class FileCache {
|
|
|
222
224
|
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
223
225
|
|
|
224
226
|
if (isJSON) {
|
|
225
|
-
await
|
|
227
|
+
await outputJson(filepath, data, {spaces: 2});
|
|
226
228
|
} else {
|
|
227
|
-
await
|
|
229
|
+
await writeFile(filepath, data);
|
|
228
230
|
}
|
|
229
231
|
|
|
230
232
|
return filepath;
|
|
@@ -242,9 +244,9 @@ class FileCache {
|
|
|
242
244
|
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
243
245
|
|
|
244
246
|
if (isJSON) {
|
|
245
|
-
|
|
247
|
+
outputJsonSync(filepath, data, {spaces: 2});
|
|
246
248
|
} else {
|
|
247
|
-
|
|
249
|
+
writeFileSync(filepath, data);
|
|
248
250
|
}
|
|
249
251
|
|
|
250
252
|
return filepath;
|
|
@@ -259,7 +261,7 @@ class FileCache {
|
|
|
259
261
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
260
262
|
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
261
263
|
|
|
262
|
-
return await
|
|
264
|
+
return await readJson(filepath);
|
|
263
265
|
}
|
|
264
266
|
|
|
265
267
|
/**
|
|
@@ -271,7 +273,7 @@ class FileCache {
|
|
|
271
273
|
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
272
274
|
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
273
275
|
|
|
274
|
-
return
|
|
276
|
+
return existsSync(filepath);
|
|
275
277
|
}
|
|
276
278
|
|
|
277
279
|
/**
|
|
@@ -296,9 +298,9 @@ class FileCache {
|
|
|
296
298
|
let filepath = path.join(basepath, filename);
|
|
297
299
|
|
|
298
300
|
if (isJSON) {
|
|
299
|
-
await
|
|
301
|
+
await outputJson(filepath, data, {spaces: 2});
|
|
300
302
|
} else {
|
|
301
|
-
await
|
|
303
|
+
await writeFile(filepath, data);
|
|
302
304
|
}
|
|
303
305
|
|
|
304
306
|
return filepath;
|
|
@@ -332,7 +334,7 @@ class FileCache {
|
|
|
332
334
|
|
|
333
335
|
const fileData = csv.jsonToCSV(dedupedData);
|
|
334
336
|
|
|
335
|
-
await
|
|
337
|
+
await writeFile(filePath, fileData);
|
|
336
338
|
|
|
337
339
|
return {
|
|
338
340
|
data: dedupedData,
|
|
@@ -346,7 +348,7 @@ class FileCache {
|
|
|
346
348
|
* @param {String} data - a valid binary image
|
|
347
349
|
* @param {Object} options - a resolved file name
|
|
348
350
|
*/
|
|
349
|
-
async
|
|
351
|
+
async writeContentFile(data, options) {
|
|
350
352
|
if (!options.storagePath || !options.outputPath) {
|
|
351
353
|
options = this.resolveImageFileName(options.filename);
|
|
352
354
|
}
|
|
@@ -357,34 +359,30 @@ class FileCache {
|
|
|
357
359
|
if (options.optimize && imageTransform.canTransformToFormat(fileExt)) {
|
|
358
360
|
try {
|
|
359
361
|
const originalStoragePath = imageTransform.generateOriginalImageName(options.storagePath);
|
|
360
|
-
await
|
|
362
|
+
await this.saveFile(originalStoragePath, data);
|
|
361
363
|
const optimizedStoragePath = options.storagePath;
|
|
362
364
|
const optimizedData = await imageTransform.resizeFromBuffer(data, {width: 2000});
|
|
363
|
-
await
|
|
365
|
+
await this.saveFile(optimizedStoragePath, optimizedData);
|
|
364
366
|
} catch (error) {
|
|
365
367
|
// Silently fail and only save the original image without manipulation
|
|
366
368
|
// TODO: Catch errors and push to ctx.errors
|
|
367
|
-
await
|
|
369
|
+
await this.saveFile(options.storagePath, data);
|
|
368
370
|
}
|
|
369
371
|
} else {
|
|
370
|
-
await
|
|
372
|
+
await this.saveFile(options.storagePath, data);
|
|
371
373
|
}
|
|
372
374
|
|
|
373
375
|
return options.outputPath;
|
|
374
376
|
}
|
|
375
377
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
await fs.outputFile(options.storagePath, data);
|
|
386
|
-
|
|
387
|
-
return options.outputPath;
|
|
378
|
+
/**
|
|
379
|
+
* Alias fs-extra's 'outputFile' so we can more easily mock it
|
|
380
|
+
*
|
|
381
|
+
* @param {String} string - absolute path to where file should be saved
|
|
382
|
+
* @param {String|Buffer|Uint8Array} data - the file data to be saved
|
|
383
|
+
*/
|
|
384
|
+
async saveFile(storagePath, data) {
|
|
385
|
+
return outputFile(storagePath, data);
|
|
388
386
|
}
|
|
389
387
|
|
|
390
388
|
/**
|
|
@@ -403,7 +401,7 @@ class FileCache {
|
|
|
403
401
|
}
|
|
404
402
|
|
|
405
403
|
try {
|
|
406
|
-
return
|
|
404
|
+
return existsSync(pathToCheck);
|
|
407
405
|
} catch (error) {
|
|
408
406
|
return false;
|
|
409
407
|
}
|
|
@@ -417,7 +415,7 @@ class FileCache {
|
|
|
417
415
|
let siteCachePath = path.join(this.cacheBaseDir, this.cacheKey);
|
|
418
416
|
|
|
419
417
|
try {
|
|
420
|
-
await
|
|
418
|
+
await remove(siteCachePath);
|
|
421
419
|
return true;
|
|
422
420
|
} catch (error) {
|
|
423
421
|
throw new errors.NotFoundError({message: 'Unknown file type'});
|
|
@@ -430,7 +428,7 @@ class FileCache {
|
|
|
430
428
|
async emptyCacheDir() {
|
|
431
429
|
const directory = this.cacheBaseDir + '/';
|
|
432
430
|
|
|
433
|
-
|
|
431
|
+
existsSync(directory, (err) => {
|
|
434
432
|
if (err) {
|
|
435
433
|
throw err;
|
|
436
434
|
}
|
|
@@ -438,18 +436,18 @@ class FileCache {
|
|
|
438
436
|
});
|
|
439
437
|
|
|
440
438
|
let itemsToDelete = [];
|
|
441
|
-
const dirContents =
|
|
439
|
+
const dirContents = readdirSync(directory).map((fileName) => {
|
|
442
440
|
return path.join(directory, fileName);
|
|
443
441
|
});
|
|
444
442
|
|
|
445
443
|
dirContents.forEach((item) => {
|
|
446
|
-
if (
|
|
444
|
+
if (lstatSync(item).isDirectory()) {
|
|
447
445
|
itemsToDelete.push(item);
|
|
448
446
|
}
|
|
449
447
|
});
|
|
450
448
|
|
|
451
449
|
itemsToDelete.forEach((item) => {
|
|
452
|
-
|
|
450
|
+
rmdir(item, {recursive: true}, (err) => {
|
|
453
451
|
if (err) {
|
|
454
452
|
throw err;
|
|
455
453
|
}
|
package/lib/csv.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {join} from 'path';
|
|
2
2
|
import {parse} from 'csv-parse';
|
|
3
3
|
import {parse as parseSync} from 'csv-parse/sync';
|
|
4
|
-
import
|
|
4
|
+
import {createReadStream} from 'node:fs';
|
|
5
|
+
import {outputFile, ensureDirSync} from 'fs-extra/esm';
|
|
5
6
|
import {isDate} from 'date-fns';
|
|
6
7
|
import errors from '@tryghost/errors';
|
|
7
8
|
|
|
@@ -14,7 +15,7 @@ const parseCSV = (filePath, options = {skip_lines_with_error: true, columns: tru
|
|
|
14
15
|
const parser = parse(options);
|
|
15
16
|
const data = [];
|
|
16
17
|
return new Promise((resolve, reject) => {
|
|
17
|
-
const fileStream =
|
|
18
|
+
const fileStream = createReadStream(filePath);
|
|
18
19
|
fileStream
|
|
19
20
|
.pipe(parser)
|
|
20
21
|
.on('data', async (row) => {
|
|
@@ -83,12 +84,12 @@ const jsonToCSV = (data, fields = Object.keys(data[0])) => {
|
|
|
83
84
|
|
|
84
85
|
const writeCSV = async (data, filePath, fileName) => {
|
|
85
86
|
// Ensure the directory we want to wite to exists
|
|
86
|
-
|
|
87
|
+
ensureDirSync(filePath);
|
|
87
88
|
|
|
88
89
|
const outputPath = join(filePath, fileName || `${Date.now()}.csv`);
|
|
89
90
|
|
|
90
91
|
try {
|
|
91
|
-
await
|
|
92
|
+
await outputFile(outputPath, data);
|
|
92
93
|
} catch (error) {
|
|
93
94
|
throw new errors.InternalServerError({message: 'Could not create CSV file', error});
|
|
94
95
|
}
|
package/lib/ghostJSON.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {readJson} from 'fs-extra/esm';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Read a JSON file containing Ghost JSON data
|
|
@@ -11,7 +11,7 @@ import fs from 'fs-extra';
|
|
|
11
11
|
* @param {String} jsonPath - name of file to read
|
|
12
12
|
*/
|
|
13
13
|
const read = async (jsonPath) => {
|
|
14
|
-
return await
|
|
14
|
+
return await readJson(jsonPath);
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
export default {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.6",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,21 +20,21 @@
|
|
|
20
20
|
"access": "public"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"eslint": "8.
|
|
23
|
+
"eslint": "8.30.0",
|
|
24
24
|
"jest": "29.3.1",
|
|
25
25
|
"jest-extended": "3.2.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@tryghost/errors": "1.2.
|
|
28
|
+
"@tryghost/errors": "1.2.20",
|
|
29
29
|
"@tryghost/image-transform": "1.2.3",
|
|
30
|
-
"@tryghost/zip": "1.1.
|
|
31
|
-
"adm-zip": "0.5.
|
|
30
|
+
"@tryghost/zip": "1.1.31",
|
|
31
|
+
"adm-zip": "0.5.10",
|
|
32
32
|
"csv-parse": "5.3.3",
|
|
33
33
|
"date-fns": "2.29.3",
|
|
34
|
-
"fs-extra": "
|
|
34
|
+
"fs-extra": "11.1.0",
|
|
35
35
|
"lodash": "4.17.21",
|
|
36
|
-
"sharp": "0.31.
|
|
36
|
+
"sharp": "0.31.3",
|
|
37
37
|
"transliteration": "2.3.5"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "f5a6a06ad2eb953bda77b11359ed135482d9dfc7"
|
|
40
40
|
}
|