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

feat: use default library prefix #1563

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 16 additions & 1 deletion src/lib/library/library.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
url,
} from '@angular-devkit/schematics';
import { parse } from 'jsonc-parser';
import { readFileSync } from 'fs';
import { normalizeToKebabOrSnakeCase } from '../../utils/formatting';
import {
DEFAULT_LANGUAGE,
Expand Down Expand Up @@ -43,6 +44,20 @@ export function main(options: LibraryOptions): Rule {
]);
}

function getDefaultLibraryPrefix(defaultLibraryPrefix = '@app') {
try {
const nestCliJson = JSON.parse(
readFileSync('./nest-cli.json', 'utf-8'),
Copy link
Member

Choose a reason for hiding this comment

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

at @netjs/cli repo we have this code to read the configuration file: https://github.com/nestjs/nest-cli/blob/d7c855ad3d8493bc5e43ebcb3199aab75232d7a4/lib/configuration/nest-configuration.loader.ts#L17
image

I'm not sure if we should do the same here...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know that. There are a few json files.
But latest version of nestjs generates 'nest-cli.json' file. doesn't it?
This feature that I suggested is new, so I think supporting only 'nest-cli.json' is ok.
But becuase @micalevisk 's suggestion is very good idea and It is not that hard to fix, I don't care.
which is better?

Copy link
Member

Choose a reason for hiding this comment

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

We should use the NestConfigurationLoader here

Copy link
Contributor Author

@bejewel-kyoungmin bejewel-kyoungmin Oct 24, 2023

Choose a reason for hiding this comment

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

@kamilmysliwiec
Thank you for your advice.
I have investigated whole code in schematics repo, but schematics project only care about these two files, 'nest-cli.json', 'nest.json'.
Only nest-cli project has NestConfigurationLoader. and it's FileSystemReader class read some json files.
Well, I just pushed new code that is checking both nest-cli.json and nest.json to get defaultLibraryPrefix.
If you want something else, give me any directions in more detail. I'm ready to willingly apply that.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done : )
I have just added FileSystemReader class to this package. test files, too.
But the methods in FileSystemReader return Promise.
If I use these methods that return promise, I have to add async keyword to a lot of function.
So I put new sync methods in FileSystemReader

);
if (nestCliJson.hasOwnProperty('defaultLibraryPrefix')) {
return nestCliJson['defaultLibraryPrefix'];
}
} catch (e) {
}

return defaultLibraryPrefix;
}

function transform(options: LibraryOptions): LibraryOptions {
const target: LibraryOptions = Object.assign({}, options);
const defaultSourceRoot =
Expand All @@ -58,7 +73,7 @@ function transform(options: LibraryOptions): LibraryOptions {
? join(normalize(defaultSourceRoot), target.path)
: normalize(defaultSourceRoot);

target.prefix = target.prefix || '@app';
target.prefix = target.prefix || getDefaultLibraryPrefix();
return target;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/library/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"prefix": {
"type": "string",
"description": "The prefix of the library.",
"x-prompt": "What prefix would you like to use for the library (default: @app)?"
"x-prompt": "What prefix would you like to use for the library (default: @app or 'defaultLibraryPrefix' setting value)?"
},
"language": {
"type": "string",
Expand Down