Skip to content

Commit

Permalink
chore: move to ESM, update all dependencies (#113)
Browse files Browse the repository at this point in the history
* 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 <targos@protonmail.com>

* Update bin/cmd.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/tap.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update bin/cmd.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/format-pretty.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/utils.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update test/utils-test.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update lib/validator.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update test/cli-test.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update test/validator.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update test/cli-test.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Update test/cli-test.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* fixup! Update test/cli-test.mjs

* Update test/validator.mjs

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* fixup! Update test/validator.mjs

* fixup: use package.json type field rather than .mjs file extension

---------

Co-authored-by: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
Trott and targos committed Mar 30, 2023
1 parent e172a82 commit 8cf9e86
Show file tree
Hide file tree
Showing 33 changed files with 136 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

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

steps:
- uses: actions/checkout@v3
Expand Down
38 changes: 21 additions & 17 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#!/usr/bin/env node

'use strict'

const exec = require('child_process').exec
const fs = require('fs')
const http = require('http')
const https = require('https')
const nopt = require('nopt')
const path = require('path')
const pretty = require('../lib/format-pretty')
const formatTap = require('../lib/format-tap')
const Validator = require('../lib')
const Tap = require('../lib/tap')
const utils = require('../lib/utils')
const subsystem = require('../lib/rules/subsystem')
import { exec } from 'node:child_process'
import fs from 'node:fs'
import http from 'node:http'
import https from 'node:https'
import path from 'node:path'
import nopt from 'nopt'
import pretty from '../lib/format-pretty.js'
import formatTap from '../lib/format-tap.js'
import Validator from '../lib/validator.js'
import Tap from '../lib/tap.js'
import * as utils from '../lib/utils.js'
import subsystem from '../lib/rules/subsystem.js'

const knownOpts = {
help: Boolean,
version: Boolean,
Expand All @@ -34,14 +33,19 @@ const shortHand = {
}

const parsed = nopt(knownOpts, shortHand)
const usage = require('help')()

if (parsed.help) {
usage()
const usagePath = path.join(new URL(import.meta.url).pathname, '../usage.txt')
const help = await fs.promises.readFile(usagePath, 'utf8')
console.log(help)
process.exit(0)
}

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

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict'
import Validator from './lib/validator.js'

module.exports = require('./lib')
export default Validator
8 changes: 3 additions & 5 deletions lib/format-pretty.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const chalk = require('chalk')
const utils = require('./utils')
import chalk from 'chalk'
import * as utils from './utils.js'

const MAX_LINE_COL_LEN = 6

module.exports = function formatPretty (context, msgs, validator, opts) {
export default function formatPretty (context, msgs, validator, opts) {
opts = Object.assign({
detailed: false
}, opts)
Expand Down
4 changes: 1 addition & 3 deletions lib/format-tap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

module.exports = function formatTap (t, context, msgs, validator) {
export default function formatTap (t, context, msgs, validator) {
for (const m of msgs) {
switch (m.level) {
case 'pass': {
Expand Down
4 changes: 1 addition & 3 deletions lib/rule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

module.exports = class Rule {
export default class Rule {
constructor (opts) {
opts = Object.assign({
options: {},
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/co-authored-by-is-trailer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'co-authored-by-is-trailer'

module.exports = {
export default {
id,
meta: {
description: 'enforce that "Co-authored-by:" lines are trailers',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/fixes-url.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const id = 'fixes-url'
const github = new RegExp('^https://github\\.com/[\\w-]+/[\\w-]+/' +
'(issues|pull)/\\d+(#issuecomment-\\d+|#discussion_r\\d+)?$'
)

module.exports = {
export default {
id,
meta: {
description: 'enforce format of Fixes URLs',
Expand Down
12 changes: 0 additions & 12 deletions lib/rules/index.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/rules/line-after-title.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'line-after-title'

module.exports = {
export default {
id,
meta: {
description: 'enforce a blank newline after the commit title',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/line-length.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'line-length'

module.exports = {
export default {
id,
meta: {
description: 'enforce max length of lines in commit body',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/metadata-end.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'metadata-end'

module.exports = {
export default {
id,
meta: {
description: 'enforce that metadata is at the end of commit messages',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/pr-url.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

const id = 'pr-url'
const prUrl = /^https:\/\/github\.com\/[\w-]+\/[\w-]+\/pull\/\d+$/

module.exports = {
export default {
id,
meta: {
description: 'enforce PR-URL',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/reviewers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'reviewers'

module.exports = {
export default {
id,
meta: {
description: 'enforce having reviewers',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/subsystem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const id = 'subsystem'

const validSubsystems = [
Expand Down Expand Up @@ -81,7 +79,7 @@ const validSubsystems = [
'zlib'
]

module.exports = {
export default {
id,
meta: {
description: 'enforce subsystem validity',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/title-format.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'title-format'

module.exports = {
export default {
id,
meta: {
description: 'enforce commit title format',
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/title-length.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const id = 'title-length'

module.exports = {
export default {
id,
meta: {
description: 'enforce max length of commit title',
Expand Down
9 changes: 3 additions & 6 deletions lib/tap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const util = require('util')
import util from 'node:util'
import { Readable } from 'node:stream'

class Test {
constructor (tap, name) {
Expand Down Expand Up @@ -49,9 +48,7 @@ class Test {
}
}

const Readable = require('stream').Readable

module.exports = class Tap extends Readable {
export default class Tap extends Readable {
constructor () {
super()
this._wroteVersion = false
Expand Down
31 changes: 13 additions & 18 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
'use strict'
import chalk from 'chalk'

const chalk = require('chalk')
const CHECK = chalk.green('✔')
const X = chalk.red('✖')
const WARN = chalk.yellow('⚠')
export const CHECK = chalk.green('✔')
export const X = chalk.red('✖')
export const WARN = chalk.yellow('⚠')

exports.CHECK = CHECK
exports.X = X
exports.WARN = WARN

exports.rightPad = function rightPad (str, max) {
export function rightPad (str, max) {
const diff = max - str.length + 1
if (diff > 0) {
return `${str}${' '.repeat(diff)}`
}
return str
}

exports.leftPad = function leftPad (str, max) {
export function leftPad (str, max) {
const diff = max - str.length + 1
if (diff > 0) {
return `${' '.repeat(diff)}${str}`
}
return str
}

exports.header = (sha, status) => {
export function header (sha, status) {
switch (status) {
case 'skip':
case 'pass': {
Expand All @@ -37,21 +32,21 @@ exports.header = (sha, status) => {
}
}

exports.describeRule = function describeRule (rule, max = 20) {
export function describeRule (rule, max = 20) {
if (rule.meta && rule.meta.description) {
const desc = rule.meta.description
const title = exports.leftPad(rule.id, max)
const title = leftPad(rule.id, max)
console.log(' %s %s', chalk.red(title), chalk.dim(desc))
}
}

exports.describeSubsystem = function describeSubsystem (subsystems, max = 20) {
export function describeSubsystem (subsystems, max = 20) {
if (subsystems) {
for (let sub = 0; sub < subsystems.length; sub = sub + 3) {
console.log('%s %s %s',
chalk.green(exports.leftPad(subsystems[sub] || '', max)),
chalk.green(exports.leftPad(subsystems[sub + 1] || '', max)),
chalk.green(exports.leftPad(subsystems[sub + 2] || '', max))
chalk.green(leftPad(subsystems[sub] || '', max)),
chalk.green(leftPad(subsystems[sub + 1] || '', max)),
chalk.green(leftPad(subsystems[sub + 2] || '', max))
)
}
}
Expand Down
34 changes: 28 additions & 6 deletions lib/index.js → lib/validator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
'use strict'
import EE from 'node:events'
import Parser from 'gitlint-parser-node'
import BaseRule from './rule.js'

const EE = require('events')
const Parser = require('gitlint-parser-node')
const BaseRule = require('./rule')
const RULES = require('./rules')
// Rules
import coAuthoredByIsTrailer from './rules/co-authored-by-is-trailer.js'
import fixesUrl from './rules/fixes-url.js'
import lineAfterTitle from './rules/line-after-title.js'
import lineLength from './rules/line-length.js'
import metadataEnd from './rules/metadata-end.js'
import prUrl from './rules/pr-url.js'
import reviewers from './rules/reviewers.js'
import subsystem from './rules/subsystem.js'
import titleFormat from './rules/title-format.js'
import titleLength from './rules/title-length.js'

module.exports = class ValidateCommit extends EE {
const RULES = {
'co-authored-by-is-trailer': coAuthoredByIsTrailer,
'fixes-url': fixesUrl,
'line-after-title': lineAfterTitle,
'line-length': lineLength,
'metadata-end': metadataEnd,
'pr-url': prUrl,
reviewers,
subsystem,
'title-format': titleFormat,
'title-length': titleLength
}

export default class ValidateCommit extends EE {
constructor (options) {
super()

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"version": "3.20.0",
"description": "Validate the commit message for a particular commit in node core",
"main": "index.js",
"type": "module",
"scripts": {
"pretest": "standard && check-pkg",
"test": "tap -j4 --no-check-coverage --cov test/**/*.js test/*.js",
"posttest": "tap --no-check-coverage --coverage-report=text-summary",
"test-ci": "npm run test -- --no-check-coverage --coverage-report=lcov"
},
"dependencies": {
"chalk": "^4.1.2",
"chalk": "^5.2.0",
"gitlint-parser-node": "^1.1.0",
"help": "^3.0.2",
"nopt": "^6.0.0"
"nopt": "^7.0.0"
},
"devDependencies": {
"check-pkg": "^2.1.1",
Expand Down

0 comments on commit 8cf9e86

Please sign in to comment.