diff --git a/package-lock.json b/package-lock.json index 696cf6b..82f2b38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "avrios-schematics", - "version": "2.4.1", + "version": "2.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "avrios-schematics", - "version": "2.4.1", + "version": "2.5.0", "license": "MIT", "devDependencies": { "@angular-devkit/architect": "0.1300.1", diff --git a/src/collection.json b/src/collection.json index bd0b2a4..1fe868b 100644 --- a/src/collection.json +++ b/src/collection.json @@ -30,6 +30,11 @@ "description": "Create an Avrios library", "factory": "./library", "schema": "./library/schema.json" + }, + "extract-app-libs": { + "description": "Extract app libs from an Avrios project", + "factory": "./extract-app-libs", + "schema": "./extract-app-libs/schema.json" } } } diff --git a/src/extract-app-libs/files/__name@dasherize__..eslintrc.json.template b/src/extract-app-libs/files/__name@dasherize__..eslintrc.json.template new file mode 100644 index 0000000..3584f47 --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__..eslintrc.json.template @@ -0,0 +1,7 @@ +import { NgModule } from '@angular/core'; +import { <%= classify(name) %> } from './<%= dasherize(name) %>'; + +@NgModule({ + providers: [<%= classify(name) %>] +}) +export class <%= classify(name) %>Module {} diff --git a/src/extract-app-libs/files/__name@dasherize__.jest.config.ts.template b/src/extract-app-libs/files/__name@dasherize__.jest.config.ts.template new file mode 100644 index 0000000..e01d059 --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__.jest.config.ts.template @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { <%= classify(name) %> } from './<%= dasherize(name) %>'; + +describe('<%= classify(name) %>', () => { + let lib: <%= classify(name) %>; + + beforeEach(() => { + TestBed.configureTestingModule({}); + lib = TestBed.inject(<%= classify(name) %>); + }); + + it('should be instantiated', () => { + expect(lib).toBeTruthy(); + }); +}); diff --git a/src/extract-app-libs/files/__name@dasherize__.tsconfig-linter.json.template b/src/extract-app-libs/files/__name@dasherize__.tsconfig-linter.json.template new file mode 100644 index 0000000..f67ebb3 --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__.tsconfig-linter.json.template @@ -0,0 +1,4 @@ +export class <%= classify(name) %> { + constructor() { + } +} diff --git a/src/extract-app-libs/files/__name@dasherize__.tsconfig.json.template b/src/extract-app-libs/files/__name@dasherize__.tsconfig.json.template new file mode 100644 index 0000000..478d66a --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__.tsconfig.json.template @@ -0,0 +1,5 @@ + +export class <%= classify(name) %> { + constructor() { + } +} diff --git a/src/extract-app-libs/files/__name@dasherize__.tsconfig.lib.json.template b/src/extract-app-libs/files/__name@dasherize__.tsconfig.lib.json.template new file mode 100644 index 0000000..f67ebb3 --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__.tsconfig.lib.json.template @@ -0,0 +1,4 @@ +export class <%= classify(name) %> { + constructor() { + } +} diff --git a/src/extract-app-libs/files/__name@dasherize__.tsconfig.spec.json.template b/src/extract-app-libs/files/__name@dasherize__.tsconfig.spec.json.template new file mode 100644 index 0000000..f67ebb3 --- /dev/null +++ b/src/extract-app-libs/files/__name@dasherize__.tsconfig.spec.json.template @@ -0,0 +1,4 @@ +export class <%= classify(name) %> { + constructor() { + } +} diff --git a/src/extract-app-libs/index.js b/src/extract-app-libs/index.js new file mode 100644 index 0000000..c1d65f3 --- /dev/null +++ b/src/extract-app-libs/index.js @@ -0,0 +1,64 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const core_1 = require("@angular-devkit/core"); +const schematics_1 = require("@angular-devkit/schematics"); +const validation_1 = require("../utilities/validation"); +const parse_name_1 = require("../utilities/parse-name"); +function default_1(options) { + return (host) => { + const projectJsonFile = '/project.json'; + const { prefix, project, skipStartConfig, tags: tagsString } = options; + const dasherizedName = core_1.strings.dasherize(project); + const parsedPath = (0, parse_name_1.parseName)(`libs/${dasherizedName}`, project); + (0, validation_1.validateName)(parsedPath.name); + const projectLibsRoot = parsedPath.path; + const tags = tagsString ? tagsString.split(',').map(s => s.trim()) : []; + const libraryNames = host.getDir(projectLibsRoot).subdirs; + libraryNames.forEach(libraryName => { + (0, validation_1.validateName)(libraryName); + const sourceRoot = `${projectLibsRoot}/src/lib/${libraryName}`; + const projectConfig = { + sourceRoot, + prefix, + tags, + projectType: 'library', + generators: {}, + targets: { + lint: { + executor: '@nrwl/linter:eslint', + options: { + lintFilePatterns: [ + `${sourceRoot}/**/*.ts`, + `${sourceRoot}/**/*.html` + ], + eslintConfig: `${projectLibsRoot}/.eslintrc.json` + } + }, + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: `${projectLibsRoot}/jest.config.ts`, + passWithNoTests: true, + testPathPattern: [`lib/${libraryName}/`] + } + } + } + }; + host.exists(`${projectLibsRoot}/${libraryName}/${projectJsonFile}`) || host.create(`${projectLibsRoot}/${libraryName}/${projectJsonFile}`, JSON.stringify(projectConfig)); + }); + options.project = parsedPath.name; + options.path = parsedPath.path; + const rules = [ + (_) => host + ]; + if (!skipStartConfig) { + rules.push((0, schematics_1.apply)((0, schematics_1.url)('./files'), [ + (0, schematics_1.applyTemplates)(Object.assign(Object.assign({}, core_1.strings), options)), + (0, schematics_1.move)(projectLibsRoot) + ])); + } + return (0, schematics_1.chain)(rules); + }; +} +exports.default = default_1; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/src/extract-app-libs/index.ts b/src/extract-app-libs/index.ts new file mode 100644 index 0000000..ce0c7f0 --- /dev/null +++ b/src/extract-app-libs/index.ts @@ -0,0 +1,95 @@ +import { strings } from '@angular-devkit/core'; +import { + Rule, + Tree, + apply, + applyTemplates, + chain, + move, + url +} from '@angular-devkit/schematics'; + +import { validateName } from '../utilities/validation'; +import { parseName } from '../utilities/parse-name'; + +interface LibraryOptions { + project: string; + prefix: string; + skipStartConfig: boolean; + tags?: string; + namespace?: string; + path?: string; +} + +export default function (options: LibraryOptions): Rule { + return (host: Tree) => { + const projectJsonFile = '/project.json'; + const { prefix, project, skipStartConfig, tags: tagsString } = options; + const dasherizedName = strings.dasherize(project); + + const parsedPath = parseName(`libs/${dasherizedName}`, project); + + validateName(parsedPath.name); + + const projectLibsRoot = parsedPath.path; + const tags = tagsString ? tagsString.split(',').map(s => s.trim()) : []; + + const libraryNames = host.getDir(projectLibsRoot).subdirs; + libraryNames.forEach(libraryName => { + validateName(libraryName); + const sourceRoot = `${projectLibsRoot}/src/lib/${libraryName}`; + + const projectConfig = { + sourceRoot, + prefix, + tags, + projectType: 'library', + generators: {}, + targets: { + lint: { + executor: '@nrwl/linter:eslint', + options: { + lintFilePatterns: [ + `${sourceRoot}/**/*.ts`, + `${sourceRoot}/**/*.html` + ], + eslintConfig: `${projectLibsRoot}/.eslintrc.json` + } + }, + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: `${projectLibsRoot}/jest.config.ts`, + passWithNoTests: true, + testPathPattern: [`lib/${libraryName}/`] + } + } + } + }; + + host.exists(`${projectLibsRoot}/${libraryName}/${projectJsonFile}`) || host.create(`${projectLibsRoot}/${libraryName}/${projectJsonFile}`, JSON.stringify(projectConfig)); + }); + + options.project = parsedPath.name; + options.path = parsedPath.path; + + const rules: any[] = [ + (_: any) => host + ] + + if (!skipStartConfig) { + rules.push( + apply(url('./files'), [ + applyTemplates({ + ...strings, + ...options + }), + move(projectLibsRoot) + ]) + ); + } + + + return chain(rules); + }; +} diff --git a/src/extract-app-libs/schema.json b/src/extract-app-libs/schema.json new file mode 100644 index 0000000..7d45cef --- /dev/null +++ b/src/extract-app-libs/schema.json @@ -0,0 +1,57 @@ +{ + "$schema": "http://json-schema.org/schema", + "$id": "SchematicsAngularComponent", + "title": "Angular Extract libraries", + "type": "object", + "description": "Creates a new generic Library definition in the given or default project.", + "properties": { + "project": { + "type": "string", + "description": "The name of the project this libraries belong to. If none given then the libraries are added to the the shared scope.", + "$default": { + "$source": "projectName" + } + }, + "namespace": { + "type": "string", + "description": "The namespace of the Library. Normally matches the project name.", + "$default": { + "$source": "argv", + "index": 0 + }, + "x-prompt": "What namespace would you like to use for the Libraries?" + }, + "tags": { + "type": "string", + "description": "The list if tags the libraries are allowed to use.", + "$default": { + "$source": "argv", + "index": 1 + }, + "x-prompt": "Enter the set of tags that determine the boundaries of the libraries, delimited by comma. You can check the available list of tags in .eslintrc file. Ex: scope:shared." + }, + "prefix": { + "type": "string", + "description": "The prefix to apply to the generated libraries.", + "alias": "p", + "default": "avr", + "oneOf": [ + { + "maxLength": 0 + }, + { + "minLength": 1, + "format": "html-selector" + } + ] + }, + "skipStartConfig": { + "type": "boolean", + "description": "When true, does not create ... files for the new libraries.", + "default": true + } + }, + "required": [ + "project" + ] + }