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 cabfaae
Show file tree
Hide file tree
Showing 5 changed files with 33 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();
});

test('should generate a monorepo setup', async () => {
Expand Down
5 changes: 4 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 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

0 comments on commit cabfaae

Please sign in to comment.