Skip to content

Commit 76380a6

Browse files
Meligyfilipesilva
authored andcommitted
feat(generate): create parent directories required for blueprints if they do not exist
Fix angular#3307 Close angular#3912
1 parent 1a5313d commit 76380a6

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

packages/angular-cli/utilities/dynamic-path-parser.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ module.exports = function dynamicPathParser(project, entityName) {
3333
} else if (fs.existsSync(withPlus)) {
3434
return withPlus;
3535
}
36-
37-
throw `Invalid path: "${withoutPlus}"" is not a valid path.`
36+
37+
// Folder not found, create it, and return it
38+
fs.mkdirSync(withoutPlus);
39+
return withoutPlus;
40+
3841
}, parsedOutputPath.root);
3942
outputPath = path.join(newPath, parsedOutputPath.name);
4043
}

tests/acceptance/generate-component.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ describe('Acceptance: ng generate component', function () {
178178
});
179179
});
180180

181+
it(`non${path.sep}existing${path.sep}dir${path.sep}myComp will create dir and succeed`, () => {
182+
const testPath =
183+
path.join(root, 'tmp', 'foo', 'src', 'app', 'non', 'existing', 'dir', 'my-comp', 'my-comp.component.ts');
184+
const appModule = path.join(root, 'tmp', 'foo', 'src', 'app', 'app.module.ts');
185+
return ng(['generate', 'component', `non${path.sep}existing${path.sep}dir${path.sep}myComp`])
186+
.then(() => expect(existsSync(testPath)).to.equal(true))
187+
.then(() => readFile(appModule, 'utf-8'))
188+
.then(content => {
189+
// Expect that the app.module contains a reference to my-comp and its import.
190+
expect(content)
191+
.matches(/import.*MyCompComponent.*from '.\/non\/existing\/dir\/my-comp\/my-comp.component';/);
192+
});
193+
});
194+
181195
it('my-comp --inline-template', function () {
182196
return ng(['generate', 'component', 'my-comp', '--inline-template']).then(() => {
183197
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-comp', 'my-comp.component.html');

tests/e2e/tests/generate/component/component-path-case.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import {join} from 'path';
22
import {ng} from '../../../utils/process';
3-
import {expectFileToExist, createDir} from '../../../utils/fs';
3+
import {expectFileToExist} from '../../../utils/fs';
44

55

66
export default function() {
7-
const rootDir = join('src', 'app', 'Upper-Dir');
8-
createDir(rootDir);
7+
const upperDirs = join('non', 'existing', 'dir');
8+
const rootDir = join('src', 'app', upperDirs);
9+
910
const componentDir = join(rootDir, 'test-component');
1011
const componentTwoDir = join(rootDir, 'test-component-two');
1112

12-
return ng('generate', 'component', 'Upper-Dir/test-component')
13+
return ng('generate', 'component', `${upperDirs}/test-component`)
1314
.then(() => expectFileToExist(componentDir))
1415
.then(() => expectFileToExist(join(componentDir, 'test-component.component.ts')))
1516
.then(() => expectFileToExist(join(componentDir, 'test-component.component.spec.ts')))
1617
.then(() => expectFileToExist(join(componentDir, 'test-component.component.html')))
1718
.then(() => expectFileToExist(join(componentDir, 'test-component.component.css')))
18-
.then(() => ng('generate', 'component', 'Upper-Dir/Test-Component-Two'))
19+
.then(() => ng('generate', 'component', `${upperDirs}/Test-Component-Two`))
1920
.then(() => expectFileToExist(join(componentTwoDir, 'test-component-two.component.ts')))
2021
.then(() => expectFileToExist(join(componentTwoDir, 'test-component-two.component.spec.ts')))
2122
.then(() => expectFileToExist(join(componentTwoDir, 'test-component-two.component.html')))

0 commit comments

Comments
 (0)