+1
-1
@@ -5,3 +5,3 @@ { | ||
| "author": "jinkwon.lee <uzmystic@gmail.com>", | ||
| "version": "1.0.3", | ||
| "version": "1.0.4", | ||
| "main": "src/index.js", | ||
@@ -8,0 +8,0 @@ "files": [ |
+1
-1
@@ -40,3 +40,3 @@ # csv2file | ||
| ```bash | ||
| csv2file -i ./example/example.csv -t ./example/template.sql | ||
| csv2file -i ./sample/sample.csv -t ./sample/sample.tmpl.sql | ||
| ``` | ||
@@ -43,0 +43,0 @@ |
+28
-27
@@ -5,29 +5,32 @@ const mustache = require('mustache'); | ||
| module.exports = async function csv2WTF(params, options) { | ||
| const opt = { | ||
| columns: true, | ||
| ...options, | ||
| }; | ||
| async function csv2file(params, options) { | ||
| return new Promise(async (resolve, reject) => { | ||
| const opt = { | ||
| columns: true, | ||
| ...options, | ||
| }; | ||
| try { | ||
| const input = await readFile(params.input); | ||
| const template = await readFile(params.template); | ||
| const outputStm = fs.createWriteStream(params.output); | ||
| try { | ||
| const input = await readFile(params.input); | ||
| const template = await readFile(params.template); | ||
| const outputStm = fs.createWriteStream(params.output); | ||
| const parser = parse(input, { | ||
| columns: opt.columns, | ||
| }); | ||
| const parser = parse(input, { | ||
| columns: opt.columns, | ||
| }); | ||
| parser.on('readable', function () { | ||
| while (1) { | ||
| const data = this.read(); | ||
| if (!data) { | ||
| break; | ||
| parser.on('readable', function () { | ||
| while (1) { | ||
| const data = this.read(); | ||
| if (!data) { | ||
| resolve(); | ||
| break; | ||
| } | ||
| outputStm.write(render(template, data)); | ||
| } | ||
| outputStm.write(render(template, data) + '\n'); | ||
| } | ||
| }); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
| }); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| }); | ||
| } | ||
@@ -39,8 +42,6 @@ | ||
| async function writeFile(path, data) { | ||
| return fs.writeFileSync(path, data) | ||
| } | ||
| function render(template, data) { | ||
| return mustache.render(template, data); | ||
| } | ||
| module.exports = csv2file; |
4938
1.06%38
2.7%