Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular: Use Nx function to read non-angularCli configs #13558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@angular/forms": "^11.0.0",
"@angular/platform-browser": "^11.0.0",
"@angular/platform-browser-dynamic": "^11.0.0",
"@nrwl/workspace": "^11.1.4",
"@types/autoprefixer": "^9.4.0",
"@types/jest": "^25.1.1",
"jest": "^26.0.0",
Expand All @@ -90,10 +91,16 @@
"@angular/platform-browser": ">=6.0.0",
"@angular/platform-browser-dynamic": ">=6.0.0",
"@babel/core": "*",
"@nrwl/workspace": ">=11.1.0",
mandarini marked this conversation as resolved.
Show resolved Hide resolved
"rxjs": "^6.0.0",
"typescript": "^3.4.0 || >=4.0.0",
"zone.js": "^0.8.29 || ^0.9.0 || ^0.10.0 || ^0.11.0"
},
"peerDependenciesMeta": {
"@nrwl/workspace": {
"optional": true
}
},
"engines": {
"node": ">=8.0.0"
},
Expand Down
35 changes: 25 additions & 10 deletions app/angular/src/server/angular-cli_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,32 @@ function getTsConfigOptions(tsConfigPath: Path) {
}

export function getAngularCliConfig(dirToSearch: string) {
const possibleConfigNames = ['angular.json', 'workspace.json'];
const possibleConfigPaths = possibleConfigNames.map((name) => path.join(dirToSearch, name));

const validIndex = possibleConfigPaths.findIndex((configPath) => fs.existsSync(configPath));

if (validIndex === -1) {
logger.error(`Could not find angular.json using ${possibleConfigPaths[0]}`);
return undefined;
let angularCliConfig;
try {
/**
* Apologies for the following line
* If there's a better way to do it, let's do it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's better (or if it's possible)🙈 . but an alternative without try/catch would be to let storybook choose if it should use nx or not 🤷‍♂️

Something like this:
const isNxWorkspace = fs.existsSync(path.join(dirToSearch, 'nx.json'))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that it will need to access the readWorkspaceConfig() from @nrwl/workspace, so I apologized for using the eslint-disable to let me require it... :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@ThibaudAV ThibaudAV Jan 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay,
my point is more about the try/catch. I find the way we handle the nominal case (without nx) in a Catch is not pretty. :/
and I propose this solution if it works and suits you of course :)

const isNxWorkspace = fs.existsSync(path.join(dirToSearch, 'nx.json'))
if(isNxWorkspace) {
 return require('@nrwl/workspace').readWorkspaceConfig({
      format: 'angularCli',
    });
}
... 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ThibaudAV are you ok to go with the current state?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 🚀 , I have plans to improve this part. I will do it with a dedicated PR ;)

*/
/* eslint-disable global-require */
angularCliConfig = require('@nrwl/workspace').readWorkspaceConfig({
format: 'angularCli',
});
} catch (e) {
const possibleConfigNames = ['angular.json', 'workspace.json'];
const possibleConfigPaths = possibleConfigNames.map((name) => path.join(dirToSearch, name));

const validIndex = possibleConfigPaths.findIndex((configPath) => fs.existsSync(configPath));

if (validIndex === -1) {
logger.error(`Could not find angular.json using ${possibleConfigPaths[0]}`);
return undefined;
}

angularCliConfig = JSON.parse(
stripJsonComments(fs.readFileSync(possibleConfigPaths[validIndex], 'utf8'))
);
}

return JSON.parse(stripJsonComments(fs.readFileSync(possibleConfigPaths[validIndex], 'utf8')));
return angularCliConfig;
}

export function getLeadingAngularCliProject(ngCliConfig: any) {
Expand Down
Loading