Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir as needed #3

Merged
merged 1 commit into from
Jan 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ npm install map-file

### <code>mapFile({from, to, map=<var>identity</var>}, callback=<var>done</var>)</code>

- `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

Expand Down
24 changes: 17 additions & 7 deletions map-file.js
Original file line number Diff line number Diff line change
@@ -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)
})
}
})
}

Expand Down
1 change: 0 additions & 1 deletion test-read.txt

This file was deleted.

1 change: 0 additions & 1 deletion test-write.txt

This file was deleted.

54 changes: 41 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})