/
githubmirror
/
gutenberg
Обзор
Документация
Войти
/
githubmirror
/
gutenberg
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
tools/release/lib/utils.js
148 строк
3 KB
Marco Ciampini
ESLint: Replace strict config with bulk suppressions (#81248)
07 авг 2026, 14:34
Не верифицирован
07 авг 2026, 14:34
7f43eaf
Код
Авторство
О чём код?
const fs = require( 'fs' ); const childProcess = require( 'child_process' ); const { randomUUID } = require( 'crypto' ); const path = require( 'path' ); const os = require( 'os' ); const { confirm } = require( '@inquirer/prompts' ); const { log, formats } = require( './logger' ); /** * Utility to run a child script * * @typedef {NodeJS.ProcessEnv} Env * * @param {string} script Script to run. * @param {string=} cwd Working directory. * @param {Env=} env Additional environment variables to pass to the script. */ function runShellScript( script, cwd, env = {} ) { return new Promise( ( resolve, reject ) => { childProcess.exec( script, { cwd, env: { NO_CHECKS: 'true', PATH: process.env.PATH, HOME: process.env.HOME, USER: process.env.USER, ...env, }, }, function ( error, stdout, stderr ) { if ( error ) { console.log( stdout ); // Sometimes the error message is thrown via stdout. console.log( stderr ); reject( error ); } else { resolve( true ); } } ); } ); } /** * Small utility used to read an uncached version of a JSON file * * @param {string} fileName */ function readJSONFile( fileName ) { const data = fs.readFileSync( fileName, 'utf8' ); return JSON.parse( data ); } /** * Common logic wrapping a step in the process. * * @param {string} name Step name. * @param {string} abortMessage Abort message. * @param {Function} handler Step logic. */ async function runStep( name, abortMessage, handler ) { try { await handler(); } catch ( exception ) { log( formats.error( 'The following error happened during the "' + formats.warning( name ) + '" step:' ) + '\n\n', exception, formats.error( '\n\n' + abortMessage ) ); process.exit( 1 ); } } /** * Asks the user for a confirmation to continue or abort otherwise. * * @param {string} message Confirmation message. * @param {boolean} isDefault Default reply. * @param {string} abortMessage Abort message. */ async function askForConfirmation( message, isDefault = true, abortMessage = 'Aborting.' ) { let isReady = false; try { isReady = await confirm( { default: isDefault, message, } ); } catch ( error ) { if ( error instanceof Error && error.name === 'ExitPromptError' ) { console.log( 'Cancelled.' ); process.exit( 1 ); } throw error; } if ( ! isReady ) { log( formats.error( '\n' + abortMessage ) ); process.exit( 1 ); } } /** * Generates a random temporary path in the OS's tmp dir. * * @return {string} Temporary Path. */ function getRandomTemporaryPath() { return path.join( os.tmpdir(), randomUUID() ); } /** * Scans the given directory and returns an array of file paths. * * @param {string} dir The path to the directory to scan. * * @return {string[]} An array of file paths. */ function getFilesFromDir( dir ) { if ( ! fs.existsSync( dir ) ) { console.log( 'Directory does not exist: ', dir ); return []; } return fs .readdirSync( dir, { withFileTypes: true } ) .filter( ( dirent ) => dirent.isFile() ) .map( ( dirent ) => path.join( dir, dirent.name ) ); } module.exports = { askForConfirmation, runStep, readJSONFile, runShellScript, getRandomTemporaryPath, getFilesFromDir, };