@tryghost/mg-fs-utils 0.12.1 → 0.12.3
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 +15 -2
- package/lib/csv.js +18 -1
- package/package.json +4 -4
package/lib/FileCache.js
CHANGED
|
@@ -149,6 +149,10 @@ class FileCache {
|
|
|
149
149
|
return src.replace(fileNameNoExt, safeFileNameNoExt);
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
+
ensureJsonExtension({filename, isJSON = true}) {
|
|
153
|
+
return (isJSON && !filename.endsWith('.json')) ? `${filename}.json` : filename; // Ensure the `.json` extension is only added if needed
|
|
154
|
+
}
|
|
155
|
+
|
|
152
156
|
resolveImageFileName(filename) {
|
|
153
157
|
return this.resolveFileName(filename, 'images');
|
|
154
158
|
}
|
|
@@ -220,7 +224,7 @@ class FileCache {
|
|
|
220
224
|
* @param {Boolean} isJSON - defaults to write a JSON file
|
|
221
225
|
*/
|
|
222
226
|
async writeTmpFile(data, filename, isJSON = true) {
|
|
223
|
-
let fileNameWithExt =
|
|
227
|
+
let fileNameWithExt = this.ensureJsonExtension({filename, isJSON});
|
|
224
228
|
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
225
229
|
|
|
226
230
|
if (isJSON) {
|
|
@@ -240,7 +244,7 @@ class FileCache {
|
|
|
240
244
|
* @param {Boolean} isJSON - defaults to write a JSON file
|
|
241
245
|
*/
|
|
242
246
|
writeTmpFileSync(data, filename, isJSON = true) {
|
|
243
|
-
let fileNameWithExt =
|
|
247
|
+
let fileNameWithExt = this.ensureJsonExtension({filename, isJSON});
|
|
244
248
|
let filepath = join(this.tmpDir, fileNameWithExt);
|
|
245
249
|
|
|
246
250
|
if (isJSON) {
|
|
@@ -385,6 +389,15 @@ class FileCache {
|
|
|
385
389
|
return outputFile(storagePath, data);
|
|
386
390
|
}
|
|
387
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Alias fs-extra's 'remove' so we can more easily mock it
|
|
394
|
+
*
|
|
395
|
+
* @param {String} string - absolute path to where file should be deleted
|
|
396
|
+
*/
|
|
397
|
+
async deleteFileOrDir(storagePath) {
|
|
398
|
+
return remove(storagePath);
|
|
399
|
+
}
|
|
400
|
+
|
|
388
401
|
/**
|
|
389
402
|
* Check if we've got this file already
|
|
390
403
|
*
|
package/lib/csv.js
CHANGED
|
@@ -97,9 +97,26 @@ const writeCSV = async (data, filePath, fileName) => {
|
|
|
97
97
|
return outputPath;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Check if a CSV has the required keys
|
|
102
|
+
* @param {string} filePath Path to CSV file
|
|
103
|
+
* @param {array} theKeys Array of keys the CSV must include
|
|
104
|
+
* @returns {boolean}
|
|
105
|
+
*/
|
|
106
|
+
const hasKeys = async (filePath, theKeys = []) => {
|
|
107
|
+
let parsed = await parseCSV(filePath);
|
|
108
|
+
let firstRow = parsed[0];
|
|
109
|
+
let colKeys = Object.keys(firstRow);
|
|
110
|
+
|
|
111
|
+
let hasRequiredKeys = theKeys.every(v => colKeys.includes(v));
|
|
112
|
+
|
|
113
|
+
return hasRequiredKeys;
|
|
114
|
+
};
|
|
115
|
+
|
|
100
116
|
export default {
|
|
101
117
|
parseCSV,
|
|
102
118
|
parseString,
|
|
103
119
|
jsonToCSV,
|
|
104
|
-
writeCSV
|
|
120
|
+
writeCSV,
|
|
121
|
+
hasKeys
|
|
105
122
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mg-fs-utils",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.3",
|
|
4
4
|
"repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"eslint": "8.35.0",
|
|
24
|
-
"jest": "29.
|
|
24
|
+
"jest": "29.5.0",
|
|
25
25
|
"jest-extended": "3.2.4"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@tryghost/image-transform": "1.2.3",
|
|
30
30
|
"@tryghost/zip": "1.1.32",
|
|
31
31
|
"adm-zip": "0.5.10",
|
|
32
|
-
"csv-parse": "5.3.
|
|
32
|
+
"csv-parse": "5.3.6",
|
|
33
33
|
"date-fns": "2.29.3",
|
|
34
34
|
"fs-extra": "11.1.0",
|
|
35
35
|
"lodash": "4.17.21",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"sharp": "0.31.3",
|
|
38
38
|
"transliteration": "2.3.5"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "3dcab8e838ec029cdf46a8df53e1067f3b2fb0b7"
|
|
41
41
|
}
|