Skip to content

Commit

Permalink
Convert from CommonJS module to ECMAScript module.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Dec 10, 2024
1 parent 941ef55 commit 4e9bb43
Show file tree
Hide file tree
Showing 12 changed files with 1,430 additions and 663 deletions.
8 changes: 7 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ charset = utf-8
trim_trailing_whitespace = true

[*.js]
insert_final_newline = true
insert_final_newline = true

[*.cjs]
insert_final_newline = true

[*.mjs]
insert_final_newline = true
57 changes: 26 additions & 31 deletions markdownlint.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
#!/usr/bin/env node

'use strict';

const fs = require('node:fs');
const path = require('node:path');
const Module = require('node:module');
const os = require('node:os');
const process = require('node:process');
const {program} = require('commander');
const glob = require('glob');
const markdownlint = require('markdownlint');
const rc = require('run-con');
const minimatch = require('minimatch');
const jsonpointer = require('jsonpointer');
const pkg = require('./package.json');

import fs from 'node:fs';
import path from 'node:path';
import Module from 'node:module';
import os from 'node:os';
import process from 'node:process';
import {program} from 'commander';
import {glob} from 'glob';
import {applyFixes} from 'markdownlint';
import {lint, readConfig} from 'markdownlint/sync';
import rc from 'run-con';
import {minimatch} from 'minimatch';
import jsonpointer from 'jsonpointer';

const require = Module.createRequire(import.meta.url);
const options = program.opts();
// The following two values are copied from package.json (and validated by tests)
const version = '0.43.0';
const description = 'MarkdownLint Command Line Interface';

function posixPath(p) {
return p.split(path.sep).join(path.posix.sep);
Expand Down Expand Up @@ -60,7 +62,7 @@ function readConfiguration(userConfigFile) {
for (const projectConfigFile of projectConfigFiles) {
try {
fs.accessSync(projectConfigFile, fs.R_OK);
const projectConfig = markdownlint.readConfigSync(projectConfigFile, configParsers);
const projectConfig = readConfig(projectConfigFile, configParsers);
config = {...config, ...projectConfig};
break;
} catch {
Expand All @@ -73,7 +75,7 @@ function readConfiguration(userConfigFile) {
if (userConfigFile) {
try {
const jsConfigFile = /\.c?js$/i.test(userConfigFile);
const userConfig = jsConfigFile ? require(path.resolve(processCwd, userConfigFile)) : markdownlint.readConfigSync(userConfigFile, configParsers);
const userConfig = jsConfigFile ? require(path.resolve(processCwd, userConfigFile)) : readConfig(userConfigFile, configParsers);
config = require('deep-extend')(config, userConfig);
} catch (error) {
console.error(`Cannot read or parse config file '${userConfigFile}': ${error.message}`);
Expand Down Expand Up @@ -195,8 +197,8 @@ function concatArray(item, array) {
}

program
.version(pkg.version)
.description(pkg.description)
.version(version)
.description(description)
.usage('[options] <files|directories|globs>')
.option('-c, --config <configFile>', 'configuration file (JSON, JSONC, JS, YAML, or TOML)')
.option('--configPointer <pointer>', 'JSON Pointer to object within configuration file', '')
Expand Down Expand Up @@ -241,7 +243,7 @@ function loadCustomRules(rules) {
return rules.flatMap(rule => {
try {
const resolvedPath = [tryResolvePath(rule)];
const fileList = prepareFileList(resolvedPath, ['js']).flatMap(filepath => require(filepath.absolute));
const fileList = prepareFileList(resolvedPath, ['js', 'cjs', 'mjs']).flatMap(filepath => require(filepath.absolute));
if (fileList.length === 0) {
throw new Error('No such rule');
}
Expand Down Expand Up @@ -300,30 +302,23 @@ function lintAndPrint(stdin, files) {
};
}

if (options.json) {
lintOptions.resultVersion = 3;
}

if (options.fix) {
const fixOptions = {
...lintOptions,
resultVersion: 3
};
const fixOptions = {...lintOptions};
for (const file of files) {
fixOptions.files = [file];
const fixResult = markdownlint.sync(fixOptions);
const fixResult = lint(fixOptions);
const fixes = fixResult[file].filter(error => error.fixInfo);
if (fixes.length > 0) {
const originalText = fs.readFileSync(file, fsOptions);
const fixedText = markdownlint.applyFixes(originalText, fixes);
const fixedText = applyFixes(originalText, fixes);
if (originalText !== fixedText) {
fs.writeFileSync(file, fixedText, fsOptions);
}
}
}
}

const lintResult = markdownlint.sync(lintOptions);
const lintResult = lint(lintOptions);
printResult(lintResult);
}

Expand Down
Loading

0 comments on commit 4e9bb43

Please sign in to comment.