Skip to content

Commit

Permalink
fix(cli): output correct path when swizzling bare-file component in s…
Browse files Browse the repository at this point in the history
…ubfolder (#7369)

* fix(cli): output correct path when swizzling bare-file component in subfolder

* fix snapshot
  • Loading branch information
Josh-Cena authored May 8, 2022
1 parent f29bb73 commit 87c7639
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
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`] = `
".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
14 changes: 14 additions & 0 deletions packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts
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

0 comments on commit 87c7639

Please sign in to comment.