Skip to content

Commit 8cf9e86

Browse files
Trotttargos
andauthored
chore: move to ESM, update all dependencies (#113)
* chore: update nopt from 6.x to 7.0.0 nopt@7 drops support for Node.js 12.x so we'll drop support too. BREAKING CHANGE: drop support for Node.js 12.x * chore: switch to ESM in preparation for chalk update * chore: update chalk 4.x to 5.2.0 * Update .github/workflows/node.js.yml Co-authored-by: Michaël Zasso <[email protected]> * Update bin/cmd.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/tap.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update bin/cmd.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/format-pretty.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/utils.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update test/utils-test.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update lib/validator.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update test/cli-test.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update test/validator.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update test/cli-test.mjs Co-authored-by: Michaël Zasso <[email protected]> * Update test/cli-test.mjs Co-authored-by: Michaël Zasso <[email protected]> * fixup! Update test/cli-test.mjs * Update test/validator.mjs Co-authored-by: Michaël Zasso <[email protected]> * fixup! Update test/validator.mjs * fixup: use package.json type field rather than .mjs file extension --------- Co-authored-by: Michaël Zasso <[email protected]>
1 parent e172a82 commit 8cf9e86

33 files changed

+136
-174
lines changed

.github/workflows/node.js.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
strategy:
1818
matrix:
19-
node-version: [12.x, 14.x, 16.x, 17.x, 18.x, 19.x]
19+
node-version: [14.x, 16.x, 18.x, 19.x]
2020

2121
steps:
2222
- uses: actions/checkout@v3

bin/cmd.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#!/usr/bin/env node
22

3-
'use strict'
4-
5-
const exec = require('child_process').exec
6-
const fs = require('fs')
7-
const http = require('http')
8-
const https = require('https')
9-
const nopt = require('nopt')
10-
const path = require('path')
11-
const pretty = require('../lib/format-pretty')
12-
const formatTap = require('../lib/format-tap')
13-
const Validator = require('../lib')
14-
const Tap = require('../lib/tap')
15-
const utils = require('../lib/utils')
16-
const subsystem = require('../lib/rules/subsystem')
3+
import { exec } from 'node:child_process'
4+
import fs from 'node:fs'
5+
import http from 'node:http'
6+
import https from 'node:https'
7+
import path from 'node:path'
8+
import nopt from 'nopt'
9+
import pretty from '../lib/format-pretty.js'
10+
import formatTap from '../lib/format-tap.js'
11+
import Validator from '../lib/validator.js'
12+
import Tap from '../lib/tap.js'
13+
import * as utils from '../lib/utils.js'
14+
import subsystem from '../lib/rules/subsystem.js'
15+
1716
const knownOpts = {
1817
help: Boolean,
1918
version: Boolean,
@@ -34,14 +33,19 @@ const shortHand = {
3433
}
3534

3635
const parsed = nopt(knownOpts, shortHand)
37-
const usage = require('help')()
3836

3937
if (parsed.help) {
40-
usage()
38+
const usagePath = path.join(new URL(import.meta.url).pathname, '../usage.txt')
39+
const help = await fs.promises.readFile(usagePath, 'utf8')
40+
console.log(help)
41+
process.exit(0)
4142
}
4243

4344
if (parsed.version) {
44-
console.log('core-validate-commit', 'v' + require('../package').version)
45+
const pkgJsonPath = path.join(new URL(import.meta.url).pathname, '../../package.json')
46+
const pkgJson = await fs.promises.readFile(pkgJsonPath, 'utf8')
47+
const { version } = JSON.parse(pkgJson)
48+
console.log(`core-validate-commit v${version}`)
4549
process.exit(0)
4650
}
4751

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
'use strict'
1+
import Validator from './lib/validator.js'
22

3-
module.exports = require('./lib')
3+
export default Validator

lib/format-pretty.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict'
2-
3-
const chalk = require('chalk')
4-
const utils = require('./utils')
1+
import chalk from 'chalk'
2+
import * as utils from './utils.js'
53

64
const MAX_LINE_COL_LEN = 6
75

8-
module.exports = function formatPretty (context, msgs, validator, opts) {
6+
export default function formatPretty (context, msgs, validator, opts) {
97
opts = Object.assign({
108
detailed: false
119
}, opts)

lib/format-tap.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
module.exports = function formatTap (t, context, msgs, validator) {
1+
export default function formatTap (t, context, msgs, validator) {
42
for (const m of msgs) {
53
switch (m.level) {
64
case 'pass': {

lib/rule.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict'
2-
3-
module.exports = class Rule {
1+
export default class Rule {
42
constructor (opts) {
53
opts = Object.assign({
64
options: {},

lib/rules/co-authored-by-is-trailer.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'co-authored-by-is-trailer'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce that "Co-authored-by:" lines are trailers',

lib/rules/fixes-url.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
'use strict'
2-
31
const id = 'fixes-url'
42
const github = new RegExp('^https://github\\.com/[\\w-]+/[\\w-]+/' +
53
'(issues|pull)/\\d+(#issuecomment-\\d+|#discussion_r\\d+)?$'
64
)
75

8-
module.exports = {
6+
export default {
97
id,
108
meta: {
119
description: 'enforce format of Fixes URLs',

lib/rules/index.js

-12
This file was deleted.

lib/rules/line-after-title.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'line-after-title'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce a blank newline after the commit title',

lib/rules/line-length.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'line-length'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce max length of lines in commit body',

lib/rules/metadata-end.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'metadata-end'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce that metadata is at the end of commit messages',

lib/rules/pr-url.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
31
const id = 'pr-url'
42
const prUrl = /^https:\/\/github\.com\/[\w-]+\/[\w-]+\/pull\/\d+$/
53

6-
module.exports = {
4+
export default {
75
id,
86
meta: {
97
description: 'enforce PR-URL',

lib/rules/reviewers.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'reviewers'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce having reviewers',

lib/rules/subsystem.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict'
2-
31
const id = 'subsystem'
42

53
const validSubsystems = [
@@ -81,7 +79,7 @@ const validSubsystems = [
8179
'zlib'
8280
]
8381

84-
module.exports = {
82+
export default {
8583
id,
8684
meta: {
8785
description: 'enforce subsystem validity',

lib/rules/title-format.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'title-format'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce commit title format',

lib/rules/title-length.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict'
2-
31
const id = 'title-length'
42

5-
module.exports = {
3+
export default {
64
id,
75
meta: {
86
description: 'enforce max length of commit title',

lib/tap.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict'
2-
3-
const util = require('util')
1+
import util from 'node:util'
2+
import { Readable } from 'node:stream'
43

54
class Test {
65
constructor (tap, name) {
@@ -49,9 +48,7 @@ class Test {
4948
}
5049
}
5150

52-
const Readable = require('stream').Readable
53-
54-
module.exports = class Tap extends Readable {
51+
export default class Tap extends Readable {
5552
constructor () {
5653
super()
5754
this._wroteVersion = false

lib/utils.js

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
'use strict'
1+
import chalk from 'chalk'
22

3-
const chalk = require('chalk')
4-
const CHECK = chalk.green('✔')
5-
const X = chalk.red('✖')
6-
const WARN = chalk.yellow('⚠')
3+
export const CHECK = chalk.green('✔')
4+
export const X = chalk.red('✖')
5+
export const WARN = chalk.yellow('⚠')
76

8-
exports.CHECK = CHECK
9-
exports.X = X
10-
exports.WARN = WARN
11-
12-
exports.rightPad = function rightPad (str, max) {
7+
export function rightPad (str, max) {
138
const diff = max - str.length + 1
149
if (diff > 0) {
1510
return `${str}${' '.repeat(diff)}`
1611
}
1712
return str
1813
}
1914

20-
exports.leftPad = function leftPad (str, max) {
15+
export function leftPad (str, max) {
2116
const diff = max - str.length + 1
2217
if (diff > 0) {
2318
return `${' '.repeat(diff)}${str}`
2419
}
2520
return str
2621
}
2722

28-
exports.header = (sha, status) => {
23+
export function header (sha, status) {
2924
switch (status) {
3025
case 'skip':
3126
case 'pass': {
@@ -37,21 +32,21 @@ exports.header = (sha, status) => {
3732
}
3833
}
3934

40-
exports.describeRule = function describeRule (rule, max = 20) {
35+
export function describeRule (rule, max = 20) {
4136
if (rule.meta && rule.meta.description) {
4237
const desc = rule.meta.description
43-
const title = exports.leftPad(rule.id, max)
38+
const title = leftPad(rule.id, max)
4439
console.log(' %s %s', chalk.red(title), chalk.dim(desc))
4540
}
4641
}
4742

48-
exports.describeSubsystem = function describeSubsystem (subsystems, max = 20) {
43+
export function describeSubsystem (subsystems, max = 20) {
4944
if (subsystems) {
5045
for (let sub = 0; sub < subsystems.length; sub = sub + 3) {
5146
console.log('%s %s %s',
52-
chalk.green(exports.leftPad(subsystems[sub] || '', max)),
53-
chalk.green(exports.leftPad(subsystems[sub + 1] || '', max)),
54-
chalk.green(exports.leftPad(subsystems[sub + 2] || '', max))
47+
chalk.green(leftPad(subsystems[sub] || '', max)),
48+
chalk.green(leftPad(subsystems[sub + 1] || '', max)),
49+
chalk.green(leftPad(subsystems[sub + 2] || '', max))
5550
)
5651
}
5752
}

lib/index.js renamed to lib/validator.js

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1-
'use strict'
1+
import EE from 'node:events'
2+
import Parser from 'gitlint-parser-node'
3+
import BaseRule from './rule.js'
24

3-
const EE = require('events')
4-
const Parser = require('gitlint-parser-node')
5-
const BaseRule = require('./rule')
6-
const RULES = require('./rules')
5+
// Rules
6+
import coAuthoredByIsTrailer from './rules/co-authored-by-is-trailer.js'
7+
import fixesUrl from './rules/fixes-url.js'
8+
import lineAfterTitle from './rules/line-after-title.js'
9+
import lineLength from './rules/line-length.js'
10+
import metadataEnd from './rules/metadata-end.js'
11+
import prUrl from './rules/pr-url.js'
12+
import reviewers from './rules/reviewers.js'
13+
import subsystem from './rules/subsystem.js'
14+
import titleFormat from './rules/title-format.js'
15+
import titleLength from './rules/title-length.js'
716

8-
module.exports = class ValidateCommit extends EE {
17+
const RULES = {
18+
'co-authored-by-is-trailer': coAuthoredByIsTrailer,
19+
'fixes-url': fixesUrl,
20+
'line-after-title': lineAfterTitle,
21+
'line-length': lineLength,
22+
'metadata-end': metadataEnd,
23+
'pr-url': prUrl,
24+
reviewers,
25+
subsystem,
26+
'title-format': titleFormat,
27+
'title-length': titleLength
28+
}
29+
30+
export default class ValidateCommit extends EE {
931
constructor (options) {
1032
super()
1133

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
"version": "3.20.0",
44
"description": "Validate the commit message for a particular commit in node core",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"pretest": "standard && check-pkg",
89
"test": "tap -j4 --no-check-coverage --cov test/**/*.js test/*.js",
910
"posttest": "tap --no-check-coverage --coverage-report=text-summary",
1011
"test-ci": "npm run test -- --no-check-coverage --coverage-report=lcov"
1112
},
1213
"dependencies": {
13-
"chalk": "^4.1.2",
14+
"chalk": "^5.2.0",
1415
"gitlint-parser-node": "^1.1.0",
15-
"help": "^3.0.2",
16-
"nopt": "^6.0.0"
16+
"nopt": "^7.0.0"
1717
},
1818
"devDependencies": {
1919
"check-pkg": "^2.1.1",

0 commit comments

Comments
 (0)