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 19, 2024
1 parent 591622f commit 854d032
Show file tree
Hide file tree
Showing 40 changed files with 1,133 additions and 633 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:tools": "yarn nx run-many --target=build --projects=eslint-plugin,workspace --parallel $(yarn get:cpus-number)",
"build:lint": "yarn nx run-many --target=build --projects=eslint-plugin --parallel $(yarn get:cpus-number)",
"build:swagger-gen": "yarn nx run-many --target=build-swagger --parallel $(yarn get:cpus-number)",
"prepare:publish": "prepare-publish $(yarn workspaces:list | shx sed \"s/^(.+)\\$/\\$1\\/dist/\")",
"prepare:publish": "prepare-publish $(yarn workspaces:list | yarn node -e 'let data=\"\"; process.openStdin().on(\"data\", (c) => data+=c).on(\"end\", () => process.stdout.write(data.split(/[\\n\\r]+/).slice(0, -1).map((l) => l + \"/dist\").join(require(\"os\").EOL)));')",
"publish": "yarn run prepare:publish && yarn nx run-many --target=publish --parallel $(yarn get:cpus-number) --nx-bail --access=public",
"publish:extensions": "yarn nx run-many --target=publish-extension --parallel $(yarn get:cpus-number)",
"publish:extensions:affected": "yarn nx affected --target=publish-extension --parallel $(yarn get:cpus-number)",
Expand All @@ -34,14 +34,14 @@
"doc:generate:json": "yarn update-doc-summary ./docs && yarn compodoc -e json -d .",
"start:modules": "yarn run build:dev:modules && yarn run watch:modules",
"storybook": "yarn doc:generate:json && yarn ng run storybook:extract-style && start-storybook -p 6006",
"verdaccio:start": "docker run -d -it --rm --name verdaccio -p 4873:4873 -v \"$(shx pwd)/.verdaccio/conf\":/verdaccio/conf verdaccio/verdaccio",
"verdaccio:start-persistent": "docker run -d -it --rm --name verdaccio -p 4873:4873 -v \"$(shx pwd)/.verdaccio/conf\":/verdaccio/conf -v \"$(shx pwd)/.verdaccio/storage\":/verdaccio/storage:z verdaccio/verdaccio",
"verdaccio:start": "podman run -d -it --rm --name verdaccio -p 4873:4873 -v \"$(yarn get:current-dir)/.verdaccio/conf\":/verdaccio/conf verdaccio/verdaccio",
"verdaccio:start-persistent": "podman run -d -it --rm --name verdaccio -p 4873:4873 -v \"$(yarn get:current-dir)/.verdaccio/conf\":/verdaccio/conf -v \"$(yarn get:current-dir)/.verdaccio/storage\":/verdaccio/storage:z verdaccio/verdaccio",
"verdaccio:clean": "rimraf -g \".verdaccio/storage/@{o3r,ama-sdk,ama-terasu}\"",
"verdaccio:login": "yarn cpy --cwd=./.verdaccio/conf .npmrc . --rename=.npmrc-logged && npx --yes npm-cli-login -u verdaccio -p verdaccio -e [email protected] -r http://127.0.0.1:4873 --config-path \".verdaccio/conf/.npmrc-logged\"",
"verdaccio:publish": "yarn verdaccio:clean && yarn set:version 999.0.0 --include \"!**/!(dist)/package.json\" --include !package.json && yarn verdaccio:login && yarn run publish --userconfig \".verdaccio/conf/.npmrc-logged\" --tag=latest --@o3r:registry=http://127.0.0.1:4873 --@ama-sdk:registry=http://127.0.0.1:4873 --@ama-terasu:registry=http://127.0.0.1:4873",
"verdaccio:stop": "docker container stop $(docker ps -a -q --filter=\"name=verdaccio\")",
"verdaccio:stop": "podman container stop $(podman ps -a -q --filter=\"name=verdaccio\")",
"watch:vscode-extension": "yarn nx run vscode-extension:compile:watch",
"workspaces:list": "yarn workspaces list --no-private --json | shx sed \"s/.*\\\"location\\\":\\\"(.*?)\\\".*/\\$1/\""
"workspaces:list": "yarn workspaces list --no-private --json | yarn node -e 'let data=\"\"; process.openStdin().on(\"data\", (c) => data+=c).on(\"end\", () => process.stdout.write(data.split(/[\\n\\r]+/).slice(0, -1).map((l) => JSON.parse(l).location).join(require(\"os\").EOL)));'"
},
"lint-staged": {
"*.ts": [
Expand Down Expand Up @@ -231,7 +231,6 @@
"sass": "~1.70.0",
"sass-loader": "^14.0.0",
"semver": "^7.5.2",
"shx": "^0.3.4",
"standard-version": "^9.0.0",
"stylelint": "^16.0.2",
"stylelint-scss": "^6.0.0",
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 { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { createSchematicWithMetricsIfInstalled, 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';
function ngAddFn(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 @@ -5,6 +5,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';

/**
* Add Otter application to an Angular Project
Expand All @@ -15,14 +16,13 @@ function ngAddFn(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 @@ function ngAddFn(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 @@ -5,6 +5,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 @@ -17,9 +18,9 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
const {
getDefaultOptionsForSchematic,
getO3rPeerDeps,
getProjectNewDependenciesType,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
ngAddPackages,
setupDependencies,
ngAddPeerDependencyPackages,
removePackages,
registerPackageCollectionSchematics
Expand All @@ -36,16 +37,21 @@ function ngAddFn(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 @@ -4,6 +4,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 @@ -14,8 +15,8 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext): Promise<Rule> => {
try {
const {
ngAddPackages,
getProjectNewDependenciesType,
setupDependencies,
getProjectNewDependenciesTypes,
getWorkspaceConfig,
getO3rPeerDeps,
registerPackageCollectionSchematics,
Expand All @@ -25,8 +26,15 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
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 @@ -45,13 +53,10 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
useOtterConfig: undefined
}
}),
ngAddPackages(depsInfo.o3rPeerDeps, {
skipConfirmation: true,
version: depsInfo.packageVersion,
parentPackageInfo: depsInfo.packageName,
setupDependencies({
projectName: options.projectName,
dependencyType,
workingDirectory
dependencies,
ngAddToRun: depsInfo.o3rPeerDeps
}),
() => 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 @@ -142,9 +142,9 @@
"generatorDependencies": {
"@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",
"@ngrx/router-store": "~17.1.0",
"@ngrx/effects": "~17.1.0",
"@ngrx/store-devtools": "~17.1.0",
"@o3r/store-sync": "workspace:^",
"@types/jest": "~29.5.2",
"nx": "~17.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,60 @@ import { chain, noop, Rule } from '@angular-devkit/schematics';
import { applyEsLintFix, createSchematicWithMetricsIfInstalled, install, ngAddPackages, getO3rPeerDeps } from '@o3r/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
*
* @param options
*/
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (_tree, context) => {
// retrieve dependencies following the /^@o3r\/.*/ pattern within the peerDependencies of the current module
const depsInfo = getO3rPeerDeps(path.resolve(__dirname, '..', '..', 'package.json'));
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` })
]);
try {
// use dynamic import to properly raise an exception if it is not an Otter project.
const { getProjectNewDependenciesTypes, applyEsLintFix, install, getO3rPeerDeps } = await import('@o3r/schematics');
// retrieve dependencies following the /^@o3r\/.*/ pattern within the peerDependencies of the current module
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(),
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
context.logger.error(`[ERROR]: Adding <%= featureName %> has failed.
If the error is related to missing @o3r dependencies you need to install '@o3r/core' to be able to use the <%= featureName %> package. Please run 'ng add @o3r/core' .
Otherwise, use the error message as guidance.`);
throw (e);
}
};
}

export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
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 854d032

Please sign in to comment.