Skip to content

Commit

Permalink
Tweeze filenames for tokens, fix custom delimiters
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
RyanZim committed May 16, 2017
1 parent 7f8b1fe commit e5f99ab
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
42 changes: 25 additions & 17 deletions lib/rock.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function fetchFile (filePath, rock, options) {
var files = [filePath]
return tweezers.readFilesAndExtract(files, topen, tclose)
})
.then(function promptUser (tokenObj) {
.then(function promptUser (tokens) {
var replacements = {
'-file': topen + '-file' + tclose,
'-literal': topen
Expand All @@ -56,7 +56,7 @@ function fetchFile (filePath, rock, options) {
replacements = rutil.extend(replacements, options.templateValues)

return new Promise(function (resolve) {
rlp(tokenObj.tokens, options.defaultValues, replacements).end(function (results) {
rlp(tokens, options.defaultValues, replacements).end(function (results) {
resolve(results)
})
})
Expand All @@ -76,6 +76,10 @@ function fetchRepo (projectPath, repoPath, options) {
var projectRockPath = path.join(projectPath, '.rock')
var projectRockConf = path.join(projectRockPath, 'rock.json')
var projectRockObj = {} // empty config
var delimiters = {
open: '{{',
close: '}}'
}

if (!options) options = {}
if (!options.defaultValues) options.defaultValues = {}
Expand Down Expand Up @@ -106,10 +110,7 @@ function fetchRepo (projectPath, repoPath, options) {
if (data) {
projectRockObj = data

if (projectRockObj.tokens) {
S.TMPL_OPEN = projectRockObj.tokens.open || S.TMPL_OPEN
S.TMPL_CLOSE = projectRockObj.tokens.close || S.TMPL_CLOSE
}
if (projectRockObj.tokens) delimiters = projectRockObj.tokens
}
})
}
Expand Down Expand Up @@ -141,40 +142,47 @@ function fetchRepo (projectPath, repoPath, options) {
})
.then(function tweezeFiles (files) {
// console.log('tweezefiles')
return tweezers.readFilesAndExtract(files, S.TMPL_OPEN, S.TMPL_CLOSE)
return tweezers.readFilesAndExtract(files, delimiters.open, delimiters.close)
.then(function (tokens) {
tokens = rutil.dedupe(tokens.concat(tweezers.extractArray(files))).sort()
return {
tokens,
files
}
})
})
.then(function promptUser (tokenObj) {
.then(function promptUser (obj) {
var { tokens, files } = obj
var replacements = {
'-file': S.TMPL_OPEN + '-file' + S.TMPL_CLOSE,
'-literal': S.TMPL_OPEN
'-file': delimiters.open + '-file' + delimiters.close,
'-literal': delimiters.open
}

replacements = rutil.extend(replacements, dt.eval('-date-'))
replacements = rutil.extend(replacements, options.templateValues)

return new Promise(function (resolve) {
rlp(tokenObj.tokens, options.defaultValues, replacements).end(function (results) {
rlp(tokens, options.defaultValues, replacements).end(function (results) {
resolve({
tokenObj: tokenObj,
files,
replacements: results
})
})
})
})
.then(function outputFiles (results) {
var tokenObj = results.tokenObj
var replacements = results.replacements
delete tokenObj.tokens // we just want to process the files
var files = results.files

// For each file:
return Promise.all(Object.keys(tokenObj).map(function (file) {
return Promise.all(files.map(function (file) {
// Read it:
return fs.readFile(file, 'utf8')
.then(function renderTemplate (data) {
var newFileName = S(file).template(replacements).s
var newFileName = S(file).template(replacements, delimiters.open, delimiters.close).s
replacements['-file'] = path.basename(newFileName)

var newFileData = S(data).template(replacements).s
var newFileData = S(data).template(replacements, delimiters.open, delimiters.close).s

return fs.writeFile(newFileName, newFileData)
.then(function () {
Expand Down
14 changes: 6 additions & 8 deletions lib/tweezers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var fs = require('fs-promise')
var tweeze = require('tweezers')
var rutil = require('./util')

var me = module.exports
me.open = null
Expand All @@ -14,24 +13,23 @@ function readFileAndExtract (file, callback) {
}

function readFilesAndExtract (files, open, close) {
var fileObj = {}
var tokens = {}
var tokens = []

me.open = open || me.open
me.close = close || me.open

return Promise.all(files.map(function (file) {
return readFileAndExtract(file)
.then(function (obj) {
rutil.extend(tokens, obj)
fileObj[file] = Object.keys(obj)
tokens = tokens.concat(Object.keys(obj))
})
}))
.then(function () {
fileObj.tokens = Object.keys(tokens)
fileObj.tokens.sort()
return fileObj
return tokens
})
}

module.exports.readFilesAndExtract = readFilesAndExtract
module.exports.extractArray = function (array) {
return array.map(item => Object.keys(tweeze(item))).reduce((a, b) => a.concat(b), [])
}
5 changes: 5 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ me.extend = function (o1, o2) {
})
return o1
}

// This only works if the items are primitives
me.dedupe = function (a) {
return a.filter((item, pos) => a.indexOf(item) === pos)
}
1 change: 1 addition & 0 deletions test/resources/rocks/filename/{{test}}.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello World!')
23 changes: 23 additions & 0 deletions test/rock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var TEST_PATH = ''

var rockRepo1 = P('test/resources/rocks/node-lib')
var rockRepo2 = P('test/resources/rocks/node-lib-tmpl')
var rockRepo3 = P('test/resources/rocks/filename')

function AFE (file1, file2) {
EQ(fs.readFileSync(file1).toString(), fs.readFileSync(file2).toString())
Expand All @@ -34,6 +35,28 @@ describe('rock', function () {
return TEST(rockRepo2)
})
})

describe('> when tokens are only in filenames', function () {
it('should work anyway', function () {
var testPath = path.join(TEST_PATH, 'filename-test')

// Make test dir:
fs.mkdirSync(testPath)
process.chdir(testPath)

var templateValues = {
'test': 'hello-world'
}

return rock.fetchRepo('project', rockRepo3, {templateValues: templateValues})
.then(function () {
var outDir = path.join(testPath, 'project')

assert(fs.existsSync(outDir))
assert(fs.existsSync(path.join(outDir, 'hello-world.js')))
})
})
})
})
})

Expand Down

0 comments on commit e5f99ab

Please sign in to comment.