Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Update NgPackagr builder #532

Merged
merged 3 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
677 changes: 556 additions & 121 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"mini-css-extract-plugin": "~0.2.0",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
"ng-packagr": "^2.1.0",
"ng-packagr": "^2.2.0",
"node-sass": "^4.7.2",
"opn": "^5.1.0",
"parse5": "^4.0.0",
Expand All @@ -130,6 +130,7 @@
"protractor": "^5.1.2",
"raw-loader": "^0.5.1",
"request": "^2.83.0",
"resolve": "^1.5.0",
"rxjs": "^5.5.6",
"sass-loader": "^6.0.7",
"semver": "^5.3.0",
Expand Down
10 changes: 6 additions & 4 deletions packages/angular_devkit/build_ng_packagr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
"dependencies": {
"@angular-devkit/architect": "0.0.0",
"@angular-devkit/core": "0.0.0",
"rxjs": "0.0.0",
"resolve": "0.0.0",
"ng-packagr": "0.0.0"
"resolve": "^1.5.0",
"rxjs": "^5.5.6"
},
"peerDependencies": {
"ng-packagr": "^2.2.0"
}
}
}
32 changes: 21 additions & 11 deletions packages/angular_devkit/build_ng_packagr/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
* found in the LICENSE file at https://angular.io/license
*/

import { BuildEvent, Builder, BuilderContext, Target } from '@angular-devkit/architect';
import { getSystemPath } from '@angular-devkit/core';
import {
BuildEvent,
Builder,
BuilderConfiguration,
BuilderContext,
} from '@angular-devkit/architect';
import { getSystemPath, normalize, resolve } from '@angular-devkit/core';
import * as ngPackagr from 'ng-packagr';
import { resolve as resolvePath } from 'path';
import { Observable } from 'rxjs/Observable';

// XX: blatantly copy-pasted from 'require-project-module.ts'
const resolve = require('resolve');
// TODO move this function to architect or somewhere else where it can be imported from.
// Blatantly copy-pasted from 'require-project-module.ts'.
function requireProjectModule(root: string, moduleName: string) {
const resolve = require('resolve');

return require(resolve.sync(moduleName, { basedir: root }));
}

Expand All @@ -27,22 +33,26 @@ export class NgPackagrBuilder implements Builder<NgPackagrBuilderOptions> {

constructor(public context: BuilderContext) { }

run(target: Target<NgPackagrBuilderOptions>): Observable<BuildEvent> {
const root = getSystemPath(target.root);
const options = target.options;
run(builderConfig: BuilderConfiguration<NgPackagrBuilderOptions>): Observable<BuildEvent> {
const root = this.context.workspace.root;
const options = builderConfig.options;

if (!options.project) {
throw new Error('A "project" must be specified to build a library\'s npm package.');
}

return new Observable(obs => {
const projectNgPackagr = requireProjectModule(root, 'ng-packagr') as typeof ngPackagr;
const packageJsonPath = resolvePath(root, options.project);
const projectNgPackagr = requireProjectModule(
getSystemPath(root), 'ng-packagr') as typeof ngPackagr;
const packageJsonPath = getSystemPath(resolve(root, normalize(options.project)));

projectNgPackagr.ngPackagr()
.forProject(packageJsonPath)
.build()
.then(() => obs.complete())
.then(() => {
obs.next({ success: true });
obs.complete();
})
.catch((e) => obs.error(e));
});
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import { Architect, TargetSpecifier } from '@angular-devkit/architect';
import { experimental, join, normalize } from '@angular-devkit/core';
import { NodeJsSyncHost } from '@angular-devkit/core/node';
import { concatMap, tap } from 'rxjs/operators';


describe('NgPackagr Builder', () => {
it('works', (done) => {
const workspaceFile = normalize('angular.json');
const devkitRoot = normalize((global as any)._DevKitRoot); // tslint:disable-line:no-any
const workspaceRoot = join(devkitRoot,
'tests/@angular_devkit/build_ng_packagr/ng-packaged/');

// TODO: move TestProjectHost from build-webpack to architect, or somewhere else, where it
// can be imported from.
const host = new NodeJsSyncHost();
const workspace = new experimental.workspace.Workspace(workspaceRoot, host);
const targetSpec: TargetSpecifier = { project: 'lib', target: 'build' };

return workspace.loadWorkspaceFromHost(workspaceFile).pipe(
concatMap(ws => new Architect(ws).loadArchitect()),
concatMap(arch => arch.run(arch.getBuilderConfiguration(targetSpec))),
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
).subscribe(undefined, done.fail, done);
}, 30000);
});
1 change: 1 addition & 0 deletions packages/angular_devkit/build_webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"postcss-url": "^7.3.1",
"raw-loader": "^0.5.1",
"request": "^2.83.0",
"resolve": "^1.5.0",
"rxjs": "^5.5.6",
"sass-loader": "^6.0.7",
"silent-error": "^1.1.0",
Expand Down
16 changes: 4 additions & 12 deletions packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function updateTsConfig(npmPackageName: string, entryFilePath: string) {
};
}

function addDependenciesAndScriptsToPackageJson(name: string, sourceDir: string) {
function addDependenciesAndScriptsToPackageJson() {

return (host: Tree) => {
if (!host.exists('package.json')) { return host; }
Expand All @@ -92,19 +92,13 @@ function addDependenciesAndScriptsToPackageJson(name: string, sourceDir: string)
json.devDependencies = {
'@angular/compiler': '^5.0.0',
'@angular/compiler-cli': '^5.0.0',
'ng-packagr': '^2.1.0',
'ng-packagr': '^2.2.0',
'tsickle': '>=0.25.5',
'tslib': '^1.7.1',
'typescript': '>=2.4.2',
// de-structure last keeps existing user dependencies
...json.devDependencies,
};

// Add package.json script
if (!json.scripts) {
json.scripts = {};
}
json.scripts[`libs:${name}:build`] = `ng-packagr -p ${sourceDir}/package.json`;
});
};
}
Expand Down Expand Up @@ -138,10 +132,8 @@ export default function (options: GenerateLibraryOptions): Rule {
branchAndMerge(chain([
mergeWith(templateSource),
])),
options.skipPackageJson ? noop() : addDependenciesAndScriptsToPackageJson(name,
forwardSlashes(sourceDir)),
options.skipTsConfig ? noop() : updateTsConfig(name,
forwardSlashes(entryFilePath)),
options.skipPackageJson ? noop() : addDependenciesAndScriptsToPackageJson(),
options.skipTsConfig ? noop() : updateTsConfig(name, forwardSlashes(entryFilePath)),
])(host, context);
};
}
10 changes: 1 addition & 9 deletions packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,10 @@ describe('Library Schematic', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, mockTree);

const packageJson = getJsonFileContent(tree, 'package.json');
expect(packageJson.devDependencies['ng-packagr']).toEqual('^2.1.0');
expect(packageJson.devDependencies['ng-packagr']).toEqual('^2.2.0');
// TODO ...
});

it(`should add a script for ng-packagr`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, mockTree);

const packageJson = getJsonFileContent(tree, 'package.json');
expect(packageJson.scripts['libs:foo:build'])
.toEqual('ng-packagr -p my-libs/foo/package.json');
});

it(`should not override existing users dependencies`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, mockTree);

Expand Down

This file was deleted.

13 changes: 0 additions & 13 deletions tests/@angular_devkit/build_ng_packagr/ng-packaged/.editorconfig

This file was deleted.

27 changes: 0 additions & 27 deletions tests/@angular_devkit/build_ng_packagr/ng-packaged/README.md

This file was deleted.

18 changes: 18 additions & 0 deletions tests/@angular_devkit/build_ng_packagr/ng-packaged/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "../../../../packages/angular_devkit/core/src/workspace/workspace-schema.json",
"version": 1,
"projects": {
"lib": {
"root": "projects/lib",
"projectType": "library",
"architect": {
"build": {
"builder": "../../../../packages/angular_devkit/build_ng_packagr:build",
"options": {
"project": "projects/lib/package.json"
}
}
}
}
}
}

This file was deleted.

Loading