@tryghost/mg-fs-utils 0.16.6 → 0.16.7
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 +6 -31
- package/lib/csv.js +7 -1
- package/lib/read-zip.js +0 -4
- package/package.json +6 -6
package/lib/FileCache.js
CHANGED
|
@@ -4,7 +4,7 @@ import {tmpdir} from 'node:os';
|
|
|
4
4
|
import {createHash} from 'node:crypto';
|
|
5
5
|
import * as dotenv from 'dotenv';
|
|
6
6
|
import _ from 'lodash';
|
|
7
|
-
import {writeFileSync, readdirSync,
|
|
7
|
+
import {writeFileSync, readdirSync, rmSync, lstatSync, existsSync} from 'node:fs';
|
|
8
8
|
import {writeFile} from 'node:fs/promises';
|
|
9
9
|
import {outputJson, outputJsonSync, mkdirpSync, readJson, remove, outputFile} from 'fs-extra/esm';
|
|
10
10
|
import imageTransform from '@tryghost/image-transform';
|
|
@@ -232,7 +232,7 @@ export default class FileCache {
|
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
// @TODO: use content type on request to infer this, rather than assuming jpeg?
|
|
235
|
-
if (type === '
|
|
235
|
+
if (type === 'images' && !_.includes(knownImageExtensions, ext)) {
|
|
236
236
|
if (!ext) {
|
|
237
237
|
filename += '.jpeg';
|
|
238
238
|
} else {
|
|
@@ -444,26 +444,13 @@ export default class FileCache {
|
|
|
444
444
|
pathToCheck = join(this[`${type}Dir`], filename);
|
|
445
445
|
}
|
|
446
446
|
|
|
447
|
-
|
|
448
|
-
return existsSync(pathToCheck);
|
|
449
|
-
} catch (error) {
|
|
450
|
-
return false;
|
|
451
|
-
}
|
|
447
|
+
return existsSync(pathToCheck);
|
|
452
448
|
}
|
|
453
449
|
|
|
454
450
|
async emptyCurrentCacheDir() {
|
|
455
|
-
if (!this.cacheKey) {
|
|
456
|
-
return false;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
451
|
let siteCachePath = join(this.cacheBaseDir, this.cacheKey);
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
await remove(siteCachePath);
|
|
463
|
-
return true;
|
|
464
|
-
} catch (error) {
|
|
465
|
-
throw new errors.NotFoundError({message: 'Unknown file type'});
|
|
466
|
-
}
|
|
452
|
+
await remove(siteCachePath);
|
|
453
|
+
return true;
|
|
467
454
|
}
|
|
468
455
|
|
|
469
456
|
/**
|
|
@@ -472,13 +459,6 @@ export default class FileCache {
|
|
|
472
459
|
async emptyCacheDir() {
|
|
473
460
|
const directory = this.cacheBaseDir + '/';
|
|
474
461
|
|
|
475
|
-
existsSync(directory, (err) => {
|
|
476
|
-
if (err) {
|
|
477
|
-
throw err;
|
|
478
|
-
}
|
|
479
|
-
return true;
|
|
480
|
-
});
|
|
481
|
-
|
|
482
462
|
let itemsToDelete = [];
|
|
483
463
|
const dirContents = readdirSync(directory).map((fileName) => {
|
|
484
464
|
return join(directory, fileName);
|
|
@@ -491,12 +471,7 @@ export default class FileCache {
|
|
|
491
471
|
});
|
|
492
472
|
|
|
493
473
|
itemsToDelete.forEach((item) => {
|
|
494
|
-
|
|
495
|
-
if (err) {
|
|
496
|
-
throw err;
|
|
497
|
-
}
|
|
498
|
-
return true;
|
|
499
|
-
});
|
|
474
|
+
rmSync(item, {recursive: true});
|
|
500
475
|
});
|
|
501
476
|
|
|
502
477
|
return {
|
package/lib/csv.js
CHANGED
|
@@ -15,6 +15,9 @@ const parseCSV = (filePath, options = {skip_lines_with_error: true, columns: tru
|
|
|
15
15
|
const data = [];
|
|
16
16
|
return new Promise((resolve, reject) => {
|
|
17
17
|
const fileStream = createReadStream(filePath);
|
|
18
|
+
fileStream.on('error', (error) => {
|
|
19
|
+
return reject(error);
|
|
20
|
+
});
|
|
18
21
|
fileStream
|
|
19
22
|
.pipe(parser)
|
|
20
23
|
.on('data', async (row) => {
|
|
@@ -43,7 +46,10 @@ const parseString = (csvString, options = {skip_lines_with_error: true, columns:
|
|
|
43
46
|
* @param {Array} data - the data to format
|
|
44
47
|
* @param {Array} fields - the fields to pick and use as column header
|
|
45
48
|
*/
|
|
46
|
-
const jsonToCSV = (data, fields = Object.keys(data[0])) => {
|
|
49
|
+
const jsonToCSV = (data, fields = (data && data[0] && typeof data[0] === 'object') ? Object.keys(data[0]) : []) => {
|
|
50
|
+
if (!data || !Array.isArray(data) || data.length === 0) {
|
|
51
|
+
return fields.length > 0 ? `${fields.join(',')}\r\n` : '';
|
|
52
|
+
}
|
|
47
53
|
let csv = `${fields.join(',')}\r\n`;
|
|
48
54
|
let entry;
|
|
49
55
|
let field;
|
package/lib/read-zip.js
CHANGED
|
@@ -15,10 +15,6 @@ const readZipEntries = async (zipPath = null) => {
|
|
|
15
15
|
let files = [];
|
|
16
16
|
|
|
17
17
|
for (const entry of Object.entries(zipEntries)) {
|
|
18
|
-
if ((entry[1].name.match(/^__MACOSX/) || entry[1].name.match(/\.DS_Store$/))) {
|
|
19
|
-
continue;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
18
|
if (entry && !entry[1].isDirectory) {
|
|
23
19
|
files.push(entry[0]);
|
|
24
20
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.7",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "module",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"dev": "echo \"Implement me!\"",
|
|
11
|
-
"test": "c8 --check-coverage --lines
|
|
11
|
+
"test": "c8 --check-coverage --lines 100 --branches 100 --functions 100 --statements 100 node --test 'test/**/*.test.js'",
|
|
12
12
|
"lint": "eslint . --ext .js --cache",
|
|
13
13
|
"posttest": "yarn lint"
|
|
14
14
|
},
|
|
@@ -24,17 +24,17 @@
|
|
|
24
24
|
"eslint": "8.57.1"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@tryghost/errors": "3.0.
|
|
27
|
+
"@tryghost/errors": "3.0.3",
|
|
28
28
|
"@tryghost/image-transform": "1.4.12",
|
|
29
29
|
"@tryghost/string": "0.3.1",
|
|
30
|
-
"@tryghost/zip": "3.0.
|
|
30
|
+
"@tryghost/zip": "3.0.3",
|
|
31
31
|
"adm-zip": "0.5.16",
|
|
32
|
-
"csv-parse": "6.1
|
|
32
|
+
"csv-parse": "6.2.1",
|
|
33
33
|
"dotenv": "17.3.1",
|
|
34
34
|
"fs-extra": "11.3.4",
|
|
35
35
|
"lodash": "4.17.23",
|
|
36
36
|
"sharp": "0.34.5",
|
|
37
37
|
"transliteration": "2.6.1"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "dfaf02f58e6598ec4abb56a568eb1b6a65ee12f4"
|
|
40
40
|
}
|