Skip to content

Commit

Permalink
fix(devkit): generateFiles should rename paths recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Feb 12, 2021
1 parent 6be07fb commit 55afe0f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/devkit/src/generators/generate-files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Tree } from '@nrwl/devkit';
import { createTree } from '@nrwl/devkit/testing';
import { join } from 'path';
import { generateFiles } from './generate-files';

function recursiveTree(
tree: Tree,
root: string,
path: string = '',
paths = []
): string[] {
const full = join(root, path);

if (!tree.exists(full)) {
return undefined;
}

if (!tree.isFile(full)) {
const children = tree.children(full);

return children.reduce((acc, curr) => {
return recursiveTree(tree, root, join(path, curr), paths);
}, []);
}
paths.push(path);

return paths.sort();
}

describe('generateFiles', () => {
let tree: Tree;

beforeEach(() => {
tree = createTree();
});

it('should generate files from template in a directory', () => {
const target = './tools/target';
const name = 'my-project';
const projectName = 'my-project-api';

generateFiles(tree, join(__dirname, '../tests/generate-files'), target, {
dot: '.',
name,
projectName,
tmpl: '',
});

expect(recursiveTree(tree, target)).toEqual([
`${name}.service.ts`,
`src/common-util.ts`,
`src/${projectName}/create-${name}.input.ts`,
`src/${projectName}/${name}/${projectName}.${name}.model.ts`,
`src/${projectName}/output/.gitkeep`,
`src/${name}.module.ts`,
]);
});
});
2 changes: 1 addition & 1 deletion packages/devkit/src/generators/generate-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function replaceSegmentsInPath(
substitutions: { [k: string]: any }
) {
Object.entries(substitutions).forEach(([t, r]) => {
filePath = filePath.replace(`__${t}__`, r);
filePath = filePath.split(`__${t}__`).join(r);
});
return filePath;
}
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 55afe0f

Please sign in to comment.