From 67ba7e088a744e1d30502d516e521f6001ef0311 Mon Sep 17 00:00:00 2001 From: Dominik Henneke Date: Tue, 10 Aug 2021 15:09:22 +0200 Subject: [PATCH] Only write the updated `mkdocs.yml` file if the content was updated Signed-off-by: Dominik Henneke --- .changeset/techdocs-three-icons-collect.md | 7 +++++ .../__fixtures__/mkdocs_with_comments.yml | 3 +++ .../src/stages/generate/helpers.test.ts | 26 +++++++++++++++++++ .../src/stages/generate/helpers.ts | 17 ++++++++---- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 .changeset/techdocs-three-icons-collect.md create mode 100644 packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_with_comments.yml diff --git a/.changeset/techdocs-three-icons-collect.md b/.changeset/techdocs-three-icons-collect.md new file mode 100644 index 0000000000..a3c005f82d --- /dev/null +++ b/.changeset/techdocs-three-icons-collect.md @@ -0,0 +1,7 @@ +--- +'@backstage/techdocs-common': patch +--- + +Only write the updated `mkdocs.yml` file if the content was updated. + +This keeps local files unchanged if the `dir` annotation is used in combination with the `file` location. diff --git a/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_with_comments.yml b/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_with_comments.yml new file mode 100644 index 0000000000..3924224560 --- /dev/null +++ b/packages/techdocs-common/src/stages/generate/__fixtures__/mkdocs_with_comments.yml @@ -0,0 +1,3 @@ +site_name: Test site name +site_description: Test site description +# This is a comment that is removed after editing diff --git a/packages/techdocs-common/src/stages/generate/helpers.test.ts b/packages/techdocs-common/src/stages/generate/helpers.test.ts index 50bcc45343..fcf506098e 100644 --- a/packages/techdocs-common/src/stages/generate/helpers.test.ts +++ b/packages/techdocs-common/src/stages/generate/helpers.test.ts @@ -61,6 +61,9 @@ const mkdocsYmlWithInvalidDocDir = fs.readFileSync( const mkdocsYmlWithInvalidDocDir2 = fs.readFileSync( resolvePath(__filename, '../__fixtures__/mkdocs_invalid_doc_dir2.yml'), ); +const mkdocsYmlWithComments = fs.readFileSync( + resolvePath(__filename, '../__fixtures__/mkdocs_with_comments.yml'), +); const mockLogger = getVoidLogger(); const rootDir = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir'; @@ -163,6 +166,7 @@ describe('helpers', () => { '/mkdocs_with_repo_url.yml': mkdocsYmlWithRepoUrl, '/mkdocs_with_edit_uri.yml': mkdocsYmlWithEditUri, '/mkdocs_with_extensions.yml': mkdocsYmlWithExtensions, + '/mkdocs_with_comments.yml': mkdocsYmlWithComments, }); }); @@ -258,6 +262,28 @@ describe('helpers', () => { 'https://github.com/neworg/newrepo', ); }); + + it('should not update mkdocs.yml if nothing should be changed', async () => { + const parsedLocationAnnotation: ParsedLocationAnnotation = { + type: 'dir', + target: '/unsupported/path', + }; + + await patchMkdocsYmlPreBuild( + '/mkdocs_with_comments.yml', + mockLogger, + parsedLocationAnnotation, + scmIntegrations, + ); + + const updatedMkdocsYml = await fs.readFile('/mkdocs_with_comments.yml'); + + expect(updatedMkdocsYml.toString()).toContain( + '# This is a comment that is removed after editing', + ); + expect(updatedMkdocsYml.toString()).not.toContain('edit_uri'); + expect(updatedMkdocsYml.toString()).not.toContain('repo_url'); + }); }); describe('addBuildTimestampMetadata', () => { diff --git a/packages/techdocs-common/src/stages/generate/helpers.ts b/packages/techdocs-common/src/stages/generate/helpers.ts index aa4402323c..0ac05e7dd2 100644 --- a/packages/techdocs-common/src/stages/generate/helpers.ts +++ b/packages/techdocs-common/src/stages/generate/helpers.ts @@ -217,6 +217,10 @@ export const patchMkdocsYmlPreBuild = async ( parsedLocationAnnotation: ParsedLocationAnnotation, scmIntegrations: ScmIntegrationRegistry, ) => { + // We only want to override the mkdocs.yml if it has actually changed. This is relevant if + // used with a 'dir' location on the file system as this would permanently update the file. + let didEdit = false; + let mkdocsYmlFileString; try { mkdocsYmlFileString = await fs.readFile(mkdocsYmlPath, 'utf8'); @@ -256,6 +260,7 @@ export const patchMkdocsYmlPreBuild = async ( if (result.repo_url || result.edit_uri) { mkdocsYml.repo_url = result.repo_url; mkdocsYml.edit_uri = result.edit_uri; + didEdit = true; logger.info( `Set ${JSON.stringify( @@ -266,11 +271,13 @@ export const patchMkdocsYmlPreBuild = async ( } try { - await fs.writeFile( - mkdocsYmlPath, - yaml.dump(mkdocsYml, { schema: MKDOCS_SCHEMA }), - 'utf8', - ); + if (didEdit) { + await fs.writeFile( + mkdocsYmlPath, + yaml.dump(mkdocsYml, { schema: MKDOCS_SCHEMA }), + 'utf8', + ); + } } catch (error) { logger.warn( `Could not write to ${mkdocsYmlPath} after updating it before running the generator. ${error.message}`,