Skip to content

ng generate doesn't work with multiple apps #7418

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

Closed
mbrezovsky opened this issue Aug 17, 2017 · 14 comments
Closed

ng generate doesn't work with multiple apps #7418

mbrezovsky opened this issue Aug 17, 2017 · 14 comments
Assignees
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken

Comments

@mbrezovsky
Copy link

I have defined two applications in angular-cli.json with names app (default) and spa. When I generate any part of app (component, class, etc.), it is always generated to default application folder /app.
I tried to use option --app spa, it's ignored and files are generated to /app folder.

Any way how to fix that?

angular-cli.json

{
  "project": {
    "version": "1.3.0",
    "name": "web"
  },
  "apps": [
    {
      "name": "spa",
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "spa.html",
      "main": "main-spa.ts",
      "test": "test-spa.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "spa",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    },
    {
      "name": "app",
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "inline": {
      "style": false,
      "template": false
    },
    "spec": {
      "class": false,
      "component": true,
      "directive": true,
      "module": false,
      "pipe": true,
      "service": true
    }
  }
}

Versions.

ng version                   
    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.3.0
node: 8.2.1
os: linux x64
@angular/common: 2.4.2
@angular/compiler: 2.4.2
@angular/compiler-cli: 2.4.2
@angular/core: 2.4.2
@angular/forms: 2.4.0
@angular/http: 2.4.0
@angular/platform-browser: 2.4.0
@angular/platform-browser-dynamic: 2.4.0
@angular/platform-server: 2.4.2
@angular/router: 3.4.0
@angular/cli: 1.3.0

node -v      
v8.2.1
@deebloo
Copy link
Contributor

deebloo commented Aug 17, 2017

I think the value for --app is supposed to be the index of the app cofig not the name. So --app=1

@mbrezovsky
Copy link
Author

Using index of app in --app option created a module with the right selector in case of component but all files are gerenerated to wrong directory.
I have two applications with names app and spa with separated directories. Is it supported by angular-cli?

@joshvito
Copy link

+1
I have multiple app definitions in my .angular-cli.json file. When I use the ng g module my-module the new module always gets created in the /src/app/ directory.

How can I use generate command to do the following?

//exec from new project directory
// I would expect the following modules to be created
ng g module my-module --app app1  =>  /src/app1/my-module/my-module.module.ts
ng g module my-other --app app2  => /src/app2/my-other/my-other.module.ts

I've tried exec from the directory where I want the module to be generated too, and it still creates the module in the wrong location. e,g, /src/app

// exec from /project/src/my-spp/
ng g module my-module --app app1  => creates /project/src/app/my-module/my-module.module.ts

Am I missing a config setting?

See my app definitions below.

 "apps": [
    {
      "root": "src",
      "outDir": "dist/outcomes-mgmt",
      "assets": [],
      "index": "index.html",
      "main": "outcomes-mgmt.ts",
      "name": "outcomes-mgmt",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "out",
      "styles": [],
      "scripts": [],
      "environmentSource": ...,
      "environments": ...
    },
    {
      "root": "src",
      "outDir": "dist/planning-mgmt",
      "assets": [],
      "index": "index.html",
      "main": "planning-mgmt.ts",
      "name": "planning-mgmt",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "pln",
      "styles": [],
      "scripts": [],
      "environmentSource": ...,
      "environments": ...
    }
  ]

@grizzm0
Copy link
Contributor

grizzm0 commented Aug 20, 2017

The app folder is a hardcoded string and can not be changed. You need to change the root folder from "src" to "src/foobar". The path for the "application files" for that app will then be "src/foobar/app".

@colideum
Copy link

@grizzm0 so, if it is hardcoded, it means error? because as i understand --app is exactly made for the purpose of defining the place of the app

@grizzm0
Copy link
Contributor

grizzm0 commented Aug 21, 2017

Each app in the apps array in .angular-cli.json has the structure you see inside "src" in a new project.

If you want to create a new app you need to move the entire content of ./src to ./src/app-name and change the apps.0.root key from "src" to "src/app-name".

The new structure would be

src/app-name/app/app.module.ts
src/app-name/app/...
src/app-name/index.html
src/app-name/main.ts
src/app-name/...

There's currently an issue with sharing code between different apps in AOT. See #7309 for more info.

@Brocco Brocco self-assigned this Aug 29, 2017
@Brocco Brocco added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken labels Aug 29, 2017
@scurk1415
Copy link

I have the same problem. 2 applications (but both have different source directories - /src and /src-upp), but the CLI always generates in the first no matter if i use --app or not

@jehoshua02
Copy link

The reason for this is that hard coded 'app' is appended to the "root" from .angular-cli.json:

const appRoot = path.join(sourceDir, 'app');

See https://github.com/angular/angular-cli/blob/master/packages/@angular/cli/utilities/dynamic-path-parser.ts#L16

What is the reason for forcing everything into 'app'? This is troublesome especially for fitting angular-cli into existing hybrid projects where the "app" folder is already named something else, for example "ng2-app", and it is silly to have "ng2-app/app".

#showstopper

@mur-tha
Copy link

mur-tha commented Oct 4, 2017

If I understand this correctly, the CLI is using the app at index 0 to set the generate path and not adjusting when setting the --app flag. I got a workaround working by setting a dummy app as the first app in the array and only giving that app the root property.

"apps": [ { "root": "src" },

It seems that the "root" property is all it needs to work.

Then with this setup, I just enter the app folder I need as part of the module name in the generate command for the new item. This seems to put it in the right place.

ng g module spa/my-new-module
create src/app/spa/my-new-module/my-new-module.module.ts

@vecernik
Copy link

vecernik commented Oct 5, 2017

using 1.5.0-beta.3, setting "root": "src/extra-app" will disallow a compiler to use other (i.e. shared) sources, usually located in src/.
I tried to set "appRoot": "extra-app", but it's ignored:
ng g c extra-app/features/device/SideDevices --skip-import -d
create src/app/extra-app/features/device/side-devices/side-devices.component.styl (0 bytes)
create src/app/extra-app/features/device/side-devices/side-devices.component.html (31 bytes)
create src/app/extra-app/features/device/side-devices/side-devices.component.spec.ts (664 bytes)
create src/app/extra-app/features/device/side-devices/side-devices.component.ts (293 bytes)

also I can't manually select a module path+filename, it always produces error "Error: Could not find an NgModule"

@Brocco
Copy link
Contributor

Brocco commented Oct 5, 2017

This should be handled via PR #7775.

@kasimkha
Copy link

I do not see a fix for this issue, how do i add components using cli in my multi-app project

@kmanaseryan
Copy link

kmanaseryan commented Jun 4, 2018

In .angular-cli.json besides having "name" property for the app you need also have "appRoot" as well.
e.g. "appRoot": "app1"

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken
Projects
None yet
Development

No branches or pull requests