Skip to content

Commit

Permalink
fix: the tsconfig spec generated for library contains several issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaulve-1A committed Jan 10, 2025
1 parent a083fe5 commit 52a91bf
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 61 deletions.
10 changes: 10 additions & 0 deletions packages/@o3r/workspace/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as path from 'node:path';
import {
getDefaultExecSyncOptions,
getGitDiff,
getPackageManager,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand Down Expand Up @@ -95,6 +96,10 @@ describe('new otter workspace', () => {
const execAppOptions = { ...getDefaultExecSyncOptions(), cwd: workspacePath };
const libName = 'test-library';
const inLibraryPath = path.resolve(workspacePath, 'libs', libName);
// TODO Remove node-linker once https://github.com/AmadeusITGroup/otter/issues/2620 is completed
if (getPackageManager() === 'yarn') {
packageManagerExec({ script: 'config', args: ['set', 'nodeLinker', 'node-modules'] }, execAppOptions);
}
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();

const generatedLibFiles = [
Expand All @@ -115,13 +120,18 @@ 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(() => packageManagerExec({ script: 'ng', args: ['test', '--watch=false', '--browsers=ChromeHeadless'] }, execAppOptions)).not.toThrow();

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

View workflow job for this annotation

GitHub Actions / it-tests / it-tests (ubuntu-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 exec ng test -- --watch=false --browsers=ChromeHeadless STDERR: - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/untouched-lib/src/lib/untouched-lib.component.spec.ts:3:39 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { UntouchedLibComponent } from './untouched-lib.component'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/untouched-lib/src/lib/untouched-lib.service.spec.ts:3:37 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.service.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { UntouchedLibService } from './untouched-lib.service'; ~~~~~~~~~~~~~~~~~~~~~~~~~·· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/test-lib/src/lib/test-lib.component.spec.ts:3:34 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibComponent } from './test-lib.component'; ~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/test-lib/src/lib/test-lib.service.spec.ts:3:32 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.service.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibService } from './test-lib.service'; ~~~~~~~~~~~~~~~~~~~~·· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/test-library/src/lib/test-library.component.spec.ts:3:38 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibraryComponent } from './test-library.component'; ~~~~~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/test-library/src/lib/test-library.service.spec.ts:3:36 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.service.ts

Check failure on line 123 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 ng test --watch=false --browsers=ChromeHeadless STDERR: - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/untouched-lib/src/lib/untouched-lib.component.spec.ts:3:39 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { UntouchedLibComponent } from './untouched-lib.component'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/untouched-lib/src/lib/untouched-lib.service.spec.ts:3:37 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.service.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { UntouchedLibService } from './untouched-lib.service'; ~~~~~~~~~~~~~~~~~~~~~~~~~·· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/test-lib/src/lib/test-lib.component.spec.ts:3:34 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibComponent } from './test-lib.component'; ~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/test-lib/src/lib/test-lib.service.spec.ts:3:32 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.service.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibService } from './test-lib.service'; ~~~~~~~~~~~~~~~~~~~~·· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.·· Error: libs/test-library/src/lib/test-library.component.spec.ts:3:38 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.component.ts' is not listed within the file list of project '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.· 3 import { TestLibraryComponent } from './test-library.component'; ~~~~~~~~~~~~~~~~~~~~~~~~~~·· Error: libs/test-library/src/lib/test-library.service.spec.ts:3:36 - error TS6307: File '/home/runner/work/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.service.ts' is no

Check failure on line 123 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 ng test --watch=false --browsers=ChromeHeadless STDERR: - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/untouched-lib/src/lib/untouched-lib.component.spec.ts:3:39 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { UntouchedLibComponent } from './untouched-lib.component';· ~~~~~~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/untouched-lib/src/lib/untouched-lib.service.spec.ts:3:37 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { UntouchedLibService } from './untouched-lib.service';· ~~~~~~~~~~~~~~~~~~~~~~~~~··· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/test-lib/src/lib/test-lib.component.spec.ts:3:34 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibComponent } from './test-lib.component';· ~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/test-lib/src/lib/test-lib.service.spec.ts:3:32 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibService } from './test-lib.service';· ~~~~~~~~~~~~~~~~~~~~··· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/test-library/src/lib/test-library.component.spec.ts:3:38 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibraryComponent } from './test-library.component';· ~~~~~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/test-library/src/lib/test-library.service.spec.ts:3:36 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec.json'.

Check failure on line 123 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 exec ng test -- --watch=false --browsers=ChromeHeadless STDERR: - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/untouched-lib/src/lib/untouched-lib.component.spec.ts:3:39 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { UntouchedLibComponent } from './untouched-lib.component';· ~~~~~~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/untouched-lib/src/lib/untouched-lib.service.spec.ts:3:37 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/src/lib/untouched-lib.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/untouched-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { UntouchedLibService } from './untouched-lib.service';· ~~~~~~~~~~~~~~~~~~~~~~~~~··· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/test-lib/src/lib/test-lib.component.spec.ts:3:34 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibComponent } from './test-lib.component';· ~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/test-lib/src/lib/test-lib.service.spec.ts:3:32 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/src/lib/test-lib.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-lib/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibService } from './test-lib.service';· ~~~~~~~~~~~~~~~~~~~~··· ✔ Browser application bundle generation complete. ✔ Browser application bundle generation complete. - Generating browser application bundles (phase: setup)... ✔ Browser application bundle generation complete. Error: error TS6304: Composite projects may not disable declaration emit.··· Error: libs/test-library/src/lib/test-library.component.spec.ts:3:38 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.component.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec.json'. Projects must list all files or use an 'include' pattern.··· 3 import { TestLibraryComponent } from './test-library.component';· ~~~~~~~~~~~~~~~~~~~~~~~~~~··· Error: libs/test-library/src/lib/test-library.service.spec.ts:3:36 - error TS6307: File 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/src/lib/test-library.service.ts' is not listed within the file list of project 'D:/a/otter/it-tests/test-app-workspace3/libs/test-library/tsconfig.spec
});

test('should generate a monorepo setup', async () => {
const { workspacePath } = o3rEnvironment.testEnvironment;
const defaultOptions = getDefaultExecSyncOptions();

const execAppOptions = { ...defaultOptions, cwd: workspacePath, env: { ...defaultOptions.env, NX_CLOUD_ACCESS_TOKEN: '' } };
// TODO Remove node-linker once https://github.com/AmadeusITGroup/otter/issues/2620 is completed
if (getPackageManager() === 'yarn') {
packageManagerExec({ script: 'config', args: ['set', 'nodeLinker', 'node-modules'] }, execAppOptions);
}
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
const rootPackageJson = JSON.parse(await fs.readFile(path.join(workspacePath, 'package.json'), 'utf8')) as PackageJson;
expect(rootPackageJson.scripts).toHaveProperty('build', 'lerna run build');
Expand Down
52 changes: 47 additions & 5 deletions packages/@o3r/workspace/schematics/library/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ jest.mock('@angular-devkit/schematics', () => {
});

const collectionPath = path.join(__dirname, '..', '..', 'collection.json');
const angularJsonFile = `{
"version": 1,
"projects": {
"my-new-module": {
"projectType": "library",
"root": "packages-test/my-new-module"
}
}
}`;

describe('New module generator', () => {
let initialTree: Tree;
Expand All @@ -36,7 +45,7 @@ describe('New module generator', () => {
});

it('should generate the minimum mandatory files', async () => {
initialTree.create('angular.json', '{"version": 1, "projects": {} }');
initialTree.create('angular.json', angularJsonFile);
initialTree.create('package.json', '{ "version": "0.0.0-test" }');
initialTree.create('/packages-test/my-new-module/package.json', '{ "version": "0.0.0-test" }');
initialTree.create('/packages-test/my-new-module/ng-package.json', '{ }');
Expand All @@ -58,11 +67,42 @@ describe('New module generator', () => {
expect(tree.exists('/packages-test/my-new-module/project.json')).toBe(false);
expect(JSON.parse(tree.readContent('/tsconfig.base.json')).compilerOptions.paths['@my/new-module']).toContain('packages-test/my-new-module/src/public-api');
expect(JSON.parse(tree.readContent('/tsconfig.build.json')).compilerOptions.paths['@my/new-module'][0]).toBe('packages-test/my-new-module/dist');
expect(tree.exists('/packages-test/my-new-module/testing/setup-jest.ts')).toBe(false);
expect(JSON.parse(tree.readContent('/packages-test/my-new-module/package.json')).scripts.test).toContain('ng test my-new-module');
expect(tree.exists('/packages-test/my-new-module/jest.config.js')).toBe(false);
expect(tree.files.length).toBeGreaterThanOrEqual(9);
});

// eslint-disable-next-line jest/no-disabled-tests -- TODO: Should be re-enable when the following issue #2066 is fixed
describe.skip('in NX monorepo', () => {
it('should generate an project with jest files', async () => {
initialTree.create('angular.json', angularJsonFile);
initialTree.create('package.json', '{ "version": "0.0.0-test" }');
initialTree.create('/packages-test/my-new-module/package.json', '{ "version": "0.0.0-test" }');
initialTree.create('/packages-test/my-new-module/ng-package.json', '{ }');
const runner = new SchematicTestRunner('schematics', collectionPath);
const angularPackageJson = require.resolve('@schematics/angular/package.json');
const o3rCorePackageJson = require.resolve('@o3r/core/package.json');
runner.registerCollection('@o3r/core', path.resolve(path.dirname(o3rCorePackageJson), require(o3rCorePackageJson).schematics));
runner.registerCollection('@schematics/angular', path.resolve(path.dirname(angularPackageJson), require(angularPackageJson).schematics));
jest.spyOn(require('@angular-devkit/schematics'), 'externalSchematic');
const tree = await runner.runSchematic('library', {
path: 'packages-test',
name: '@my/new-module',
skipLinter: true,
skipInstall: true,
testingFramework: 'jest'

}, initialTree);
expect(tree.exists('/packages-test/my-new-module/testing/setup-jest.ts')).toBe(true);
expect(tree.exists('/packages-test/my-new-module/jest.config.js')).toBe(true);
expect(JSON.parse(tree.readContent('/packages-test/my-new-module/package.json')).scripts.test).toContain('jest');
(tree.readJson('/packages-test/my-new-module/tsconfig.spec.json') as { references: { path: string }[] })
.references
.forEach((ref) => {
expect(tree.exists(path.join('/package-test/my-new-module', ref.path)));
});
});

describe('in NX monorepo', () => {
it('should generate Nx project.json with given name', async () => {
initialTree.create('nx.json', '{"workspaceLayout": { "libsDir": "packages-test" } }');
initialTree.create('angular.json', '{"version": 1, "projects": {} }');
Expand All @@ -77,14 +117,16 @@ describe('New module generator', () => {
runner.registerCollection('@schematics/angular', path.resolve(path.dirname(angularPackageJson), require(angularPackageJson).schematics));
runner.registerCollection('@nx/workspace', path.resolve(path.dirname(nxWorkspacePackageJson), require(nxWorkspacePackageJson).generators));
const tree = await runner.runExternalSchematic('schematics', 'library', {
path: 'packages-test',
name: '@my/new-module',
projectName: 'test-module-name',
skipLinter: true
}, initialTree);

expect(tree.exists('/packages-test/my-new-module/project.json')).toBe(true);
expect(tree.readContent('/packages-test/my-new-module/project.json')).toContain('"name": "test-module-name"');
const projectJson: any = tree.readJson('/packages-test/my-new-module/project.json');
expect(projectJson.name).toBe('test-module-name');
expect(tree.exists(projectJson.targets.test.options.jestConfig)).toBe(true);
expect(projectJson.targets.test.executor).toBe('@nrwl/jest:jest');
});
});
});
38 changes: 33 additions & 5 deletions packages/@o3r/workspace/schematics/library/rules/rules.ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import type {
NgGenerateModuleSchema,
} from '../schema';
import {
setUpAngularTestPackageJson,
setUpJestOnPackageJson,
updateNgPackagrFactory,
updatePackageDependenciesFactory,
} from './shared';
Expand All @@ -50,21 +52,47 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
const o3rCorePackageJson: PackageJson & { generatorDependencies?: Record<string, string> } = JSON.parse(readFileSync(o3rCorePackageJsonPath).toString());
const otterVersion = o3rCorePackageJson.dependencies!['@o3r/schematics'];

const templateNg = apply(url('./templates/ng'), [
const templateNg = apply(url('./templates/ng/common'), [
template({
...options,
tsconfigSpecPath: findConfigFileRelativePath(tree,
['tsconfig.test.json', 'tsconfig.spec.json', 'tsconfig.jest.json', 'tsconfig.jasmine.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath),
tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath),
tsconfigBuildPath: findConfigFileRelativePath(tree, ['tsconfig.build.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath)
}),
renameTemplateFiles(),
move(options.targetPath)
]);

const templateCommonJest = apply(url('./templates/jest'), [
template({
...options,
tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath)
}),
renameTemplateFiles(),
move(options.targetPath)
]);
const templateNgJest = apply(url('./templates/ng/jest'), [
template({
...options,
tsconfigSpecPath: findConfigFileRelativePath(tree,
['tsconfig.test.json', 'tsconfig.spec.json', 'tsconfig.jest.json', 'tsconfig.jasmine.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath),
tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath)
}),
renameTemplateFiles(),
move(options.targetPath)
]);
const packageJsonContent = tree.readText('/package.json');
const hasJestInstalled = options.testingFramework === 'jest' || packageJsonContent.match('jest');
return chain([
mergeWith(templateNg, MergeStrategy.Overwrite),
updatePackageDependenciesFactory(options.targetPath, otterVersion!, o3rCorePackageJson, options),
...hasJestInstalled
? [
mergeWith(templateCommonJest, MergeStrategy.Overwrite),
mergeWith(templateNgJest, MergeStrategy.Overwrite),
setUpJestOnPackageJson(options)
]
: [
setUpAngularTestPackageJson(options)
],
updatePackageDependenciesFactory(options.targetPath, otterVersion!, o3rCorePackageJson, { ...options, useJest: !!hasJestInstalled }),
updateNgPackagrFactory(options.targetPath),
(t) => {
const genPackageJsonPath = path.posix.join(options.targetPath, 'package.json');
Expand Down
30 changes: 27 additions & 3 deletions packages/@o3r/workspace/schematics/library/rules/rules.nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ export function nxGenerateModule(options: NgGenerateModuleSchema & { packageJson
return path.posix.join(workspaceLayout.libsDir, options.name);
};

const setUpJestOnProjectJson = (tree: Tree) => {
// const projectJsonContent = tree.readJson(`${options.targetPath}/project.json`) as { targets: any };
// projectJsonContent.targets ||= {};
// projectJsonContent.targets.test = {
// test: {
// executor: '@nx/jest:jest',
// options: {
// jestConfig: `${options.targetPath}/jest.config.js`
// }
// }
// };
// tree.overwrite(`${options.targetPath}/project.json`, JSON.stringify(projectJsonContent, null, 2));
return tree;
};

/**
* Update Nx templates
* @param tree File tree
Expand All @@ -105,25 +120,34 @@ export function nxGenerateModule(options: NgGenerateModuleSchema & { packageJson
const o3rCorePackageJsonPath = path.resolve(__dirname, '..', '..', '..', 'package.json');
const o3rCorePackageJson: PackageJson & { generatorDependencies?: Record<string, string> } = JSON.parse(readFileSync(o3rCorePackageJsonPath).toString());
const otterVersion = o3rCorePackageJson.dependencies!['@o3r/schematics'];

const templateNx = apply(url('./templates/nx'), [
template({
...options,
path: targetPath,
projectRoot: path.posix.resolve(targetPath, options.name),
projectRoot: targetPath,
otterVersion,
tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], targetPath),
runner: getPackageManagerRunner(getWorkspaceConfig(tree))
}),
renameTemplateFiles(),
move(targetPath)
]);
const templateJest = apply(url('./templates/jest'), [
template({
...options,
tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], targetPath)
}),
renameTemplateFiles(),
move(targetPath)
]);
rules.push(mergeWith(templateNx, MergeStrategy.Overwrite));

return chain([
...rules,
updatePackageDependenciesFactory(targetPath, otterVersion!, o3rCorePackageJson, options),
updatePackageDependenciesFactory(targetPath, otterVersion!, o3rCorePackageJson, { ...options, useJest: true }),
updateNgPackagrFactory(targetPath),
mergeWith(templateJest, MergeStrategy.Overwrite),
setUpJestOnProjectJson,
(t) => {
const packageJson = t.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson;
packageJson.name = options.packageJsonName;
Expand Down
49 changes: 42 additions & 7 deletions packages/@o3r/workspace/schematics/library/rules/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'node:path';
import type {
Rule,
Tree,
} from '@angular-devkit/schematics';
import {
enforceTildeRange,
Expand All @@ -14,6 +15,36 @@ import {
NgGenerateModuleSchema,
} from '../schema';

/**
* Set jest files and script in the generated library.
* @param options
*/
export function setUpJestOnPackageJson(options: NgGenerateModuleSchema & { targetPath: string }) {
return (tree: Tree) => {
const packageJsonPath = path.join(options.targetPath, 'package.json');
const packageJsonContent = tree.readJson(packageJsonPath) as PackageJson;
packageJsonContent.scripts ||= {};
packageJsonContent.scripts.test ||= 'jest';
tree.overwrite(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
return tree;
};
}

/**
* Set jest files and script in the generated library.
* @param options
*/
export function setUpAngularTestPackageJson(options: NgGenerateModuleSchema & { targetPath: string }) {
return (tree: Tree) => {
const packageJsonPath = path.join(options.targetPath, 'package.json');
const packageJsonContent = tree.readJson(packageJsonPath) as PackageJson;
packageJsonContent.scripts ||= {};
packageJsonContent.scripts.test ||= `ng test ${options.name}`;
tree.overwrite(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
return tree;
};
}

/**
* Generate rule to update generated package.json file
* @param targetPath Path of the generated files
Expand All @@ -25,7 +56,7 @@ export function updatePackageDependenciesFactory(
targetPath: string,
otterVersion: string,
o3rCorePackageJson: PackageJson & { generatorDependencies?: Record<string, string> },
options: NgGenerateModuleSchema): Rule {
options: NgGenerateModuleSchema & { useJest?: boolean }): Rule {
return (tree) => {
const packageJson = tree.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson;
const runner = getPackageManagerRunner(getWorkspaceConfig(tree));
Expand Down Expand Up @@ -56,20 +87,24 @@ export function updatePackageDependenciesFactory(
'@angular/platform-browser': packageJson.peerDependencies['@angular/common'],
'@angular/platform-browser-dynamic': packageJson.peerDependencies['@angular/common'],
'@schematics/angular': o3rCorePackageJson.peerDependencies!['@schematics/angular'],
'@types/jest': o3rCorePackageJson.generatorDependencies!['@types/jest'],
'@typescript-eslint/eslint-plugin': o3rCorePackageJson.generatorDependencies!['@typescript-eslint/parser'],
'@typescript-eslint/parser': o3rCorePackageJson.generatorDependencies!['@typescript-eslint/parser'],
'cpy-cli': o3rCorePackageJson.generatorDependencies!['cpy-cli'],
eslint: o3rCorePackageJson.generatorDependencies!.eslint,
'eslint-import-resolver-node': o3rCorePackageJson.generatorDependencies!['eslint-import-resolver-node'],
'eslint-plugin-jest': o3rCorePackageJson.generatorDependencies!['eslint-plugin-jest'],
...options.useJest
? {
'@types/jest': o3rCorePackageJson.generatorDependencies!['@types/jest'],
'eslint-plugin-jest': o3rCorePackageJson.generatorDependencies!['eslint-plugin-jest'],
jest: o3rCorePackageJson.generatorDependencies!.jest,
'jest-environment-jsdom': o3rCorePackageJson.generatorDependencies!.jest,
'jest-junit': o3rCorePackageJson.generatorDependencies!['jest-junit'],
'jest-preset-angular': o3rCorePackageJson.generatorDependencies!['jest-preset-angular']
}
: {},
'eslint-plugin-jsdoc': o3rCorePackageJson.generatorDependencies!['eslint-plugin-jsdoc'],
'eslint-plugin-prefer-arrow': o3rCorePackageJson.generatorDependencies!['eslint-plugin-prefer-arrow'],
'eslint-plugin-unicorn': o3rCorePackageJson.generatorDependencies!['eslint-plugin-unicorn'],
jest: o3rCorePackageJson.generatorDependencies!.jest,
'jest-environment-jsdom': o3rCorePackageJson.generatorDependencies!.jest,
'jest-junit': o3rCorePackageJson.generatorDependencies!['jest-junit'],
'jest-preset-angular': o3rCorePackageJson.generatorDependencies!['jest-preset-angular'],
rxjs: o3rCorePackageJson.peerDependencies!.rxjs,
typescript: o3rCorePackageJson.peerDependencies!.typescript,
'zone.js': o3rCorePackageJson.generatorDependencies!['zone.js']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"composite": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"composite": true,
"outDir": "test",
"rootDir": ".",
"rootDir": "."
},
"include": [
"./src/**/*.spec.ts"
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 52a91bf

Please sign in to comment.