From 1477cc0dbdee4b50fee4b1b98346812868148aa5 Mon Sep 17 00:00:00 2001 From: Christian Falch Date: Thu, 22 May 2025 10:06:14 -0700 Subject: [PATCH] simplify logging in prebuild scripts (#51527) Summary: To reduce reduntant code by repeating the logging functionality in each JS module, this commit introduces a factory for creating a logger with a given prefix. - Create factory `createLogger` - Remove redundant log implementations - Changed to use factory in hermes.js and ios-prebuild.js bypass-github-export-checks ## Changelog: [IOS] [CHANGED] - simplified logging in prebuild scripts Pull Request resolved: https://github.com/facebook/react-native/pull/51527 Test Plan: No tests so far. Reviewed By: cortinico Differential Revision: D75213656 Pulled By: cipolleschi fbshipit-source-id: 8403cfb8ed76ca3a30cfaaeabcd61ac790e7f0a1 --- packages/react-native/scripts/ios-prebuild.js | 4 ++- .../scripts/ios-prebuild/hermes.js | 19 ++--------- .../ios-prebuild/reactNativeDependencies.js | 19 ++--------- .../scripts/ios-prebuild/utils.js | 32 +++++++++++-------- 4 files changed, 27 insertions(+), 47 deletions(-) diff --git a/packages/react-native/scripts/ios-prebuild.js b/packages/react-native/scripts/ios-prebuild.js index 5d9b4af36c5..efa8689d2e1 100644 --- a/packages/react-native/scripts/ios-prebuild.js +++ b/packages/react-native/scripts/ios-prebuild.js @@ -14,7 +14,7 @@ const { } = require('./ios-prebuild/reactNativeDependencies'); const { createFolderIfNotExists, - prebuildLog, + createLogger, throwIfOnEden, } = require('./ios-prebuild/utils'); const {execSync} = require('child_process'); @@ -27,6 +27,8 @@ const packageJsonPath = path.join( 'package.json', ); +const prebuildLog = createLogger('Prebuild'); + // $FlowIgnore[unsupported-syntax] const {version: currentVersion} = require(packageJsonPath); diff --git a/packages/react-native/scripts/ios-prebuild/hermes.js b/packages/react-native/scripts/ios-prebuild/hermes.js index d6345a2bf14..11cc1d07185 100644 --- a/packages/react-native/scripts/ios-prebuild/hermes.js +++ b/packages/react-native/scripts/ios-prebuild/hermes.js @@ -8,12 +8,15 @@ * @format */ +const {createLogger} = require('./utils'); const {execSync} = require('child_process'); const fs = require('fs'); const path = require('path'); const stream = require('stream'); const {promisify} = require('util'); + const pipeline = promisify(stream.pipeline); +const hermesLog = createLogger('Hermes'); /** * Downloads hermes artifacts from the specified version and build type. If you want to specify a specific @@ -382,22 +385,6 @@ function abort(message /*: string */) { throw new Error(message); } -function hermesLog( - message /*: string */, - level /*: 'info' | 'warning' | 'error' */ = 'warning', -) { - // Simple log coloring for terminal output - const prefix = '[Hermes] '; - let colorFn = (x /*:string*/) => x; - if (process.stdout.isTTY) { - if (level === 'info') colorFn = x => `\x1b[32m${x}\x1b[0m`; - else if (level === 'error') colorFn = x => `\x1b[31m${x}\x1b[0m`; - else colorFn = x => `\x1b[33m${x}\x1b[0m`; - } - - console.log(colorFn(prefix + message)); -} - module.exports = { prepareHermesArtifactsAsync, }; diff --git a/packages/react-native/scripts/ios-prebuild/reactNativeDependencies.js b/packages/react-native/scripts/ios-prebuild/reactNativeDependencies.js index ae09025e60d..582a49fed3d 100644 --- a/packages/react-native/scripts/ios-prebuild/reactNativeDependencies.js +++ b/packages/react-native/scripts/ios-prebuild/reactNativeDependencies.js @@ -8,6 +8,7 @@ * @format */ +const {createLogger} = require('./utils'); const {execSync} = require('child_process'); const fs = require('fs'); const path = require('path'); @@ -15,6 +16,8 @@ const stream = require('stream'); const {promisify} = require('util'); const pipeline = promisify(stream.pipeline); +const dependencyLog = createLogger('ReactNativeDependencies'); + /** * Downloads ReactNativeDependencies artifacts from the specified version and build type. If you want to specify a specific * version of ReactNativeDependencies, use the RNDEP_VERSION environment variable. The path to the artifacts will be inside @@ -366,22 +369,6 @@ function abort(message /*: string */) { throw new Error(message); } -function dependencyLog( - message /*: string */, - level /*: 'info' | 'warning' | 'error' */ = 'warning', -) { - // Simple log coloring for terminal output - const prefix = '[ReactNativeDependencies] '; - let colorFn = (x /*:string*/) => x; - if (process.stdout.isTTY) { - if (level === 'info') colorFn = x => `\x1b[32m${x}\x1b[0m`; - else if (level === 'error') colorFn = x => `\x1b[31m${x}\x1b[0m`; - else colorFn = x => `\x1b[33m${x}\x1b[0m`; - } - - console.log(colorFn(prefix + message)); -} - module.exports = { prepareReactNativeDependenciesArtifactsAsync, }; diff --git a/packages/react-native/scripts/ios-prebuild/utils.js b/packages/react-native/scripts/ios-prebuild/utils.js index 7f5143dbd55..a433c28cf6e 100644 --- a/packages/react-native/scripts/ios-prebuild/utils.js +++ b/packages/react-native/scripts/ios-prebuild/utils.js @@ -37,24 +37,28 @@ function throwIfOnEden() { throw new Error('Cannot prepare the iOS prebuilds on an Eden checkout'); } -function prebuildLog( - message /*: string */, - level /*: 'info' | 'warning' | 'error' */ = 'warning', -) { - // Simple log coloring for terminal output - const prefix = '[Prebuild] '; - let colorFn = (x /*:string*/) => x; - if (process.stdout.isTTY) { - if (level === 'info') colorFn = x => `\x1b[32m${x}\x1b[0m`; - else if (level === 'error') colorFn = x => `\x1b[31m${x}\x1b[0m`; - else colorFn = x => `\x1b[33m${x}\x1b[0m`; - } +function createLogger( + prefix /*: string */, +) /*: (message: string, level?: 'info' | 'warning' | 'error') => void */ { + return function ( + message /*: string */, + level /*: 'info' | 'warning' | 'error' */ = 'info', + ) { + // Simple log coloring for terminal output + const resolvedPrefix = `[${prefix}] `; + let colorFn = (x /*:string*/) => x; + if (process.stdout.isTTY) { + if (level === 'info') colorFn = x => `\x1b[32m${x}\x1b[0m`; + else if (level === 'error') colorFn = x => `\x1b[31m${x}\x1b[0m`; + else colorFn = x => `\x1b[33m${x}\x1b[0m`; + } - console.log(colorFn(prefix + message)); + console.log(colorFn(resolvedPrefix) + message); + }; } module.exports = { createFolderIfNotExists, throwIfOnEden, - prebuildLog, + createLogger, };