diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-16 23:08:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-16 23:09:31 +0100 |
| commit | 36d98ebbd6463899737008eaa5147449c9d8e7f2 (patch) | |
| tree | 80adf97bc0394495432aae6d31f805dd8c2f2b3e | |
| parent | 0d13c71b3b8d4adb8d59e5e586f6e5f1a284107d (diff) | |
Prefer fs.promises to promisfying fs functions
| -rw-r--r-- | lib/docx/files.js | 7 | ||||
| -rw-r--r-- | lib/fs.js | 16 | ||||
| -rw-r--r-- | lib/main.js | 8 | ||||
| -rw-r--r-- | lib/unzip.js | 7 | ||||
| -rw-r--r-- | test/docx/files.tests.js | 9 | ||||
| -rw-r--r-- | test/mammoth.tests.js | 13 | ||||
| -rw-r--r-- | test/testing.js | 7 | ||||
| -rw-r--r-- | test/unzip.tests.js | 5 |
8 files changed, 39 insertions, 33 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js index 0d1adb9..255cad4 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -1,10 +1,10 @@ -var fs = require("fs"); var url = require("url"); var os = require("os"); var dirname = require("path").dirname; var resolvePath = require("path").resolve; var isAbsolutePath = require('path-is-absolute'); +var fs = require("../fs"); var promises = require("../promises"); @@ -26,7 +26,7 @@ function Files(options) { function read(uri, encoding) { return resolveUri(uri).then(function(path) { - return readFile(path, encoding).catch(function(error) { + return fs.readFile(path, encoding).catch(function(error) { var message = "could not open external image: '" + uri + "' (document directory: '" + base + "')\n" + error.message; return promises.reject(new Error(message)); }); @@ -50,9 +50,6 @@ function Files(options) { } -var readFile = promises.promisify(fs.readFile.bind(fs)); - - function uriToPath(uriString, platform) { if (!platform) { platform = os.platform(); diff --git a/lib/fs.js b/lib/fs.js new file mode 100644 index 0000000..5e2e583 --- /dev/null +++ b/lib/fs.js @@ -0,0 +1,16 @@ +var fs = require("fs"); + +var promises = require("./promises"); + +function readFile(path, options) { + return promises.when(fs.promises.readFile(path, options)); +} + +function writeFile(path, data, options) { + return promises.when(fs.promises.writeFile(path, data, options)); +} + +exports.createWriteStream = fs.createWriteStream.bind(fs); +exports.readFile = readFile; +exports.readFileSync = fs.readFileSync.bind(fs); +exports.writeFile = writeFile; diff --git a/lib/main.js b/lib/main.js index 54aeebb..9cc8196 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,11 +1,11 @@ /* global process */ -var fs = require("fs"); var path = require("path"); var mammoth = require("./"); -var promises = require("./promises"); +var fs = require("./fs"); var images = require("./images"); +var promises = require("./promises"); function main(argv) { var docxPath = argv["docx-path"]; @@ -31,7 +31,7 @@ function main(argv) { return element.read().then(function(imageBuffer) { var imagePath = path.join(outputDir, filename); - return promises.nfcall(fs.writeFile, imagePath, imageBuffer); + return fs.writeFile(imagePath, imageBuffer); }).then(function() { return {src: filename}; }); @@ -54,7 +54,7 @@ function main(argv) { function readStyleMap(styleMapPath) { if (styleMapPath) { - return promises.nfcall(fs.readFile, styleMapPath, "utf8"); + return fs.readFile(styleMapPath, "utf8"); } else { return promises.resolve(null); } diff --git a/lib/unzip.js b/lib/unzip.js index 75f486f..45bd8bc 100644 --- a/lib/unzip.js +++ b/lib/unzip.js @@ -1,15 +1,12 @@ -var fs = require("fs"); - +var fs = require("./fs"); var promises = require("./promises"); var zipfile = require("./zipfile"); exports.openZip = openZip; -var readFile = promises.promisify(fs.readFile); - function openZip(options) { if (options.path) { - return readFile(options.path).then(zipfile.openArrayBuffer); + return fs.readFile(options.path).then(zipfile.openArrayBuffer); } else if (options.buffer) { return promises.resolve(zipfile.openArrayBuffer(options.buffer)); } else if (options.file) { diff --git a/test/docx/files.tests.js b/test/docx/files.tests.js index 5ddda4a..91de108 100644 --- a/test/docx/files.tests.js +++ b/test/docx/files.tests.js @@ -1,16 +1,13 @@ var path = require("path"); -var fs = require("fs"); var assert = require("assert"); -var promises = require("../../lib/promises"); var Files = require("../../lib/docx/files").Files; var uriToPath = require("../../lib/docx/files").uriToPath; +var fs = require("../../lib/fs"); var testing = require("../testing"); var test = require("../test")(module); -var readFile = promises.promisify(fs.readFile.bind(fs)); - test("Files", { "when external file access is disabled then reading file raises error": function() { @@ -24,7 +21,7 @@ test("Files", { var filePath = path.resolve(testing.testPath("tiny-picture.png")); var files = new Files({externalFileAccess: true}); return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) { - return readFile(filePath, "base64").then(function(expectedContents) { + return fs.readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); }); }); @@ -37,7 +34,7 @@ test("Files", { relativeToFile: testing.testPath("./document.docx") }); return files.read("tiny-picture.png", "base64").then(function(contents) { - return readFile(filePath, "base64").then(function(expectedContents) { + return fs.readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); }); }); diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 038cbdf..0291c6a 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -1,10 +1,9 @@ var assert = require("assert"); var path = require("path"); -var fs = require("fs"); var _ = require("underscore"); var mammoth = require("../"); -var promises = require("../lib/promises"); +var fs = require("../lib/fs"); var results = require("../lib/results"); var testing = require("./testing"); @@ -23,7 +22,7 @@ test('should convert docx containing one paragraph to single p element', functio test('should convert docx represented by a Buffer', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); - return promises.nfcall(fs.readFile, docxPath) + return fs.readFile(docxPath) .then(function(buffer) { return mammoth.convertToHtml({buffer: buffer}); }) @@ -124,7 +123,7 @@ test('embedded style maps can be disabled', function() { test('embedded style map can be written using toBuffer() and then read', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); - return promises.nfcall(fs.readFile, docxPath) + return fs.readFile(docxPath) .then(function(buffer) { return mammoth.embedStyleMap({buffer: buffer}, "p => h1"); }) @@ -141,7 +140,7 @@ test('embedded style map can be written using toBuffer() and then read', functio test('embedded style map can be written using toArrayBuffer() and then read', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); - return promises.nfcall(fs.readFile, docxPath) + return fs.readFile(docxPath) .then(function(buffer) { return mammoth.embedStyleMap({buffer: buffer}, "p => h1"); }) @@ -158,7 +157,7 @@ test('embedded style map can be written using toArrayBuffer() and then read', fu test('embedded style map can be retrieved', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); - return promises.nfcall(fs.readFile, docxPath) + return fs.readFile(docxPath) .then(function(buffer) { return mammoth.embedStyleMap({buffer: buffer}, "p => h1"); }) @@ -495,7 +494,7 @@ test('extractRawText only retains raw text', function() { test('extractRawText can use .docx files represented by a Buffer', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); - return promises.nfcall(fs.readFile, docxPath) + return fs.readFile(docxPath) .then(function(buffer) { return mammoth.extractRawText({buffer: buffer}); }) diff --git a/test/testing.js b/test/testing.js index 3948556..ef21ba9 100644 --- a/test/testing.js +++ b/test/testing.js @@ -1,8 +1,9 @@ var path = require("path"); -var fs = require("fs"); -var promises = require("../lib/promises"); var _ = require("underscore"); +var fs = require("../lib/fs"); +var promises = require("../lib/promises"); + exports.testPath = testPath; exports.testData = testData; exports.createFakeDocxFile = createFakeDocxFile; @@ -15,7 +16,7 @@ function testPath(filename) { function testData(testDataPath) { var fullPath = testPath(testDataPath); - return promises.nfcall(fs.readFile, fullPath, "utf-8"); + return fs.readFile(fullPath, "utf-8"); } function createFakeDocxFile(files) { diff --git a/test/unzip.tests.js b/test/unzip.tests.js index 4e1bd61..f3a0fef 100644 --- a/test/unzip.tests.js +++ b/test/unzip.tests.js @@ -1,10 +1,9 @@ -var fs = require("fs"); var assert = require("assert"); var path = require("path"); var test = require("./test")(module); +var fs = require("../lib/fs"); var unzip = require("../lib/unzip"); -var promises = require("../lib/promises"); test("unzip fails if given empty object", function() { return unzip.openZip({}).then(function() { @@ -25,7 +24,7 @@ test("unzip can open local zip file", function() { test('unzip can open Buffer', function() { var zipPath = path.join(__dirname, "test-data/hello.zip"); - return promises.nfcall(fs.readFile, zipPath) + return fs.readFile(zipPath) .then(function(buffer) { return unzip.openZip({buffer: buffer}); }) |
