@tryghost/mg-fs-utils 0.9.2 → 0.10.0

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 CHANGED
@@ -1,4 +1,11 @@
1
- module.exports.FileCache = require('./lib/FileCache');
2
- module.exports.zip = require('./lib/zip');
3
- module.exports.ghostJSON = require('./lib/ghostJSON');
4
- module.exports.csv = require('./lib/csv');
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
- const _ = require('lodash');
2
- const fs = require('fs-extra');
3
- const path = require('path');
4
- const crypto = require('crypto');
5
- const os = require('os');
6
- const imageTransform = require('@tryghost/image-transform');
7
- const errors = require('@tryghost/errors');
8
- const csv = require('./csv');
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'];
@@ -48,6 +48,8 @@ class FileCache {
48
48
  * - image files
49
49
  * - /content/media
50
50
  * - non-image media files like videos or audio files
51
+ * - /content/files
52
+ * - non-image and non-media files like pdfs
51
53
  */
52
54
  get cacheDir() {
53
55
  if (!this._cacheDir) {
@@ -58,6 +60,7 @@ class FileCache {
58
60
  if (this.options && this.options.contentDir) {
59
61
  fs.mkdirpSync(path.join(this.imageDir));
60
62
  fs.mkdirpSync(path.join(this.mediaDir));
63
+ fs.mkdirpSync(path.join(this.filesDir));
61
64
  } else {
62
65
  fs.mkdirpSync(path.join(this.zipDir));
63
66
  }
@@ -93,6 +96,14 @@ class FileCache {
93
96
  return path.join(this.zipDir, this.mediaPath);
94
97
  }
95
98
 
99
+ get filesPath() {
100
+ return path.join('content', 'files');
101
+ }
102
+
103
+ get filesDir() {
104
+ return path.join(this.zipDir, this.filesPath);
105
+ }
106
+
96
107
  get defaultCacheFileName() {
97
108
  if (this.batchName) {
98
109
  return `gh-${this.cacheName}-batch-${this.batchName}-${Date.now()}`;
@@ -167,6 +178,9 @@ class FileCache {
167
178
  } else if (type === 'media') {
168
179
  typeDir = this.mediaDir;
169
180
  typePath = this.mediaPath;
181
+ } else {
182
+ typeDir = this.filesDir;
183
+ typePath = this.filesPath;
170
184
  }
171
185
 
172
186
  // CASE: Some image URLs are very long and can cause various issues with storage.
@@ -220,6 +234,26 @@ class FileCache {
220
234
  return filepath;
221
235
  }
222
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
+
223
257
  /**
224
258
  * Read a JSON file containing temporary data
225
259
  *
@@ -232,6 +266,18 @@ class FileCache {
232
266
  return await fs.readJson(filepath);
233
267
  }
234
268
 
269
+ /**
270
+ * Read a JSON file containing temporary data
271
+ *
272
+ * @param {String} filename - name of file to read
273
+ */
274
+ hasTmpJSONFile(filename) {
275
+ let fileNameWithExt = (filename.endsWith('.json')) ? filename : `${filename}.json`; // Ensure the `.json` extension is only added if needed
276
+ let filepath = path.join(this.tmpDir, fileNameWithExt);
277
+
278
+ return fs.existsSync(filepath);
279
+ }
280
+
235
281
  /**
236
282
  * Create a CSV or JSON file with our processed data
237
283
  *
@@ -354,13 +400,17 @@ class FileCache {
354
400
  hasFile(filename, type) {
355
401
  let pathToCheck = filename;
356
402
 
357
- if (type && !_.includes(['tmp', 'json', 'image'], type)) {
403
+ if (type && !_.includes(['tmp', 'json', 'image', 'zip'], type)) {
358
404
  throw new errors.NotFoundError({message: 'Unknown file type'});
359
405
  } else if (type) {
360
406
  pathToCheck = path.join(this[`${type}Dir`], filename);
361
407
  }
362
408
 
363
- return fs.existsSync(pathToCheck);
409
+ try {
410
+ return fs.existsSync(pathToCheck);
411
+ } catch (error) {
412
+ return false;
413
+ }
364
414
  }
365
415
 
366
416
  /**
@@ -403,4 +453,6 @@ class FileCache {
403
453
  }
404
454
  }
405
455
 
406
- module.exports = FileCache;
456
+ export {
457
+ FileCache
458
+ };
package/lib/csv.js CHANGED
@@ -1,14 +1,14 @@
1
- const {parse} = require('csv-parse');
2
- const {parse: parseSync} = require('csv-parse/sync');
3
- const fs = require('fs-extra');
4
- const {isDate} = require('date-fns');
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
- module.exports.parse = (filePath, options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true}) => {
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
- module.exports.parseString = (csvString, options = {skip_lines_with_error: true, columns: true, skip_empty_lines: true}) => {
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
- module.exports.jsonToCSV = (data, fields = Object.keys(data[0])) => {
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
- const fs = require('fs-extra');
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
- module.exports.read = async (jsonPath) => {
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
- const AdmZip = require('adm-zip');
2
- const {compress} = require('@tryghost/zip');
3
- const path = require('path');
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
- module.exports.read = (zipPath, callback) => {
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
- module.exports.write = (zipPath, contentFolder, fileName) => {
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
- module.exports._private = _private;
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.9.2",
3
+ "version": "0.10.0",
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": "NODE_ENV=testing mocha './test/**/*.test.js'",
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.15",
28
+ "@tryghost/errors": "1.2.18",
23
29
  "@tryghost/image-transform": "1.2.2",
24
- "@tryghost/zip": "1.1.28",
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.0"
36
+ "sharp": "0.31.1"
31
37
  },
32
- "gitHead": "370c7f9973e7518cfb6433e694c8fbbf022c32cf"
38
+ "gitHead": "35cb5c1ca6a826e1e596c4f2e209ab690fa06fa0"
33
39
  }