Prototype of a CLI for Angular 2 applications based on the ember-cli project.
This project is very much still a work in progress.
The CLI is now in beta. If you wish to collaborate while the project is still young, check out our issue list.
We're updating the build system in Angular-CLI to use webpack instead of Broccoli.
You can install and update your projects using these instructions.
The generated project has dependencies that require Node 4 or greater.
- Installation
- Usage
- Generating a New Project
- Generating Components, Directives, Pipes and Services
- Generating a Route
- Creating a Build
- Build Targets and Environment Files
- Bundling
- Running Unit Tests
- Running End-to-End Tests
- Deploying the App via GitHub Pages
- Linting and formatting code
- Support for offline applications
- Commands autocompletion
- CSS preprocessor integration
- 3rd Party Library Installation
- Updating angular-cli
- Known Issues
- Development Hints for hacking on angular-cli
BEFORE YOU INSTALL: please read the prerequisites
npm install -g angular-cli
ng --help
ng new PROJECT_NAME
cd PROJECT_NAME
ng serve
Navigate to http://localhost:4200/
. The app will automatically reload if you change any of the source files.
You can configure the default HTTP port and the one used by the LiveReload server with two command-line options :
ng serve --port 4201 --live-reload-port 49153
You can use the ng generate
(or just ng g
) command to generate Angular components:
ng generate component my-new-component
ng g component my-new-component # using the alias
# components support relative path generation
# if in the directory src/app/feature/ and you run
ng g component new-cmp
# your component will be generated in src/app/feature/new-cmp
# but if you were to run
ng g component ../newer-cmp
# your component will be generated in src/app/newer-cmp
You can find all possible blueprints in the table below:
Scaffold | Usage |
---|---|
Component | ng g component my-new-component |
Directive | ng g directive my-new-directive |
Pipe | ng g pipe my-new-pipe |
Service | ng g service my-new-service |
Class | ng g class my-new-class |
Interface | ng g interface my-new-interface |
Enum | ng g enum my-new-enum |
Generating routes in the CLI has been disabled for the time being. A new router and new route generation blueprints are coming.
You can read the official documentation for the new Router here: https://angular.io/docs/ts/latest/guide/router.html. Please note that even though route generation is disabled, building your projects with routing is still fully supported.
ng build
The build artifacts will be stored in the dist/
directory.
A build can specify both a build target (development
or production
) and an
environment file to be used with that build. By default, the development build
target is used.
At build time, src/app/environments/environment.ts
will be replaced by
src/app/environments/environment.{NAME}.ts
where NAME
is the argument
provided to the --environment
flag.
These options also apply to the serve command. If you do not pass a value for environment
,
it will default to dev
for development
and prod
for production
.
# these are equivalent
ng build --target=production --environment=prod
ng build --prod --env=prod
ng build --prod
# and so are these
ng build --target=development --environment=dev
ng build --dev --e=dev
ng build --dev
ng build
You can also add your own env files other than dev
and prod
by creating a
src/app/environments/environment.{NAME}.ts
and use them by using the --env=NAME
flag on the build/serve commands.
Builds created with the -prod
flag via ng build -prod
or ng serve -prod
bundle
all dependencies into a single file, and make use of tree-shaking techniques.
ng test
Tests will execute after a build is executed via Karma, and it will automatically watch your files for changes. You can run tests a single time via --watch=false
.
ng e2e
Before running the tests make sure you are serving the app via ng serve
.
End-to-end tests are run via Protractor.
You can deploy your apps quickly via:
ng github-pages:deploy --message "Optional commit message"
This will do the following:
- creates GitHub repo for the current project if one doesn't exist
- rebuilds the app in production mode at the current
HEAD
- creates a local
gh-pages
branch if one doesn't exist - moves your app to the
gh-pages
branch and creates a commit - edit the base tag in index.html to support github pages
- pushes the
gh-pages
branch to github - returns back to the original
HEAD
Creating the repo requires a token from github, and the remaining functionality relies on ssh authentication for all git operations that communicate with github.com. To simplify the authentication, be sure to setup your ssh keys.
If you are deploying a user or organization page, you can instead use the following command:
ng github-pages:deploy --user-page --message "Optional commit message"
This command pushes the app to the master
branch on the github repo instead
of pushing to gh-pages
, since user and organization pages require this.
You can lint your app code by running ng lint
.
This will use the lint
npm script that in generated projects uses tslint
.
You can modify the these scripts in package.json
to run whatever tool you prefer.
The --mobile
flag has been disabled temporarily. Sorry for the inconvenience.
Angular-CLI includes support for offline applications via the --
flag on ng new
. Support is experimental, please see the angular/mobile-toolkit project and https://mobile.angular.io/ for documentation on how to make use of this functionality.
To turn on auto completion use the following commands:
For bash:
ng completion >> ~/.bashrc
source ~/.bashrc
For zsh:
ng completion >> ~/.zshrc
source ~/.zshrc
Windows users using gitbash:
ng completion >> ~/.bash_profile
source ~/.bash_profile
Angular-CLI supports all major CSS preprocessors:
- sass/scss (http://sass-lang.com/)
- less (http://lesscss.org/)
- compass (http://compass-style.org/)
- stylus (http://stylus-lang.com/)
To use these prepocessors simply add the file to your component's styleUrls
:
@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
title = 'app works!';
}
When generating a new project you can also define which extention you want for style files:
ng new sassy-project --style=sass
Or set the default style on an existing project:
ng set defaults.styleExt scss
Simply install your library via npm install lib-name --save
and import it in your code.
If the library does not include typings, you can install them using npm:
npm install moment --save
npm install @types/moment --save-dev
To update angular-cli
to a new version, you must update both the global package and your project's local package.
Global package:
npm uninstall -g angular-cli
npm cache clean
npm install -g angular-cli@latest
Local project package:
rm -rf node_modules dist tmp
npm install --save-dev angular-cli@latest
ng init
Running ng init
will check for changes in all the auto-generated files created by ng new
and allow you to update yours. You are offered four choices for each changed file: y
(overwrite), n
(don't overwrite), d
(show diff between your file and the updated file) and h
(help).
Carefully read the diffs for each code file, and either accept the changes or incorporate them manually after ng init
finishes.
The main cause of errors after an update is failing to incorporate these updates into your code.
You can find more details about changes between versions in CHANGELOG.md.
This project is currently a prototype so there are many known issues. Just to mention a few:
- All blueprints/scaffolds are in TypeScript only, in the future blueprints in all dialects officially supported by Angular will be available.
- On Windows you need to run the
build
andserve
commands with Admin permissions, otherwise the performance is not good. - The initial installation as well as
ng new
take too long because of lots of npm dependencies. - Many existing ember addons are not compatible with Angular apps built via angular-cli.
- When you
ng serve
remember that the generated project has dependencies that require Node 4 or greater.
git clone https://github.com/angular/angular-cli.git
cd angular-cli
npm link
npm link
is very similar to npm install -g
except that instead of downloading the package
from the repo, the just cloned angular-cli/
folder becomes the global package.
Any changes to the files in the angular-cli/
folder will immediately affect the global angular-cli
package,
allowing you to quickly test any changes you make to the cli project.
Now you can use angular-cli
via the command line:
ng new foo
cd foo
npm link angular-cli
ng serve
npm link angular-cli
is needed because by default the globally installed angular-cli
just loads
the local angular-cli
from the project which was fetched remotely from npm.
npm link angular-cli
symlinks the global angular-cli
package to the local angular-cli
package.
Now the angular-cli
you cloned before is in three places:
The folder you cloned it into, npm's folder where it stores global packages and the angular-cli
project you just created.
You can also use ng new foo --link-cli
to automatically link the angular-cli
package.
Please read the official npm-link documentation and the npm-link cheatsheet for more information.
MIT