diff --git a/bin/cmd.js b/bin/cmd.js index 54f2419..865b5a4 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,8 +1,16 @@ #!/usr/bin/env node +/* eslint-disable no-var, no-eval */ -if (process.version.match(/v(\d+)\./)[1] < 4) { - console.error('standardx: Node v4 or greater is required. `standardx` did not run.') +var match = process.version.match(/v(\d+)\.(\d+)/) +var major = parseInt(match[1], 10) +var minor = parseInt(match[2], 10) + +if (major >= 12 || (major === 12 && minor >= 20)) { + eval('import("standard-engine")').then(function (standardEngine) { + eval('import("../options.js")').then(function (options) { + standardEngine.cli(options.default) + }) + }) } else { - const opts = require('../options') - require('standard-engine').cli(opts) + console.error('standardx: Node 12.20.0 or greater is required. `standardx` did not run.') } diff --git a/index.js b/index.js index db132d3..3481110 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ /*! standardx. MIT License. Feross Aboukhadijeh */ -const Linter = require('standard-engine').linter -const opts = require('./options') +import standardEngine from 'standard-engine' +import opts from './options.js' -module.exports = new Linter(opts) +const Linter = standardEngine.linter + +export default new Linter(opts) diff --git a/options.js b/options.js index 9e85992..863870d 100644 --- a/options.js +++ b/options.js @@ -1,17 +1,18 @@ -const pkg = require('./package.json') -const stdVersion = require('standard/package.json').version -const stdOpts = require('standard/options.js') +import { readFileSync } from 'node:fs' +import stdOpts from 'standard/options.js' -const opts = Object.assign({}, stdOpts, { +const stdVersion = stdOpts.version +const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8')) +const baseConfig = JSON.parse(readFileSync(stdOpts.eslintConfig.configFile, 'utf-8')) + +export default Object.assign({}, stdOpts, { bugs: pkg.bugs.url, cmd: 'standardx', eslintConfig: { - baseConfig: require('standard/eslintrc.json'), + baseConfig, useEslintrc: true }, homepage: pkg.homepage, tagline: 'Use JavaScript Standard Style (tweaked by standardx)', version: `${pkg.version} (standard ${stdVersion})` }) - -module.exports = opts diff --git a/package.json b/package.json index dde1d92..c64af42 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "bugs": { "url": "https://github.com/standard/standardx/issues" }, + "type": "module", "dependencies": { "standard": "^16.0.1", "standard-engine": "^14.0.1" @@ -18,7 +19,10 @@ "devDependencies": { "cross-spawn": "^7.0.3", "tap-spec": "^5.0.0", - "tape": "^5.0.1" + "tape": "^5.3.1" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "homepage": "https://github.com/standard/standardx", "keywords": [], diff --git a/test/index.js b/test/index.js index 7f62a7a..451c45c 100644 --- a/test/index.js +++ b/test/index.js @@ -1,8 +1,8 @@ -const path = require('path') -const test = require('tape') -const crossSpawn = require('cross-spawn') +import { fileURLToPath } from 'node:url' +import test from 'tape' +import crossSpawn from 'cross-spawn' -const CMD_PATH = path.join(__dirname, '..', 'bin', 'cmd.js') +const CMD_PATH = fileURLToPath(new URL('../bin/cmd.js', import.meta.url)) test('command line usage: --help', function (t) { t.plan(2) @@ -16,7 +16,7 @@ test('command line usage: --help', function (t) { test('test-repo-eslintrc allows snake_case', function (t) { t.plan(4) - const jsPath = path.join(__dirname, 'test-repo-eslintrc', 'index.js') + const jsPath = fileURLToPath(new URL('./test-repo-eslintrc/index.js', import.meta.url)) const result = crossSpawn.sync(CMD_PATH, [jsPath]) t.error(result.error) @@ -29,7 +29,7 @@ test('test-repo-eslintrc allows snake_case', function (t) { test('test-repo-package-json allows snake_case', function (t) { t.plan(4) - const jsPath = path.join(__dirname, 'test-repo-package-json', 'index.js') + const jsPath = fileURLToPath(new URL('./test-repo-package-json/index.js', import.meta.url)) const result = crossSpawn.sync(CMD_PATH, [jsPath]) t.error(result.error)