diff --git a/packages/techdocs-cli/src/commands/generate/generate.ts b/packages/techdocs-cli/src/commands/generate/generate.ts index 53735d628a..8445dd9ec6 100644 --- a/packages/techdocs-cli/src/commands/generate/generate.ts +++ b/packages/techdocs-cli/src/commands/generate/generate.ts @@ -21,6 +21,7 @@ import Docker from 'dockerode'; import { TechdocsGenerator, ParsedLocationAnnotation, + getMkDocsYml, } from '@backstage/plugin-techdocs-node'; import { ContainerRunner, @@ -54,6 +55,10 @@ export default async function generate(opts: OptionValues) { await fs.ensureDir(outputDir); + const { path: mkDocsYmlPath, configIsTemporary } = await getMkDocsYml( + sourceDir, + ); + const config = new ConfigReader({ techdocs: { generator: { @@ -109,5 +114,11 @@ export default async function generate(opts: OptionValues) { siteOptions: { name: opts.siteName }, }); + if (configIsTemporary) { + process.on('exit', async () => { + fs.rmSync(mkDocsYmlPath, {}); + }); + } + logger.info('Done!'); } diff --git a/packages/techdocs-cli/src/commands/serve/mkdocs.ts b/packages/techdocs-cli/src/commands/serve/mkdocs.ts index 6c84298c2b..1a471cf1a6 100644 --- a/packages/techdocs-cli/src/commands/serve/mkdocs.ts +++ b/packages/techdocs-cli/src/commands/serve/mkdocs.ts @@ -19,7 +19,8 @@ import openBrowser from 'react-dev-utils/openBrowser'; import { createLogger } from '../../lib/utility'; import { runMkdocsServer } from '../../lib/mkdocsServer'; import { LogFunc, waitForSignal } from '../../lib/run'; -import { getMkdocsYml } from '@backstage/plugin-techdocs-node'; +import { getMkDocsYml } from '@backstage/plugin-techdocs-node'; +import fs from 'fs-extra'; export default async function serveMkdocs(opts: OptionValues) { const logger = createLogger({ verbose: opts.verbose }); @@ -28,7 +29,10 @@ export default async function serveMkdocs(opts: OptionValues) { const localAddr = `http://127.0.0.1:${opts.port}`; const expectedDevAddr = opts.docker ? dockerAddr : localAddr; - await getMkdocsYml('./', opts.siteName); + const { path: mkDocsYmlPath, configIsTemporary } = await getMkDocsYml( + './', + opts.siteName, + ); // We want to open browser only once based on a log. let boolOpenBrowserTriggered = false; @@ -74,4 +78,10 @@ export default async function serveMkdocs(opts: OptionValues) { // Keep waiting for user to cancel the process await waitForSignal([childProcess]); + + if (configIsTemporary) { + process.on('exit', async () => { + fs.rmSync(mkDocsYmlPath, {}); + }); + } } diff --git a/packages/techdocs-cli/src/commands/serve/serve.ts b/packages/techdocs-cli/src/commands/serve/serve.ts index 1e835c2c8d..48e48d4f66 100644 --- a/packages/techdocs-cli/src/commands/serve/serve.ts +++ b/packages/techdocs-cli/src/commands/serve/serve.ts @@ -22,7 +22,8 @@ import HTTPServer from '../../lib/httpServer'; import { runMkdocsServer } from '../../lib/mkdocsServer'; import { LogFunc, waitForSignal } from '../../lib/run'; import { createLogger } from '../../lib/utility'; -import { getMkdocsYml } from '@backstage/plugin-techdocs-node'; +import { getMkDocsYml } from '@backstage/plugin-techdocs-node'; +import fs from 'fs-extra'; function findPreviewBundlePath(): string { try { @@ -65,7 +66,10 @@ export default async function serve(opts: OptionValues) { ? mkdocsDockerAddr : mkdocsLocalAddr; - await getMkdocsYml('./', opts.siteName); + const { path: mkDocsYmlPath, configIsTemporary } = await getMkDocsYml( + './', + opts.siteName, + ); let mkdocsServerHasStarted = false; const mkdocsLogFunc: LogFunc = data => { @@ -143,4 +147,10 @@ export default async function serve(opts: OptionValues) { }); await waitForSignal([mkdocsChildProcess]); + + if (configIsTemporary) { + process.on('exit', async () => { + fs.rmSync(mkDocsYmlPath, {}); + }); + } } diff --git a/plugins/techdocs-node/src/stages/generate/helpers.test.ts b/plugins/techdocs-node/src/stages/generate/helpers.test.ts index aad7706ab1..e635542a7c 100644 --- a/plugins/techdocs-node/src/stages/generate/helpers.test.ts +++ b/plugins/techdocs-node/src/stages/generate/helpers.test.ts @@ -25,7 +25,7 @@ import { ParsedLocationAnnotation } from '../../helpers'; import { createOrUpdateMetadata, getGeneratorKey, - getMkdocsYml, + getMkDocsYml, getRepoUrlFromLocationAnnotation, patchIndexPreBuild, storeEtagMetadata, @@ -517,7 +517,7 @@ describe('helpers', () => { }); }); - describe('getMkdocsYml', () => { + describe('getMkDocsYml', () => { const inputDir = resolvePath(__filename, '../__fixtures__/'); const siteOptions = { name: mockEntity.metadata.title, @@ -530,7 +530,7 @@ describe('helpers', () => { path: mkdocsPath, content, configIsTemporary, - } = await getMkdocsYml(inputDir, siteOptions); + } = await getMkDocsYml(inputDir, siteOptions); expect(mkdocsPath).toBe(key); expect(content).toBe(mkdocsYml.toString()); @@ -544,7 +544,7 @@ describe('helpers', () => { path: mkdocsPath, content, configIsTemporary, - } = await getMkdocsYml(inputDir, siteOptions); + } = await getMkDocsYml(inputDir, siteOptions); expect(mkdocsPath).toBe(key); expect(content).toBe(mkdocsYml.toString()); expect(configIsTemporary).toBe(false); @@ -562,7 +562,7 @@ describe('helpers', () => { path: mkdocsPath, content, configIsTemporary, - } = await getMkdocsYml(inputDir, defaultSiteOptions); + } = await getMkDocsYml(inputDir, defaultSiteOptions); expect(mkdocsPath).toBe(key); expect(content).toBe(mkdocsDefaultYml.toString()); @@ -571,7 +571,7 @@ describe('helpers', () => { it('throws when neither .yml nor .yaml nor default file is present', async () => { const invalidInputDir = resolvePath(__filename); - await expect(getMkdocsYml(invalidInputDir, siteOptions)).rejects.toThrow( + await expect(getMkDocsYml(invalidInputDir, siteOptions)).rejects.toThrow( /Could not read MkDocs YAML config file mkdocs.yml or mkdocs.yaml or default for validation/, ); }); diff --git a/plugins/techdocs-node/src/stages/generate/helpers.ts b/plugins/techdocs-node/src/stages/generate/helpers.ts index e16293f906..27c24ea66e 100644 --- a/plugins/techdocs-node/src/stages/generate/helpers.ts +++ b/plugins/techdocs-node/src/stages/generate/helpers.ts @@ -193,7 +193,7 @@ export const generateMkdocsYml = async ( * @param siteOptions - options for the site: `name` property will be used in mkdocs.yml for the * required `site_name` property, default value is "Documentation Site" */ -export const getMkdocsYml = async ( +export const getMkDocsYml = async ( inputDir: string, siteOptions?: { name?: string }, ): Promise<{ path: string; content: string; configIsTemporary: boolean }> => { diff --git a/plugins/techdocs-node/src/stages/generate/index.ts b/plugins/techdocs-node/src/stages/generate/index.ts index 3bd42643c1..fa36486a0f 100644 --- a/plugins/techdocs-node/src/stages/generate/index.ts +++ b/plugins/techdocs-node/src/stages/generate/index.ts @@ -15,7 +15,7 @@ */ export { TechdocsGenerator } from './techdocs'; export { Generators } from './generators'; -export { getMkdocsYml } from './helpers'; +export { getMkDocsYml } from './helpers'; export type { GeneratorBase, GeneratorOptions, diff --git a/plugins/techdocs-node/src/stages/generate/techdocs.ts b/plugins/techdocs-node/src/stages/generate/techdocs.ts index b71148060e..93285b7468 100644 --- a/plugins/techdocs-node/src/stages/generate/techdocs.ts +++ b/plugins/techdocs-node/src/stages/generate/techdocs.ts @@ -24,7 +24,7 @@ import { } from '@backstage/integration'; import { createOrUpdateMetadata, - getMkdocsYml, + getMkDocsYml, patchIndexPreBuild, runCommand, storeEtagMetadata, @@ -100,7 +100,7 @@ export class TechdocsGenerator implements GeneratorBase { } = options; // Do some updates to mkdocs.yml before generating docs e.g. adding repo_url - const { path: mkdocsYmlPath, content } = await getMkdocsYml( + const { path: mkdocsYmlPath, content } = await getMkDocsYml( inputDir, siteOptions, );