Skip to content

Commit

Permalink
fix: fixes in generated tsconfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrednic-1A committed Oct 27, 2023
1 parent 7981e07 commit a90c6c9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
2 changes: 2 additions & 0 deletions packages/@o3r/core/schematics/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RunSchematicTask } from '@angular-devkit/schematics/tasks';
import { NgAddSchematicsSchema } from '../ng-add/schema';
import { type Schema as ApplicationOptions } from '@schematics/angular/application/schema';
import { Style } from '@schematics/angular/application/schema';
import { updateProjectTsConfig } from '../rule-factories';

/**
* Add an Otter application to a monorepo
Expand Down Expand Up @@ -71,6 +72,7 @@ export function generateApplication(options: NgGenerateApplicationSchema): Rule
projectRoot,
style: Style.Scss}),
addProjectSpecificFiles(targetPath, rootDependencies),
updateProjectTsConfig(targetPath, 'tsconfig.app.json'),
(_host: Tree, c: SchematicContext) => {
c.addTask(new RunSchematicTask <Partial<NgAddSchematicsSchema>>('ng-add', extendedOptions));
}
Expand Down
34 changes: 7 additions & 27 deletions packages/@o3r/core/schematics/library/rules/rules.ng.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { PackageJson, TsConfigJson } from 'type-fest';
import type { NgGenerateModuleSchema } from '../schema';
import { findConfigFileRelativePath } from '@o3r/schematics';
import * as ts from 'typescript';
import { apply, chain, externalSchematic, MergeStrategy, mergeWith, move, renameTemplateFiles, Rule, template, url } from '@angular-devkit/schematics';
import * as path from 'node:path';
import { readFileSync } from 'node:fs';
import { updateNgPackagrFactory, updatePackageDependenciesFactory } from './shared';
import { updateProjectTsConfig } from '../../rule-factories';

/**
* generate the rules to adapt the library generated by ng cli
Expand Down Expand Up @@ -54,29 +54,6 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
])(tree, context);
};

/**
* Update the tsconfig generated by the Angular library generator
* @param tree File tree
* @param context Context of the schematics
*/
const updateLibTsconfig: Rule = (tree, context) => {
const tsconfigPath = path.posix.join(options.path || '', options.name, 'tsconfig.lib.json');
if (!tree.exists(tsconfigPath)) {
context.logger.warn(`The file ${tsconfigPath} was not found, the update will not be applied`);
return tree;
}

const tsconfig = ts.parseConfigFileTextToJson(tsconfigPath, tree.readText(tsconfigPath));
if (!tsconfig || !tsconfig.config) {
context.logger.error(`Error parsing ${tsconfigPath}, the update will not be applied`, tsconfig?.error as any);
return tree;
}

tsconfig.config.extends = path.posix.resolve(path.relative(options.path || '.', '.'), 'tsconfig.base.json');
tree.overwrite(tsconfigPath, JSON.stringify(tsconfig, null, 2));
return tree;
};

/**
* Update the root tsconfig files mappings
* @param tree File tree
Expand All @@ -90,7 +67,10 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
if (configFile?.compilerOptions?.paths) {
configFile.compilerOptions.paths = Object.fromEntries(
Object.entries(configFile.compilerOptions.paths).filter(([pathName, _]) => pathName !== options.name));
configFile.compilerOptions.paths[options.packageJsonName] = [path.posix.join(relativeTargetPath, 'src', 'public_api')];
configFile.compilerOptions.paths[options.packageJsonName] = [
path.posix.join(relativeTargetPath, 'dist'),
path.posix.join(relativeTargetPath, 'src', 'public-api')
];
tree.overwrite(tsconfigBase, JSON.stringify(configFile, null, 2));
} else {
context.logger.warn(`${tsconfigBase} does not contain path mapping, the edition will be skipped`);
Expand All @@ -106,7 +86,7 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
Object.entries(configFile.compilerOptions.paths).filter(([pathName, _]) => pathName !== options.name));
configFile.compilerOptions.paths[options.packageJsonName] = [
path.posix.join(relativeTargetPath, 'dist'),
path.posix.join(relativeTargetPath, 'src', 'public_api')
path.posix.join(relativeTargetPath, 'src', 'public-api')
];
tree.overwrite(tsconfigBuild, JSON.stringify(configFile, null, 2));
} else {
Expand All @@ -123,7 +103,7 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
prefix: options.prefix
})(t, c),
updateNgTemplate,
updateLibTsconfig,
updateProjectTsConfig(options.targetPath, 'tsconfig.lib.json', {updateInputFiles: true}),
updateTsConfigFiles
])(tree, context);
};
Expand Down
1 change: 1 addition & 0 deletions packages/@o3r/core/schematics/library/rules/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function updateNgPackagrFactory(targetPath: string): Rule {
return (tree) => {
const ngPackagr = tree.readJson(path.posix.join(targetPath, 'ng-package.json')) as any;
ngPackagr.$schema = 'https://raw.githubusercontent.com/ng-packagr/ng-packagr/master/src/ng-package.schema.json';
ngPackagr.dest = './dist';
tree.overwrite(path.posix.join(targetPath, 'ng-package.json'), JSON.stringify(ngPackagr, null, 2));
return tree;
};
Expand Down
42 changes: 42 additions & 0 deletions packages/@o3r/core/schematics/rule-factories/monorepo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { SchematicsException } from '@angular-devkit/schematics';
import type { PackageJson } from 'type-fest';
import { DEFAULT_ROOT_FOLDERS, isNxContext, setupSchematicsDefaultParams, WorkspaceLayout, WorkspaceSchematics } from '@o3r/schematics';
import * as path from 'node:path';
import * as ts from 'typescript';
import { findConfigFileRelativePath } from '@o3r/schematics';

/**
* Update root package.json to include workspaces
Expand Down Expand Up @@ -75,3 +78,42 @@ export function filterPackageJsonScripts(tree: Tree, _context: SchematicContext)
tree.overwrite(rootPackageJsonPath, JSON.stringify(rootPackageJsonObject, null, 2));
return tree;
}

/**
* Update the tsconfig generated by the Angular generators
* * extends the base tsconfig
* * force the outDir to ./dist
* * update input compiler files if needed
* @param targetPath
* @param tsconfigName
*/
export function updateProjectTsConfig(targetPath: string, tsconfigName: string, options = {updateInputFiles: false}): Rule {
return (tree, context) => {
const tsconfigPath = path.posix.join(targetPath, tsconfigName);
if (!tree.exists(tsconfigPath)) {
context.logger.warn(`The file ${tsconfigPath} was not found, the update will not be applied`);
return tree;
}

const tsconfig = ts.parseConfigFileTextToJson(tsconfigPath, tree.readText(tsconfigPath));
if (!tsconfig || !tsconfig.config) {
context.logger.error(`Error parsing ${tsconfigPath}, the update will not be applied`, tsconfig?.error as any);
return tree;
}

if (options?.updateInputFiles) {
tsconfig.config = Object.fromEntries(Object.entries(tsconfig.config).filter(([propName, _]) => propName !== 'files'));
tsconfig.config.include = ['./src/**/*.ts'];
}
const baseTsConfig = findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], targetPath);
if (baseTsConfig) {
tsconfig.config.extends = baseTsConfig;
}

tsconfig.config.compilerOptions ||= {};
tsconfig.config.compilerOptions.outDir = './dist';
tree.overwrite(tsconfigPath, JSON.stringify(tsconfig.config, null, 2));
return tree;
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export function updateTsConfig(targetPath: string, projectName: string, scope: s
configWithPath.content.compilerOptions ||= {};
configWithPath.content.compilerOptions.paths ||= {};
configWithPath.content.compilerOptions.paths[`${scope ? `@${scope}/` : ''}${projectName}`] = [
`${relativeTargetPath}/dist`,
`${relativeTargetPath}/src/index`
];
configWithPath.content.compilerOptions.paths[`${scope ? `@${scope}/` : ''}${projectName}/*`] = [
`${relativeTargetPath}/dist/*`,
`${relativeTargetPath}/src/*`
];

Expand Down
2 changes: 1 addition & 1 deletion packages/@o3r/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const isNgNewOptions = (arg: string) => {
};

const createNgProject = () => {
const { error } = spawnSync(process.execPath, [binPath, 'new', ...args.filter(isNgNewOptions)], {
const { error } = spawnSync(process.execPath, [binPath, 'new', ...args.filter(isNgNewOptions), '--skip-install'], {
stdio: 'inherit'
});

Expand Down

0 comments on commit a90c6c9

Please sign in to comment.