Skip to content

Commit

Permalink
fix(misc): handle cwd correctly when generating artifacts with as-pro…
Browse files Browse the repository at this point in the history
…vided (#22411)
  • Loading branch information
leosvelperez committed Mar 21, 2024
1 parent a837f7f commit c20e00c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
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

0 comments on commit c20e00c

Please sign in to comment.