From c660515c607e955a407969598fb93ebcf9757602 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 11 Apr 2017 16:36:51 -0700 Subject: [PATCH 1/2] feat(@angular/cli): making app root configurable --- packages/@angular/cli/blueprints/component/index.ts | 3 ++- packages/@angular/cli/blueprints/directive/index.ts | 3 ++- packages/@angular/cli/blueprints/ng/files/angular-cli.json | 1 + packages/@angular/cli/blueprints/pipe/index.ts | 3 ++- packages/@angular/cli/lib/config/schema.json | 7 ++++++- packages/@angular/cli/utilities/dynamic-path-parser.ts | 2 +- packages/@angular/cli/utilities/find-parent-module.ts | 6 +++--- 7 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/@angular/cli/blueprints/component/index.ts b/packages/@angular/cli/blueprints/component/index.ts index a7d311b74f13..5dd4ffd900d1 100644 --- a/packages/@angular/cli/blueprints/component/index.ts +++ b/packages/@angular/cli/blueprints/component/index.ts @@ -114,7 +114,8 @@ export default Blueprint.extend({ resolveModulePath(options.module, this.project, this.project.root, appConfig); } else { try { - this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath); + this.pathToModule = + findParentModule(this.project.root, appConfig.root, this.generatePath, appConfig.appRoot); } catch (e) { if (!options.skipImport) { throw `Error locating module for declaration\n\t${e}`; diff --git a/packages/@angular/cli/blueprints/directive/index.ts b/packages/@angular/cli/blueprints/directive/index.ts index d5c9bf0f0b79..6b9cb8689b0a 100644 --- a/packages/@angular/cli/blueprints/directive/index.ts +++ b/packages/@angular/cli/blueprints/directive/index.ts @@ -67,7 +67,8 @@ export default Blueprint.extend({ resolveModulePath(options.module, this.project, this.project.root, appConfig); } else { try { - this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath); + this.pathToModule = + findParentModule(this.project.root, appConfig.root, this.generatePath, appConfig.appRoot); } catch (e) { if (!options.skipImport) { throw `Error locating module for declaration\n\t${e}`; diff --git a/packages/@angular/cli/blueprints/ng/files/angular-cli.json b/packages/@angular/cli/blueprints/ng/files/angular-cli.json index 93a828e33062..02fe9d2a3826 100644 --- a/packages/@angular/cli/blueprints/ng/files/angular-cli.json +++ b/packages/@angular/cli/blueprints/ng/files/angular-cli.json @@ -5,6 +5,7 @@ }, "apps": [ { + "appRoot": "app", "root": "<%= sourceDir %>", "outDir": "dist", "assets": [ diff --git a/packages/@angular/cli/blueprints/pipe/index.ts b/packages/@angular/cli/blueprints/pipe/index.ts index b8e7242bbd79..bf7653779c11 100644 --- a/packages/@angular/cli/blueprints/pipe/index.ts +++ b/packages/@angular/cli/blueprints/pipe/index.ts @@ -61,7 +61,8 @@ export default Blueprint.extend({ resolveModulePath(options.module, this.project, this.project.root, appConfig); } else { try { - this.pathToModule = findParentModule(this.project.root, appConfig.root, this.generatePath); + this.pathToModule = + findParentModule(this.project.root, appConfig.root, this.generatePath, appConfig.appRoot); } catch (e) { if (!options.skipImport) { throw `Error locating module for declaration\n\t${e}`; diff --git a/packages/@angular/cli/lib/config/schema.json b/packages/@angular/cli/lib/config/schema.json index a18bae04ae31..2cf6244e6855 100644 --- a/packages/@angular/cli/lib/config/schema.json +++ b/packages/@angular/cli/lib/config/schema.json @@ -33,9 +33,14 @@ "type": "string", "description": "Name of the app." }, - "root": { + "appRoot": { "type": "string", + "default": "app", "description": "The root directory of the app." + }, + "root": { + "type": "string", + "description": "The root src directory for the apps." }, "outDir": { "type": "string", diff --git a/packages/@angular/cli/utilities/dynamic-path-parser.ts b/packages/@angular/cli/utilities/dynamic-path-parser.ts index fd0b259bc4ea..4c0d840342d8 100644 --- a/packages/@angular/cli/utilities/dynamic-path-parser.ts +++ b/packages/@angular/cli/utilities/dynamic-path-parser.ts @@ -13,7 +13,7 @@ export interface DynamicPathOptions { export function dynamicPathParser(options: DynamicPathOptions) { const projectRoot = options.project.root; const sourceDir = options.appConfig.root; - const appRoot = path.join(sourceDir, 'app'); + const appRoot = path.join(sourceDir, options.appConfig.appRoot || 'app'); const cwd = process.env.PWD; const rootPath = path.join(projectRoot, appRoot); diff --git a/packages/@angular/cli/utilities/find-parent-module.ts b/packages/@angular/cli/utilities/find-parent-module.ts index 027ed87ed5c3..33807a88e0e7 100644 --- a/packages/@angular/cli/utilities/find-parent-module.ts +++ b/packages/@angular/cli/utilities/find-parent-module.ts @@ -3,12 +3,12 @@ import * as path from 'path'; const SilentError = require('silent-error'); export default function findParentModule( - projectRoot: string, appRoot: string, currentDir: string): string { + projectRoot: string, srcRoot: string, currentDir: string, appRoot = 'app'): string { - const sourceRoot = path.join(projectRoot, appRoot, 'app'); + const sourceRoot = path.join(projectRoot, srcRoot, appRoot); // trim currentDir - currentDir = currentDir.replace(path.join(appRoot, 'app'), ''); + currentDir = currentDir.replace(path.join(srcRoot, appRoot), ''); let pathToCheck = path.join(sourceRoot, currentDir); From cc5c7bb9b5e3e6828c02ee23f16b551c717a025b Mon Sep 17 00:00:00 2001 From: Sumit Arora Date: Tue, 25 Apr 2017 15:46:28 -0400 Subject: [PATCH 2/2] test: adding tests for appRoot config --- tests/e2e/tests/commands/set/set-approot.ts | 26 +++++++++++++++++++++ tests/e2e/utils/fs.ts | 11 +++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/e2e/tests/commands/set/set-approot.ts diff --git a/tests/e2e/tests/commands/set/set-approot.ts b/tests/e2e/tests/commands/set/set-approot.ts new file mode 100644 index 000000000000..1a337cd5fb08 --- /dev/null +++ b/tests/e2e/tests/commands/set/set-approot.ts @@ -0,0 +1,26 @@ +import {ng} from '../../../utils/process'; +import {rename} from '../../../utils/fs'; + +export default function() { + return Promise.resolve() + .then(() => ng('get', 'apps.0.appRoot')) + .then(({ stdout }) => { + if (!stdout.match(/app/)) { + throw new Error(`Expected "app", received "${JSON.stringify(stdout)}".`); + } + }) + .then(() => ng('set', 'apps.0.appRoot' , 'app1')) + .then(() => ng('get', 'apps.0.appRoot')) + .then(({ stdout }) => { + if (!stdout.match(/app1/)) { + throw new Error(`Expected "app1", received "${JSON.stringify(stdout)}".`); + } + }) + .then(() => rename('./src/app', './src/app1')) + .then(() => ng('g', 'c', 'hello', '-d')) + .then(({ stdout }) => { + if (!stdout.match(/app1/)) { + throw new Error(`Expected "app", received "${JSON.stringify(stdout)}".`); + } + }); +} diff --git a/tests/e2e/utils/fs.ts b/tests/e2e/utils/fs.ts index 0ffba4787393..0c46716b6a91 100644 --- a/tests/e2e/utils/fs.ts +++ b/tests/e2e/utils/fs.ts @@ -71,6 +71,17 @@ export function createDir(path: string) { _recursiveMkDir(path); } +export function rename(from: string, to: string) { + return new Promise((resolve, reject) => { + fs.rename(from, to, (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); +} function _recursiveMkDir(path: string) { if (fs.existsSync(path)) {