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
  • Loading branch information
Josh-Cena committed May 8, 2022
1 parent f29bb73 commit 8627d9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
17 changes: 17 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,23 @@ describe('eject', () => {
`);
});

it(`eject ${Components.Sibling}`, async () => {
const result = await testEject('eject', Components.Sibling);
expect(result.createdFiles).toEqual([
// TODO do we really want to copy those Sibling components?
// It's hard to filter those reliably
// (index.* is not good, we need to include styles.css too)
'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 8627d9e

Please sign in to comment.