From 042049437b0bed346f2f456093770d61e5f7e478 Mon Sep 17 00:00:00 2001 From: Ryan Van Etten Date: Sat, 13 Jan 2018 16:52:48 -0800 Subject: [PATCH] mkdir as needed --- README.md | 13 ++++++++---- map-file.js | 24 +++++++++++++++------- test-read.txt | 1 - test-write.txt | 1 - test.js | 54 ++++++++++++++++++++++++++++++++++++++------------ 5 files changed, 67 insertions(+), 26 deletions(-) delete mode 100644 test-read.txt delete mode 100644 test-write.txt diff --git a/README.md b/README.md index ee7493b..d57749c 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,15 @@ npm install map-file ### mapFile({from, to, map=identity}, callback=done) -- `from` is the reading filename. It may be relative or absolute. -- `to` is the writing filename. It may be relative or absolute. -- `map` is the function you use to map the content. The default returns the original text. -- `callback` is a function to call after mapping. The default throws errors and logs success. +- `from` is the reading filename + - supports relative or absolute +- `to` is the writing filename + - supports relative or absolute + - makes nonexistent directories via `fs.mkdir` +- `map` is the function you use to map the content + - default returns the original text +- `callback` is a function to call after mapping + - default throws errors and logs success ## Usage diff --git a/map-file.js b/map-file.js index 3068a93..0b89457 100644 --- a/map-file.js +++ b/map-file.js @@ -1,21 +1,31 @@ const fs = require("fs") const path = require("path") const utf8 = "utf8" +const made = dir => "." === dir || fs.existsSync(path.resolve(dir)) const same = text => text const done = file => err => { if (err) throw err console.log("Wrote:", file) } -const mapFile = ({from, to, map = same}, cb) => { - if (void 0 === cb) cb = done(to) - from = path.resolve(from) - to = path.resolve(to) - fs.readFile(from, utf8, (err, text) => { +const mapFile = ({from, to, map=same}, cb=done(to)) => { + const write = text => { + fs.writeFile(path.resolve(to), text, utf8, cb) + } + + fs.readFile(path.resolve(from), utf8, (err, text) => { if (err) throw err text = map(text) - if (void 0 === text) cb(new Error("text => undefined")) - else fs.writeFile(to, text, utf8, cb) + if (void 0 === text) { + cb(new Error("text => undefined")) + } else { + const destination = path.dirname(to) + if (made(destination)) write(text) + else fs.mkdir(path.resolve(destination), err => { + if (err) throw err + write(text) + }) + } }) } diff --git a/test-read.txt b/test-read.txt deleted file mode 100644 index b14df64..0000000 --- a/test-read.txt +++ /dev/null @@ -1 +0,0 @@ -Hi diff --git a/test-write.txt b/test-write.txt deleted file mode 100644 index 45b983b..0000000 --- a/test-write.txt +++ /dev/null @@ -1 +0,0 @@ -hi diff --git a/test.js b/test.js index a4374c3..b7340ee 100644 --- a/test.js +++ b/test.js @@ -1,23 +1,51 @@ const mapFile = require("./") const assert = require("assert") const fs = require("fs") -const from = "test-read.txt" -const to = "test-write.txt" +const cp = require("child_process") +const path = require("path") const utf8 = "utf8" -const transform = text => text.toLowerCase() +const folder = "test-folder" +const from = `${folder}/test-read.txt` +const original = "HELLO" +const method = "toLowerCase" +const transform = text => text[method]() +const cases = [ + `${folder}/test-write.txt`, + `${folder}/deep/test-write.txt` +] -fs.readFile(from, utf8, (err, original) => { - mapFile({ from, to, map: text => { - assert.strictEqual(text, original) - console.log(from, original.trim()) - return transform(text) - }}, err => { +cp.exec(`rm -rf ${folder} && mkdir ${folder}`, err => { + if (err) throw err + console.log("created:", folder) + console.log() + fs.writeFile(path.resolve(from), original, err => { if (err) throw err - fs.readFile(to, utf8, (err, output) => { + cases.forEach(to => { + mapFile({ from, to, map: text => { + assert.strictEqual(text, original) + console.log("read:", from) + console.log("text:", original.trim()) + console.log() + return transform(text) + }}, err => { + if (err) throw err + fs.readFile(to, utf8, (err, output) => { + if (err) throw err + assert.strictEqual(output, transform(original)) + console.log("map:", method) + console.log("wrote:", to) + console.log("text:", output.trim()) + console.log() + }) + }) + }) + cp.exec(`rm -rf ${folder}`, err => { if (err) throw err - console.log(to, output.trim()) - assert.strictEqual(output, transform(original)) - console.log("Tests passed =)") + console.log("deleted:", folder) + console.log() + console.log("tests: passed =)") + console.log(`node: ${process.version}`) + console.log() }) }) })