Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: build builder and schematics for o3r lib #2727

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/@o3r/core/schematics/ng-add-create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {
createSchematicWithMetricsIfInstalled,
findConfigFileRelativePath,
getPackageManagerRunner,
getPackageManagerExecutor,
} from '@o3r/schematics';
import type {
PackageJson,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
];

Expand All @@ -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: [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

@kpanot kpanot Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be instead something like:

Suggested change
const tsConfigPath = '/tsconfig.json';
const tsConfigPath =['/tsconfig.lib.json', '/tsconfig.app.json', '/tsconfig.json'].filter((file) => tree.exists(file));

and run on all the file found?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only about the root tsconfig file so there won't be app/lib ones

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting for #2648 to be merged and to adapt this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed offline, we would want to target the root tsconfig.json

if (tree.exists(tsConfigPath)) {
const tsConfig = ts.parseConfigFileTextToJson(tsConfigPath, tree.readText(tsConfigPath)).config as TsConfigJson;
if (tsConfig.compilerOptions) {
if (tsConfig.compilerOptions.noPropertyAccessFromIndexSignature) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to update their tsconfig?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is where the compilerOptions that are incompatible with our project are generated.
To avoid any confusion it is best to update them at the root rather than to add them in the tsconfig.build.json which is expanding the root one.

In this particular case, we are removing noPropertyAccessFromIndexSignature because of some lint issues.

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;
};
Expand Down
7 changes: 5 additions & 2 deletions packages/@o3r/workspace/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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',
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
} from '@angular-devkit/schematics';
import {
enforceTildeRange,
getPackageManagerExecutor,
getPackageManagerRunner,
getWorkspaceConfig,
} from '@o3r/schematics';
Expand All @@ -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;
Expand All @@ -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'],
Expand Down
Loading