Use fs-extra and async file read method

This commit is contained in:
Himanshu Mishra
2020-11-26 14:56:23 +01:00
parent 4b53294a6c
commit dbca620ff9
3 changed files with 14 additions and 16 deletions
@@ -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'",
);
});
@@ -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}`,
+1 -1
View File
@@ -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.
*/