From dfed96fddb11b6b477ee1f066a5c5cc1432a7288 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 8 Dec 2025 21:41:49 +0800 Subject: [PATCH] Update dependency n-readlines to v3 (#18447) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: fisker --- package.json | 2 +- scripts/build/packages/prettier.js | 12 +++++-- src/utilities/get-interpreter.js | 56 ++++++++++++++++++------------ yarn.lock | 10 +++--- 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index b4f96427d..2a169bff4 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "meriyah": "7.0.0", "micromatch": "4.0.8", "minimist": "1.2.8", - "n-readlines": "1.0.1", + "n-readlines": "3.4.0", "outdent": "0.8.0", "oxc-parser": "0.101.0", "parse-json": "8.3.0", diff --git a/scripts/build/packages/prettier.js b/scripts/build/packages/prettier.js index c1d6484c1..14d65dd2f 100644 --- a/scripts/build/packages/prettier.js +++ b/scripts/build/packages/prettier.js @@ -56,9 +56,15 @@ const mainModule = { }, { module: require.resolve("n-readlines"), - find: "const readBuffer = new Buffer(this.options.readChunk);", - replacement: - "const readBuffer = Buffer.alloc(this.options.readChunk);", + process(text) { + text = text.replace( + "const fs = require('fs')", + 'import fs from "node:fs"', + ); + text = text.replace("module.exports = ", "export default "); + + return text; + }, }, // `parse-json` use another copy of `@babel/code-frame` { diff --git a/src/utilities/get-interpreter.js b/src/utilities/get-interpreter.js index f980abd66..17e19da67 100644 --- a/src/utilities/get-interpreter.js +++ b/src/utilities/get-interpreter.js @@ -1,36 +1,48 @@ import readlines from "n-readlines"; +import { toPath } from "url-or-path"; + +/** + * @param {string | URL} file + * @returns {string | undefined} + */ +function readFileFirstLine(file) { + const liner = new readlines(toPath(file)); + const firstLineBuffer = liner.next(); + + if (typeof liner.fd === "number") { + liner.close(); + } + + return firstLineBuffer?.toString("utf8"); +} /** * @param {string | URL} file * @returns {string | undefined} */ function getInterpreter(file) { + let firstLine; try { - const liner = new readlines(file); - const firstLineBuffer = liner.next(); - - if (firstLineBuffer === false) { - return; - } - - liner.close(); - - const firstLine = firstLineBuffer.toString("utf8"); - - // #!/bin/env node, #!/usr/bin/env node - const m1 = firstLine.match(/^#!\/(?:usr\/)?bin\/env\s+(\S+)/u); - if (m1) { - return m1[1]; - } - - // #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node - const m2 = firstLine.match(/^#!\/(?:usr\/(?:local\/)?)?bin\/(\S+)/u); - if (m2) { - return m2[1]; - } + firstLine = readFileFirstLine(file); } catch { // couldn't open the file } + + if (!firstLine) { + return; + } + + // #!/bin/env node, #!/usr/bin/env node + const m1 = firstLine.match(/^#!\/(?:usr\/)?bin\/env\s+(\S+)/u); + if (m1) { + return m1[1]; + } + + // #!/bin/node, #!/usr/bin/node, #!/usr/local/bin/node + const m2 = firstLine.match(/^#!\/(?:usr\/(?:local\/)?)?bin\/(\S+)/u); + if (m2) { + return m2[1]; + } } export default getInterpreter; diff --git a/yarn.lock b/yarn.lock index 8595d530d..08e2379df 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6819,10 +6819,10 @@ __metadata: languageName: node linkType: hard -"n-readlines@npm:1.0.1": - version: 1.0.1 - resolution: "n-readlines@npm:1.0.1" - checksum: 10/4f0ddcd26347040eb219d8fc8c3620ce086095a355e64791298822b5882fdd646d69b733f6c519bcadc5c64f540e8f13a399e39cfb22e80e25a16000eca7572e +"n-readlines@npm:3.4.0": + version: 3.4.0 + resolution: "n-readlines@npm:3.4.0" + checksum: 10/8655b550cf25023c5692705ff4c0829955bedaf89e8cdc19bf09ef35501dc31d5ac8e14b35d0e1d60bdb509c41637150e67b07aa0e4b5e6295c8031252fff507 languageName: node linkType: hard @@ -7606,7 +7606,7 @@ __metadata: meriyah: "npm:7.0.0" micromatch: "npm:4.0.8" minimist: "npm:1.2.8" - n-readlines: "npm:1.0.1" + n-readlines: "npm:3.4.0" nano-spawn: "npm:2.0.0" node-style-text: "npm:2.1.2" npm-run-all2: "npm:8.0.4"