Browse Source

add create lib command

pull/13716/head
muhammedaltug 4 years ago
parent
commit
ce94aeabb9
  1. 5
      npm/ng-packs/packages/schematics/src/collection.json
  2. 44
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/.eslintrc.json.template
  3. 7
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/ng-package.json.template
  4. 12
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/__libraryName@kebab__-config.module.ts.template
  5. 1
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/enums/index.ts.template
  6. 3
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/enums/route-names.ts.template
  7. 1
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/providers/index.ts.template
  8. 26
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/providers/route.provider.ts.template
  9. 3
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/public-api.ts.template
  10. 44
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template
  11. 7
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/ng-package.json.template
  12. 11
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/package.json.template
  13. 18
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/lib/__libraryName@kebab__-routing.module.ts.template
  14. 22
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/lib/__libraryName@kebab__.module.ts.template
  15. 4
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/public-api.ts.template
  16. 26
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/test.ts.template
  17. 20
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.lib.json.template
  18. 10
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.lib.prod.json.template
  19. 17
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.spec.json.template
  20. 7
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/ng-package.json.template
  21. 11
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/lib/__target@kebab__-__libraryName@kebab__.module.ts.template
  22. 1
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/lib/index.ts.template
  23. 1
      npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/public-api.ts.template
  24. 159
      npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts
  25. 17
      npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts
  26. 1
      npm/ng-packs/packages/schematics/src/commands/create-lib/models/index.ts
  27. 36
      npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json
  28. 1
      npm/ng-packs/packages/schematics/src/constants/symbols.ts
  29. 1
      npm/ng-packs/packages/schematics/src/enums/exception.ts
  30. 10
      npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts
  31. 52
      npm/ng-packs/packages/schematics/src/utils/angular-schematic/generate-lib.ts
  32. 1
      npm/ng-packs/packages/schematics/src/utils/angular-schematic/index.ts
  33. 1
      npm/ng-packs/packages/schematics/src/utils/index.ts
  34. 9
      npm/ng-packs/packages/schematics/src/utils/lib.ts
  35. 2
      npm/ng-packs/packages/schematics/src/utils/source.ts
  36. 34
      npm/ng-packs/packages/schematics/src/utils/workspace.ts
  37. 6
      npm/ng-packs/scripts/build-schematics.ts

5
npm/ng-packs/packages/schematics/src/collection.json

@ -24,6 +24,11 @@
"description": "ABP API Generator Schematics",
"factory": "./commands/api",
"schema": "./commands/api/schema.json"
},
"create-lib": {
"description": "ABP Create Library Schematics",
"factory": "./commands/create-lib",
"schema": "./commands/create-lib/schema.json"
}
}
}

44
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/.eslintrc.json.template

@ -0,0 +1,44 @@
{
"extends": "../../.eslintrc.json",
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts"
],
"parserOptions": {
"project": [
"projects/<%= kebab(libraryName) %>/tsconfig.lib.json",
"projects/<%= kebab(libraryName) %>/tsconfig.spec.json"
],
"createDefaultProgram": true
},
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "lib",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "lib",
"style": "kebab-case"
}
]
}
},
{
"files": [
"*.html"
],
"rules": {}
}
]
}

7
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/ng-package.json.template

@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/<%= kebab(libraryName) %>/config",
"lib": {
"entryFile": "src/public-api.ts"
}
}

12
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/__libraryName@kebab__-config.module.ts.template

@ -0,0 +1,12 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
import { <%= macro(libraryName) %>_ROUTE_PROVIDERS } from './providers/route.provider';
@NgModule()
export class <%= pascal(libraryName) %>ConfigModule {
static forRoot(): ModuleWithProviders<<%= pascal(libraryName) %>ConfigModule> {
return {
ngModule: <%= pascal(libraryName) %>ConfigModule,
providers: [<%= macro(libraryName) %>_ROUTE_PROVIDERS],
};
}
}

1
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/enums/index.ts.template

@ -0,0 +1 @@
export * from './route-names';

3
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/enums/route-names.ts.template

@ -0,0 +1,3 @@
export const enum e<%= pascal(libraryName) %>RouteNames {
<%= pascal(libraryName) %> = '<%= pascal(libraryName) %>',
}

1
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/providers/index.ts.template

@ -0,0 +1 @@
export * from './route.provider';

26
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/providers/route.provider.ts.template

@ -0,0 +1,26 @@
import { eLayoutType, RoutesService } from '@abp/ng.core';
import { APP_INITIALIZER } from '@angular/core';
import { e<%= pascal(libraryName) %>RouteNames } from '../enums/route-names';
export const <%= macro(libraryName) %>_ROUTE_PROVIDERS = [
{
provide: APP_INITIALIZER,
useFactory: configureRoutes,
deps: [RoutesService],
multi: true,
},
];
export function configureRoutes(routesService: RoutesService) {
return () => {
routesService.add([
{
path: '/<%= kebab(libraryName) %>',
name: e<%= pascal(libraryName) %>RouteNames.<%= pascal(libraryName) %>,
iconClass: 'fas fa-book',
layout: eLayoutType.application,
order: 3,
},
]);
};
}

3
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/config/src/public-api.ts.template

@ -0,0 +1,3 @@
export * from './enums';
export * from './<%= kebab(libraryName) %>-config.module';
export * from './providers';

44
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/karma.conf.js.template

@ -0,0 +1,44 @@
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')
],
client: {
jasmine: {
// you can add configuration options for Jasmine here
// the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
// for example, you can disable the random execution with `random: false`
// or set a specific seed with `seed: 4321`
},
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
jasmineHtmlReporter: {
suppressAll: true // removes the duplicated traces
},
coverageReporter: {
dir: require('path').join(__dirname, '../../coverage/my-project-name'),
subdir: '.',
reporters: [
{ type: 'html' },
{ type: 'text-summary' }
]
},
reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
});
};

7
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/ng-package.json.template

@ -0,0 +1,7 @@
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/<%= kebab(libraryName) %>",
"lib": {
"entryFile": "src/public-api.ts"
}
}

11
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/package.json.template

@ -0,0 +1,11 @@
{
"name": "@<%= kebab(libraryName) %>/<%= kebab(libraryName) %>",
"version": "0.0.1",
"peerDependencies": {
"@abp/ng.core": "<%= abpVersion %>",
"@abp/ng.theme.shared": "<%= abpVersion %>"
},
"dependencies": {
"tslib": "^2.1.0"
}
}

18
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/lib/__libraryName@kebab__-routing.module.ts.template

@ -0,0 +1,18 @@
import { NgModule } from '@angular/core';
import { DynamicLayoutComponent } from '@abp/ng.core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: '',
pathMatch: 'full',
component: DynamicLayoutComponent,
children: [],
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class <%= pascal(libraryName) %>RoutingModule {}

22
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/lib/__libraryName@kebab__.module.ts.template

@ -0,0 +1,22 @@
import { NgModule, NgModuleFactory, ModuleWithProviders } from '@angular/core';
import { CoreModule, LazyModuleFactory } from '@abp/ng.core';
import { ThemeSharedModule } from '@abp/ng.theme.shared';
import { <%= pascal(libraryName) %>RoutingModule } from './<%= kebab(libraryName) %>-routing.module';
@NgModule({
declarations: [],
imports: [CoreModule, ThemeSharedModule, <%= pascal(libraryName) %>RoutingModule],
exports: [],
})
export class <%= pascal(libraryName) %>Module {
static forChild(): ModuleWithProviders<<%= pascal(libraryName) %>Module> {
return {
ngModule: <%= pascal(libraryName) %>Module,
providers: [],
};
}
static forLazy(): NgModuleFactory<<%= pascal(libraryName) %>Module> {
return new LazyModuleFactory(<%= pascal(libraryName) %>Module.forChild());
}
}

4
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/public-api.ts.template

@ -0,0 +1,4 @@
/*
* Public API Surface of my-project-name
*/
export * from './lib/<%= kebab(libraryName) %>.module';

26
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/src/test.ts.template

@ -0,0 +1,26 @@
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'zone.js';
import 'zone.js/testing';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
declare const require: {
context(path: string, deep?: boolean, filter?: RegExp): {
keys(): string[];
<T>(id: string): T;
};
};
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

20
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.lib.json.template

@ -0,0 +1,20 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "../../tsconfig.prod.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}

10
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.lib.prod.json.template

@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"declarationMap": false
},
"angularCompilerOptions": {
"compilationMode": "partial"
}
}

17
npm/ng-packs/packages/schematics/src/commands/create-lib/files-package/__libraryName@kebab__/tsconfig.spec.json.template

@ -0,0 +1,17 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/spec",
"types": [
"jasmine"
]
},
"files": [
"src/test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}

7
npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/ng-package.json.template

@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/<%= kebab(target) %>/<%= kebab(libraryName) %>",
"lib": {
"entryFile": "src/public-api.ts"
}
}

11
npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/lib/__target@kebab__-__libraryName@kebab__.module.ts.template

@ -0,0 +1,11 @@
import { ModuleWithProviders, NgModule } from '@angular/core';
@NgModule()
export class <%= pascal(target) %><%= pascal(libraryName) %>Module {
static forRoot(): ModuleWithProviders<<%= pascal(target) %><%= pascal(libraryName) %>Module> {
return {
ngModule:<%= pascal(target) %><%= pascal(libraryName) %>Module,
providers: []
}
}
}

1
npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/lib/index.ts.template

@ -0,0 +1 @@
export * from './<%= kebab(target) %>-<%= kebab(libraryName) %>.module';

1
npm/ng-packs/packages/schematics/src/commands/create-lib/files-secondary-entrypoint/__libraryName@kebab__/src/public-api.ts.template

@ -0,0 +1 @@
export * from './lib/index';

159
npm/ng-packs/packages/schematics/src/commands/create-lib/index.ts

@ -0,0 +1,159 @@
import {
applyTemplates,
chain,
move,
noop,
Rule,
SchematicContext,
SchematicsException,
Tree,
url,
} from '@angular-devkit/schematics';
import { GenerateLibSchema } from './models/generate-lib-schema';
import {
applyWithOverwrite,
getWorkspace,
interpolate,
isLibrary,
JSONFile,
kebab,
resolveProject,
updateWorkspace,
} from '../../utils';
import * as cases from '../../utils/text';
import { Exception } from '../../enums';
import { join, normalize } from '@angular-devkit/core';
import {
ProjectDefinition,
WorkspaceDefinition,
} from '@angular-devkit/core/src/workspace/definitions';
import { addLibToWorkspaceFile } from '../../utils/angular-schematic/generate-lib';
export default function (schema: GenerateLibSchema) {
return async (tree: Tree) => {
if (schema.override || !(await checkLibExist(schema, tree))) {
return chain([createLibrary(schema)]);
}
};
}
async function checkLibExist(options: GenerateLibSchema, tree: Tree) {
const packageName = kebab(options.packageName);
if (options.isSecondaryEntrypoint) {
const lib = await resolveProject(tree, options.target);
const ngPackagePath = `${lib?.definition.root}/${packageName}/ng-package.json`;
const packageInfo = tree.read(ngPackagePath);
if (packageInfo) {
throw new SchematicsException(
interpolate(Exception.LibraryAlreadyExists, `${lib.name}/${packageName}`),
);
}
return false;
}
const target = await resolveProject(tree, options.packageName, null);
if (target) {
throw new SchematicsException(interpolate(Exception.LibraryAlreadyExists, packageName));
}
return false;
}
function createLibrary(options: GenerateLibSchema): Rule {
return async (tree: Tree, _context: SchematicContext) => {
const target = await resolveProject(tree, options.packageName, null);
if (!target || options.override) {
if (options.isModuleTemplate) {
return createLibFromModuleTemplate(tree, options);
}
if (options.isSecondaryEntrypoint) {
return createLibSecondaryEntry(tree, options);
}
} else {
throw new SchematicsException(
interpolate(Exception.LibraryAlreadyExists, options.packageName),
);
}
};
}
async function resolvePackagesDirFromAngularJson(host: Tree) {
const workspace = await getWorkspace(host);
const projectFolder = readFirstLibInAngularJson(workspace);
return projectFolder?.root?.split('/')?.[0] || 'projects';
}
function readFirstLibInAngularJson(workspace: WorkspaceDefinition): ProjectDefinition | undefined {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, props] =
Array.from(workspace.projects.entries()).find(([_, value]) => isLibrary(value)) || [];
return props;
}
async function createLibFromModuleTemplate(tree: Tree, options: GenerateLibSchema) {
const packagesDir = await resolvePackagesDirFromAngularJson(tree);
const packageJson = JSON.parse(tree.read('./package.json')!.toString());
const abpVersion = packageJson.dependencies['@abp/ng.core'];
return chain([
applyWithOverwrite(url('./files-package'), [
applyTemplates({
...cases,
libraryName: options.packageName,
abpVersion,
}),
move(normalize(packagesDir)),
]),
addLibToWorkspaceIfNotExist(options.packageName, packagesDir),
]);
}
export function addLibToWorkspaceIfNotExist(name: string, packagesDir: string): Rule {
return async (tree: Tree, _: SchematicContext) => {
const workspace = await getWorkspace(tree);
const packageName = kebab(name);
const isProjectExist = workspace.projects.has(packageName);
const projectRoot = join(normalize(packagesDir), packageName);
const pathImportLib = `${packagesDir}/${packageName}`;
return chain([
isProjectExist
? updateWorkspace(w => {
w.projects.delete(packageName);
})
: noop(),
addLibToWorkspaceFile(projectRoot, packageName),
updateTsConfig(packageName, pathImportLib),
]);
};
}
export function updateTsConfig(packageName: string, path: string) {
return (host: Tree) => {
const files = ['tsconfig.json', 'tsconfig.app.json', 'tsconfig.base.json'];
const tsConfig = files.find(f => host.exists(f));
if (!tsConfig) {
return host;
}
const file = new JSONFile(host, tsConfig);
const jsonPath = ['compilerOptions', 'paths', packageName];
file.modify(jsonPath, [`${path}/src/public-api.ts`]);
};
}
export async function createLibSecondaryEntry(tree: Tree, options: GenerateLibSchema) {
const targetLib = await resolveProject(tree, options.target);
const packageName = `${kebab(targetLib.name)}/${kebab(options.packageName)}`;
const importPath = `${targetLib.definition.root}/${kebab(options.packageName)}`;
return chain([
applyWithOverwrite(url('./files-secondary-entrypoint'), [
applyTemplates({
...cases,
libraryName: options.packageName,
target: targetLib.name,
}),
move(normalize(targetLib.definition.root)),
updateTsConfig(packageName, importPath),
]),
]);
}

17
npm/ng-packs/packages/schematics/src/commands/create-lib/models/generate-lib-schema.ts

@ -0,0 +1,17 @@
export interface GenerateLibSchema {
/**
* Angular package name will create
*/
packageName: string;
/**
* İs the package a library or a library module
*/
isSecondaryEntrypoint: boolean;
isModuleTemplate: boolean;
override: boolean;
target: string;
}

1
npm/ng-packs/packages/schematics/src/commands/create-lib/models/index.ts

@ -0,0 +1 @@
export * from './generate-lib-schema';

36
npm/ng-packs/packages/schematics/src/commands/create-lib/schema.json

@ -0,0 +1,36 @@
{
"$schema": "http://json-schema.org/schema",
"$id": "SchematicsABPModuleTemplateCreator",
"title": "ABP Module Template Generator API Schema",
"type": "object",
"properties": {
"packageName": {
"description": "The name of the package will create",
"type": "string",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "Please enter the package name will create"
},
"isSecondaryEntrypoint": {
"description": "Is secondary entrypoint",
"type": "boolean",
"$default": false,
"x-prompt": "Is secondary entrypoint?"
},
"isModuleTemplate": {
"description": "Is module template",
"type": "boolean",
"$default": true,
"x-prompt": "Is module template?"
},
"override": {
"description": "Override existing files",
"type": "boolean",
"$default": false,
"x-prompt": "Override existing files?"
}
},
"required": []
}

1
npm/ng-packs/packages/schematics/src/constants/symbols.ts

@ -0,0 +1 @@
export const NOT_FOUND_VALUE = Symbol('NOT_FOUND_VALUE');

1
npm/ng-packs/packages/schematics/src/enums/exception.ts

@ -13,4 +13,5 @@ export const enum Exception {
NoEnvironment = '[Environment Not Found] An environment file cannot be located in "{0}" project.',
NoApiUrl = '[API URL Not Found] Cannot resolve API URL for "{1}" remote service name from "{0}" project.',
NoRootNamespace = '[Root Namespace Not Found] Cannot resolve root namespace for "{1}" api from "{0}" project.',
LibraryAlreadyExists = '[Library Already Exists] "{0}" already exists. Use --override to override.',
}

10
npm/ng-packs/packages/schematics/src/models/generate-proxy-schema.ts

@ -23,4 +23,14 @@ export interface GenerateProxySchema {
* Url to API definition
*/
url?: string;
/**
* Create a lib for proxy generation
*/
createTarget?: boolean;
/**
* Secondary entrypoint for proxy generation
*/
entryPoint?: string;
}

52
npm/ng-packs/packages/schematics/src/utils/angular-schematic/generate-lib.ts

@ -0,0 +1,52 @@
import { Rule, Tree } from '@angular-devkit/schematics';
import { Builders, JSONFile, ProjectType, updateWorkspace } from '../angular';
export function updateTsConfig(packageName: string, ...paths: string[]) {
return (host: Tree) => {
if (!host.exists('tsconfig.json')) {
return host;
}
const file = new JSONFile(host, 'tsconfig.json');
const jsonPath = ['compilerOptions', 'paths', packageName];
const value = file.get(jsonPath);
file.modify(jsonPath, Array.isArray(value) ? [...value, ...paths] : paths);
};
}
export function addLibToWorkspaceFile(projectRoot: string, projectName: string): Rule {
return updateWorkspace(workspace => {
workspace.projects.add({
name: projectName,
root: projectRoot,
sourceRoot: `${projectRoot}/src`,
projectType: ProjectType.Library,
prefix: 'lib',
targets: {
build: {
builder: Builders.NgPackagr,
defaultConfiguration: 'production',
options: {
project: `${projectRoot}/ng-package.json`,
},
configurations: {
production: {
tsConfig: `${projectRoot}/tsconfig.lib.prod.json`,
},
development: {
tsConfig: `${projectRoot}/tsconfig.lib.json`,
},
},
},
test: {
builder: Builders.Karma,
options: {
main: `${projectRoot}/src/test.ts`,
tsConfig: `${projectRoot}/tsconfig.spec.json`,
karmaConfig: `${projectRoot}/karma.conf.js`,
},
},
},
});
});
}

1
npm/ng-packs/packages/schematics/src/utils/angular-schematic/index.ts

@ -0,0 +1 @@
export * from './generate-lib';

1
npm/ng-packs/packages/schematics/src/utils/index.ts

@ -7,6 +7,7 @@ export * from './enum';
export * from './file';
export * from './generics';
export * from './import';
export * from './lib';
export * from './methods';
export * from './model';
export * from './namespace';

9
npm/ng-packs/packages/schematics/src/utils/lib.ts

@ -0,0 +1,9 @@
import { Tree } from '@angular-devkit/schematics';
import { resolveProject } from "./workspace";
export async function createTargetLibIfNotExist(tree: Tree, target: string) {
const lib = await resolveProject(tree, target, null);
if (!lib) {
}
}

2
npm/ng-packs/packages/schematics/src/utils/source.ts

@ -23,7 +23,7 @@ export function createApiDefinitionGetter(params: GenerateProxySchema) {
if (!sourceUrl) {
sourceUrl = getSourceUrl(host, source, apiName);
}
return await getApiDefinition(sourceUrl);
return await getApiDefinition(sourceUrl!);
};
}

34
npm/ng-packs/packages/schematics/src/utils/workspace.ts

@ -3,9 +3,11 @@ import { strings, workspaces } from '@angular-devkit/core';
import { SchematicsException, Tree } from '@angular-devkit/schematics';
import { Exception } from '../enums';
import { Project } from '../models';
import { getWorkspace, getWorkspaceSchema, ProjectType, WorkspaceSchema } from './angular';
import { getWorkspace, ProjectType, WorkspaceSchema } from './angular';
import { findEnvironmentExpression } from './ast';
import { readFileInTree } from './common';
import { NOT_FOUND_VALUE } from '../constants/symbols';
import { parseJson } from '@angular/cli/utilities/json-file';
export function isLibrary(project: workspaces.ProjectDefinition): boolean {
return project.extensions['projectType'] === ProjectType.Library;
@ -33,8 +35,11 @@ export function readWorkspaceSchema(tree: Tree) {
return workspaceSchema;
}
export async function resolveProject(tree: Tree, name: string): Promise<Project> {
export async function resolveProject<T = any>(
tree: Tree,
name: string,
notFoundValue: T = NOT_FOUND_VALUE as unknown as any,
): Promise<Project | T> {
name = name || readWorkspaceSchema(tree).defaultProject!;
const workspace = await getWorkspace(tree);
let definition: Project['definition'] | undefined;
@ -61,7 +66,30 @@ export async function resolveProject(tree: Tree, name: string): Promise<Project>
definition = workspace.projects.get(name);
} catch (_) {}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!definition && notFoundValue !== NOT_FOUND_VALUE) {
return notFoundValue;
}
if (!definition) throw new SchematicsException(Exception.NoProject);
return { name, definition };
}
export function getWorkspaceSchema(host: Tree): WorkspaceSchema {
const path = getWorkspacePath(host);
const configBuffer = host.read(path);
if (configBuffer === null) {
throw new SchematicsException(`Could not find (${path})`);
}
const content = configBuffer.toString();
return parseJson(content) as Record<string, any> as WorkspaceSchema;
}
export function getWorkspacePath(host: Tree): string {
const possibleFiles = ['/angular.json', '/.angular.json'];
const path = possibleFiles.filter(path => host.exists(path))[0];
return path;
}

6
npm/ng-packs/scripts/build-schematics.ts

@ -20,6 +20,12 @@ class FileCopy {
const PACKAGE_TO_BUILD = 'schematics';
const FILES_TO_COPY_AFTER_BUILD: (FileCopy | string)[] = [
{ src: 'src/commands/create-lib/schema.json', dest: 'commands/create-lib/schema.json' },
{ src: 'src/commands/create-lib/files-package', dest: 'commands/create-lib/files-package' },
{
src: 'src/commands/create-lib/files-secondary-entrypoint',
dest: 'commands/create-lib/files-secondary-entrypoint',
},
{ src: 'src/commands/proxy-add/schema.json', dest: 'commands/proxy-add/schema.json' },
{ src: 'src/commands/proxy-index/schema.json', dest: 'commands/proxy-index/schema.json' },
{ src: 'src/commands/proxy-refresh/schema.json', dest: 'commands/proxy-refresh/schema.json' },

Loading…
Cancel
Save