Skip to content

Commit

Permalink
fix: build builders and schematics for o3r lib
Browse files Browse the repository at this point in the history
  • Loading branch information
vscaiceanu-1a committed Jan 28, 2025
1 parent beda44b commit 5bb56db
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
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';
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;
};
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();

Check failure on line 127 in packages/@o3r/workspace/schematics/index.it.spec.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-latest, yarn, o3r-project-with-app)

new otter workspace › should add a library to an existing workspace

expect(received).not.toThrow() Error name: "Error" Error message: "Command failed: yarn workspace test-library build:builders STDERR:· OUTPUT: ,schematics/ng-add/index.ts:3:21 - error TS2307: Cannot find module 'node:fs' or its corresponding type declarations.· 3 import * as fs from 'node:fs'; ~~~~~~~~~· schematics/ng-add/index.ts:4:23 - error TS2307: Cannot find module 'node:path' or its corresponding type declarations.· 4 import * as path from 'node:path'; ~~~~~~~~~~~· schematics/ng-add/index.ts:6:38 - error TS2304: Cannot find name '__dirname'.· 6 const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json'); ~~~~~~~~~·· Found 3 errors.· ," 115 | // Yarn doesn't log errors on stderr, so we need to get them from stdout to have them in the reports 116 | > 117 | throw new Error(`Command failed: ${args.join(' ')}\nSTDERR:\n${err.stderr?.toString() || ''}\nOUTPUT:\n${err.output?.toString() || ''}`); | ^ 118 | } 119 | } 120 | at execCmd (packages/@o3r/test-helpers/src/utilities/package-manager.ts:117:11) at packageManagerWorkspaceRun (packages/@o3r/test-helpers/src/utilities/package-manager.ts:237:10) at packageManagerRunOnProject (packages/@o3r/test-helpers/src/utilities/package-manager.ts:248:26) at packages/@o3r/workspace/schematics/index.it.spec.ts:127:44 at Object.<anonymous> (.yarn/cache/expect-npm-29.7.0-62e9f7979e-63f97bc51f.zip/node_modules/expect/build/toThrowMatchers.js:74:11) at Object.throwingMatcher [as toThrow] (.yarn/cache/expect-npm-29.7.0-62e9f7979e-63f97bc51f.zip/node_modules/expect/build/index.js:320:21) at Object.<anonymous> (packages/@o3r/workspace/schematics/index.it.spec.ts:127:120) at Object.<anonymous> (schematics/index.it.spec.ts:127:120)

Check failure on line 127 in packages/@o3r/workspace/schematics/index.it.spec.ts

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, yarn, o3r-project-with-app)

new otter workspace › should add a library to an existing workspace

expect(received).not.toThrow() Error name: "Error" Error message: "Command failed: yarn workspace test-library build:builders STDERR:· OUTPUT: ,schematics/ng-add/index.ts:3:21 - error TS2307: Cannot find module 'node:fs' or its corresponding type declarations.··· 3 import * as fs from 'node:fs';· ~~~~~~~~~··· schematics/ng-add/index.ts:4:23 - error TS2307: Cannot find module 'node:path' or its corresponding type declarations.··· 4 import * as path from 'node:path';· ~~~~~~~~~~~··· schematics/ng-add/index.ts:6:38 - error TS2304: Cannot find name '__dirname'.··· 6 const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');· ~~~~~~~~~····· Found 3 errors.··· ," 115 | // Yarn doesn't log errors on stderr, so we need to get them from stdout to have them in the reports 116 | > 117 | throw new Error(`Command failed: ${args.join(' ')}\nSTDERR:\n${err.stderr?.toString() || ''}\nOUTPUT:\n${err.output?.toString() || ''}`); | ^ 118 | } 119 | } 120 | at execCmd (packages/@o3r/test-helpers/src/utilities/package-manager.ts:117:11) at packageManagerWorkspaceRun (packages/@o3r/test-helpers/src/utilities/package-manager.ts:237:10) at packageManagerRunOnProject (packages/@o3r/test-helpers/src/utilities/package-manager.ts:248:26) at packages/@o3r/workspace/schematics/index.it.spec.ts:127:44 at Object.<anonymous> (.yarn/cache/expect-npm-29.7.0-62e9f7979e-63f97bc51f.zip/node_modules/expect/build/toThrowMatchers.js:74:11) at Object.throwingMatcher [as toThrow] (.yarn/cache/expect-npm-29.7.0-62e9f7979e-63f97bc51f.zip/node_modules/expect/build/index.js:320:21) at Object.<anonymous> (packages/@o3r/workspace/schematics/index.it.spec.ts:127:120) at Object.<anonymous> (schematics/index.it.spec.ts:127:120)
});

test('should generate a monorepo setup', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@o3r/workspace/schematics/library/rules/shared.ts
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 Down

0 comments on commit 5bb56db

Please sign in to comment.