@tryghost/mg-fs-utils 0.11.1 → 0.11.2

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.
Files changed (2) hide show
  1. package/lib/csv.js +19 -1
  2. package/package.json +2 -2
package/lib/csv.js CHANGED
@@ -1,7 +1,9 @@
1
+ import {join} from 'path';
1
2
  import {parse} from 'csv-parse';
2
3
  import {parse as parseSync} from 'csv-parse/sync';
3
4
  import fs from 'fs-extra';
4
5
  import {isDate} from 'date-fns';
6
+ import errors from '@tryghost/errors';
5
7
 
6
8
  /**
7
9
  * Parse a CSV file and make available as JSON
@@ -79,8 +81,24 @@ const jsonToCSV = (data, fields = Object.keys(data[0])) => {
79
81
  return csv;
80
82
  };
81
83
 
84
+ const writeCSV = async (data, filePath, fileName) => {
85
+ // Ensure the directory we want to wite to exists
86
+ fs.ensureDirSync(filePath);
87
+
88
+ const outputPath = join(filePath, fileName || `${Date.now()}.csv`);
89
+
90
+ try {
91
+ await fs.outputFile(outputPath, data);
92
+ } catch (error) {
93
+ throw new errors.InternalServerError({message: 'Could not create CSV file', error});
94
+ }
95
+
96
+ return outputPath;
97
+ };
98
+
82
99
  export default {
83
100
  parseCSV,
84
101
  parseString,
85
- jsonToCSV
102
+ jsonToCSV,
103
+ writeCSV
86
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-fs-utils",
3
- "version": "0.11.1",
3
+ "version": "0.11.2",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-fs-utils",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -36,5 +36,5 @@
36
36
  "sharp": "0.31.2",
37
37
  "transliteration": "2.3.5"
38
38
  },
39
- "gitHead": "83d2a7e6b254a6d8b5c54b3b143b379276250b36"
39
+ "gitHead": "bd45d15f77f395e28462208b8e313bf67358ec32"
40
40
  }