Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the template containing directory to be excluded from the output path #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ If you specify a folder as a template, then the folder will be created in the ou

Cookiecutter will recursively copy all files within your templates folder and replace any occurrences of your specified fields.

If you don't want this behavior, specify a file as your template's `templatePath`.
The default behavior is to add a new folder at the destination; `destinationPath/ComponentName`. This can be avoided by adding `excludeDirectory: true` to your configuration file.

## Custom config location

Expand Down
4 changes: 2 additions & 2 deletions src/lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function replaceFields(string, fields) {

function renderFiles({templateName, fields}, configLocation) {
const config = getTemplateConfig(templateName, configLocation);
const excludeDirectory = config.excludeDirectory || false;
const pwd = process.env.PWD;
const destinationDirectory = path.resolve(pwd, config.outputPath);
const templateDirectory = path.resolve(pwd, config.templatePath);
Expand Down Expand Up @@ -51,12 +52,11 @@ function renderFiles({templateName, fields}, configLocation) {

if (isFolderTemplate) {
const {base} = path.parse(templateDirectory);

return {
src: filePath,
dest: path.join(
destinationDirectory,
replaceFields(base, fields),
(!excludeDirectory ? replaceFields(base, fields) : ''),
replaceFields(filePath.replace(templateDirectory, ''), fields)
),
};
Expand Down
21 changes: 20 additions & 1 deletion src/lib/tests/__snapshots__/renderer.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ exports[`renderFiles() should correctly render the files when the template is a
"
`;

exports[`renderFiles() should correctly render the files when the template is a folder excluding the containing directory if set 1`] = `
"console.log('Bar');
"
`;

exports[`renderFiles() should correctly render the files when the template is a folder excluding the containing directory if set 2`] = `
"import React from 'react';

const number = A;

class Bar React.Component {
render() {
return <div>Hello world {number}</div>;
}
}

export default Bar"
`;

exports[`renderFiles() should correctly render the files when the template is just a path 1`] = `
"import React from 'react';

Expand All @@ -41,4 +60,4 @@ class Baz React.Component {
export default Baz"
`;

exports[`renderFiles() should throw an error if the output of a template allready exists 1`] = `"src/lib/tests/test-templates-output/Bar/index.js already exists."`;
exports[`renderFiles() should throw an error if the output of a template already exists 1`] = `"src/lib/tests/test-templates-output/Bar/index.js already exists."`;
24 changes: 23 additions & 1 deletion src/lib/tests/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,29 @@ describe('renderFiles()', () => {
expect(fs.readFileSync(`${outputPath }/Bar/sub-folder/Bar.js`, 'utf8')).toMatchSnapshot();
});

it('should throw an error if the output of a template allready exists', () => {
it('should correctly render the files when the template is a folder excluding the containing directory if set', () => {

getTemplateConfig.mockImplementation(() => {
return {
templatePath: `${__dirname }/test-templates/Foo`,
excludeDirectory: true,
outputPath,
};
});

renderFiles({
templateName: '',
fields: {
COMPONENT_NAME: 'Bar',
specialNumber: 'A',
},
});

expect(fs.readFileSync(`${outputPath }/Bar/Bar.js`, 'utf8')).toMatchSnapshot();
expect(fs.readFileSync(`${outputPath }/index.js`, 'utf8')).toMatchSnapshot();
});

it('should throw an error if the output of a template already exists', () => {

getTemplateConfig.mockImplementation(() => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('COMPONENT_NAME');
11 changes: 11 additions & 0 deletions src/lib/tests/test-templates/Foo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

const number = specialNumber;

class COMPONENT_NAME React.Component {
render() {
return <div>Hello world {number}</div>;
}
}

export default COMPONENT_NAME