Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
feat: Schematic for extracting libraries from projects
Browse files Browse the repository at this point in the history
JIRA-Ticket: AV-27910
  • Loading branch information
jeka-gom committed Dec 20, 2023
1 parent 528c54d commit 504e1ef
Show file tree
Hide file tree
Showing 11 changed files with 263 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
import { <%= classify(name) %> } from './<%= dasherize(name) %>';

@NgModule({
providers: [<%= classify(name) %>]
})
export class <%= classify(name) %>Module {}
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class <%= classify(name) %> {
constructor() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export class <%= classify(name) %> {
constructor() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class <%= classify(name) %> {
constructor() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class <%= classify(name) %> {
constructor() {
}
}
64 changes: 64 additions & 0 deletions src/extract-app-libs/index.js

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

95 changes: 95 additions & 0 deletions src/extract-app-libs/index.ts
Original file line number Diff line number Diff line change
@@ -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);
};
}
57 changes: 57 additions & 0 deletions src/extract-app-libs/schema.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 504e1ef

Please sign in to comment.