Skip to content

Commit

Permalink
Return paths of created files to the callback on success.
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshua Wuyts <i@yoshuawuyts.com>

Closes #6
  • Loading branch information
insin authored and yoshuawuyts committed Dec 21, 2015
1 parent e6544d2 commit af17c6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ const vars = { foo: 'bar' }
const inDir = path.join(process.cwd(), 'templates')
const outDir = path.join(process.cwd(), 'dist')

copy(inDir, outDir, vars, (err) => {
copy(inDir, outDir, vars, (err, createdFiles) => {
if (err) throw err
createdFiles.forEach(filePath => console.log(`Created ${filePath}`))
console.log('done!')
})
```
Expand All @@ -37,7 +38,8 @@ with variables. Takes the following arguments:
with a `_`. Files are populated with variables using the `{{varName}}` syntax.
- __targetDir__: the output directory
- __vars__: An object with variables that are injected into the template files.
- __cb(err)__: A callback that is called on completion.
- __cb(err, createdFiles)__: A callback that is called on completion, with
paths to created files if there were no errors.

## See Also
- [maxstache-stream](https://github.com/yoshuawuyts/maxstache-stream)
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ function copyTemplateDir (srcDir, outDir, vars, cb) {

const rs = readdirp({ root: srcDir })
const streams = []
const createdFiles = []

// create a new stream for every file emitted
rs.on('data', function (file) {
createdFiles.push(path.join(outDir, removeUnderscore(file.path)))
streams.push(writeFile(outDir, vars, file))
})

// delegate errors & close streams
eos(rs, function (err) {
if (err) return cb(err)
parallel(streams, cb)
parallel(streams, function (err) {
if (err) return cb(err)
cb(null, createdFiles)
})
})
})
}
Expand Down
29 changes: 19 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,34 @@ test('should assert input values', function (t) {
})

test('should write a bunch of files', function (t) {
t.plan(11)
t.plan(21)

function checkCreatedFileNames (names, check) {
t.notEqual(names.indexOf('.a'), -1, '.a ' + check)
t.notEqual(names.indexOf('c'), -1, 'c ' + check)
t.notEqual(names.indexOf('1.txt'), -1, '1.txt ' + check)
t.notEqual(names.indexOf('2.txt'), -1, '2.txt ' + check)
t.notEqual(names.indexOf('3.txt'), -1, '3.txt ' + check)
t.notEqual(names.indexOf('foo' + path.sep + '.b'), -1, 'foo/.b ' + check)
t.notEqual(names.indexOf('foo' + path.sep + 'd'), -1, 'foo/d ' + check)
t.notEqual(names.indexOf('foo' + path.sep + '4.txt'), -1, 'foo/4.txt ' + check)
}

const inDir = path.join(__dirname, 'fixtures')
const outDir = path.join(__dirname, '../tmp')
copy(inDir, outDir, function (err) {
copy(inDir, outDir, function (err, createdFiles) {
t.error(err)
t.ok(Array.isArray(createdFiles), 'createdFiles is an array')
t.equal(createdFiles.length, 8)
checkCreatedFileNames(createdFiles.map(function (filePath) {
return path.relative(outDir, filePath)
}), 'reported as created')

readdirp({ root: outDir }).pipe(concat({ object: true }, function (arr) {
t.ok(Array.isArray(arr), 'is array')

const names = arr.map(function (file) { return file.path })
t.notEqual(names.indexOf('.a'), -1, '.a exists')
t.notEqual(names.indexOf('c'), -1, 'c exists')
t.notEqual(names.indexOf('1.txt'), -1, '1.txt exists')
t.notEqual(names.indexOf('2.txt'), -1, '2.txt exists')
t.notEqual(names.indexOf('3.txt'), -1, '3.txt exists')
t.notEqual(names.indexOf('foo' + path.sep + '.b'), -1, 'foo/.b exists')
t.notEqual(names.indexOf('foo' + path.sep + 'd'), -1, 'foo/d exists')
t.notEqual(names.indexOf('foo' + path.sep + '4.txt'), -1, 'foo/4.txt exists')
checkCreatedFileNames(names, 'exists')

rimraf(outDir, function (err) {
t.error(err)
Expand Down

0 comments on commit af17c6b

Please sign in to comment.