From b98f1d0b7ee69a96f86c7d7fcdaf0b6ff111b2fa Mon Sep 17 00:00:00 2001 From: Krzysztof Jan Modras Date: Thu, 24 Mar 2016 11:14:21 +0100 Subject: [PATCH] handle nested paths Quick fix to handle path like: `dir1/dir2/file.js` --- index.js | 2 +- test/index.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index dda0d8e..79ceb42 100644 --- a/index.js +++ b/index.js @@ -95,7 +95,7 @@ ConfigReplace.prototype.processFile = function(config, filePath) { ConfigReplace.prototype.writeFile = function(destPath, contents) { if (!fs.existsSync(path.dirname(destPath))) { - fs.mkdirSync(path.dirname(destPath)); + fs.mkdirpSync(path.dirname(destPath)); } fs.writeFileSync(destPath, contents, { encoding: 'utf8' }); diff --git a/test/index.js b/test/index.js index b57cef5..0956f66 100644 --- a/test/index.js +++ b/test/index.js @@ -159,4 +159,36 @@ describe('config-replace', function() { expect(newContent).to.not.equal(oldContent); }); }); + + it('handle nested paths', function() { + var root, configReplace, builder; + + root = tmp.in(join(process.cwd(), 'tmp')); + + fs.writeFileSync(join(root, 'config.json'), '{"color":"red"}'); + fs.mkdirSync(join(root, 'dir1')); + fs.mkdirSync(join(root, 'dir1', 'dir2')); + fs.writeFileSync(join(root, 'dir1', 'dir2', 'index.html'), "{{color}}"); + + configReplace = new ConfigReplace( + root, + root, { + files: ['dir1/dir2/index.html'], + configPath: 'config.json', + patterns: [{ + match: /\{\{color\}\}/g, + replacement: 'red' + }] + } + ); + + builder = new broccoli.Builder(configReplace); + + return builder.build().then(function(results) { + var resultsPath = join(results.directory, 'dir1', 'dir2', 'index.html'), + contents = fs.readFileSync(resultsPath, { encoding: 'utf8' }); + + assert.equal(contents.trim(), 'red'); + }) + }); });