Skip to content

Instantly share code, notes, and snippets.

@juristr
Created January 17, 2023 09:55
Show Gist options
  • Save juristr/3f93cc7f1cc056ff6301d4c9b79febb5 to your computer and use it in GitHub Desktop.
Save juristr/3f93cc7f1cc056ff6301d4c9b79febb5 to your computer and use it in GitHub Desktop.
import {
formatFiles,
getProjects,
logger,
Tree,
updateProjectConfiguration
} from '@nrwl/devkit';
/**
* Angular Material relies on the projects to have either a build target
* with a tsconfig or alternatively a test target with a tsconfig.
* Details here: https://github.com/angular/components/blob/9107ac078b6aadd5c1a57dc31a9d57d5d1736cfd/src/cdk/schematics/ng-update/devkit-migration-rule.ts#L82-L89
*
* Nx libs usually don't have build configs (unless they're buildable or publishable) and the according
* test targets don't need a reference to a tsconfig file, which is why Nx doesn't add it to the angular.json targets.
* This however causes Angular Material migrations to not run properly. This generator
* fixes the angular.json, adding a tsconfig to the test target if it doesn't exist.
*/
export default async function (host: Tree, schema: any) {
const projects = getProjects(host);
projects.forEach((config, key) => {
if (config.targets['test']?.options) {
const tsConfig = config.targets['test'].options.tsConfig;
if (!tsConfig) {
config.targets[
'test'
].options.tsConfig = `${config.root}/tsconfig.spec.json`;
updateProjectConfiguration(host, key, config);
logger.info(`Migrated ${key}`);
}
}
});
await formatFiles(host);
}
@craigsh
Copy link

craigsh commented Jan 17, 2023

Need to check that config.targets is defined:

if (!config.targets) {
	return;
}

@juristr
Copy link
Author

juristr commented Jan 17, 2023

@craigsh oh yeah that's totally possible. this is a quick script I pulled out of my notes :). Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment