@tryghost/mg-fs-utils 0.26.2 → 0.26.4
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 +2 -2
- package/lib/FileCache.js +15 -20
- package/lib/csv.js +17 -8
- package/lib/ghost-json.js +1 -1
- package/lib/read-zip.js +1 -3
- package/lib/utils.js +2 -4
- package/lib/zip.js +5 -5
- package/package.json +10 -13
package/README.md
CHANGED
|
@@ -35,8 +35,8 @@ Follow the instructions for the top-level repo.
|
|
|
35
35
|
|
|
36
36
|
## Test
|
|
37
37
|
|
|
38
|
-
- `pnpm
|
|
39
|
-
-
|
|
38
|
+
- `pnpm test` runs the package tests
|
|
39
|
+
- Linting and formatting are run from the repo root (`pnpm lint`, `pnpm format`)
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
|
package/lib/FileCache.js
CHANGED
|
@@ -16,10 +16,7 @@ const __dirname = new URL('.', import.meta.url).pathname;
|
|
|
16
16
|
|
|
17
17
|
// Define the .end path for this project, as well as parent env files
|
|
18
18
|
dotenv.config({
|
|
19
|
-
path: [
|
|
20
|
-
join(__dirname, '../', '.env'),
|
|
21
|
-
'.env'
|
|
22
|
-
]
|
|
19
|
+
path: [join(__dirname, '../', '.env'), '.env']
|
|
23
20
|
});
|
|
24
21
|
|
|
25
22
|
const basePath = 'mg';
|
|
@@ -149,7 +146,7 @@ export default class FileCache {
|
|
|
149
146
|
}
|
|
150
147
|
|
|
151
148
|
convertMbToBytes(mb) {
|
|
152
|
-
return
|
|
149
|
+
return mb * 1048576;
|
|
153
150
|
}
|
|
154
151
|
|
|
155
152
|
sanitizeFileName(src) {
|
|
@@ -166,7 +163,7 @@ export default class FileCache {
|
|
|
166
163
|
}
|
|
167
164
|
|
|
168
165
|
ensureJsonExtension({filename, isJSON = true}) {
|
|
169
|
-
return
|
|
166
|
+
return isJSON && !filename.endsWith('.json') ? `${filename}.json` : filename; // Ensure the `.json` extension is only added if needed
|
|
170
167
|
}
|
|
171
168
|
|
|
172
169
|
resolveImageFileName(filename) {
|
|
@@ -292,7 +289,7 @@ export default class FileCache {
|
|
|
292
289
|
* @param {String} filename - name of file to read
|
|
293
290
|
*/
|
|
294
291
|
async readTmpJSONFile(filename) {
|
|
295
|
-
let fileNameWithExt =
|
|
292
|
+
let fileNameWithExt = filename.endsWith('.json') ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
296
293
|
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
297
294
|
|
|
298
295
|
return await readJson(filepath);
|
|
@@ -304,7 +301,7 @@ export default class FileCache {
|
|
|
304
301
|
* @param {String} filename - name of file to read
|
|
305
302
|
*/
|
|
306
303
|
hasTmpJSONFile(filename) {
|
|
307
|
-
let fileNameWithExt =
|
|
304
|
+
let fileNameWithExt = filename.endsWith('.json') ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
|
|
308
305
|
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
309
306
|
|
|
310
307
|
return existsSync(filepath);
|
|
@@ -321,8 +318,7 @@ export default class FileCache {
|
|
|
321
318
|
const {isJSON} = options;
|
|
322
319
|
|
|
323
320
|
// Create a temporary version first
|
|
324
|
-
let filename = options.tmpFilename
|
|
325
|
-
|| (isJSON ? this.defaultTmpJSONFileName : this.defaultTmpCSVFileName);
|
|
321
|
+
let filename = options.tmpFilename || (isJSON ? this.defaultTmpJSONFileName : this.defaultTmpCSVFileName);
|
|
326
322
|
|
|
327
323
|
await this.writeTmpFile(data, filename, isJSON);
|
|
328
324
|
|
|
@@ -363,7 +359,7 @@ export default class FileCache {
|
|
|
363
359
|
const filePath = join(this.cacheDir, `report-${fileName}.csv`);
|
|
364
360
|
|
|
365
361
|
const seen = new Set();
|
|
366
|
-
const dedupedData = report.data.filter(
|
|
362
|
+
const dedupedData = report.data.filter(e => {
|
|
367
363
|
if (seen.has(e.src)) {
|
|
368
364
|
return false;
|
|
369
365
|
}
|
|
@@ -434,11 +430,11 @@ export default class FileCache {
|
|
|
434
430
|
}
|
|
435
431
|
|
|
436
432
|
/**
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
433
|
+
* Check if we've got this file already
|
|
434
|
+
*
|
|
435
|
+
* @param {String} filename
|
|
436
|
+
* @param {String} type - one of tmp, json, image
|
|
437
|
+
*/
|
|
442
438
|
hasFile(filename, type) {
|
|
443
439
|
let pathToCheck = filename;
|
|
444
440
|
|
|
@@ -464,17 +460,17 @@ export default class FileCache {
|
|
|
464
460
|
const directory = this.cacheBaseDir + '/';
|
|
465
461
|
|
|
466
462
|
let itemsToDelete = [];
|
|
467
|
-
const dirContents = readdirSync(directory).map(
|
|
463
|
+
const dirContents = readdirSync(directory).map(fileName => {
|
|
468
464
|
return join(directory, fileName);
|
|
469
465
|
});
|
|
470
466
|
|
|
471
|
-
dirContents.forEach(
|
|
467
|
+
dirContents.forEach(item => {
|
|
472
468
|
if (lstatSync(item).isDirectory()) {
|
|
473
469
|
itemsToDelete.push(item);
|
|
474
470
|
}
|
|
475
471
|
});
|
|
476
472
|
|
|
477
|
-
itemsToDelete.forEach(
|
|
473
|
+
itemsToDelete.forEach(item => {
|
|
478
474
|
rmSync(item, {recursive: true});
|
|
479
475
|
});
|
|
480
476
|
|
|
@@ -484,4 +480,3 @@ export default class FileCache {
|
|
|
484
480
|
};
|
|
485
481
|
}
|
|
486
482
|
}
|
|
487
|
-
|
package/lib/csv.js
CHANGED
|
@@ -10,21 +10,26 @@ import errors from '@tryghost/errors';
|
|
|
10
10
|
* @param {String} filePath - name of file to write
|
|
11
11
|
* @param {Object} options - optional options to pass to `csv-parse`
|
|
12
12
|
*/
|
|
13
|
-
const parseCSV = (
|
|
13
|
+
const parseCSV = (
|
|
14
|
+
filePath,
|
|
15
|
+
options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true, trim: true}
|
|
16
|
+
) => {
|
|
14
17
|
const parser = parse(options);
|
|
15
18
|
const data = [];
|
|
16
19
|
return new Promise((resolve, reject) => {
|
|
17
20
|
const fileStream = createReadStream(filePath);
|
|
18
|
-
fileStream.on('error',
|
|
21
|
+
fileStream.on('error', error => {
|
|
19
22
|
return reject(error);
|
|
20
23
|
});
|
|
21
24
|
fileStream
|
|
22
25
|
.pipe(parser)
|
|
23
|
-
.on('data', async
|
|
26
|
+
.on('data', async row => {
|
|
24
27
|
data.push(row);
|
|
25
|
-
})
|
|
28
|
+
})
|
|
29
|
+
.on('end', () => {
|
|
26
30
|
return resolve(data);
|
|
27
|
-
})
|
|
31
|
+
})
|
|
32
|
+
.on('error', error => {
|
|
28
33
|
return reject(error);
|
|
29
34
|
});
|
|
30
35
|
});
|
|
@@ -35,7 +40,10 @@ const parseCSV = (filePath, options = {skip_lines_with_error: true, columns: tru
|
|
|
35
40
|
* @param {String} csvString - CSV as a string
|
|
36
41
|
* @param {Object} options - optional options to pass to `csv-parse`
|
|
37
42
|
*/
|
|
38
|
-
const parseString = (
|
|
43
|
+
const parseString = (
|
|
44
|
+
csvString,
|
|
45
|
+
options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true, trim: true}
|
|
46
|
+
) => {
|
|
39
47
|
const data = parseSync(csvString, options);
|
|
40
48
|
|
|
41
49
|
return data;
|
|
@@ -46,7 +54,7 @@ const parseString = (csvString, options = {skip_lines_with_error: true, columns:
|
|
|
46
54
|
* @param {Array} data - the data to format
|
|
47
55
|
* @param {Array} fields - the fields to pick and use as column header
|
|
48
56
|
*/
|
|
49
|
-
const jsonToCSV = (data, fields =
|
|
57
|
+
const jsonToCSV = (data, fields = data && data[0] && typeof data[0] === 'object' ? Object.keys(data[0]) : []) => {
|
|
50
58
|
if (!data || !Array.isArray(data) || data.length === 0) {
|
|
51
59
|
return fields.length > 0 ? `${fields.join(',')}\r\n` : '';
|
|
52
60
|
}
|
|
@@ -67,7 +75,8 @@ const jsonToCSV = (data, fields = (data && data[0] && typeof data[0] === 'object
|
|
|
67
75
|
if (entry[field] !== null) {
|
|
68
76
|
if (entry[field] instanceof Date) {
|
|
69
77
|
fieldToAdd = entry[field].toISOString();
|
|
70
|
-
} else if (entry[field] && entry[field].toString().indexOf(',') !== -1) {
|
|
78
|
+
} else if (entry[field] && entry[field].toString().indexOf(',') !== -1) {
|
|
79
|
+
// If the field contains a comma, wrap it in quotes
|
|
71
80
|
fieldToAdd = `"${entry[field]}"`;
|
|
72
81
|
} else {
|
|
73
82
|
fieldToAdd = entry[field];
|
package/lib/ghost-json.js
CHANGED
package/lib/read-zip.js
CHANGED
package/lib/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {extname, basename} from 'node:path';
|
|
2
2
|
import {slugify} from '@tryghost/string';
|
|
3
3
|
|
|
4
|
-
const cacheNameFromPath =
|
|
4
|
+
const cacheNameFromPath = path => {
|
|
5
5
|
let ext = extname(path);
|
|
6
6
|
let base = basename(path, ext);
|
|
7
7
|
let slug = slugify(base);
|
|
@@ -10,6 +10,4 @@ const cacheNameFromPath = (path) => {
|
|
|
10
10
|
return noUnderscore;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
export {
|
|
14
|
-
cacheNameFromPath
|
|
15
|
-
};
|
|
13
|
+
export {cacheNameFromPath};
|
package/lib/zip.js
CHANGED
|
@@ -6,7 +6,7 @@ import errors from '@tryghost/errors';
|
|
|
6
6
|
|
|
7
7
|
const _private = {};
|
|
8
8
|
|
|
9
|
-
_private.openZipForRead =
|
|
9
|
+
_private.openZipForRead = zipPath => {
|
|
10
10
|
return AdmZip(zipPath);
|
|
11
11
|
};
|
|
12
12
|
|
|
@@ -23,7 +23,7 @@ const read = (zipPath, callback) => {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// If the zip has one entry and its another zip, unzip & read that instead
|
|
26
|
-
const preCheckEntries = zip.getEntries().filter(
|
|
26
|
+
const preCheckEntries = zip.getEntries().filter(entry => {
|
|
27
27
|
return !(entry.entryName.match(/^__MACOSX/) || entry.entryName.match(/\.DS_Store$/));
|
|
28
28
|
});
|
|
29
29
|
|
|
@@ -35,7 +35,7 @@ const read = (zipPath, callback) => {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
// Entries is cleaned first
|
|
38
|
-
let entries = zip.getEntries().filter(
|
|
38
|
+
let entries = zip.getEntries().filter(entry => {
|
|
39
39
|
return !(entry.entryName.match(/^__MACOSX/) || entry.entryName.match(/\.DS_Store$/));
|
|
40
40
|
});
|
|
41
41
|
// If the first entry is a directory, and every entry starts with the same directory, we have a top level directory
|
|
@@ -47,7 +47,7 @@ const read = (zipPath, callback) => {
|
|
|
47
47
|
entries.shift();
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
entries.forEach(
|
|
50
|
+
entries.forEach(zipEntry => {
|
|
51
51
|
// If we have a topLevelDirectory, remove reference to it from the entry name
|
|
52
52
|
let entryName = hasTopLevelDir ? zipEntry.entryName.replace(firstEntry.entryName, '') : zipEntry.entryName;
|
|
53
53
|
callback(entryName, zipEntry);
|
|
@@ -63,7 +63,7 @@ const write = async (zipPath, contentFolder, fileName) => {
|
|
|
63
63
|
return await zip.compress(contentFolder, outputPath);
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
const deleteFile = async
|
|
66
|
+
const deleteFile = async fileToDelete => {
|
|
67
67
|
ensureFileSync(fileToDelete);
|
|
68
68
|
|
|
69
69
|
return await remove(fileToDelete);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.4",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/TryGhost/migrate.git",
|
|
@@ -18,25 +18,22 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"c8": "
|
|
22
|
-
"eslint": "8.57.1"
|
|
21
|
+
"c8": "12.0.0"
|
|
23
22
|
},
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"@tryghost/errors": "3.
|
|
26
|
-
"@tryghost/image-transform": "1.4.
|
|
24
|
+
"@tryghost/errors": "3.3.6",
|
|
25
|
+
"@tryghost/image-transform": "1.4.17",
|
|
27
26
|
"@tryghost/string": "0.3.5",
|
|
28
|
-
"@tryghost/zip": "3.
|
|
29
|
-
"adm-zip": "0.
|
|
30
|
-
"csv-parse": "7.0.
|
|
27
|
+
"@tryghost/zip": "3.5.5",
|
|
28
|
+
"adm-zip": "0.6.0",
|
|
29
|
+
"csv-parse": "7.0.2",
|
|
31
30
|
"dotenv": "17.4.2",
|
|
32
|
-
"fs-extra": "11.
|
|
33
|
-
"sharp": "0.35.
|
|
31
|
+
"fs-extra": "11.4.0",
|
|
32
|
+
"sharp": "0.35.3",
|
|
34
33
|
"transliteration": "2.6.1"
|
|
35
34
|
},
|
|
36
35
|
"scripts": {
|
|
37
36
|
"dev": "echo \"Implement me!\"",
|
|
38
|
-
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'"
|
|
39
|
-
"posttest": "pnpm lint",
|
|
40
|
-
"lint": "eslint . --ext .js --cache"
|
|
37
|
+
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'"
|
|
41
38
|
}
|
|
42
39
|
}
|