From bc8118d97659e47d38d7e5a353853513beffe9c0 Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Sat, 21 May 2022 23:31:31 +0800 Subject: [PATCH] tools: refactor update-authors.js to ESM PR-URL: https://github.com/nodejs/node/pull/43098 Reviewed-By: Antoine du Hamel --- .github/workflows/authors.yml | 4 +-- AUTHORS | 2 +- .../{update-authors.js => update-authors.mjs} | 26 ++++++++----------- 3 files changed, 14 insertions(+), 18 deletions(-) rename tools/{update-authors.js => update-authors.mjs} (86%) diff --git a/.github/workflows/authors.yml b/.github/workflows/authors.yml index ffd169df9b6ef5..01504ed227b419 100644 --- a/.github/workflows/authors.yml +++ b/.github/workflows/authors.yml @@ -15,7 +15,7 @@ jobs: with: fetch-depth: '0' # This is required to actually get all the authors persist-credentials: false - - run: tools/update-authors.js # Run the AUTHORS tool + - run: tools/update-authors.mjs # Run the AUTHORS tool - uses: gr2m/create-or-update-pull-request-action@v1 # Create a PR or update the Action's existing PR env: GITHUB_TOKEN: ${{ secrets.GH_USER_TOKEN }} @@ -24,7 +24,7 @@ jobs: body: > Here are some new additions to the AUTHORS file. This is an automatically generated PR by the - `authors.yml` GitHub Action, which runs `tools/update-authors.js`. + `authors.yml` GitHub Action, which runs `tools/update-authors.mjs`. branch: actions/authors-update # Custom branch *just* for this Action. commit-message: 'meta: update AUTHORS' labels: meta diff --git a/AUTHORS b/AUTHORS index 91c110a0f13161..323f8ffb084ed5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3460,4 +3460,4 @@ William Marlow Keyhan Vakil <60900335+airtable-keyhanvakil@users.noreply.github.com> Feng Yu -# Generated by tools/update-authors.js +# Generated by tools/update-authors.mjs diff --git a/tools/update-authors.js b/tools/update-authors.mjs similarity index 86% rename from tools/update-authors.js rename to tools/update-authors.mjs index 5d8a5e7b3685a3..971fe160e3cf71 100755 --- a/tools/update-authors.js +++ b/tools/update-authors.mjs @@ -1,11 +1,9 @@ #!/usr/bin/env node -// Usage: tools/update-author.js [--dry] +// Usage: tools/update-author.mjs [--dry] // Passing --dry will redirect output to stdout rather than write to 'AUTHORS'. -'use strict'; -const { spawn } = require('child_process'); -const path = require('path'); -const fs = require('fs'); -const readline = require('readline'); +import { spawn } from 'node:child_process'; +import fs from 'node:fs'; +import readline from 'node:readline'; class CaseIndifferentMap { _map = new Map(); @@ -33,7 +31,7 @@ output.write('# Authors ordered by first contribution.\n\n'); const mailmap = new CaseIndifferentMap(); { - const lines = fs.readFileSync(path.resolve(__dirname, '../', '.mailmap'), + const lines = fs.readFileSync(new URL('../.mailmap', import.meta.url), { encoding: 'utf8' }).split('\n'); for (let line of lines) { line = line.trim(); @@ -55,7 +53,7 @@ const mailmap = new CaseIndifferentMap(); const previousAuthors = new CaseIndifferentMap(); { - const lines = fs.readFileSync(path.resolve(__dirname, '../', 'AUTHORS'), + const lines = fs.readFileSync(new URL('../AUTHORS', import.meta.url), { encoding: 'utf8' }).split('\n'); for (let line of lines) { line = line.trim(); @@ -85,9 +83,9 @@ const seen = new Set(); // by GitHub now. const authorRe = /(^Author:|^Co-authored-by:)\s+(?[^<]+)\s+(?<[^>]+>)/i; -rl.on('line', (line) => { +for await (const line of rl) { const match = line.match(authorRe); - if (!match) return; + if (!match) continue; let { author, email } = match.groups; const emailLower = email.toLowerCase(); @@ -99,7 +97,7 @@ rl.on('line', (line) => { } if (seen.has(email)) { - return; + continue; } seen.add(email); @@ -109,8 +107,6 @@ rl.on('line', (line) => { console.warn('Author name already in AUTHORS file. Possible duplicate:'); console.warn(` ${author} ${email}`); } -}); +} -rl.on('close', () => { - output.end('\n# Generated by tools/update-authors.js\n'); -}); +output.end('\n# Generated by tools/update-authors.mjs\n');