-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertCsvToJson.js
147 lines (129 loc) · 3.74 KB
/
convertCsvToJson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const Promise = require('bluebird')
const path = require('path')
const pify = require('pify')
const fs = pify(require('fs'))
const aws = require('aws-sdk')
const BUCKET = 'tradle.io'
const DICTIONARIES_FOLDER = 'dictionaries/'
module.exports = { convertToJson }
const s3 = new aws.S3()
aws.config.setPromisesDependency(Promise);
async function convertToJson ({modelsDir, file, lang}) {
if (!lang) {
const parts = file.split('.')
if (parts.length !== 2 || parts[1].toLowerCase() !== 'csv') {
console.log('the -f options should pass CSV file with ".cvs" extension')
return
}
const fnParts = parts[0].split('_')
if (fnParts.length < 2) {
console.log(
'file name should be like dictionary_es.csv. The _es is for language'
)
return
}
// eslint-disable-next-line prefer-destructuring
lang = fnParts[1]
}
modelsDir = path.resolve(modelsDir)
let models
const dparts = modelsDir.split('/')
const dir = dparts[dparts.length - 2]
if (modelsDir.indexOf('.json') === -1) {
let modelFiles = await fs.readdir(modelsDir)
modelFiles = modelFiles.filter(modelFile => (/\.json$/).test(modelFile))
models = modelFiles.map(modelFile => {
return require(path.join(modelsDir, modelFile))
})
} else {
const ddir = path.resolve(dir)
if (!fs.existsSync(ddir))
fs.mkdirSync(ddir)
models = require(modelsDir)
}
if (Array.isArray(models)) {
const m = {}
models.forEach(model => {
m[model.id] = model
})
models = m
}
file = path.resolve(file)
const lines = fs.readFileSync(file, { encoding: 'utf8' })
.toString()
.split('\n')
const headers = lines[0].split(',').map(h => {
return h.charAt(0) === '"' ? h.slice(1, -1) : h
})
const isModelIdx = headers.findIndex(h => h === 'isModel')
const modelIdx = headers.findIndex(h => h === 'model')
const nameIdx = headers.findIndex(h => h === 'name')
const translatedIdx = headers.findIndex(h => h === lang)
const result = []
for (let i=1; i<lines.length; i += 1) {
const obj = {};
if (!lines[i].length)
continue
const currentline = lines[i].split(',')
const isModel = currentline[isModelIdx]
let modelId = !isModel && currentline[modelIdx]
let model
if (modelId) {
modelId = stripQuotes(modelId)
model = models[modelId]
}
const isEnum = model && model.enum
const name = stripQuotes(currentline[nameIdx])
const translatedTitle = stripQuotes(currentline[translatedIdx])
if (isEnum && !isModel) {
const elm = result.find(r => r.name === modelId)
if (!elm.enum)
elm.enum = {}
elm.enum[name] = translatedTitle
continue
}
for (let j=0; j<headers.length; j += 1) {
if (headers[j] === 'isModel') {
obj.type = isModel ? 'model' : 'propertyName'
continue
}
if (headers[j] === 'model') {
if (modelId)
obj.model = modelId
continue
}
if (headers[j] === 'name') {
obj.name = name
continue
}
const val = currentline[j]
if (!val.length || val === '""')
continue
obj[headers[j]] = stripQuotes(val)
}
result.push(obj);
}
let fn = `/dictionary_${lang.replace('-', '')}.json`
if (dir)
fn = `${dir}${fn}`
else
fn = `.${fn}`
const paramsPut = {
Body: Buffer.from(JSON.stringify(result, 0, 2)),
Bucket: BUCKET,
Key: `${DICTIONARIES_FOLDER}${fn}`,
ACL: 'public-read'
};
s3.putObject(paramsPut, (err, data) => {
if (err) {
// An error occurred
console.log(err, err.stack)
} else {
// Successful response
console.log(data)
}
});
}
function stripQuotes (val) {
return val.charAt(0) === '"' ? val.slice(1, -1) : val
}