Skip to content

Commit

Permalink
Replaced fs.promises.readFile() with fs.readFile(), since `fs.pro…
Browse files Browse the repository at this point in the history
…mises` does not exist in Node 8
  • Loading branch information
JamesMessinger committed Jun 20, 2019
1 parent d62a921 commit 4b7970a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from "fs";
import * as SVGO from "svgo";
import { GroupedImageNodes, ImageNodeGroup } from "./image-node";
import { readFile } from "./read-file";

/**
* A cache of the contents of of SVG files. This saves us from reading the same files
Expand Down Expand Up @@ -56,7 +56,7 @@ export class SvgCache extends Map<string, string> {
this._hits += (nodes.length - 1);

// Read the SVG file
let content = await fs.promises.readFile(path, "utf8");
let content = await readFile(path, "utf8");

// Optimize the contents, if enabled
if (optimizer) {
Expand Down
11 changes: 11 additions & 0 deletions src/read-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as fs from "fs";

/**
* Asynchronously reads the specified file and returns its contents as a string.
*/
export async function readFile(path: string, encoding: string): Promise<string> {
return new Promise((resolve, reject) => {
// tslint:disable-next-line: ban
fs.readFile(path, encoding, (err, contents) => err ? reject(err) : resolve(contents));
});
}
4 changes: 2 additions & 2 deletions test/utils/compare-contents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const fs = require("fs");
const { readFile } = require("../../lib/read-file");
const { expect } = require("chai");

module.exports = compareContents;
Expand All @@ -9,6 +9,6 @@ module.exports = compareContents;
* Compares the given file contents to the expected contents.
*/
async function compareContents (actualContents, fileName) {
let expectedContents = await fs.promises.readFile(`test/fixtures/modified/${fileName}`, "utf8");
let expectedContents = await readFile(`test/fixtures/modified/${fileName}`, "utf8");
expect(actualContents).to.equal(expectedContents);
}

0 comments on commit 4b7970a

Please sign in to comment.