@tryghost/mg-fs-utils 0.9.3 → 0.10.1
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/index.js +11 -4
- package/lib/FileCache.js +37 -11
- package/lib/csv.js +12 -7
- package/lib/ghostJSON.js +6 -2
- package/lib/zip.js +14 -9
- package/package.json +12 -6
package/index.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {FileCache} from './lib/FileCache.js';
|
|
2
|
+
import zip from './lib/zip.js';
|
|
3
|
+
import ghostJSON from './lib/ghostJSON.js';
|
|
4
|
+
import csv from './lib/csv.js';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
FileCache,
|
|
8
|
+
zip,
|
|
9
|
+
ghostJSON,
|
|
10
|
+
csv
|
|
11
|
+
};
|
package/lib/FileCache.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import _ from 'lodash';
|
|
5
|
+
import fs from 'fs-extra';
|
|
6
|
+
import imageTransform from '@tryghost/image-transform';
|
|
7
|
+
import errors from '@tryghost/errors';
|
|
8
|
+
import csv from './csv.js';
|
|
9
9
|
|
|
10
10
|
const basePath = 'mg';
|
|
11
11
|
const knownImageExtensions = ['.jpg', '.jpeg', '.gif', '.png', '.svg', '.svgz', '.ico', 'webp'];
|
|
@@ -234,6 +234,26 @@ class FileCache {
|
|
|
234
234
|
return filepath;
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Create a file to store temporary data
|
|
239
|
+
*
|
|
240
|
+
* @param {Object} data - a valid Ghost JSON object or data to store
|
|
241
|
+
* @param {String} filename - name of file to write
|
|
242
|
+
* @param {Boolean} isJSON - defaults to write a JSON file
|
|
243
|
+
*/
|
|
244
|
+
writeTmpFileSync(data, filename, isJSON = true) {
|
|
245
|
+
let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
246
|
+
let filepath = path.join(this.tmpDir, fileNameWithExt);
|
|
247
|
+
|
|
248
|
+
if (isJSON) {
|
|
249
|
+
fs.outputJsonSync(filepath, data, {spaces: 2});
|
|
250
|
+
} else {
|
|
251
|
+
fs.writeFileSync(filepath, data);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return filepath;
|
|
255
|
+
}
|
|
256
|
+
|
|
237
257
|
/**
|
|
238
258
|
* Read a JSON file containing temporary data
|
|
239
259
|
*
|
|
@@ -380,13 +400,17 @@ class FileCache {
|
|
|
380
400
|
hasFile(filename, type) {
|
|
381
401
|
let pathToCheck = filename;
|
|
382
402
|
|
|
383
|
-
if (type && !_.includes(['tmp', 'json', 'image'], type)) {
|
|
403
|
+
if (type && !_.includes(['tmp', 'json', 'image', 'zip'], type)) {
|
|
384
404
|
throw new errors.NotFoundError({message: 'Unknown file type'});
|
|
385
405
|
} else if (type) {
|
|
386
406
|
pathToCheck = path.join(this[`${type}Dir`], filename);
|
|
387
407
|
}
|
|
388
408
|
|
|
389
|
-
|
|
409
|
+
try {
|
|
410
|
+
return fs.existsSync(pathToCheck);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
/**
|
|
@@ -429,4 +453,6 @@ class FileCache {
|
|
|
429
453
|
}
|
|
430
454
|
}
|
|
431
455
|
|
|
432
|
-
|
|
456
|
+
export {
|
|
457
|
+
FileCache
|
|
458
|
+
};
|
package/lib/csv.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {parse} from 'csv-parse';
|
|
2
|
+
import {parse as parseSync} from 'csv-parse/sync';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import {isDate} from 'date-fns';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Parse a CSV file and make available as JSON
|
|
8
8
|
* @param {String} filePath - name of file to write
|
|
9
9
|
* @param {Object} options - optional options to pass to `csv-parse`
|
|
10
10
|
*/
|
|
11
|
-
|
|
11
|
+
const parseCSV = (filePath, options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true}) => {
|
|
12
12
|
const parser = parse(options);
|
|
13
13
|
const data = [];
|
|
14
14
|
return new Promise((resolve, reject) => {
|
|
@@ -30,7 +30,7 @@ module.exports.parse = (filePath, options = {skip_lines_with_error: true, column
|
|
|
30
30
|
* @param {String} csvString - CSV as a string
|
|
31
31
|
* @param {Object} options - optional options to pass to `csv-parse`
|
|
32
32
|
*/
|
|
33
|
-
|
|
33
|
+
const parseString = (csvString, options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true}) => {
|
|
34
34
|
const data = parseSync(csvString, options);
|
|
35
35
|
|
|
36
36
|
return data;
|
|
@@ -41,7 +41,7 @@ module.exports.parseString = (csvString, options = {skip_lines_with_error: true,
|
|
|
41
41
|
* @param {Array} data - the data to format
|
|
42
42
|
* @param {Array} fields - the fields to pick and use as column header
|
|
43
43
|
*/
|
|
44
|
-
|
|
44
|
+
const jsonToCSV = (data, fields = Object.keys(data[0])) => {
|
|
45
45
|
let csv = `${fields.join(',')}\r\n`;
|
|
46
46
|
let entry;
|
|
47
47
|
let field;
|
|
@@ -79,3 +79,8 @@ module.exports.jsonToCSV = (data, fields = Object.keys(data[0])) => {
|
|
|
79
79
|
return csv;
|
|
80
80
|
};
|
|
81
81
|
|
|
82
|
+
export default {
|
|
83
|
+
parseCSV,
|
|
84
|
+
parseString,
|
|
85
|
+
jsonToCSV
|
|
86
|
+
};
|
package/lib/ghostJSON.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Read a JSON file containing Ghost JSON data
|
|
@@ -10,6 +10,10 @@ const fs = require('fs-extra');
|
|
|
10
10
|
*
|
|
11
11
|
* @param {String} jsonPath - name of file to read
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
const read = async (jsonPath) => {
|
|
14
14
|
return await fs.readJson(jsonPath);
|
|
15
15
|
};
|
|
16
|
+
|
|
17
|
+
export default {
|
|
18
|
+
read
|
|
19
|
+
};
|
package/lib/zip.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import AdmZip from 'adm-zip';
|
|
3
|
+
import zip from '@tryghost/zip';
|
|
4
|
+
import errors from '@tryghost/errors';
|
|
5
|
+
|
|
4
6
|
const _private = {};
|
|
5
|
-
const errors = require('@tryghost/errors');
|
|
6
7
|
|
|
7
8
|
_private.openZipForRead = (zipPath) => {
|
|
8
9
|
return AdmZip(zipPath);
|
|
@@ -13,8 +14,8 @@ _private.openZipForRead = (zipPath) => {
|
|
|
13
14
|
* - Flattens the structure if there's one top-level directory, so we only get the files inside
|
|
14
15
|
* @TODO: Refactor to use @tryghost/zip to extract zip files and drop adm-zip dependency
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
|
-
let zip;
|
|
17
|
+
const read = (zipPath, callback) => {
|
|
18
|
+
let zip; // eslint-disable-line no-shadow
|
|
18
19
|
try {
|
|
19
20
|
zip = _private.openZipForRead(zipPath);
|
|
20
21
|
} catch (error) {
|
|
@@ -41,10 +42,14 @@ module.exports.read = (zipPath, callback) => {
|
|
|
41
42
|
});
|
|
42
43
|
};
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
const write = (zipPath, contentFolder, fileName) => {
|
|
45
46
|
const outputPath = path.join(zipPath, fileName || `ghost-import-${Date.now()}.zip`);
|
|
46
47
|
|
|
47
|
-
return compress(contentFolder, outputPath);
|
|
48
|
+
return zip.compress(contentFolder, outputPath);
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
export default {
|
|
52
|
+
read,
|
|
53
|
+
write,
|
|
54
|
+
_private
|
|
55
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "index.js",
|
|
8
|
+
"type": "module",
|
|
8
9
|
"scripts": {
|
|
9
10
|
"dev": "echo \"Implement me!\"",
|
|
10
|
-
"test": "
|
|
11
|
+
"test": "node --experimental-vm-modules $(yarn bin jest)",
|
|
11
12
|
"lint": "eslint . --ext .js --cache",
|
|
12
13
|
"posttest": "yarn lint"
|
|
13
14
|
},
|
|
@@ -18,16 +19,21 @@
|
|
|
18
19
|
"publishConfig": {
|
|
19
20
|
"access": "public"
|
|
20
21
|
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"eslint": "8.25.0",
|
|
24
|
+
"jest": "29.1.2",
|
|
25
|
+
"jest-extended": "3.1.0"
|
|
26
|
+
},
|
|
21
27
|
"dependencies": {
|
|
22
|
-
"@tryghost/errors": "1.2.
|
|
28
|
+
"@tryghost/errors": "1.2.18",
|
|
23
29
|
"@tryghost/image-transform": "1.2.2",
|
|
24
|
-
"@tryghost/zip": "1.1.
|
|
30
|
+
"@tryghost/zip": "1.1.29",
|
|
25
31
|
"adm-zip": "0.5.9",
|
|
26
32
|
"csv-parse": "5.3.0",
|
|
27
33
|
"date-fns": "2.29.3",
|
|
28
34
|
"fs-extra": "10.1.0",
|
|
29
35
|
"lodash": "4.17.21",
|
|
30
|
-
"sharp": "0.31.
|
|
36
|
+
"sharp": "0.31.1"
|
|
31
37
|
},
|
|
32
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "8f119b6c370337a90aa51dfbdb8d46ed7f5d154c"
|
|
33
39
|
}
|