Skip to content

Commit

Permalink
feat: usage of ESM imports instead of CommonJS (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting authored Sep 17, 2021
1 parent 9f616cc commit a11665f
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 37 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/old-test.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}

- name: Cache Node dependencies
uses: actions/[email protected]
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 14 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -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.')
}
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*! semistandard. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
// 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)
14 changes: 9 additions & 5 deletions options.js
Original file line number Diff line number Diff line change
@@ -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))
}
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -66,6 +65,7 @@
],
"license": "MIT",
"main": "index.js",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/standard/semistandard.git"
Expand Down
17 changes: 9 additions & 8 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
20 changes: 10 additions & 10 deletions test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit a11665f

Please sign in to comment.