Skip to content

Commit

Permalink
feat(angular8): rewrites the library to use schematics and builders (#32
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kyubisation authored Jun 28, 2019
1 parent 5e24f88 commit ffc305f
Show file tree
Hide file tree
Showing 162 changed files with 1,164 additions and 677,552 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ typings/
*.js
!test/**/*.js
*.d.ts
.rpt2_cache

# Others
.vscode
dist
dist-go
junit.xml
src/go-cli/app
src/go-cli/app.exe

# Go
cli/app
cli/app.exe
cli/pkg
cli/vendor
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "node_modules\\typescript\\lib"
"typescript.tsdk": "node_modules\\typescript\\lib",
"go.formatTool": "goimports"
}
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# 8.0.0-beta.0 (2019-06-30)

### Features

* Rewrote the library to use Angular schematics and builders
* Added --recursive flag to the native CLI

### Breaking Changes

* Removed the npm CLI and most of the existing internal implementation
* Dropped support for configuration embedded in html

# 2.0.0 (2019-04-14)

### Features
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
identification within third-party archives.

Copyright 2019 Lukas Spirig
Copyright 2019 Daniel Habenicht (go-cli)
Copyright 2019 Daniel Habenicht (cli)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
172 changes: 64 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,75 @@ The Angular CLI provides build time configuration (via environment.ts).
In a Continuous Delivery environment this is sometimes not enough.

## How it works & Limitations
Environment variables are used for configuration. This package provides a CLI command
to search for usages at build time and a CLI command for inserting populated
Environment variables are used for configuration.
This package provides an Angular CLI builder to search for usages at build time.
A [native CLI](#on-host-server-or-in-dockerfile) can be used to insert populated
environment variables into index.html file(s) into the head tag or by replacing `<!--CONFIG-->`
(Missing environment variables will be represented by null). This should be done
(Missing environment variables will be represented by `null`). This should be done
on the host serving the bundled angular files.

## Version 8 Rewrite
Version 8.x of this package is a complete rewrite with Angular schematics and builders.
If you require support for older Angular versions,
[Version 2.x](https://www.npmjs.com/package/angular-server-side-configuration/v/2.0.0)
of this library can be used, as it is Angular version agnostic.

## Getting Started
```
npm install --save angular-server-side-configuration
ng add angular-server-side-configuration
```
This will configure the appropriate files.

### Wrap ng build
Wrap the `ng build` command with `ngssc` (e.g. `ngssc ng build` or `ngssc --wrap-aot ng build`
if AoT retention is needed). This will generate a ngssc.json file in dist (or configurable via `--dist`),
which contains the configurations needed for `ngssc insert`. (See [ngssc](#ngssc) and [ngssc.json](#ngsscjson))

`package.json`
```json
...
"scripts": {
...
"build": "ngssc ng build --prod",
...
}
...
Alternatively, if you want to configure the files yourself:
```
npm install --save angular-server-side-configuration
```

### angular.json
Ensure you have an `ngsscbuild` entry in your project `architect` section.
To use the builder run `ng run your-project-name:ngsscbuild:production`.
You can add additional configurations in angular.json, which can be executed
by replacing `production` with your configuration name in the previous command.

If you need to build multiple angular bundles with the same environment file (e.g. to build i18n bundles),
wrap the different `ng build` scripts.
The builder will analyze the configured `ngsscConfigurationFile` to detect
used environment variables and generate an [ngssc.json](#ngsscjson) in the defined
`outputPath` in the referenced `browserTarget`.

`package.json`
```json
...
"scripts": {
"projects": {
...
"build": "ngssc ng npm run build:ng",
"build:ng": "ng build -c en && ng build -c de && ng build -c fr",
"your-project-name": {
...
"architect": {
...
"ngsscbuild": {
"builder": "angular-server-side-configuration:ngsscbuild",
"options": {
"aotSupport": true, // Set this to true, if you need to use
// environment variables inside AoT contexts
// (e.g. forRoot(...) or forChild(...))
"browserTarget": "your-project-name:build",
"ngsscConfigurationFile": "src/environments/environment.prod.ts"
},
"configurations": {
"production": {
"browserTarget": "your-project-name:build:production"
}
}
}
...
}
...
}
...
}
...
```

This will create an ngssc.json in you dist directory (configurable via --dist, e.g. `ngssc --dist ng build --prod`)
or embed the configuration in the html files in the dist directory. You can pass an existing ngssc.json file
to the command via --config (e.g. `ngssc --config ngssc.json`).


#### AoT
If you use environment variables in Module.forRoot or Module.forChild (or other AoT code sections)
the `--wrap-aot` flag must be used with the `ngssc` command (e.g. `ngssc --wrap-aot ng build --prod`).

### environment.prod.ts
angular-server-side-configuration supports two variants for using environment variables: process.env.* or NG_ENV.*
angular-server-side-configuration supports two variants for using environment variables:
process.env.* or NG_ENV.*

#### process.env.*
Use process.env.NAME in your environment.prod.ts, where NAME is the
Expand All @@ -82,7 +98,6 @@ export const environment = {

#### NG_ENV.*
Import NG_ENV from `angular-server-side-configuration/ng-env`
(or `angular-server-side-configuration/ng4-env` for Angular 4 or 5)
and use NG_ENV.NAME in your environment.prod.ts, where NAME is the
environment variable that should be used.

Expand All @@ -106,34 +121,36 @@ It is however the safest option.
<head>
<meta charset="utf-8">
<title>Angular Example</title>
<!--CONFIG-->
<base href="/">

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!--CONFIG-->
</head>
<body>
<app-root></app-root>
</body>
</html>
```

### On host server or in Dockerfile (or similar)
```bash
npm install -g angular-server-side-configuration
ngssc insert /path/to/ngssc.json/file
```

#### Native CLI
### On host server or in Dockerfile
This library provides a native implementation for the `insert` command of the CLI. Go to
[Releases](https://github.com/kyubisation/angular-server-side-configuration/releases) and download the appropriate
executable for your server environment.
(See [build.sh](https://github.com/kyubisation/angular-server-side-configuration/blob/master/src/go-cli/build.sh) for
[Releases](https://github.com/kyubisation/angular-server-side-configuration/releases)
and download the appropriate executable for your server environment.
(See [build.sh](https://github.com/kyubisation/angular-server-side-configuration/blob/master/cli/build.sh) for
build details of the native CLI. Please open an [Issue](https://github.com/kyubisation/angular-server-side-configuration/issues/new)
if you need an additional environment.)

Thanks to [DanielHabenicht](https://github.com/DanielHabenicht) for the input and contribution.

#### ngssc insert
Usage: ngssc insert [options] [directory]

| Options | Description |
| --- | --- |
| `-r, --recursive` | Recursively searches for ngssc.json files and applies the contained configuration |
| `--dry` | Perform the insert without actually inserting the variables |

##### Minimal Example
Dockerfile
```Dockerfile
Expand All @@ -153,56 +170,6 @@ ngssc insert /usr/share/nginx/html
nginx -g 'daemon off;'
```

## CLI
angular-server-side-configuration provides a CLI.

```bash
npm install angular-server-side-configuration -g
ngssc --help
```

### ngssc
Usage: ngssc [options] [ng...]

Detect used environment variables and either generates ngssc.json in given dist or embeds the information in the html files in dist

| Options | Description |
| --- | --- |
| `-ef, --environment-file` | The environment file in which to detect environment variables and optionally tokenize when using --wrap-aot. Defaults to src/environments/environment.prod.ts |
| `-a, --wrap-aot` | Tokenize variables to to retain during AoT compilation |
| `--dist` | The output path of the ng build. Defaults to dist/ |
| `--html-file-pattern` | The file pattern where the environment variables should be inserted. Defaults to **/index.html |
| `-h, --insert-in-head` | Whether to configure to try to insert the environment variables in the head tag. Defaults to configuring replacing <!--CONFIG--> |
| `-e, --embed-in-html` | Whether to embed the ngssc information into the html file found by --html-file-pattern in --dist instead of generating ngssc.json in --dist |
| `-c, --config <ngssc.json>` | Use an existing ngssc.json file as base configuration |
| `-h, --help` | output usage information |

### Insert
Usage: ngssc insert [options] [directory]

Insert environment variables. Looks for an ngssc.json file inside the current or given directory.
Alternatively use the --config-in-html flag. Directory defaults to current working directory

| Options | Description |
| --- | --- |
| `-i, --config-in-html` | Recursively searches for html files and applies the configuration found inside |
| `--dry` | Perform the insert without actually inserting the variables |
| `-h, --help` | output usage information |

### Init
Usage: ngssc init [options] [directory]

Initialize an angular project with angular-server-side-configuration (Directory defaults to current working directory)

| Options | Description |
| --- | --- |
| `-ef, --environment-file` | The environment file to initialize. environmentFile defaults to src/environments/environment.prod.ts |
| `--npm` | Install angular-service-side-configuration via npm. Default |
| `--yarn` | Install angular-service-side-configuration via yarn |
| `--process-env` | Initialize with process.env variant. Default |
| `--ng-env` | Initialize with NG_ENV variant |
| `-h, --help` | output usage information |

### ngssc.json

The package provides a JSON schema: `ngssc.schema.json",`
Expand All @@ -212,20 +179,9 @@ The package provides a JSON schema: `ngssc.schema.json",`
"$schema": "./node_modules/angular-server-side-configuration/ngssc.schema.json", // Optional
"variant": "process", // Either "process" or "NG_ENV".
"environmentVariables": [], // Detected environment variables.
// Will be merged if used with an existing ngssc.json.
"filePattern": "**/index.html", // File pattern in which environment variables should be inserted.
// Can be configured via --html-file-pattern. Also used if
// --embed-in-html is used.
"insertInHead": false // By default the CLI replaces <!--CONFIG--> in your html files during
// the insert command. If insertInHead is set to true, the insert
// command tries to insert the environment variables in the head tag,
// by looking for </title> or </head>.
"filePattern": "**/index.html" // File pattern in which environment variables should be inserted.
}
```

## API Documentation

[API Documentation](https://github.com/kyubisation/angular-server-side-configuration/blob/master/documentation/angular-server-side-configuration.md)

## License
Apache License, Version 2.0
18 changes: 0 additions & 18 deletions api-extractor.json

This file was deleted.

14 changes: 5 additions & 9 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ steps:
versionSpec: '10.x'
displayName: 'Install Node.js'

- script: npm i -g yarn
displayName: Install yarn

- script: npm ci
displayName: npm ci

- script: npm test
displayName: Jest
displayName: Jest & Go Test

- task: PublishTestResults@2
condition: succeededOrFailed()
Expand All @@ -48,23 +45,22 @@ steps:
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))

- script: npm pack
displayName: npm pack dist
workingDirectory: '$(System.DefaultWorkingDirectory)/dist'
displayName: npm pack
condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/tags/v')))

- task: Npm@1
displayName: Publish dist to npm
displayName: Publish to npm
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
inputs:
command: 'publish'
workingDir: '$(System.DefaultWorkingDirectory)/dist'
workingDir: '$(System.DefaultWorkingDirectory)'
publishEndpoint: 'NPM-kyubisation'

- task: CopyFiles@2
displayName: Prepare go cli files
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
inputs:
sourceFolder: dist-go
sourceFolder: dist
targetFolder: $(Build.ArtifactStagingDirectory)

- task: GitHubRelease@0
Expand Down
5 changes: 0 additions & 5 deletions bin/ngssc

This file was deleted.

9 changes: 9 additions & 0 deletions builders/builders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"builders": {
"ngsscbuild": {
"implementation": "./ngsscbuild",
"schema": "./ngsscbuild/schema.json",
"description": "Build angular with defined builder and generates ngssc.json."
}
}
}
Loading

0 comments on commit ffc305f

Please sign in to comment.