Skip to content

Commit

Permalink
feat(devkit): add devkit library
Browse files Browse the repository at this point in the history
  • Loading branch information
wjanaszek committed Mar 26, 2020
1 parent 3761a10 commit 780598c
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# devkit-builders-build-ng-packagr-and-schematics

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `ng test devkit-builders-build-ng-packagr-and-schematics` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions libs/devkit/builders/build-ng-packagr-and-schematics/builders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"builders": {
"build": {
"implementation": "./src/build/index",
"schema": "./src/build/schema.json",
"description": "Build a library with ng-packagr."
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
name: 'devkit-builders-build-ng-packagr-and-schematics',
preset: '../../../../jest.config.js',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
coverageDirectory: '../../../../coverage/libs/devkit/builders/build-ng-packagr-and-schematics'
};
32 changes: 32 additions & 0 deletions libs/devkit/builders/build-ng-packagr-and-schematics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@valueadd-devkit/build-ng-packagr-and-schematics",
"version": "0.0.0",
"description": "ng-packagr builder with support for schematics inside library",
"experimental": true,
"main": "src/index.js",
"typings": "src/index.d.ts",
"builders": "./builders.json",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/valueadd-poland/valueadd/tree/master/libs/devkit/builders/build-ng-packagr-and-schematics"
},
"author": "ValueAdd sp. z o.o",
"license": "MIT",
"dependencies": {
"@angular-devkit/architect": "0.0.0",
"rxjs": "6.5.3"
},
"peerDependencies": {
"ng-packagr": "^4.0.0 || ^5.0.0"
},
"devDependencies": {
"@angular/compiler": "9.0.0-next.10",
"@angular/compiler-cli": "9.0.0-next.10",
"@angular-devkit/core": "0.0.0",
"ng-packagr": "~5.6.0",
"tslib": "^1.10.0"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BuilderContext, BuilderOutput, createBuilder } from '@angular-devkit/architect';
import { from, Observable } from 'rxjs';
import { mapTo, switchMap } from 'rxjs/operators';
// @ts-ignore
import { Schema as NgPackagrBuilderOptions } from './schema';
import { initialize as initializeNgPackagrBuilder } from './ng-packagr-build';
import { initialize as initializeSchematicsBuilder } from './schematics.build';

export function execute(
options: NgPackagrBuilderOptions,
context: BuilderContext
): Observable<BuilderOutput> {
return from(initializeNgPackagrBuilder(options, context.workspaceRoot)).pipe(
switchMap(packager => (options.watch ? packager.watch() : packager.build())),
switchMap(() => initializeSchematicsBuilder(options, context.workspaceRoot)),
switchMap(builder => builder.build()),
mapTo({ success: true })
);
}

export { NgPackagrBuilderOptions };
export default createBuilder<Record<string, string> & NgPackagrBuilderOptions>(execute);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { resolve } from 'path';
import { NgPackagrBuilderOptions } from '@angular-devkit/build-ng-packagr';

export async function initialize(
options: NgPackagrBuilderOptions,
root: string
): Promise<import('ng-packagr').NgPackagr> {
const packager = (await import('ng-packagr')).ngPackagr();

packager.forProject(resolve(root, options.project));

if (options.tsConfig) {
packager.withTsConfig(resolve(root, options.tsConfig));
}

return packager;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "ng-packagr Target",
"description": "ng-packagr target options for Build Architect. Use to build library projects.",
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "The file path for the ng-packagr configuration file, relative to the current workspace."
},
"tsConfig": {
"type": "string",
"description": "The full path for the TypeScript configuration file, relative to the current workspace."
},
"watch": {
"type": "boolean",
"description": "Run build when files change.",
"default": false
}
},
"additionalProperties": false,
"required": ["project"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as childProcess from 'child_process';
import { resolve } from 'path';

export interface SchematicsBuilderOptions {
tsConfig: string;
}

class Builder {
constructor(private root: string, private tsConfig: string) {}

build(): Promise<void> {
console.log('Build Schematics');
console.log(`From: ${this.root}`);
console.log(`To: ${resolve(this.root, this.tsConfig)}`);
const child = childProcess.spawn('tsc', ['-p', resolve(this.root, this.tsConfig)]);
child.stdout.on('data', data => {
console.log('stdout:', data.toString());
});
child.stderr.on('data', data => {
console.log('stderr:', data.toString());
});
return new Promise((res, reject) => {
child.on('close', code => {
console.info(`tsc exited with code ${code}`);
if (code === 0) {
res();
} else {
reject();
}
});
});
}
}

export async function initialize(
options: SchematicsBuilderOptions,
root: string
): Promise<Builder> {
return new Builder(root, options.tsConfig);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './build';
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import chalk from 'chalk';

export const error = (err: string | Error) => {
if (err instanceof Error) {
console.error(chalk.red('ERROR: ' + err.message));

if (process.env.DEBUG) {
console.error(chalk.red(err.stack) + '\n');
}
} else {
console.error(chalk.red(err));
}
};

export const warn = (msg: string) => {
console.warn(chalk.yellow('WARNING: ' + msg));
};

export const success = (msg: string) => {
console.log(chalk.green(msg));
};

export const info = (msg: string) => {
console.log(chalk.blue(msg));
};

export const msg = (msg: string) => {
console.log(chalk.white(msg));
};

export const debug = (msg: string) => {
if (process.env.DEBUG) {
console.log(chalk.inverse.cyan(`[debug] ${msg}`));
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"]
},
"include": ["**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": ".",
"rootDir": ".",
"types": [],
"target": "es5",
"module": "commonjs"
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js", "**/*.spec.jsx", "**/*.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../../../../tslint.json", "rules": [] }
1 change: 1 addition & 0 deletions scripts/validate-commit-message/commit-message.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"types": ["build", "ci", "docs", "feat", "fix", "perf", "refactor", "chore", "style", "test"],
"scopes": [
"deps",
"devkit",
"common",
"testing",
"core",
Expand Down

0 comments on commit 780598c

Please sign in to comment.