From dbca620ff9ea28456df27439c9ee77b0f402a63b Mon Sep 17 00:00:00 2001 From: Himanshu Mishra Date: Thu, 26 Nov 2020 14:56:23 +0100 Subject: [PATCH] Use fs-extra and async file read method --- .../techdocs/stages/generate/helpers.test.ts | 22 +++++++++---------- .../src/techdocs/stages/generate/helpers.ts | 6 ++--- plugins/techdocs/src/api.ts | 2 +- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.test.ts b/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.test.ts index 4938739fdf..54ef0b1e83 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.test.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.test.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import fs from 'fs'; +import fs from 'fs-extra'; import os from 'os'; import { resolve as resolvePath } from 'path'; import Stream, { PassThrough } from 'stream'; @@ -288,45 +288,43 @@ describe('helpers', () => { mockFs.restore(); }); - it('should add repo_url to mkdocs.yml', () => { + it('should add repo_url to mkdocs.yml', async () => { const parsedLocationAnnotation: ParsedLocationAnnotation = { type: 'github', target: 'https://github.com/backstage/backstage', }; - patchMkdocsYmlPreBuild( + await patchMkdocsYmlPreBuild( '/mkdocs.yml', mockLogger, parsedLocationAnnotation, ); - const updatedMkdocsYml = fs.readFileSync('/mkdocs.yml').toString(); + const updatedMkdocsYml = await fs.readFile('/mkdocs.yml'); - expect(updatedMkdocsYml).toContain( + expect(updatedMkdocsYml.toString()).toContain( "repo_url: 'https://github.com/backstage/backstage'", ); }); - it('should not override existing repo_url in mkdocs.yml', () => { + it('should not override existing repo_url in mkdocs.yml', async () => { const parsedLocationAnnotation: ParsedLocationAnnotation = { type: 'github', target: 'https://github.com/neworg/newrepo', }; - patchMkdocsYmlPreBuild( + await patchMkdocsYmlPreBuild( '/mkdocs_with_repo_url.yml', mockLogger, parsedLocationAnnotation, ); - const updatedMkdocsYml = fs - .readFileSync('/mkdocs_with_repo_url.yml') - .toString(); + const updatedMkdocsYml = await fs.readFile('/mkdocs_with_repo_url.yml'); - expect(updatedMkdocsYml).toContain( + expect(updatedMkdocsYml.toString()).toContain( "repo_url: 'https://github.com/backstage/backstage'", ); - expect(updatedMkdocsYml).not.toContain( + expect(updatedMkdocsYml.toString()).not.toContain( "repo_url: 'https://github.com/neworg/newrepo'", ); }); diff --git a/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.ts b/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.ts index 254186b484..0f60712f5e 100644 --- a/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.ts +++ b/plugins/techdocs-backend/src/techdocs/stages/generate/helpers.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fs from 'fs'; +import fs from 'fs-extra'; import { spawn } from 'child_process'; import { Writable, PassThrough } from 'stream'; import Docker from 'dockerode'; @@ -231,7 +231,7 @@ export const patchMkdocsYmlPreBuild = async ( ) => { let mkdocsYmlFileString; try { - mkdocsYmlFileString = fs.readFileSync(mkdocsYmlPath, 'utf8'); + mkdocsYmlFileString = await fs.readFile(mkdocsYmlPath, 'utf8'); } catch (error) { logger.warn( `Could not read file ${mkdocsYmlPath} before running the generator. ${error.message}`, @@ -267,7 +267,7 @@ export const patchMkdocsYmlPreBuild = async ( } try { - fs.writeFileSync(mkdocsYmlPath, yaml.safeDump(mkdocsYml), 'utf8'); + await fs.writeFile(mkdocsYmlPath, yaml.safeDump(mkdocsYml), 'utf8'); } catch (error) { logger.warn( `Could not write to ${mkdocsYmlPath} after updating it before running the generator. ${error.message}`, diff --git a/plugins/techdocs/src/api.ts b/plugins/techdocs/src/api.ts index 98e1e6c310..368705fa22 100644 --- a/plugins/techdocs/src/api.ts +++ b/plugins/techdocs/src/api.ts @@ -58,7 +58,7 @@ export class TechDocsApi implements TechDocs { * * When docs are built, we generate a techdocs_metadata.json and store it along with the generated * static files. It includes necessary data about the docs site. This method requests techdocs-backend - * which retries the TechDocs metadata. + * which retrieves the TechDocs metadata. * * @param {ParsedEntityId} entityId Object containing entity data like name, namespace, etc. */