Skip to content

Commit

Permalink
feat: move schematic to package dependencies to simplify ng-add imple…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
kpanot committed Nov 19, 2024
1 parent 72e7556 commit ce9a2ff
Show file tree
Hide file tree
Showing 97 changed files with 468 additions and 799 deletions.
1 change: 1 addition & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
"source": [
"base-build",
"{projectRoot}/ng-package.json",
"{projectRoot}/tsconfig.build.json",
"{projectRoot}/tsconfig.build.*.json",
"{projectRoot}/src/**/*",
Expand Down
5 changes: 1 addition & 4 deletions packages/@ama-sdk/client-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prepare:publish": "prepare-publish ./dist"
},
"dependencies": {
"@o3r/schematics": "workspace:^",
"@swc/helpers": "~0.5.0",
"tslib": "^2.6.2",
"uuid": "^10.0.0"
Expand All @@ -48,7 +49,6 @@
"@angular-devkit/schematics": "~18.2.0",
"@angular/cli": "~18.2.0",
"@angular/common": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@schematics/angular": "~18.2.0",
"rxjs": "^7.8.1",
"typescript": "~5.5.4"
Expand All @@ -63,9 +63,6 @@
"@angular/common": {
"optional": true
},
"@o3r/schematics": {
"optional": true
},
"@schematics/angular": {
"optional": true
},
Expand Down
38 changes: 13 additions & 25 deletions packages/@ama-sdk/client-angular/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,28 @@ import type { NgAddSchematicsSchema } from './schema';
import * as path from 'node:path';
import { NodeDependencyType } from '@schematics/angular/utility/dependencies';
import { mapMigrationFromCoreImports } from './migration/import-map';
import {
applyEsLintFix,
createSchematicWithMetricsIfInstalled,
getExternalDependenciesVersionRange,
getO3rPeerDeps,
getPackageInstallConfig,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
setupDependencies,
updateImports
} from '@o3r/schematics';

const devDependenciesToInstall: string[] = [

];


const reportMissingSchematicsDep = (logger: { error: (message: string) => any }) => (reason: any) => {
logger.error(`[ERROR]: Adding @ama-sdk/client-angular has failed.
If the error is related to missing @o3r dependencies you need to install '@o3r/schematics' as devDependency to be able to use this schematics. Please run 'ng add @o3r/schematics'.
Otherwise, use the error message as guidance.`);
throw reason;
};

/**
* Add SDk Angular Client to an Otter Project
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree, context) => {
// use dynamic import to properly raise an exception if it is not an Otter project.
const {
getPackageInstallConfig,
applyEsLintFix,
setupDependencies,
getO3rPeerDeps,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
getExternalDependenciesVersionRange,
updateImports
} = await import('@o3r/schematics');

return (tree, context) => {
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const depsInfo = getO3rPeerDeps(packageJsonPath);
Expand Down Expand Up @@ -77,7 +68,4 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
* Add SDk Angular Client to an Otter Project
* @param options
*/
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, { logger }) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
export const ngAdd = (options: NgAddSchematicsSchema): Rule => createSchematicWithMetricsIfInstalled(ngAddFn)(options);
5 changes: 1 addition & 4 deletions packages/@ama-sdk/client-beacon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prepare:publish": "prepare-publish ./dist"
},
"dependencies": {
"@o3r/schematics": "workspace:^",
"@swc/helpers": "~0.5.0",
"tslib": "^2.6.2",
"uuid": "^10.0.0"
Expand All @@ -48,7 +49,6 @@
"@angular-devkit/schematics": "~18.2.0",
"@angular/cli": "~18.2.0",
"@angular/common": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@schematics/angular": "~18.2.0",
"typescript": "~5.5.4"
},
Expand All @@ -62,9 +62,6 @@
"@angular/common": {
"optional": true
},
"@o3r/schematics": {
"optional": true
},
"@schematics/angular": {
"optional": true
},
Expand Down
38 changes: 13 additions & 25 deletions packages/@ama-sdk/client-beacon/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,28 @@ import type { NgAddSchematicsSchema } from './schema';
import * as path from 'node:path';
import { NodeDependencyType } from '@schematics/angular/utility/dependencies';
import { mapMigrationFromCoreImports } from './migration/import-map';
import {
applyEsLintFix,
createSchematicWithMetricsIfInstalled,
getExternalDependenciesVersionRange,
getO3rPeerDeps,
getPackageInstallConfig,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
setupDependencies,
updateImports
} from '@o3r/schematics';

const devDependenciesToInstall: string[] = [

];


const reportMissingSchematicsDep = (logger: { error: (message: string) => any }) => (reason: any) => {
logger.error(`[ERROR]: Adding @ama-sdk/client-beacon has failed.
If the error is related to missing @o3r dependencies you need to install '@o3r/schematics' as devDependency to be able to use this schematics. Please run 'ng add @o3r/schematics'.
Otherwise, use the error message as guidance.`);
throw reason;
};

/**
* Add SDk Beacon Client to an Otter Project
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree, context) => {
// use dynamic import to properly raise an exception if it is not an Otter project.
const {
getPackageInstallConfig,
applyEsLintFix,
setupDependencies,
getO3rPeerDeps,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
getExternalDependenciesVersionRange,
updateImports
} = await import('@o3r/schematics');

return (tree, context) => {
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const depsInfo = getO3rPeerDeps(packageJsonPath);
Expand Down Expand Up @@ -77,7 +68,4 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
* Add SDk Beacon Client to an Otter Project
* @param options
*/
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, { logger }) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
export const ngAdd = (options: NgAddSchematicsSchema): Rule => createSchematicWithMetricsIfInstalled(ngAddFn)(options);
5 changes: 1 addition & 4 deletions packages/@ama-sdk/client-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"prepare:publish": "prepare-publish ./dist"
},
"dependencies": {
"@o3r/schematics": "workspace:^",
"@swc/helpers": "~0.5.0",
"tslib": "^2.6.2",
"uuid": "^10.0.0"
Expand All @@ -48,7 +49,6 @@
"@angular-devkit/schematics": "~18.2.0",
"@angular/cli": "~18.2.0",
"@angular/common": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@schematics/angular": "~18.2.0",
"isomorphic-fetch": "^3.0.0",
"typescript": "~5.5.4"
Expand All @@ -63,9 +63,6 @@
"@angular/common": {
"optional": true
},
"@o3r/schematics": {
"optional": true
},
"@schematics/angular": {
"optional": true
},
Expand Down
37 changes: 14 additions & 23 deletions packages/@ama-sdk/client-fetch/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,30 @@ import type { NgAddSchematicsSchema } from './schema';
import * as path from 'node:path';
import { NodeDependencyType } from '@schematics/angular/utility/dependencies';
import { mapMigrationFromCoreImports } from './migration/import-map';
import {
applyEsLintFix,
createSchematicWithMetricsIfInstalled,
getExternalDependenciesVersionRange,
getO3rPeerDeps,
getPackageInstallConfig,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
setupDependencies,
updateImports
} from '@o3r/schematics';

const devDependenciesToInstall: string[] = [

];


const reportMissingSchematicsDep = (logger: { error: (message: string) => any }) => (reason: any) => {
logger.error(`[ERROR]: Adding @ama-sdk/client-fetch has failed.
If the error is related to missing @o3r dependencies you need to install '@o3r/schematics' as devDependency to be able to use this schematics. Please run 'ng add @o3r/schematics'.
Otherwise, use the error message as guidance.`);
throw reason;
};

/**
* Add SDk Fetch Client to an Otter Project
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree, context) => {
return (tree, context) => {
// use dynamic import to properly raise an exception if it is not an Otter project.
const {
getPackageInstallConfig,
applyEsLintFix,
setupDependencies,
getO3rPeerDeps,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
getExternalDependenciesVersionRange,
updateImports
} = await import('@o3r/schematics');


const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
Expand Down Expand Up @@ -77,7 +71,4 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
* Add SDk Fetch Client to an Otter Project
* @param options
*/
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, { logger }) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
export const ngAdd = (options: NgAddSchematicsSchema): Rule => createSchematicWithMetricsIfInstalled(ngAddFn)(options);
5 changes: 1 addition & 4 deletions packages/@ama-sdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"prepare:publish": "prepare-publish ./dist"
},
"dependencies": {
"@o3r/schematics": "workspace:^",
"@swc/helpers": "~0.5.0",
"tslib": "^2.6.2",
"uuid": "^10.0.0"
Expand All @@ -90,7 +91,6 @@
"@angular-devkit/schematics": "~18.2.0",
"@angular/cli": "~18.2.0",
"@angular/common": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@schematics/angular": "~18.2.0",
"isomorphic-fetch": "^3.0.0",
"rxjs": "^7.8.1",
Expand All @@ -106,9 +106,6 @@
"@angular/common": {
"optional": true
},
"@o3r/schematics": {
"optional": true
},
"@schematics/angular": {
"optional": true
},
Expand Down
26 changes: 10 additions & 16 deletions packages/@ama-sdk/core/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import * as path from 'node:path';
import * as ts from 'typescript';
import type { NgAddSchematicsSchema } from './schema';

const reportMissingSchematicsDep = (logger: { error: (message: string) => any }) => (reason: any) => {
logger.error(`[ERROR]: Adding @ama-sdk/core has failed.
If the error is related to missing @o3r dependencies you need to install '@o3r/schematics' to be able to use the @ama-sdk/core ng add. Please run 'ng add @o3r/schematics' .
Otherwise, use the error message as guidance.`);
throw reason;
};
import {
createSchematicWithMetricsIfInstalled,
getFilesInFolderFromWorkspaceProjectsInTree,
getPeerDepWithPattern,
getWorkspaceConfig,
removePackages
} from '@o3r/schematics';

/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
Expand All @@ -19,13 +19,11 @@ const reportMissingSchematicsDep = (logger: { error: (message: string) => any })
function ngAddFn(options: NgAddSchematicsSchema): Rule {

const removeImports: Rule = async () => {
const {removePackages} = await import('@o3r/schematics');
return removePackages(['@dapi/sdk-core']);
};

/* ng add rules */
const updateImports: Rule = async (tree) => {
const {getFilesInFolderFromWorkspaceProjectsInTree} = await import('@o3r/schematics');
const updateImports: Rule = (tree) => {
const files = getFilesInFolderFromWorkspaceProjectsInTree(tree, '', 'ts');
files.forEach((file) => {
const sourceFile = ts.createSourceFile(
Expand All @@ -51,8 +49,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
};


const addMandatoryPeerDeps: Rule = async (tree, context) => {
const { getPeerDepWithPattern, getWorkspaceConfig } = await import('@o3r/schematics');
const addMandatoryPeerDeps: Rule = (tree, context) => {
const workingDirectory = options?.projectName && getWorkspaceConfig(tree)?.projects[options.projectName]?.root || '.';
const peerDepToInstall = getPeerDepWithPattern(path.resolve(__dirname, '..', '..', 'package.json'));
context.addTask(new NodePackageInstallTask({
Expand All @@ -78,7 +75,4 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
* Helps to migrate from previous versions with an import replacement
* @param options
*/
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, context) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(context.logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
export const ngAdd = (options: NgAddSchematicsSchema): Rule => createSchematicWithMetricsIfInstalled(ngAddFn)(options);
3 changes: 1 addition & 2 deletions packages/@ama-sdk/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@
"@angular-devkit/core": "~18.2.0",
"@angular-devkit/schematics-cli": "^18.0.5",
"@angular/cli": "~18.2.0",
"@o3r/schematics": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.15.0",
"openapi-types": "^12.0.0",
"type-fest": "^4.10.2"
},
"dependencies": {
"@o3r/schematics": "workspace:^",
"@angular-devkit/core": "~18.2.0",
"@angular-devkit/schematics": "~18.2.0",
"chokidar": "^3.5.2",
Expand All @@ -93,7 +93,6 @@
"@nx/jest": "~19.5.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/telemetry": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.15.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
url
} from '@angular-devkit/schematics';
import { NgGenerateApiExtensionSchematicsSchema } from './schema';
import {
createSchematicWithMetricsIfInstalled
} from '@o3r/schematics';

/**
* Generate a Extension of a API core definition
Expand All @@ -31,7 +34,4 @@ function ngGenerateApiExtensionFn(options: NgGenerateApiExtensionSchematicsSchem
* Generate a Extension of a API core definition
* @param options
*/
export const ngGenerateApiExtension = (options: NgGenerateApiExtensionSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateApiExtensionFn)(options);
};
export const ngGenerateApiExtension = (options: NgGenerateApiExtensionSchematicsSchema) => createSchematicWithMetricsIfInstalled(ngGenerateApiExtensionFn)(options);
Loading

0 comments on commit ce9a2ff

Please sign in to comment.