feat: add lib and nblib schematics

This commit is contained in:
vsavkin 2017-08-19 17:14:20 -04:00
parent 0a8bb236cf
commit a8cdc06eb1
31 changed files with 286 additions and 12 deletions

View File

@ -8,22 +8,24 @@ describe('application', () => {
it('creates a new application in a workspace', () => {
runSchematic('@nrwl/nx:application --name=proj');
runSchematic('@nrwl/nx:app --name=myapp', {projectName: 'proj'});
runSchematic('@nrwl/nx:app --name=myApp', {projectName: 'proj'});
checkFilesExists(
`proj/tsconfig.json`,
`proj/WORKSPACE`,
`proj/BUILD.bazel`,
`proj/apps/myapp/BUILD.bazel`,
`proj/apps/myapp/src/index.html`,
`proj/apps/myapp/src/app/app.module.ts`,
`proj/apps/myapp/src/app/app.component.ts`
`proj/apps/my-app/BUILD.bazel`,
`proj/apps/my-app/src/index.html`,
`proj/apps/my-app/src/app/app.module.ts`,
`proj/apps/my-app/src/app/app.component.ts`
);
expect(readFile('proj/apps/myapp/src/app/app.module.ts')).toContain('bootstrap: [AppComponent]');
expect(readFile('proj/apps/my-app/src/app/app.module.ts')).toContain('bootstrap: [AppComponent]');
const cliConfig = JSON.parse(readFile('proj/.angular-cli.json'));
expect(cliConfig.apps.length).toEqual(1);
expect(cliConfig.apps[0].name).toEqual('myApp');
expect(cliConfig.apps[0].root).toEqual('apps/my-app/src');
});
it('creates multiple applications in a workspace', () => {

26
e2e/lib.test.ts Normal file
View File

@ -0,0 +1,26 @@
import {
addNgRx, checkFilesExists, cleanup, newApp, readFile, runCLI, runCommand, runSchematic,
updateFile
} from './utils';
describe('library', () => {
beforeEach(cleanup);
it('creates a new library in a workspace', () => {
runSchematic('@nrwl/nx:application --name=proj');
runSchematic('@nrwl/nx:lib --name=myLib', {projectName: 'proj'});
checkFilesExists(
`proj/tsconfig.json`,
`proj/WORKSPACE`,
`proj/BUILD.bazel`,
`proj/libs/my-lib/BUILD.bazel`,
`proj/libs/my-lib/index.ts`,
`proj/libs/my-lib/src/my-lib.ts`
);
const cliConfig = JSON.parse(readFile('proj/.angular-cli.json'));
expect(cliConfig.apps[0].name).toEqual('myLib');
expect(cliConfig.apps[0].root).toEqual('libs/my-lib/src');
});
});

26
e2e/nglib.test.ts Normal file
View File

@ -0,0 +1,26 @@
import {
addNgRx, checkFilesExists, cleanup, newApp, readFile, runCLI, runCommand, runSchematic,
updateFile
} from './utils';
describe('angular library', () => {
beforeEach(cleanup);
it('creates a new angularlibrary in a workspace', () => {
runSchematic('@nrwl/nx:application --name=proj');
runSchematic('@nrwl/nx:nglib --name=myLib', {projectName: 'proj'});
checkFilesExists(
`proj/tsconfig.json`,
`proj/WORKSPACE`,
`proj/BUILD.bazel`,
`proj/libs/my-lib/BUILD.bazel`,
`proj/libs/my-lib/index.ts`,
`proj/libs/my-lib/src/my-lib.module.ts`
);
const cliConfig = JSON.parse(readFile('proj/.angular-cli.json'));
expect(cliConfig.apps[0].name).toEqual('myLib');
expect(cliConfig.apps[0].root).toEqual('libs/my-lib/src');
});
});

View File

@ -21,8 +21,13 @@ module.exports = function(env) {
const out = path.join(process.cwd(), env.bin, env.package, 'bundles');
const src = path.join(process.cwd(), env.bin, appConfig.root);
const aliasesForApps = apps.reduce((acc, curr) => {
acc[curr.name] = path.join(process.cwd(), env.bin, path.dirname(curr.root));
return acc;
}, {});
// victor todo: remove it when ng_module rule is fixed
const alias = Object.assign({}, {
const alias = Object.assign({}, aliasesForApps, {
'@angular/core/core': '@angular/core/@angular/core.es5',
'@angular/common/common': '@angular/common/@angular/common.es5',
'@angular/platform-browser/platform-browser': '@angular/platform-browser/@angular/platform-browser.es5'

View File

@ -3,7 +3,7 @@ load("@build_bazel_rules_angular//:defs.bzl", "ng_module")
load("@nrwl//:webpack.bzl", "webpack_bundle")
ng_module(
name = "compile",
name = "module",
srcs = glob(["**/*.ts"], exclude = ["e2e/**/*.ts"]),
deps = [],
tsconfig = "//:tsconfig.json"
@ -24,7 +24,7 @@ genrule(
filegroup(
name = "compile_and_static",
srcs = [
":compile",
":module",
":copy_static"
]
)

View File

@ -1,6 +1,6 @@
import {apply, chain, mergeWith, move, Rule, externalSchematic, template, url, Tree,} from '@angular-devkit/schematics';
import {Schema} from './schema';
import {names} from '../name-utils';
import {names, toFileName} from '../name-utils';
import * as path from 'path';
import * as ts from 'typescript';
import {addBootstrapToModule, addImportToModule} from '@schematics/angular/utility/ast-utils';
@ -48,7 +48,7 @@ function addAppToAngularCliJson(fullPath: string, options: Schema): Rule {
export default function (options: Schema): Rule {
const fullPath = path.join(options.directory, options.name, options.sourceDir);
const fullPath = path.join(options.directory, toFileName(options.name), options.sourceDir);
return chain([
mergeWith(
apply(url('./files'), [

View File

@ -12,6 +12,16 @@
"schema": "./app/schema.json",
"description": "Create an Angular application."
},
"lib": {
"factory": "./lib",
"schema": "./lib/schema.json",
"description": "Create a TypeScript library."
},
"nglib": {
"factory": "./nglib",
"schema": "./nglib/schema.json",
"description": "Create an Angular library."
},
"addNgRxToModule": {
"factory": "./addNgRxToModule",
"schema": "./addNgRxToModule/schema.json",

View File

@ -0,0 +1,17 @@
package(default_visibility=["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library(
name = "<%= name %>",
srcs = glob(["**/*.ts"]),
deps = [
# "//libs/libname",
],
tsconfig = "//:tsconfig.json",
module_name = "<%= name %>"
)
filegroup(
name = "compile_and_static",
srcs = []
)

View File

@ -0,0 +1 @@
export * from './src/<%= fileName %>';

View File

@ -0,0 +1 @@
export const <%= className %> = null;

View File

@ -0,0 +1,36 @@
import {apply, branchAndMerge, chain, mergeWith, Rule, template, Tree, url} from '@angular-devkit/schematics';
import {Schema} from './schema';
import * as path from 'path';
import {names, toFileName} from '../name-utils';
function addLibToAngularCliJson(fullPath: string, schema: Schema): Rule {
return (host: Tree) => {
const source = JSON.parse(host.read('.angular-cli.json') !.toString('utf-8'));
source.apps.push({
name: schema.name,
root: fullPath,
});
host.overwrite('.angular-cli.json', JSON.stringify(source, null, 2));
return host;
};
}
export default function (options: any): Rule {
const fullPath = path.join(options.directory, toFileName(options.name), options.sourceDir);
const templateSource = apply(url('./files'), [
template({
...options,
...names(options.name),
dot: '.',
tmpl: ''
})
]);
return chain([
branchAndMerge(chain([
mergeWith(templateSource),
addLibToAngularCliJson(fullPath, options)
])),
]);
}

5
src/schematics/lib/schema.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
export interface Schema {
name: string;
directory?: string;
sourceDir?: string;
}

View File

@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAngularNewLib",
"title": "NewLib",
"type": "object",
"properties": {
"sourceDir": {
"type": "string",
"default": "src",
"alias": "sd"
},
"name": {
"type": "string"
},
"directory": {
"type": "string",
"default": "libs"
}
},
"required": [
"name"
]
}

View File

@ -0,0 +1,38 @@
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_angular//:defs.bzl", "ng_module")
ng_module(
name = "module",
srcs = glob(["**/*.ts"]),
deps = [
# "//libs/libname",
],
tsconfig = "//:tsconfig.json",
module_name = "<%= name %>"
)
genrule(
name = "copy_static",
srcs = ["src"] + glob(["src/**/*"], exclude=["**/*.ts"]),
outs = ["marker"],
cmd = """
rsync -a --exclude '*.ts' $(locations :src) $(@D)/
touch $(locations :marker)
""",
output_to_bindir = 1
)
filegroup(
name = "<%= name %>",
srcs = [
":module",
":copy_static"
]
)
filegroup(
name = "compile_and_static",
srcs = [
":<%= name %>"
]
)

View File

@ -0,0 +1 @@
export * from './src/<%= fileName %>.module';

View File

@ -0,0 +1,7 @@
import {NgModule} from '@angular/core';
@NgModule({
declarations: []
})
export class <%= className %>Module {
}

View File

@ -0,0 +1,37 @@
import {apply, branchAndMerge, chain, mergeWith, Rule, template, Tree, url} from '@angular-devkit/schematics';
import {Schema} from './schema';
import * as path from 'path';
import {names, toFileName} from '../name-utils';
function addLibToAngularCliJson(fullPath: string, schema: Schema): Rule {
return (host: Tree) => {
const source = JSON.parse(host.read('.angular-cli.json') !.toString('utf-8'));
source.apps.push({
name: schema.name,
root: fullPath,
prefix: schema.prefix ? schema.prefix : schema.name
});
host.overwrite('.angular-cli.json', JSON.stringify(source, null, 2));
return host;
};
}
export default function (options: any): Rule {
const fullPath = path.join(options.directory, toFileName(options.name), options.sourceDir);
const templateSource = apply(url('./files'), [
template({
...options,
...names(options.name),
dot: '.',
tmpl: ''
})
]);
return chain([
branchAndMerge(chain([
mergeWith(templateSource),
addLibToAngularCliJson(fullPath, options)
])),
]);
}

6
src/schematics/nglib/schema.d.ts vendored Normal file
View File

@ -0,0 +1,6 @@
export interface Schema {
name: string;
directory?: string;
sourceDir?: string;
prefix?: string;
}

View File

@ -0,0 +1,26 @@
{
"$schema": "http://json-schema.org/schema",
"id": "SchematicsAngularNewLib",
"title": "NewLib",
"type": "object",
"properties": {
"sourceDir": {
"type": "string",
"default": "src",
"alias": "sd"
},
"name": {
"type": "string"
},
"prefix": {
"type": "string"
},
"directory": {
"type": "string",
"default": "libs"
}
},
"required": [
"name"
]
}

View File

@ -3,6 +3,13 @@
"strict": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"baseUrl": "."
"baseUrl": ".",
"paths": {
"*": [
"*",
"libs/*",
"apps/*"
]
}
}
}