Skip to content

Commit

Permalink
feat: migrate schematics import to dynamic (#1469)
Browse files Browse the repository at this point in the history
## Proposed change

Migrate schematics import to dynamic

## Related issues

- 🚀 Feature #1429

<!-- Please make sure to follow the contributing guidelines on
https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
  • Loading branch information
kpanot committed Mar 12, 2024
2 parents e842556 + bb19e07 commit 0234135
Show file tree
Hide file tree
Showing 38 changed files with 1,013 additions and 880 deletions.
27 changes: 12 additions & 15 deletions packages/@ama-sdk/core/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import { chain, type Rule } from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
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;
};

/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
* Helps to migrate from previous versions with an import replacement
* @param options schema options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {

const checkSchematicsDependency: Rule = async (_, context) => {
try {
await import('@o3r/schematics');
} catch (e) {
// o3r extractors needs o3r/core as peer dep. o3r/core will install o3r/schematics
context.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 (e);
}
};

const removeImports: Rule = async () => {
const {removePackages} = await import('@o3r/schematics');
return removePackages(['@dapi/sdk-core']);
Expand Down Expand Up @@ -76,7 +70,6 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
};

return chain([
checkSchematicsDependency,
removeImports,
updateImports,
addMandatoryPeerDeps
Expand All @@ -86,5 +79,9 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
* Helps to migrate from previous versions with an import replacement
* @param options
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, context) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(context.logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
template, Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { NgGenerateApiExtensionSchematicsSchema } from './schema';

/**
Expand All @@ -32,4 +31,7 @@ function ngGenerateApiExtensionFn(options: NgGenerateApiExtensionSchematicsSchem
* Generate a Extension of a API core definition
* @param options
*/
export const ngGenerateApiExtension = createSchematicWithMetricsIfInstalled(ngGenerateApiExtensionFn);
export const ngGenerateApiExtension = (options: NgGenerateApiExtensionSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateApiExtensionFn)(options);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { promises as fs } from 'node:fs';
import * as path from 'node:path';
import { treeGlob } from '../../helpers/tree-glob';
Expand Down Expand Up @@ -92,4 +91,7 @@ function ngGenerateJavaClientCoreFn(options: NgGenerateJavaClientCoreSchematicsS
* Generate a Java client SDK source code base on swagger specification
* @param options
*/
export const ngGenerateJavaClientCore = createSchematicWithMetricsIfInstalled(ngGenerateJavaClientCoreFn);
export const ngGenerateJavaClientCore = (options: NgGenerateJavaClientCoreSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateJavaClientCoreFn)(options);
};
6 changes: 4 additions & 2 deletions packages/@ama-sdk/schematics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isJsonObject } from '@angular-devkit/core';
import { chain, externalSchematic, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as path from 'node:path';
import { readFileSync } from 'node:fs';
import { lastValueFrom } from 'rxjs';
Expand Down Expand Up @@ -177,4 +176,7 @@ function ngAddFn(): Rule {
/**
* Add Otter ama-sdk-schematics to a Project
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
export const ngAdd = (): Rule => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngAddFn)(undefined);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
url
} from '@angular-devkit/schematics';
import type { Operation, PathObject } from '@ama-sdk/core';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { readFileSync } from 'node:fs';
import * as path from 'node:path';
import * as semver from 'semver';
Expand Down Expand Up @@ -247,4 +246,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
* Generate a typescript SDK source code base on swagger specification
* @param options
*/
export const ngGenerateTypescriptSDK = createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn);
export const ngGenerateTypescriptSDK = (options: NgGenerateTypescriptSDKCoreSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn)(options);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as path from 'node:path';
import {NgGenerateMockSchematicsSchema} from './schema';

Expand Down Expand Up @@ -102,4 +101,7 @@ function ngGenerateMockFn(options: NgGenerateMockSchematicsSchema): Rule {
* Add mock
* @param options
*/
export const ngGenerateMock = createSchematicWithMetricsIfInstalled(ngGenerateMockFn);
export const ngGenerateMock = (options: NgGenerateMockSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateMockFn)(options);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import {dump, load} from 'js-yaml';
import {isAbsolute, posix, relative} from 'node:path';
import {getPackageManagerName, NpmInstall} from '../../helpers/node-install';
Expand Down Expand Up @@ -117,4 +116,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchemati
* Generate Typescript SDK shell
* @param options
*/
export const ngGenerateTypescriptSDK = createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn);
export const ngGenerateTypescriptSDK = (options: NgGenerateTypescriptSDKShellSchematicsSchema) => async () => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics');
return createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn)(options);
};
20 changes: 14 additions & 6 deletions packages/@o3r/analytics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import type { Rule } from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled, getPackageInstallConfig, setupDependencies } from '@o3r/schematics';
import type { NgAddSchematicsSchema } from './schema';
import * as path from 'node:path';

const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');

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

/**
* Add Otter analytics to an Angular Project
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
/* ng add rules */
return (tree) => {
return async (tree) => {
const { getPackageInstallConfig, setupDependencies } = await import('@o3r/schematics');
return setupDependencies({
projectName: options.projectName,
dependencies: getPackageInstallConfig(packageJsonPath, tree, options.projectName, false, !!options.exactO3rVersion)
});
};
}

/**
* Add Otter analytics to an Angular Project
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);

export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, {logger}) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
85 changes: 42 additions & 43 deletions packages/@o3r/apis-manager/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled, getPackageInstallConfig } from '@o3r/schematics';
import { chain, noop, type Rule } from '@angular-devkit/schematics';
import * as path from 'node:path';
import type { NgAddSchematicsSchema } from './schema';

const reportMissingSchematicsDep = (logger: { error: (message: string) => any }) => (reason: any) => {
logger.error(`[ERROR]: Adding @o3r/apis-manager has failed.
You need to install '@o3r/schematics' to be able to use the o3r apis-manager package. Please run 'ng add @o3r/schematics'.`);
throw reason;
};

/**
* Add Otter apis manager to an Angular Project
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const { setupDependencies, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesTypes } = await import('@o3r/schematics');
const { updateApiDependencies } = await import('../helpers/update-api-deps');
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const depsInfo = getO3rPeerDeps(packageJsonPath);
const rulesToExecute: Rule[] = [];
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const projectType = workspaceProject?.projectType || 'application';
if (projectType === 'application') {
rulesToExecute.push(updateApiDependencies(options));
}

const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `${options.exactO3rVersion ? '' : '~'}${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}],
ngAddOptions: { exactO3rVersion: options.exactO3rVersion }
};
return acc;
}, getPackageInstallConfig(packageJsonPath, tree, options.projectName, false, !!options.exactO3rVersion));

return () => chain([
...rulesToExecute,
options.skipLinter ? noop : applyEsLintFix(),
setupDependencies({
projectName: options.projectName,
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
})
])(tree, context);

} catch (e) {
// o3r apis-manager needs o3r/schematics as peer dep.
context.logger.error(`[ERROR]: Adding @o3r/apis-manager has failed.
You need to install '@o3r/schematics' to be able to use the o3r apis-manager package. Please run 'ng add @o3r/schematics' .`);
throw (e);
return async (tree) => {
const { getPackageInstallConfig } = await import('@o3r/schematics');
const { setupDependencies, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesTypes } = await import('@o3r/schematics');
const { updateApiDependencies } = await import('../helpers/update-api-deps');
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const depsInfo = getO3rPeerDeps(packageJsonPath);
const rulesToExecute: Rule[] = [];
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const projectType = workspaceProject?.projectType || 'application';
if (projectType === 'application') {
rulesToExecute.push(updateApiDependencies(options));
}
};

const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `${options.exactO3rVersion ? '' : '~'}${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}],
ngAddOptions: { exactO3rVersion: options.exactO3rVersion }
};
return acc;
}, getPackageInstallConfig(packageJsonPath, tree, options.projectName, false, !!options.exactO3rVersion));

return () => chain([
...rulesToExecute,
options.skipLinter ? noop : applyEsLintFix(),
setupDependencies({
projectName: options.projectName,
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
})
]);
};
}

/**
* Add Otter apis manager to an Angular Project
* @param options
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
export const ngAdd = (options: NgAddSchematicsSchema): Rule => async (_, { logger }) => {
const { createSchematicWithMetricsIfInstalled } = await import('@o3r/schematics').catch(reportMissingSchematicsDep(logger));
return createSchematicWithMetricsIfInstalled(ngAddFn)(options);
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { apply, MergeStrategy, mergeWith, move, renameTemplateFiles, Rule, template, url } from '@angular-devkit/schematics';
import type { NgAddSchematicsSchema } from '../schema';
import { getWorkspaceConfig } from '@o3r/schematics';

/**
* Register Otter Application module to the application
* @param options
*/
export const generateCmsConfigFile = (options: NgAddSchematicsSchema): Rule => {
return (tree, context) => {
return async (tree) => {
const { getWorkspaceConfig } = await import('@o3r/schematics');
const workingDirectory = options?.projectName && getWorkspaceConfig(tree)?.projects[options.projectName]?.root || '.';
const templateSource = apply(url('./templates'),
[
Expand All @@ -17,6 +17,6 @@ export const generateCmsConfigFile = (options: NgAddSchematicsSchema): Rule => {
]
);

return mergeWith(templateSource, MergeStrategy.Default)(tree, context);
return mergeWith(templateSource, MergeStrategy.Default);
};
};

0 comments on commit 0234135

Please sign in to comment.