Skip to content

Commit

Permalink
fix: resolve pathes relative to ng-package.json
Browse files Browse the repository at this point in the history
Previously, paths were resolved relative to the process's current working directory.
Now, paths are resolved relative to the `ng-package.json` file, making it more visible and intuitive to users.
This is in line with `.angular-cli.json`, `tsconfig.json` and friends who resolve pathes relative to the json files.
  • Loading branch information
dherges committed May 27, 2017
1 parent 36a86ca commit 852ce43
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** CLI arguments passed to `ng-packagr` and `ngPackage()`. */
export interface NgPackagrCliArguments {
/** Path to the '.ng-packagr.json' file */
/** Path to the 'ng-package.json' file */
project: string
}

/** Config object from '.ng-packagr.json' */
export interface NgPackagrConfig {
/** Config object from 'ng-package.json' */
export interface NgPackageConfig {
src: string,
dest: string,
workingDirectory: string,
Expand Down
16 changes: 10 additions & 6 deletions lib/ng-packagr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { processAssets } from './steps/assets';
import { copyFiles } from './steps/copy';
import { ngc } from './steps/ngc';
import { createPackage, readJson, preparePackage } from './steps/package';
import { createPackage, readJson, preparePackage, readPackage } from './steps/package';
import { rimraf } from './steps/rimraf';
import { rollup } from './steps/rollup';
import { remapSourcemap } from './steps/sorcery';
Expand All @@ -13,22 +13,26 @@ import { downlevelWithTsc } from './steps/tsc';
import { error, warn, info, success, debug } from './util/log';

// Interfaces
import { NgPackagrConfig, NgPackagrCliArguments } from './interfaces';
import { NgPackageConfig, NgPackagrCliArguments } from './interfaces';

// There are no type definitions available for these imports.
const fs = require('mz/fs');
const path = require('path');


export const ngPackage = (opts: NgPackagrCliArguments): Promise<any> => {
info(`Building Angular library from ${opts.project}`);
if (!path.isAbsolute(opts.project)) {
opts.project = path.resolve(process.cwd(), opts.project);
}

/** Project configuration */
let project: NgPackagrConfig;
let project: NgPackageConfig;
let tsConfig: any;
let flatModuleFile: string;
let sourcePkg: any;

return readJson(opts.project)
return readPackage(opts.project)
.then((p) => {
project = p;

Expand All @@ -46,7 +50,7 @@ export const ngPackage = (opts: NgPackagrCliArguments): Promise<any> => {
return Promise.resolve(pkg);
})
)
.then(() => readJson(`${project.src}/${project.ngc.tsconfig}`)
.then(() => readJson(`${project.ngc.tsconfig}`)
.then((cfg) => {
tsConfig = cfg;

Expand All @@ -56,7 +60,7 @@ export const ngPackage = (opts: NgPackagrCliArguments): Promise<any> => {
// 2. ASSETS
.then(() => processAssets(project.src, `${project.workingDirectory}`))
// 3. NGC
.then(() => ngc(`${project.src}/${project.ngc.tsconfig}`, `${project.workingDirectory}`))
.then(() => ngc(`${project.ngc.tsconfig}`, `${project.workingDirectory}`))
.then(() => remapSourcemap(flatModuleFile))
// 4. FESM15: ROLLUP
.then(() => rollup({
Expand Down
24 changes: 23 additions & 1 deletion lib/steps/package.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NgPackageConfig } from '../interfaces';
const fs = require('fs');
const path = require('path');

Expand Down Expand Up @@ -27,9 +28,30 @@ export const readJson = (file: string): Promise<any> => {
}


/**
* Reads an Angular package definition file from 'ng-package.json'
*
* @param package `ng-package.json` definition file
*/
export const readPackage = (ngPkg: string): Promise<NgPackageConfig> => {
const base = path.dirname(ngPkg);

return readJson(ngPkg)
.then((p: NgPackageConfig) => {
// resolve pathes relative to `ng-package.json` file
p.src = path.resolve(base, p.src);
p.dest = path.resolve(base, p.dest);
p.workingDirectory = path.resolve(base, p.workingDirectory);
p.ngc.tsconfig = path.resolve(base, p.ngc.tsconfig);
p.rollup.config = path.resolve(base, p.rollup.config);

return Promise.resolve(p);
});
}


/**
* Prepares a package...
* Prepares an Angular package from reading 'package.json'
*
* @param src Source directory containg `package.json` file
*/
Expand Down

0 comments on commit 852ce43

Please sign in to comment.