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 23, 2025
1 parent bd1de82 commit 218ffa6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 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,7 +1,7 @@
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');

Expand All @@ -10,11 +10,11 @@ const doCustomAction: Rule = (tree, _context) => {
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, _context) => {
// 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
4 changes: 4 additions & 0 deletions packages/@o3r/workspace/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -115,6 +116,9 @@ describe('new otter workspace', () => {
expect(existsSync(path.join(workspacePath, 'project'))).toBe(false);
generatedLibFiles.forEach((file) => expect(existsSync(path.join(inLibraryPath, file))).toBe(true));
expect(() => packageManagerRunOnProject(libName, true, { script: 'build' }, execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(libName, true, { script: 'prepare:build:builders' }, execAppOptions)).not.toThrow();

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

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (windows-latest, npm, 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: npm run --workspace test-library prepare:build:builders STDERR: Cannot copy `'collection.json'`: the file doesn't exist npm error Lifecycle script `prepare:build:builders` failed with error: npm error code 1 npm error path D:\\a\\otter\\it-tests\\test-app-workspace3\\libs\\test-library npm error workspace [email protected] npm error location D:\\a\\otter\\it-tests\\test-app-workspace3\\libs\\test-library npm error command failed npm error command C:\\Windows\\system32\\cmd.exe /d /s /c npm exec cpy 'collection.json' dist && npm exec cpy 'schematics/**/*.json' dist/schematics· OUTPUT: , > [email protected] prepare:build:builders > npm exec cpy 'collection.json' dist && npm exec cpy 'schematics/**/*.json' dist/schematics· ,Cannot copy `'collection.json'`: the file doesn't exist npm error Lifecycle script `prepare:build:builders` failed with error: npm error code 1 npm error path D:\\a\\otter\\it-tests\\test-app-workspace3\\libs\\test-library npm error workspace [email protected] npm error location D:\\a\\otter\\it-tests\\test-app-workspace3\\libs\\test-library npm error command failed npm error command C:\\Windows\\system32\\cmd.exe /d /s /c npm exec cpy 'collection.json' dist && npm exec cpy 'schematics/**/*.json' dist/schematics " 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:119: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:119:119) at Object.<anonymous> (schematics/index.it.spec.ts:119:119)
expect(() => packageManagerRunOnProject(libName, true, { script: 'build:builders' }, execAppOptions)).not.toThrow();

Check failure on line 120 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:120: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:120:111) at Object.<anonymous> (schematics/index.it.spec.ts:120:111)

Check failure on line 120 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:120: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:120:111) at Object.<anonymous> (schematics/index.it.spec.ts:120:111)
expect(() => packageManagerRunOnProject(libName, true, { script: 'build:schematics' }, execAppOptions)).not.toThrow();
});

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 218ffa6

Please sign in to comment.