From 55afe0f7a116af62949532d869eb25bb24adb9e5 Mon Sep 17 00:00:00 2001 From: Bram Borggreve Date: Fri, 12 Feb 2021 14:54:04 -0500 Subject: [PATCH] fix(devkit): generateFiles should rename paths recursively --- .../src/generators/generate-files.spec.ts | 58 +++++++++++++++++++ .../devkit/src/generators/generate-files.ts | 2 +- .../__name__.service.ts__tmpl__ | 0 .../src/__name__.module.ts__tmpl__ | 0 .../__projectName__.__name__.model.ts__tmpl__ | 0 .../create-__name__.input.ts__tmpl__ | 0 .../output/__dot__gitkeep__tmpl__ | 0 .../generate-files/src/common-util.ts__tmpl__ | 0 8 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 packages/devkit/src/generators/generate-files.spec.ts create mode 100644 packages/devkit/src/tests/generate-files/__name__.service.ts__tmpl__ create mode 100644 packages/devkit/src/tests/generate-files/src/__name__.module.ts__tmpl__ create mode 100644 packages/devkit/src/tests/generate-files/src/__projectName__/__name__/__projectName__.__name__.model.ts__tmpl__ create mode 100644 packages/devkit/src/tests/generate-files/src/__projectName__/create-__name__.input.ts__tmpl__ create mode 100644 packages/devkit/src/tests/generate-files/src/__projectName__/output/__dot__gitkeep__tmpl__ create mode 100644 packages/devkit/src/tests/generate-files/src/common-util.ts__tmpl__ diff --git a/packages/devkit/src/generators/generate-files.spec.ts b/packages/devkit/src/generators/generate-files.spec.ts new file mode 100644 index 00000000000000..af0ce67e3a6bae --- /dev/null +++ b/packages/devkit/src/generators/generate-files.spec.ts @@ -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`, + ]); + }); +}); diff --git a/packages/devkit/src/generators/generate-files.ts b/packages/devkit/src/generators/generate-files.ts index 051382fafa6aa0..c6a6c0de39f1f0 100644 --- a/packages/devkit/src/generators/generate-files.ts +++ b/packages/devkit/src/generators/generate-files.ts @@ -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; } diff --git a/packages/devkit/src/tests/generate-files/__name__.service.ts__tmpl__ b/packages/devkit/src/tests/generate-files/__name__.service.ts__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/devkit/src/tests/generate-files/src/__name__.module.ts__tmpl__ b/packages/devkit/src/tests/generate-files/src/__name__.module.ts__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/devkit/src/tests/generate-files/src/__projectName__/__name__/__projectName__.__name__.model.ts__tmpl__ b/packages/devkit/src/tests/generate-files/src/__projectName__/__name__/__projectName__.__name__.model.ts__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/devkit/src/tests/generate-files/src/__projectName__/create-__name__.input.ts__tmpl__ b/packages/devkit/src/tests/generate-files/src/__projectName__/create-__name__.input.ts__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/devkit/src/tests/generate-files/src/__projectName__/output/__dot__gitkeep__tmpl__ b/packages/devkit/src/tests/generate-files/src/__projectName__/output/__dot__gitkeep__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/packages/devkit/src/tests/generate-files/src/common-util.ts__tmpl__ b/packages/devkit/src/tests/generate-files/src/common-util.ts__tmpl__ new file mode 100644 index 00000000000000..e69de29bb2d1d6