Skip to content

Commit

Permalink
Prepopulate metalsmith metadata (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarmicki authored and dlmr committed Jun 13, 2017
1 parent 426fda3 commit 0de8db5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/commands/init/generateTemplate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ Handlebars.registerHelper('unless_eq', (a, b, opts) => (
export default function generateTemplate(name, src, dest, done) {
const opts = getOptions(name, src);
const metalsmith = new Metalsmith(path.join(src, 'template'));
const data = {
...metalsmith.metadata(),
metalsmith.metadata({
destDirName: path.relative(process.cwd(), dest),
inPlace: dest === process.cwd(),
};
});

if (opts.helpers) {
Object.keys(opts.helpers).forEach((key) => {
Expand All @@ -53,7 +52,7 @@ export default function generateTemplate(name, src, dest, done) {
}
log.success('Project created');
done();
logMessage(opts.completionMessage, data);
logMessage(opts.completionMessage, metalsmith.metadata());
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
prompts: {
hello: {
type: 'input',
required: true,
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{{ hello }}}
{{{ destDirName }}}
{{{ inPlace }}}
17 changes: 17 additions & 0 deletions test/commands/init/generateTemplate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ describe('commands', () => {
);
});
});

it('should always supply destDirName and inPlace data to templates', () => {
const outputDir = join(__dirname, '_output', 'alwaysSet');
generateTemplate('hey', join(__dirname, 'fixtures', 'alwaysSet'), outputDir, () => {});

return answerPrompts([
'something',
])
.then(() => readFile(join(outputDir, 'test-input')))
.then(rendered => {
expect(rendered).toBe(
'something\n' +
join('test', 'commands', 'init', 'generateTemplate', '_output', 'alwaysSet') + '\n' +
'false\n'
);
});
});
});
});
});

0 comments on commit 0de8db5

Please sign in to comment.