From a11665fc4d8c823888f37ec228cb5197601a1ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Fri, 17 Sep 2021 07:41:00 +0200 Subject: [PATCH] feat: usage of ESM imports instead of CommonJS (#271) --- .github/workflows/old-test.yml | 43 ++++++++++++++++++++++++++++++++++ .github/workflows/test.yml | 2 +- bin/cmd.js | 16 +++++++++++-- index.js | 7 +++--- options.js | 14 +++++++---- package.json | 16 ++++++------- test/api.js | 17 +++++++------- test/clone.js | 20 ++++++++-------- 8 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/old-test.yml diff --git a/.github/workflows/old-test.yml b/.github/workflows/old-test.yml new file mode 100644 index 0000000..1cc11d8 --- /dev/null +++ b/.github/workflows/old-test.yml @@ -0,0 +1,43 @@ +# Special test for the oldest version of Node.js that we "support" +# even though the linter won't actually run. Test that the command +# line program exits cleanly. + +name: Old test + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [0.10.48] + + steps: + - name: Checkout project + uses: actions/checkout@v2.3.4 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.4.0 + with: + node-version: ${{ matrix.node-version }} + + - name: Cache Node dependencies + uses: actions/cache@v2.1.6 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} + restore-keys: | + ${{ runner.os }}-node- + + - name: Install dependencies + run: npm install + + - name: Test that the command line program exits cleanly. + run: ./bin/cmd.js + shell: bash diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7f0d1c7..d5fcfd5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x, 14.x, 16.x] + node-version: [12.20.0, 14.13.1, 16.0.0] fail-fast: false steps: diff --git a/bin/cmd.js b/bin/cmd.js index 5652b5b..a32658e 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,4 +1,16 @@ #!/usr/bin/env node +/* eslint-disable no-var, no-eval */ -const opts = require('../options.js') -require('standard-engine').cli(opts) +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 { + console.error('semistandard: Node 12.20.0 or greater is required. `semistandard` did not run.') +} diff --git a/index.js b/index.js index 1bcc5d3..62ac78b 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,8 @@ /*! semistandard. MIT License. Feross Aboukhadijeh */ // programmatic usage -const Linter = require('standard-engine').linter +import engine from 'standard-engine' +import opts from './options.js' -const opts = require('./options.js') +const Linter = engine.linter -module.exports = new Linter(opts) +export default new Linter(opts) diff --git a/options.js b/options.js index bf9e1fe..9c2c443 100644 --- a/options.js +++ b/options.js @@ -1,15 +1,19 @@ -const path = require('path') -const pkg = require('./package.json') +import { fileURLToPath } from 'node:url' +import { readFileSync } from 'node:fs' +import eslint from 'eslint' -module.exports = { +const pkgUrl = new URL('./package.json', import.meta.url) +const pkg = JSON.parse(readFileSync(pkgUrl, 'utf-8')) + +export default { // cmd, homepage, bugs all pulled from package.json cmd: 'semistandard', version: pkg.version, homepage: pkg.homepage, bugs: pkg.bugs.url, tagline: 'Semicolons For All!', - eslint: require('eslint'), + eslint, eslintConfig: { - configFile: path.join(__dirname, 'eslintrc.json') + configFile: fileURLToPath(new URL('eslintrc.json', import.meta.url)) } } diff --git a/package.json b/package.json index 7748fc4..764059d 100644 --- a/package.json +++ b/package.json @@ -14,27 +14,26 @@ "url": "https://github.com/standard/semistandard/issues" }, "dependencies": { - "eslint": "^7.27.0", + "eslint": "^7.32.0", "eslint-config-semistandard": "16.0.0", "eslint-config-standard": "16.0.3", "eslint-config-standard-jsx": "10.0.0", - "eslint-plugin-import": "^2.22.1", + "eslint-plugin-import": "^2.24.2", "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^5.1.0", - "eslint-plugin-react": "~7.21.5", - "standard-engine": "^14.0.0" + "eslint-plugin-react": "~7.25.1", + "standard-engine": "^14.0.1" }, "devDependencies": { - "merge": "^1.2.1", + "merge": "^2.1.1", "mkdirp": "^1.0.4", "rimraf": "^3.0.2", "run-series": "^1.1.9", "standard": "*", - "tape": "^5.0.1", - "xtend": "^4.0.2" + "tape": "^5.3.1" }, "engines": { - "node": ">=10.12.0" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "homepage": "https://github.com/standard/semistandard", "keywords": [ @@ -66,6 +65,7 @@ ], "license": "MIT", "main": "index.js", + "type": "module", "repository": { "type": "git", "url": "https://github.com/standard/semistandard.git" diff --git a/test/api.js b/test/api.js index 4f4590c..519369e 100644 --- a/test/api.js +++ b/test/api.js @@ -1,17 +1,18 @@ -const path = require('path') -const semistandard = require('../') -const test = require('tape') -const filePath = path.resolve('./bin/cmd.js') +import { resolve } from 'node:path' +import test from 'tape' +import semistandard from '../index.js' + +const filePath = resolve('./bin/cmd.js') test('api usage', function (t) { t.plan(6) semistandard.lintFiles(['bin/cmd.js'], {}, function (err, result) { t.error(err, 'no error while linting') t.equal(typeof result, 'object', 'result is an object') - t.equal(result.errorCount, 2, 'error count 2') + t.equal(result.errorCount, 7, 'error count 7') - t.equal(path.resolve(result.results[0].filePath), filePath, 'error filepath correct') - t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'first mising semicolon message') - t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'second mising semicolon message') + t.equal(resolve(result.results[0].filePath), filePath, 'error filepath correct') + t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'first missing semicolon message') + t.equal(result.results[0].messages[0].message, 'Missing semicolon.', 'second missing semicolon message') }) }) diff --git a/test/clone.js b/test/clone.js index 99fad3b..dd091b8 100644 --- a/test/clone.js +++ b/test/clone.js @@ -8,16 +8,16 @@ * VERSION BUMP.) */ -const cp = require('child_process') -const extend = require('xtend') -const mkdirp = require('mkdirp') -const path = require('path') -const rimraf = require('rimraf') -const series = require('run-series') -const test = require('tape') +import cp from 'node:child_process' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import mkdirp from 'mkdirp' +import rimraf from 'rimraf' +import series from 'run-series' +import test from 'tape' -const TMP = path.join(__dirname, '..', 'tmp') -const SEMISTANDARD = path.join(__dirname, '..', 'bin', 'cmd.js') +const TMP = fileURLToPath(new URL('../tmp', import.meta.url)) +const SEMISTANDARD = fileURLToPath(new URL('../bin/cmd.js', import.meta.url)) // const URLS = require('./semistandard-repos.json') const URLS = [ @@ -65,7 +65,7 @@ test('lint repos', function (t) { }) function spawn (command, args, opts, cb) { - const child = cp.spawn(command, args, extend({ stdio: 'inherit' }, opts)) + const child = cp.spawn(command, args, { stdio: 'inherit', ...opts }) child.on('error', cb) child.on('close', function (code) { if (code !== 0) cb(new Error('non-zero exit code: ' + code))