diff --git a/packages/@o3r/core/schematics/ng-add-create/index.ts b/packages/@o3r/core/schematics/ng-add-create/index.ts index de5f535a8c..69fe1782a5 100644 --- a/packages/@o3r/core/schematics/ng-add-create/index.ts +++ b/packages/@o3r/core/schematics/ng-add-create/index.ts @@ -16,7 +16,7 @@ import { import { createSchematicWithMetricsIfInstalled, findConfigFileRelativePath, - getPackageManagerRunner, + getPackageManagerExecutor, } from '@o3r/schematics'; import type { PackageJson, @@ -46,10 +46,9 @@ function updateTemplatesFn(options: NgGenerateUpdateSchematicsSchema): Rule { // prepare needed deps for schematics const angularVersion = packageJson.devDependencies?.['@angular/cli'] || packageJson.devDependencies?.['@angular/core']; const otterVersion = o3rCorePackageJson.dependencies!['@o3r/schematics']; - const packageManagerRunner = getPackageManagerRunner(); + const exec = getPackageManagerExecutor(); packageJson.scripts ||= {}; - // eslint-disable-next-line @stylistic/max-len -- keep the command on the same line - packageJson.scripts['build:schematics'] = `tsc -b tsconfig.builders.json --pretty && ${packageManagerRunner} cpy 'schematics/**/*.json' dist/schematics && ${packageManagerRunner} cpy 'collection.json' dist`; + packageJson.scripts['build:schematics'] = `tsc -b tsconfig.builders.json --pretty && ${exec} cpy 'schematics/**/*.json' dist/schematics && ${exec} cpy collection.json dist`; packageJson.schematics = './collection.json'; packageJson.peerDependencies ||= {}; packageJson.peerDependencies['@angular-devkit/schematics'] = angularVersion; diff --git a/packages/@o3r/core/schematics/ng-add-create/templates/schematics/ng-add/index.ts.template b/packages/@o3r/core/schematics/ng-add-create/templates/schematics/ng-add/index.ts.template index 65ca20c3e0..fbd6f1868d 100644 --- a/packages/@o3r/core/schematics/ng-add-create/templates/schematics/ng-add/index.ts.template +++ b/packages/@o3r/core/schematics/ng-add-create/templates/schematics/ng-add/index.ts.template @@ -1,20 +1,20 @@ import { chain, noop, Rule } from '@angular-devkit/schematics'; import type { NgAddSchematicsSchema } from './schema'; +import * as fs from 'node:fs'; import * as path from 'node:path'; -import type { DependencyToAdd } from '@o3r/schematics'; const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json'); -const doCustomAction: Rule = (tree, _context) => { +const doCustomAction: Rule = (tree) => { // your custom code here return tree; }; -const dependenciesToInstall = [ +const dependenciesToInstall: string[] = [ // Add the dependencies to install here ]; -const dependenciesToNgAdd = [ +const dependenciesToNgAdd: string[] = [ // Add the dependencies to install with NgAdd here ]; @@ -31,11 +31,12 @@ Otherwise, use the error message as guidance.`); * @param options */ function ngAddFn(options: NgAddSchematicsSchema): Rule { - return async (tree, context) => { + return async (tree) => { // use dynamic import to properly raise an exception if it is not an Otter project. - const { getProjectNewDependenciesTypes, getPackageInstallConfig, applyEsLintFix, install } = await import('@o3r/schematics'); + const { getProjectNewDependenciesTypes, getPackageInstallConfig, applyEsLintFix, getWorkspaceConfig, setupDependencies } = await import('@o3r/schematics'); // current package version - const version = JSON.stringify(fs.readFileSync(packageJsonPath)).version; + const version = JSON.parse(fs.readFileSync(packageJsonPath, {encoding: 'utf8'})).version; + const workspaceProject = options.projectName ? getWorkspaceConfig(tree)?.projects[options.projectName] : undefined; const dependencies = [...dependenciesToInstall, ...dependenciesToNgAdd].reduce((acc, dep) => { acc[dep] = { inManifest: [{ diff --git a/packages/@o3r/core/schematics/rule-factories/otter-environment/index.ts b/packages/@o3r/core/schematics/rule-factories/otter-environment/index.ts index be1baaada6..e028cbbeed 100644 --- a/packages/@o3r/core/schematics/rule-factories/otter-environment/index.ts +++ b/packages/@o3r/core/schematics/rule-factories/otter-environment/index.ts @@ -18,15 +18,23 @@ import { TYPES_DEFAULT_FOLDER, } from '@o3r/schematics'; import generateEnvironments from '@schematics/angular/environments/index'; +import type { + TsConfigJson, +} from 'type-fest'; import * as ts from 'typescript'; const editTsConfigJson = (tree: Tree) => { - if (tree.exists('/tsconfig.json')) { - const tsConfig = ts.parseConfigFileTextToJson('/tsconfig.json', tree.readText('/tsconfig.json')).config; - if (tsConfig.compilerOptions?.noPropertyAccessFromIndexSignature) { - delete tsConfig.compilerOptions.noPropertyAccessFromIndexSignature; + const tsConfigPath = '/tsconfig.json'; + if (tree.exists(tsConfigPath)) { + const tsConfig = ts.parseConfigFileTextToJson(tsConfigPath, tree.readText(tsConfigPath)).config as TsConfigJson; + if (tsConfig.compilerOptions) { + if (tsConfig.compilerOptions.noPropertyAccessFromIndexSignature) { + delete tsConfig.compilerOptions.noPropertyAccessFromIndexSignature; + } + tsConfig.compilerOptions.moduleResolution = 'node'; + tsConfig.compilerOptions.declaration = true; } - tree.overwrite('/tsconfig.json', JSON.stringify(tsConfig, null, 2)); + tree.overwrite(tsConfigPath, JSON.stringify(tsConfig, null, 2)); } return tree; }; diff --git a/packages/@o3r/workspace/schematics/index.it.spec.ts b/packages/@o3r/workspace/schematics/index.it.spec.ts index 956860ad69..bd22731b82 100644 --- a/packages/@o3r/workspace/schematics/index.it.spec.ts +++ b/packages/@o3r/workspace/schematics/index.it.spec.ts @@ -91,7 +91,7 @@ describe('new otter workspace', () => { }); test('should add a library to an existing workspace', async () => { - const { workspacePath } = o3rEnvironment.testEnvironment; + const { isInWorkspace, workspacePath } = o3rEnvironment.testEnvironment; const execAppOptions = { ...getDefaultExecSyncOptions(), cwd: workspacePath }; const libName = 'test-library'; const inLibraryPath = path.resolve(workspacePath, 'libs', libName); @@ -105,6 +105,7 @@ describe('new otter workspace', () => { 'tsconfig.lib.prod.json', 'tsconfig.spec.json', 'src/public-api.ts', + 'collection.json', '.gitignore', '.npmignore', 'jest.config.js', @@ -117,11 +118,13 @@ describe('new otter workspace', () => { generatedLibFiles.forEach((file) => expect(existsSync(path.join(inLibraryPath, file))).toBe(true)); // TODO apps are generated without jest full configuration - needs to be fixed before removing this part expect(() => packageManagerExec({ script: 'ng', args: ['test', libName] }, execAppOptions)).not.toThrow(); - expect(() => packageManagerRunOnProject(libName, true, { script: 'build' }, execAppOptions)).not.toThrow(); + expect(() => packageManagerRunOnProject(libName, isInWorkspace, { script: 'build' }, execAppOptions)).not.toThrow(); // check tsconfig.lib.prod.json override const tsconfigLibProd = JSON.parse(await fs.readFile(path.join(inLibraryPath, 'tsconfig.lib.prod.json'), { encoding: 'utf8' })); expect(!!tsconfigLibProd.extends && existsSync(path.resolve(inLibraryPath, tsconfigLibProd.extends))).toBe(true); + expect(() => packageManagerRunOnProject(libName, isInWorkspace, { script: 'prepare:build:builders' }, execAppOptions)).not.toThrow(); + expect(() => packageManagerRunOnProject(libName, isInWorkspace, { script: 'build:builders' }, execAppOptions)).not.toThrow(); }); test('should generate a monorepo setup', async () => { diff --git a/packages/@o3r/workspace/schematics/library/rules/shared.ts b/packages/@o3r/workspace/schematics/library/rules/shared.ts index a500a639cf..d48133d89d 100644 --- a/packages/@o3r/workspace/schematics/library/rules/shared.ts +++ b/packages/@o3r/workspace/schematics/library/rules/shared.ts @@ -4,6 +4,7 @@ import type { } from '@angular-devkit/schematics'; import { enforceTildeRange, + getPackageManagerExecutor, getPackageManagerRunner, getWorkspaceConfig, } from '@o3r/schematics'; @@ -29,10 +30,11 @@ export function updatePackageDependenciesFactory( return (tree) => { const packageJson = tree.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson; const runner = getPackageManagerRunner(getWorkspaceConfig(tree)); + const executor = getPackageManagerExecutor(getWorkspaceConfig(tree)); packageJson.description = options.description || packageJson.description; packageJson.scripts ||= {}; packageJson.scripts.build = `${runner} ng build ${options.name}`; - packageJson.scripts['prepare:build:builders'] = `${runner} cpy 'collection.json' dist && ${runner} cpy 'schematics/**/*.json' dist/schematics`; + packageJson.scripts['prepare:build:builders'] = `${executor} cpy collection.json dist && ${executor} cpy 'schematics/**/*.json' dist/schematics`; packageJson.scripts['build:builders'] = 'tsc -b tsconfig.builders.json --pretty'; packageJson.peerDependencies ||= {}; packageJson.peerDependencies['@o3r/core'] = otterVersion; @@ -56,6 +58,7 @@ export function updatePackageDependenciesFactory( '@angular/platform-browser-dynamic': packageJson.peerDependencies['@angular/common'], '@schematics/angular': o3rCorePackageJson.peerDependencies!['@schematics/angular'], 'cpy-cli': o3rCorePackageJson.generatorDependencies!['cpy-cli'], + '@types/node': o3rCorePackageJson.devDependencies!['@types/node'], ...options.useJest ? { '@angular-builders/jest': o3rCorePackageJson.generatorDependencies!['@angular-builders/jest'],