mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Replace custom promisify implementation with util.promisify.
This commit is contained in:
parent
6d9cbfe7be
commit
18daae9525
1 changed files with 12 additions and 21 deletions
|
|
@ -6,6 +6,7 @@ const fs = require("fs");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const { URL } = require("url");
|
const { URL } = require("url");
|
||||||
|
const { promisify } = require("util");
|
||||||
const md = require("markdown-it")();
|
const md = require("markdown-it")();
|
||||||
const pluginInline = require("markdown-it-for-inline");
|
const pluginInline = require("markdown-it-for-inline");
|
||||||
const pluginKatex = require("markdown-it-katex");
|
const pluginKatex = require("markdown-it-katex");
|
||||||
|
|
@ -26,18 +27,8 @@ const version = packageJson.version;
|
||||||
|
|
||||||
const deprecatedRuleNames = [ "MD002" ];
|
const deprecatedRuleNames = [ "MD002" ];
|
||||||
|
|
||||||
function promisify(func, ...args) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
func(...args, (error, result) => {
|
|
||||||
if (error) {
|
|
||||||
return reject(error);
|
|
||||||
}
|
|
||||||
return resolve(result);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function createTestForFile(file) {
|
function createTestForFile(file) {
|
||||||
|
const markdownlintPromise = promisify(markdownlint);
|
||||||
return function testForFile(test) {
|
return function testForFile(test) {
|
||||||
const detailedResults = /[/\\]detailed-results-/.test(file);
|
const detailedResults = /[/\\]detailed-results-/.test(file);
|
||||||
test.plan(detailedResults ? 3 : 2);
|
test.plan(detailedResults ? 3 : 2);
|
||||||
|
|
@ -45,10 +36,10 @@ function createTestForFile(file) {
|
||||||
const fixedFile = file.replace(/\.md$/, ".md.fixed");
|
const fixedFile = file.replace(/\.md$/, ".md.fixed");
|
||||||
const configFile = file.replace(/\.md$/, ".json");
|
const configFile = file.replace(/\.md$/, ".json");
|
||||||
let mergedConfig = null;
|
let mergedConfig = null;
|
||||||
const actualPromise = promisify(fs.stat, configFile)
|
const actualPromise = fs.promises.stat(configFile)
|
||||||
.then(
|
.then(
|
||||||
function configFileExists() {
|
function configFileExists() {
|
||||||
return promisify(fs.readFile, configFile, helpers.utf8Encoding)
|
return fs.promises.readFile(configFile, helpers.utf8Encoding)
|
||||||
.then(JSON.parse);
|
.then(JSON.parse);
|
||||||
},
|
},
|
||||||
function noConfigFile() {
|
function noConfigFile() {
|
||||||
|
|
@ -60,7 +51,7 @@ function createTestForFile(file) {
|
||||||
...defaultConfig,
|
...defaultConfig,
|
||||||
...config
|
...config
|
||||||
};
|
};
|
||||||
return promisify(markdownlint, {
|
return markdownlintPromise({
|
||||||
"files": [ file ],
|
"files": [ file ],
|
||||||
"config": mergedConfig,
|
"config": mergedConfig,
|
||||||
"resultVersion": detailedResults ? 2 : 3
|
"resultVersion": detailedResults ? 2 : 3
|
||||||
|
|
@ -70,13 +61,13 @@ function createTestForFile(file) {
|
||||||
function diffFixedFiles(resultVersion2or3) {
|
function diffFixedFiles(resultVersion2or3) {
|
||||||
return detailedResults ?
|
return detailedResults ?
|
||||||
Promise.all([
|
Promise.all([
|
||||||
promisify(markdownlint, {
|
markdownlintPromise({
|
||||||
"files": [ file ],
|
"files": [ file ],
|
||||||
"config": mergedConfig,
|
"config": mergedConfig,
|
||||||
"resultVersion": 3
|
"resultVersion": 3
|
||||||
}),
|
}),
|
||||||
promisify(fs.readFile, file, helpers.utf8Encoding),
|
fs.promises.readFile(file, helpers.utf8Encoding),
|
||||||
promisify(fs.readFile, fixedFile, helpers.utf8Encoding)
|
fs.promises.readFile(fixedFile, helpers.utf8Encoding)
|
||||||
])
|
])
|
||||||
.then(function validateApplyFixes(fulfillments) {
|
.then(function validateApplyFixes(fulfillments) {
|
||||||
const [ resultVersion3, content, expected ] = fulfillments;
|
const [ resultVersion3, content, expected ] = fulfillments;
|
||||||
|
|
@ -107,7 +98,7 @@ function createTestForFile(file) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const expectedPromise = detailedResults ?
|
const expectedPromise = detailedResults ?
|
||||||
promisify(fs.readFile, resultsFile, helpers.utf8Encoding)
|
fs.promises.readFile(resultsFile, helpers.utf8Encoding)
|
||||||
.then(
|
.then(
|
||||||
function fileContents(contents) {
|
function fileContents(contents) {
|
||||||
const errorObjects = JSON.parse(contents);
|
const errorObjects = JSON.parse(contents);
|
||||||
|
|
@ -119,7 +110,7 @@ function createTestForFile(file) {
|
||||||
});
|
});
|
||||||
return errorObjects;
|
return errorObjects;
|
||||||
}) :
|
}) :
|
||||||
promisify(fs.readFile, file, helpers.utf8Encoding)
|
fs.promises.readFile(file, helpers.utf8Encoding)
|
||||||
.then(
|
.then(
|
||||||
function fileContents(contents) {
|
function fileContents(contents) {
|
||||||
const lines = contents.split(helpers.newLineRe);
|
const lines = contents.split(helpers.newLineRe);
|
||||||
|
|
@ -153,11 +144,11 @@ function createTestForFile(file) {
|
||||||
if (detailedResults) {
|
if (detailedResults) {
|
||||||
return test.ok(true);
|
return test.ok(true);
|
||||||
}
|
}
|
||||||
return promisify(fs.readFile, file, helpers.utf8Encoding)
|
return fs.promises.readFile(file, helpers.utf8Encoding)
|
||||||
.then(
|
.then(
|
||||||
function applyFixes(content) {
|
function applyFixes(content) {
|
||||||
const corrections = helpers.applyFixes(content, errors);
|
const corrections = helpers.applyFixes(content, errors);
|
||||||
return promisify(markdownlint, {
|
return markdownlintPromise({
|
||||||
"strings": {
|
"strings": {
|
||||||
"input": corrections
|
"input": corrections
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue