Skip to content

Commit

Permalink
feat: clean package install in ngAdd schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
kpanot committed Jan 16, 2024
1 parent dd25c3e commit b163a96
Show file tree
Hide file tree
Showing 36 changed files with 934 additions and 518 deletions.
12 changes: 6 additions & 6 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ packageExtensions:
"@types/node": ^20.0.0
esbuild: ~0.19.0
eslint: ^8.42.0
nx: ^17.1.1
nx: ~17.2.0
rxjs: ^7.8.1
typescript: ~5.2.2
"@nx/cypress@^17.1.1":
dependencies:
typescript: ~5.2.2
"@nx/devkit@*":
dependencies:
nx: ^17.1.1
nx: ~17.2.0
typescript: ~5.2.2
"@nx/eslint-plugin@^17.1.1":
dependencies:
"@typescript-eslint/parser": ^6.11.0
eslint: ^8.42.0
"@nx/eslint@^17.1.1":
dependencies:
nx: ^17.1.1
nx: ~17.2.0
"@nx/jest@^17.1.1":
dependencies:
nx: ^17.1.1
nx: ~17.2.0
typescript: ~5.2.2
"@nx/js@^17.1.1":
dependencies:
"@types/node": ^20.0.0
nx: ^17.1.1
nx: ~17.2.0
typescript: ~5.2.2
"@nx/webpack@^17.1.1":
dependencies:
"@types/node": ^20.0.0
nx: ^17.1.1
nx: ~17.2.0
typescript: ~5.2.2
postcss-loader@^7.2.4:
dependencies:
Expand Down
24 changes: 15 additions & 9 deletions packages/@o3r/apis-manager/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

import type { DependencyToAdd } from '@o3r/schematics';
import * as path from 'node:path';
import { NgAddSchematicsSchema } from './schema';

Expand All @@ -10,27 +10,33 @@ import { NgAddSchematicsSchema } from './schema';
export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const { ngAddPackages, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesType } = await import('@o3r/schematics');
const { setupDependencies, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesTypes } = await import('@o3r/schematics');
const { updateApiDependencies } = await import('../helpers/update-api-deps');
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
const rulesToExecute: Rule[] = [];
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root;
const projectType = workspaceProject?.projectType || 'application';
if (projectType === 'application') {
rulesToExecute.push(updateApiDependencies(options));
}

const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `~${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}]
};
return acc;
}, {} as Record<string, DependencyToAdd>);

return () => chain([
...rulesToExecute,
options.skipLinter ? noop : applyEsLintFix(),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
parentPackageInfo: depsInfo.packageName,
setupDependencies({
projectName: options.projectName,
dependencyType: getProjectNewDependenciesType(workspaceProject),
workingDirectory
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
})
])(tree, context);

Expand Down
23 changes: 14 additions & 9 deletions packages/@o3r/application/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'node:path';
import type { NgAddSchematicsSchema } from './schema';
import { registerDevtools } from './helpers/devtools-registration';
import { generateCmsConfigFile } from './helpers/cms-registration';
import type { DependencyToAdd } from '@o3r/schematics';


/**
Expand All @@ -15,14 +16,13 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const {
addImportToModuleFile, getAppModuleFilePath, getModuleIndex, getWorkspaceConfig, insertImportToModuleFile, ngAddPackages, getO3rPeerDeps, getProjectNewDependenciesType
addImportToModuleFile, getAppModuleFilePath, getModuleIndex, getWorkspaceConfig, insertImportToModuleFile, setupDependencies, getO3rPeerDeps, getProjectNewDependenciesTypes
} = await import('@o3r/schematics');
const { isImported } = await import('@schematics/angular/utility/ast-utils');
const ts = await import('typescript');
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));

const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root;

const addAngularAnimationPreferences: Rule = () => {
const moduleFilePath = getAppModuleFilePath(tree, context, options.projectName);
Expand Down Expand Up @@ -69,17 +69,22 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
tree.commitUpdate(recorder);
return tree;
};
const dependencyType = getProjectNewDependenciesType(workspaceProject);
const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `^${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}]
};
return acc;
}, {} as Record<string, DependencyToAdd>);

const registerDevtoolRule = await registerDevtools(options);
return () => chain([
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
parentPackageInfo: depsInfo.packageName,
setupDependencies({
projectName: options.projectName,
dependencyType,
workingDirectory
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
}),
addAngularAnimationPreferences,
registerDevtoolRule,
Expand Down
24 changes: 15 additions & 9 deletions packages/@o3r/components/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as fs from 'node:fs';
import { updateCmsAdapter } from '../cms-adapter';
import type { NgAddSchematicsSchema } from './schema';
import { registerDevtools } from './helpers/devtools-registration';
import type { DependencyToAdd } from '@o3r/schematics';

/**
* Add Otter components to an Angular Project
Expand All @@ -16,9 +17,9 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
const {
getDefaultOptionsForSchematic,
getO3rPeerDeps,
getProjectNewDependenciesType,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
ngAddPackages,
setupDependencies,
ngAddPeerDependencyPackages,
removePackages,
registerPackageCollectionSchematics
Expand All @@ -35,16 +36,21 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {

const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
const dependencyType = getProjectNewDependenciesType(workspaceProject);
const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `~${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}]
};
return acc;
}, {} as Record<string, DependencyToAdd>);
const rule = chain([
removePackages(['@otter/components']),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
parentPackageInfo: depsInfo.packageName,
setupDependencies({
projectName: options.projectName,
dependencyType,
workingDirectory
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
}),
ngAddPeerDependencyPackages(['chokidar'], packageJsonPath, NodeDependencyType.Dev, {...options, workingDirectory, skipNgAddSchematicRun: true}, '@o3r/components - install builder dependency'),
registerPackageCollectionSchematics(packageJson),
Expand Down
25 changes: 15 additions & 10 deletions packages/@o3r/configuration/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import type { NgAddSchematicsSchema } from './schema';
import { registerDevtools } from './helpers/devtools-registration';
import type { DependencyToAdd } from '@o3r/schematics';

/**
* Add Otter configuration to an Angular Project
Expand All @@ -13,8 +14,8 @@ export function ngAdd(options: NgAddSchematicsSchema) {
return async (tree: Tree, context: SchematicContext) => {
try {
const {
ngAddPackages,
getProjectNewDependenciesType,
setupDependencies,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
getO3rPeerDeps,
registerPackageCollectionSchematics,
Expand All @@ -24,8 +25,15 @@ export function ngAdd(options: NgAddSchematicsSchema) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }));
const depsInfo = getO3rPeerDeps(packageJsonPath);
const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined;
const workingDirectory = workspaceProject?.root || '.';
const dependencyType = getProjectNewDependenciesType(workspaceProject);
const dependencies = depsInfo.o3rPeerDeps.reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `~${depsInfo.packageVersion}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}]
};
return acc;
}, {} as Record<string, DependencyToAdd>);
context.logger.info(`The package ${depsInfo.packageName as string} comes with a debug mechanism`);
context.logger.info('Get more information on the following page: https://github.com/AmadeusITGroup/otter/tree/main/docs/configuration/OVERVIEW.md#Runtime-debugging');
return chain([
Expand All @@ -44,13 +52,10 @@ export function ngAdd(options: NgAddSchematicsSchema) {
useOtterConfig: undefined
}
}),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
parentPackageInfo: depsInfo.packageName,
setupDependencies({
projectName: options.projectName,
dependencyType,
workingDirectory
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
}),
await registerDevtools(options)
])(tree, context);
Expand Down
6 changes: 3 additions & 3 deletions packages/@o3r/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@
"zone.js": "~0.14.2"
},
"generatorDependencies": {
"@angular-eslint/eslint-plugin": "~17.1.0",
"@angular-eslint/eslint-plugin": "~17.2.0",
"@angular/material": "~17.0.1",
"@ngrx/router-store": "~17.0.0",
"@ngrx/effects": "~17.0.0",
"@ngrx/store-devtools": "~17.0.0",
"@o3r/store-sync": "workspace:^",
"@types/jest": "~29.5.2",
"nx": "~17.1.1",
"nx": "~17.2.0",
"@typescript-eslint/parser": "^6.11.0",
"cpy-cli": "^5.0.0",
"eslint": "^8.42.0",
"@nx/eslint-plugin": "~17.1.1",
"@nx/eslint-plugin": "~17.2.0",
"jsonc-eslint-parser": "~2.4.0",
"eslint-import-resolver-node": "^0.3.4",
"eslint-plugin-jest": "~27.6.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { chain, noop, Rule } from '@angular-devkit/schematics';
import type { NgAddSchematicsSchema } from './schema';
import * as path from 'node:path';
import { type DependencyToAdd } from '@o3r/schematics';

const doCustomAction: Rule = (tree, _context) => {
// your custom code here
return tree;
};

const dependenciesToInstall = [
// Add the dependencies to install here
];

const dependenciesToNgAdd = [
// Add the dependencies to install with NgAdd here
];

/**
* Add Otter <%= featureName %> to an Otter Project
*
Expand All @@ -16,17 +25,29 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {
return async (_tree, context) => {
try {
// use dynamic import to properly raise an exception if it is not an Otter project.
const { applyEsLintFix, install, ngAddPackages, getO3rPeerDeps } = await import('@o3r/schematics');
const { getProjectNewDependenciesTypes, applyEsLintFix, install, getO3rPeerDeps } = await import('@o3r/schematics');
// retrieve dependencies following the /^@o3r\/.*/ pattern within the peerDependencies of the current module
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
// current package version
const version = JSON.stringify(fs.readFileSync(packageJsonPath)).version;
const dependencies = [...dependenciesToInstall, ...dependenciesToNgAdd].reduce((acc, dep) => {
acc[dep] = {
inManifest: [{
range: `~${version}`,
types: getProjectNewDependenciesTypes(workspaceProject)
}]
};
return acc;
}, {} as Record<string, DependencyToAdd>);
return chain([
// optional custom action dedicated to this module
doCustomAction,
options.skipLinter ? noop() : applyEsLintFix(),
// install packages needed in the current module
options.skipInstall ? noop : install,
// add the missing Otter modules in the current project
ngAddPackages(depsInfo.o3rPeerDeps, { skipConfirmation: true, version: depsInfo.packageVersion, parentPackageInfo: `${depsInfo.packageName!} - setup` })
setupDependencies({
projectName: options.projectName,
dependencies,
ngAddToRun: dependenciesToNgAdd
})
]);
} catch (e) {
// If the installation is initialized in a non-Otter application, mandatory packages will be missing. We need to notify the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"title": "Add <%= featureName %> project",
"description": "'ng add <%= featureName %>' project",
"properties": {
"name": {
"projectName": {
"type": "string",
"description": "Project name",
"$default": {
Expand All @@ -18,8 +18,7 @@
},
"skipInstall": {
"type": "boolean",
"description": "Skip the install process",
"default": true
"description": "Skip the install process"
}
},
"additionalProperties": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SchematicOptionObject } from '@o3r/schematics';

export interface NgAddSchematicsSchema extends SchematicOptionObject {
/** Project name */
name: string | undefined;
projectName: string | undefined;

/** Skip the linter process */
skipLinter: boolean;
Expand Down
Loading

0 comments on commit b163a96

Please sign in to comment.