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

fix(misc): handle cwd correctly when generating artifacts with as-provided #22411

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
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ describe('determineArtifactNameAndDirectoryOptions', () => {
restoreCwd();
});

it('should not duplicate the cwd when the provided directory starts with the cwd and format is "as-provided"', async () => {
addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
projectType: 'application',
});
setCwd('apps/app1');

const result = await determineArtifactNameAndDirectoryOptions(tree, {
name: 'myComponent',
directory: 'apps/app1',
nameAndDirectoryFormat: 'as-provided',
artifactType: 'component',
callingGenerator: '@my-org/my-plugin:component',
});

expect(result).toStrictEqual({
artifactName: 'myComponent',
directory: 'apps/app1',
fileName: 'myComponent',
filePath: 'apps/app1/myComponent.ts',
project: 'app1',
nameAndDirectoryFormat: 'as-provided',
});

restoreCwd();
});

it('should return the options as provided when directory is provided', async () => {
addProjectConfiguration(tree, 'app1', {
root: 'apps/app1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,20 @@ function getAsProvidedOptions(
): NameAndDirectoryOptions {
const relativeCwd = getRelativeCwd();

const asProvidedDirectory = options.directory
? joinPathFragments(relativeCwd, options.directory)
: relativeCwd;
let asProvidedDirectory: string;
if (options.directory) {
// append the directory to the current working directory if it doesn't start with it
if (
options.directory === relativeCwd ||
options.directory.startsWith(`${relativeCwd}/`)
) {
asProvidedDirectory = options.directory;
} else {
asProvidedDirectory = joinPathFragments(relativeCwd, options.directory);
}
} else {
asProvidedDirectory = relativeCwd;
}
const asProvidedProject = findProjectFromPath(tree, asProvidedDirectory);

const asProvidedFileName =
Expand Down