Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Nov 19, 2024
1 parent b368049 commit 6df9c4e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 98 deletions.
2 changes: 1 addition & 1 deletion export-markdownlint-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-check

import * as helpers from "markdownlint/helpers";
import helpers from "markdownlint/helpers";
export default helpers;
2 changes: 1 addition & 1 deletion export-markdownlint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-check

import * as markdownlint from "markdownlint";
import markdownlint from "markdownlint";
export default markdownlint;
53 changes: 25 additions & 28 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env node

// @ts-check

// @ts-ignore
// eslint-disable-next-line camelcase, no-inline-comments, no-undef
const dynamicRequire = (typeof __non_webpack_require__ === "undefined") ? require : /* c8 ignore next */ __non_webpack_require__;
// Capture native require implementation for dynamic loading of modules

// Requires
const pathDefault = require("node:path");
import fsx from "node:fs";
import { createRequire } from 'node:module';
const dynamicRequire = createRequire(import.meta.url);
import os from "node:os";
import pathDefault from "node:path";
const pathPosix = pathDefault.posix;
const { pathToFileURL } = require("node:url");
const markdownlintLibrary = require("markdownlint");
import { pathToFileURL } from "node:url";
import esMain from "es-main";
import { globby } from "globby";
import micromatch from "micromatch";
import markdownlintLibrary from "markdownlint";
const {
applyFixes,
"getVersion": getLibraryVersion,
Expand All @@ -22,6 +24,7 @@ const {
"extendConfig": markdownlintExtendConfig,
"readConfig": markdownlintReadConfig
} = markdownlintPromises;
import { expandTildePath } from "markdownlint/helpers";
import appendToArray from "./append-to-array.js";
import mergeOptions from "./merge-options.js";
import resolveAndRequire from "./resolve-and-require.js";
Expand All @@ -39,13 +42,16 @@ const utf8 = "utf8";
const noop = () => null;

// Gets a JSONC parser
const getJsoncParse = () => require("./parsers/jsonc-parse.js");
import jsoncParse from "./parsers/jsonc-parse.js";
const getJsoncParse = () => jsoncParse;

// Gets a YAML parser
const getYamlParse = () => require("./parsers/yaml-parse.js");
import yamlParse from "./parsers/yaml-parse.js";
const getYamlParse = () => yamlParse;

// Gets an ordered array of parsers
const getParsers = () => require("./parsers/parsers.js");
import parsers from "./parsers/parsers.js";
const getParsers = () => parsers;

// Negates a glob
const negateGlob = (glob) => `!${glob}`;
Expand All @@ -61,15 +67,9 @@ const throwForConfigurationFile = (file, error) => {
// Return a posix path (even on Windows)
const posixPath = (p) => p.split(pathDefault.sep).join(pathPosix.sep);

// Expands a path with a tilde to an absolute path
const expandTildePath = (id) => {
const markdownlintRuleHelpers = require("markdownlint/helpers");
return markdownlintRuleHelpers.expandTildePath(id, require("node:os"));
};

// Resolves module paths relative to the specified directory
const resolveModulePaths = (dir, modulePaths) => (
modulePaths.map((path) => pathDefault.resolve(dir, expandTildePath(path)))
modulePaths.map((path) => pathDefault.resolve(dir, expandTildePath(path, os)))
);

// Read a JSON(C) or YAML file and return the object
Expand All @@ -93,7 +93,7 @@ const importOrRequireResolve = async (dirOrDirs, id, noRequire) => {
return null;
}
const dirs = Array.isArray(dirOrDirs) ? dirOrDirs : [ dirOrDirs ];
const expandId = expandTildePath(id);
const expandId = expandTildePath(id, os);
const errors = [];
try {
return resolveAndRequire(dynamicRequire, expandId, dirs);
Expand Down Expand Up @@ -212,7 +212,6 @@ const readOptionsOrConfig = async (configPath, fs, noRequire) => {

// Filter a list of files to ignore by glob
const removeIgnoredFiles = (dir, files, ignores) => {
const micromatch = require("micromatch");
return micromatch(
files.map((file) => pathPosix.relative(dir, file)),
ignores
Expand Down Expand Up @@ -538,8 +537,6 @@ const enumerateFiles = async (
})
);
// Process glob patterns
// eslint-disable-next-line no-inline-comments
const { globby } = await import(/* webpackMode: "eager" */ "globby");
const files = [
...await globby(expandedDirectories, globbyOptions),
...filteredLiteralFiles
Expand Down Expand Up @@ -889,7 +886,7 @@ const outputSummary = async (
const dirs = [ dir, ...modulePaths ];
const formattersAndParams = outputFormatters
? await importOrRequireIdsAndParams(dirs, outputFormatters, noRequire)
: [ [ require("markdownlint-cli2-formatter-default") ] ];
: [ [ (await import("markdownlint-cli2-formatter-default")).default ] ];
await Promise.all(formattersAndParams.map((formatterAndParams) => {
const [ formatter, ...formatterParams ] = formatterAndParams;
return formatter(formatterOptions, ...formatterParams);
Expand All @@ -916,7 +913,7 @@ const main = async (params) => {
} = params;
const logMessage = params.logMessage || noop;
const logError = params.logError || noop;
const fs = params.fs || require("node:fs");
const fs = params.fs || fsx;
const baseDirSystem =
(directory && pathDefault.resolve(directory)) ||
process.cwd();
Expand Down Expand Up @@ -993,7 +990,7 @@ const main = async (params) => {
// Add stdin as a non-file input if necessary
if (useStdin) {
const key = pathPosix.join(baseDir, "stdin");
const { text } = require("node:stream/consumers");
const { text } = await import("node:stream/consumers");
nonFileContents = {
...nonFileContents,
[key]: await text(process.stdin)
Expand Down Expand Up @@ -1080,13 +1077,13 @@ const main = async (params) => {
return errorsPresent ? 1 : 0;
};

// Export functions
module.exports = {
// Exports
export {
main
};

// Run if invoked as a CLI
if (require.main === module) {
if (esMain(import.meta)) {
const params = {
"argv": process.argv.slice(2),
"logMessage": console.log,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"schema/ValidatingConfiguration.md"
],
"dependencies": {
"es-main": "1.3.0",
"globby": "14.0.2",
"js-yaml": "4.1.0",
"jsonc-parser": "3.3.1",
Expand Down
2 changes: 1 addition & 1 deletion parsers/parsers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import jsoncParse from "./jsonc-parse.js";
import yamlParse from "./yaml-parse";
import yamlParse from "./yaml-parse.js";

/**
* Array of parser objects ordered by priority.
Expand Down
File renamed without changes.
27 changes: 13 additions & 14 deletions test/fs-mock-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// @ts-check

"use strict";

const path = require("node:path");
const { promisify } = require("node:util");
const test = require("ava").default;
const FsMock = require("./fs-mock");
import fsNodePromises from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import test from "ava";
import FsMock from "./fs-mock.js";

const mockPath = "/mock";
const thisFile = path.basename(__filename);
const thisFile = path.basename(import.meta.filename);
const testFile = path.join(mockPath, thisFile);

test("fsMock.stat", async (t) => {
t.plan(2);
const fs = new FsMock(__dirname);
const fs = new FsMock(import.meta.dirname);
const fsStat = promisify(fs.stat);
// @ts-ignore
const stat = await fsStat(testFile);
Expand All @@ -23,7 +22,7 @@ test("fsMock.stat", async (t) => {

test("fsMock.lstat", async (t) => {
t.plan(3);
const fs = new FsMock(__dirname);
const fs = new FsMock(import.meta.dirname);
const fsLstat = promisify(fs.lstat);
// @ts-ignore
const stat = await fsLstat(testFile);
Expand All @@ -34,7 +33,7 @@ test("fsMock.lstat", async (t) => {

test("fsMock.lstat symbolic links", async (t) => {
t.plan(3);
const fs = new FsMock(__dirname, true);
const fs = new FsMock(import.meta.dirname, true);
const fsLstat = promisify(fs.lstat);
// @ts-ignore
const stat = await fsLstat(testFile);
Expand All @@ -45,7 +44,7 @@ test("fsMock.lstat symbolic links", async (t) => {

test("fsMock.readdir", async (t) => {
t.plan(3);
const fs = new FsMock(__dirname);
const fs = new FsMock(import.meta.dirname);
const fsReaddir = promisify(fs.readdir);
// @ts-ignore
const files = await fsReaddir(mockPath);
Expand All @@ -56,7 +55,7 @@ test("fsMock.readdir", async (t) => {

test("fsMock.*", async (t) => {
t.plan(1);
const fs = new FsMock(__dirname);
const fs = new FsMock(import.meta.dirname);
const fsAccess = promisify(fs.access);
// @ts-ignore
await fsAccess(testFile);
Expand All @@ -74,13 +73,13 @@ test("fsMock.*", async (t) => {

test("fsMock.promises.*", async (t) => {
t.plan(2);
const fs = new FsMock(__dirname);
const fs = new FsMock(import.meta.dirname);
const tempName = "fs-mock.tmp";
const tempFile = path.join(mockPath, tempName);
await t.throwsAsync(() => fs.promises.access(tempFile));
await fs.promises.writeFile(tempFile, tempFile, "utf8");
await fs.promises.access(tempFile);
await fs.promises.stat(tempFile);
t.is(await fs.promises.readFile(tempFile, "utf8"), tempFile);
await require("node:fs").promises.unlink(path.join(__dirname, tempName));
await fsNodePromises.unlink(path.join(import.meta.dirname, tempName));
});
10 changes: 4 additions & 6 deletions test/fs-mock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// @ts-check

"use strict";

const fs = require("node:fs");
import fs from "node:fs";
import nodePath from "node:path";

const mapPath = (base, mockPath) => {
const path = require("node:path");
return path.resolve(base, path.relative("/mock", mockPath));
return nodePath.resolve(base, nodePath.relative("/mock", mockPath));
};

class fsMock {
Expand Down Expand Up @@ -56,4 +54,4 @@ class fsMock {
}
}

module.exports = fsMock;
export default fsMock;
12 changes: 5 additions & 7 deletions test/fs-virtual-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// @ts-check

"use strict";

const path = require("node:path");
const { promisify } = require("node:util");
const test = require("ava").default;
const FsVirtual = require("../webworker/fs-virtual");
import path from "node:path";
import { promisify } from "node:util";
import test from "ava";
import FsVirtual from "../webworker/fs-virtual.js";

const mockPath = "/mock";
const thisFile = path.basename(__filename);
const thisFile = path.basename(import.meta.filename);
const testFile = path.join(mockPath, thisFile);
const missingFile = `${mockPath}/missing`;

Expand Down
9 changes: 6 additions & 3 deletions test/markdownlint-cli2-test-exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

const exportMappings = new Map([
[ ".", ".." ],
[ ".", "../markdownlint-cli2.js" ],
[ "./markdownlint", "markdownlint" ],
[ "./markdownlint/helpers", "markdownlint/helpers" ],
[ "./parsers", "../parsers/parsers.js" ],
Expand All @@ -24,9 +24,12 @@ test("exportMappings", (t) => {

for (const [ exportName, exportPath ] of exportMappings) {
test(exportName, async (t) => {
const commonJs = !/.js$/.test(exportPath);
const importExportName = await import(exportName.replace(/^\./u, packageJson.name));
const importExportPath = await import(exportPath);
t.is(
await import(exportName.replace(/^\./u, packageJson.name)),
await import(exportPath)
commonJs ? importExportName.default : importExportName,
commonJs ? importExportPath.default : importExportPath
);
});
}
Loading

0 comments on commit 6df9c4e

Please sign in to comment.