From 8627d9ec9d6cd6769e070758a4f162ff27628f66 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 8 May 2022 12:55:04 +0800 Subject: [PATCH] fix(cli): output correct path when swizzling bare-file component in subfolder --- .../commands/swizzle/__tests__/actions.test.ts | 17 +++++++++++++++++ .../docusaurus/src/commands/swizzle/actions.ts | 15 +++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts b/packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts index 159318f623957..cda9d52b844a5 100644 --- a/packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts +++ b/packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts @@ -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([ diff --git a/packages/docusaurus/src/commands/swizzle/actions.ts b/packages/docusaurus/src/commands/swizzle/actions.ts index 1379e8238b549..d6be9f9027310 100644 --- a/packages/docusaurus/src/commands/swizzle/actions.ts +++ b/packages/docusaurus/src/commands/swizzle/actions.ts @@ -70,16 +70,16 @@ 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( @@ -87,9 +87,8 @@ export async function eject({ 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; }),