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(cli): output correct path when swizzling bare-file component in subfolder #7369

Merged
merged 2 commits into from
May 8, 2022
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 @@ -162,14 +162,14 @@ exports[`swizzle eject ComponentInFolder/ComponentInSubFolder TS: theme dir tree
└── styles.module.css"
`;

exports[`swizzle eject ComponentInFolder/Sibling JS: Sibling.css 1`] = `
exports[`swizzle eject ComponentInFolder/Sibling JS: ComponentInFolder/Sibling.css 1`] = `
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Dang, we had incorrect snapshots previously...

".testClass {
background: black;
}
"
`;

exports[`swizzle eject ComponentInFolder/Sibling JS: Sibling.tsx 1`] = `
exports[`swizzle eject ComponentInFolder/Sibling JS: ComponentInFolder/Sibling.tsx 1`] = `
"import React from 'react';
export default function Sibling() {
Expand All @@ -180,18 +180,19 @@ export default function Sibling() {

exports[`swizzle eject ComponentInFolder/Sibling JS: theme dir tree 1`] = `
"theme
├── Sibling.css
└── Sibling.tsx"
└── ComponentInFolder
├── Sibling.css
└── Sibling.tsx"
`;

exports[`swizzle eject ComponentInFolder/Sibling TS: Sibling.css 1`] = `
exports[`swizzle eject ComponentInFolder/Sibling TS: ComponentInFolder/Sibling.css 1`] = `
".testClass {
background: black;
}
"
`;

exports[`swizzle eject ComponentInFolder/Sibling TS: Sibling.tsx 1`] = `
exports[`swizzle eject ComponentInFolder/Sibling TS: ComponentInFolder/Sibling.tsx 1`] = `
"import React from 'react';
export default function Sibling() {
Expand All @@ -202,8 +203,9 @@ export default function Sibling() {

exports[`swizzle eject ComponentInFolder/Sibling TS: theme dir tree 1`] = `
"theme
├── Sibling.css
└── Sibling.tsx"
└── ComponentInFolder
├── Sibling.css
└── Sibling.tsx"
`;

exports[`swizzle eject FirstLevelComponent JS: FirstLevelComponent.css 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ describe('eject', () => {
`);
});

it(`eject ${Components.Sibling}`, async () => {
const result = await testEject('eject', Components.Sibling);
expect(result.createdFiles).toEqual([
'ComponentInFolder/Sibling.css',
'ComponentInFolder/Sibling.tsx',
]);
expect(result.tree).toMatchInlineSnapshot(`
"theme
└── ComponentInFolder
├── Sibling.css
└── Sibling.tsx"
`);
});

it(`eject ${Components.ComponentInFolder}`, async () => {
const result = await testEject('eject', Components.ComponentInFolder);
expect(result.createdFiles).toEqual([
Expand Down
15 changes: 7 additions & 8 deletions packages/docusaurus/src/commands/swizzle/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,25 @@ export async function eject({
);
}

const toPath = isDirectory
? path.join(siteDir, THEME_PATH, componentName)
: path.join(siteDir, THEME_PATH);
const toPath = path.join(siteDir, THEME_PATH);

await fs.ensureDir(toPath);

const createdFiles = await Promise.all(
filesToCopy.map(async (sourceFile: string) => {
const fileName = path.basename(sourceFile);
const targetFile = path.join(toPath, fileName);
const targetFile = path.join(
toPath,
path.relative(themePath, sourceFile),
);
try {
const fileContents = await fs.readFile(sourceFile, 'utf-8');
await fs.outputFile(
targetFile,
fileContents.trimStart().replace(/^\/\*.+?\*\/\s*/ms, ''),
);
} catch (err) {
throw new Error(
logger.interpolate`Could not copy file from path=${sourceFile} to path=${targetFile}`,
);
logger.error`Could not copy file from path=${sourceFile} to path=${targetFile}`;
throw err;
}
return targetFile;
}),
Expand Down