Skip to content

Commit

Permalink
Merge pull request #9 from loklaan/add-filename-injection
Browse files Browse the repository at this point in the history
Support variable injection in filenames
  • Loading branch information
loklaan committed Aug 10, 2016
2 parents 1785004 + bdd2088 commit 65568a6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ Copy a directory of files over to the target directory, and inject the files
with variables. Takes the following arguments:
- __templateDir__: The directory that holds the templates. Filenames prepended
with a `_` will have it removed when copying. Dotfiles need to be prepended
with a `_`. Files are populated with variables using the `{{varName}}` syntax.
with a `_`. Files and filenames are populated with variables using the
`{{varName}}` syntax.
- __targetDir__: the output directory
- __vars__: An object with variables that are injected into the template files.
- __vars__: An object with variables that are injected into the template files
and file names.
- __cb(err, createdFiles)__: A callback that is called on completion, with
paths to created files if there were no errors.

Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const maxstache = require('maxstache-stream')
const maxstacheStream = require('maxstache-stream')
const maxstache = require('maxstache')
const parallel = require('run-parallel')
const eos = require('end-of-stream')
const readdirp = require('readdirp')
Expand Down Expand Up @@ -35,7 +36,7 @@ function copyTemplateDir (srcDir, outDir, vars, cb) {

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

Expand All @@ -57,13 +58,13 @@ function writeFile (outDir, vars, file) {
const fileName = file.path
const inFile = file.fullPath
const parentDir = file.parentDir
const outFile = path.join(outDir, removeUnderscore(fileName))
const outFile = path.join(outDir, maxstache(removeUnderscore(fileName), vars))

mkdirp(path.join(outDir, parentDir), function (err) {
if (err) return done(err)

const rs = fs.createReadStream(inFile)
const ts = maxstache(vars)
const ts = maxstacheStream(vars)
const ws = fs.createWriteStream(outFile)

pump(rs, ts, ws, done)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"end-of-stream": "^1.1.0",
"graceful-fs": "^4.1.3",
"maxstache": "^1.0.0",
"maxstache-stream": "^1.0.0",
"mkdirp": "^0.5.1",
"noop2": "^2.0.0",
Expand Down
Empty file added test/fixtures/{{foo}}.txt
Empty file.
28 changes: 26 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ test('should assert input values', function (t) {
})

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

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('.txt'), -1, '.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)
Expand All @@ -34,7 +35,7 @@ test('should write a bunch of files', function (t) {
copy(inDir, outDir, function (err, createdFiles) {
t.error(err)
t.ok(Array.isArray(createdFiles), 'createdFiles is an array')
t.equal(createdFiles.length, 8)
t.equal(createdFiles.length, 9)
checkCreatedFileNames(createdFiles.map(function (filePath) {
return path.relative(outDir, filePath)
}), 'reported as created')
Expand Down Expand Up @@ -77,3 +78,26 @@ test('should inject context variables strings', function (t) {
}))
})
})

test('should inject context variables strings into filenames', function (t) {
t.plan(4)

const inDir = path.join(__dirname, 'fixtures')
const outDir = path.join(__dirname, '../tmp')
copy(inDir, outDir, { foo: 'bar' }, function (err) {
t.error(err)

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

const file = path.join(outDir, 'bar.txt')
fs.access(file, function (err, chunk) {
t.error(err, 'bar.txt exists')

rimraf(outDir, function (err) {
t.error(err)
})
})
}))
})
})

0 comments on commit 65568a6

Please sign in to comment.