mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Colocated readCSV util with members input serializer
refs 5a6ce5abfe
- Allows to move files and keep the history in one go.
- 'csv-parser' will be upgraded to 'papaparse' lib in a new package
This commit is contained in:
parent
92a4ef5d31
commit
fabe06c5c5
4 changed files with 62 additions and 66 deletions
|
@ -1,4 +1,59 @@
|
|||
const fsLib = require('../../../../../../lib/fs');
|
||||
const Promise = require('bluebird');
|
||||
const csvParser = require('csv-parser');
|
||||
const _ = require('lodash');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const readCSV = (options) => {
|
||||
const columnsToExtract = options.columnsToExtract || [];
|
||||
let results = [];
|
||||
const rows = [];
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
const readFile = fs.createReadStream(options.path);
|
||||
|
||||
readFile.on('err', function (err) {
|
||||
reject(err);
|
||||
})
|
||||
.pipe(csvParser())
|
||||
.on('data', function (row) {
|
||||
rows.push(row);
|
||||
})
|
||||
.on('end', function () {
|
||||
// If CSV is single column - return all values including header
|
||||
const headers = _.keys(rows[0]);
|
||||
|
||||
let result = {};
|
||||
const columnMap = {};
|
||||
if (columnsToExtract.length === 1 && headers.length === 1) {
|
||||
results = _.map(rows, function (value) {
|
||||
result = {};
|
||||
result[columnsToExtract[0].name] = value[headers[0]];
|
||||
return result;
|
||||
});
|
||||
} else {
|
||||
// If there are multiple columns in csv file
|
||||
// try to match headers using lookup value
|
||||
|
||||
_.map(columnsToExtract, function findMatches(column) {
|
||||
_.each(headers, function checkheader(header) {
|
||||
if (column.lookup.test(header)) {
|
||||
columnMap[column.name] = header;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
results = _.map(rows, function evaluateRow(row) {
|
||||
const result = {};
|
||||
_.each(columnMap, function returnMatches(value, key) {
|
||||
result[key] = row[value];
|
||||
});
|
||||
return result;
|
||||
});
|
||||
}
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const parse = async (filePath) => {
|
||||
const columnsToExtract = [{
|
||||
|
@ -27,10 +82,11 @@ const parse = async (filePath) => {
|
|||
lookup: /created_at/i
|
||||
}];
|
||||
|
||||
return await fsLib.readCSV({
|
||||
return await readCSV({
|
||||
path: filePath,
|
||||
columnsToExtract: columnsToExtract
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.parse = parse;
|
||||
module.exports.readCSV = readCSV;
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
module.exports = {
|
||||
get readCSV() {
|
||||
return require('./read-csv');
|
||||
}
|
||||
};
|
|
@ -1,56 +0,0 @@
|
|||
const Promise = require('bluebird');
|
||||
const csvParser = require('csv-parser');
|
||||
const _ = require('lodash');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
module.exports = function readCSV(options) {
|
||||
const columnsToExtract = options.columnsToExtract || [];
|
||||
let results = [];
|
||||
const rows = [];
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
const readFile = fs.createReadStream(options.path);
|
||||
|
||||
readFile.on('err', function (err) {
|
||||
reject(err);
|
||||
})
|
||||
.pipe(csvParser())
|
||||
.on('data', function (row) {
|
||||
rows.push(row);
|
||||
})
|
||||
.on('end', function () {
|
||||
// If CSV is single column - return all values including header
|
||||
const headers = _.keys(rows[0]);
|
||||
|
||||
let result = {};
|
||||
const columnMap = {};
|
||||
if (columnsToExtract.length === 1 && headers.length === 1) {
|
||||
results = _.map(rows, function (value) {
|
||||
result = {};
|
||||
result[columnsToExtract[0].name] = value[headers[0]];
|
||||
return result;
|
||||
});
|
||||
} else {
|
||||
// If there are multiple columns in csv file
|
||||
// try to match headers using lookup value
|
||||
|
||||
_.map(columnsToExtract, function findMatches(column) {
|
||||
_.each(headers, function checkheader(header) {
|
||||
if (column.lookup.test(header)) {
|
||||
columnMap[column.name] = header;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
results = _.map(rows, function evaluateRow(row) {
|
||||
const result = {};
|
||||
_.each(columnMap, function returnMatches(value, key) {
|
||||
result[key] = row[value];
|
||||
});
|
||||
return result;
|
||||
});
|
||||
}
|
||||
resolve(results);
|
||||
});
|
||||
});
|
||||
};
|
|
@ -1,9 +1,10 @@
|
|||
const should = require('should');
|
||||
const path = require('path');
|
||||
const fsLib = require('../../../../core/server/lib/fs');
|
||||
const csvPath = path.join(__dirname, '../../../utils/fixtures/csv/');
|
||||
const fsLib = require('../../../../../../../../core/server/api/canary/utils/serializers/input/utils/members-import-csv');
|
||||
|
||||
describe('lib/fs: read csv', function () {
|
||||
const csvPath = path.join(__dirname, '../../../../../../../utils/fixtures/csv/');
|
||||
|
||||
describe('members-import-csv: read csv', function () {
|
||||
it('read csv: one column', function (done) {
|
||||
fsLib.readCSV({
|
||||
path: csvPath + 'single-column-with-header.csv',
|
Loading…
Add table
Reference in a new issue