UNPKG

@gravityforms/gulp-tasks

Version:
81 lines (68 loc) 2.6 kB
#!/usr/bin/env node const fs = require( 'fs' ).promises; const path = require( 'path' ); const { loadEnv } = require( '../utils/tools' ); const sendSlackMessage = require( '../utils/send-slack-message' ); const getConfig = require( '../../config' ); const { config } = getConfig(); loadEnv( config.paths.root ); /** * Enables feature flags in the PHP plugin file if FEATURE_FLAGS_ENABLED is true. */ const enableFeatureFlags = async () => { if ( process.env.FEATURE_FLAGS_ENABLED === 'true' ) { try { const phpFilePath = path.join( config.paths.root, config.settings.rootPluginFile ); let phpFileContent = await fs.readFile( phpFilePath, 'utf8' ); const featureFlagLine = `add_filter( '${ config.settings.slug }_feature_flag', '__return_true' );\n`; phpFileContent += `\n${ featureFlagLine }`; await fs.writeFile( phpFilePath, phpFileContent, 'utf8' ); console.log( 'Feature flags enabled.' ); } catch ( error ) { console.error( 'Failed to enable feature flags:', error ); throw error; } } }; /** * Sets the override version in the PHP plugin file if BUILD_OVERRIDE_VERSION is specified. */ const setOverrideVersion = async () => { const overrideVersion = process.env.BUILD_OVERRIDE_VERSION; if ( overrideVersion ) { try { const phpFilePath = path.join( config.paths.root, config.settings.rootPluginFile ); let phpFileContent = await fs.readFile( phpFilePath, 'utf8' ); // Extract the current version from the header block. const versionMatch = phpFileContent.match( /Version:\s*([\d\w.\-]+)/ ); if ( ! versionMatch ) { throw new Error( 'Could not find the version in the PHP file header.' ); } const currentVersion = versionMatch[ 1 ]; const updatedContent = phpFileContent.replace( new RegExp( currentVersion, 'g' ), overrideVersion ); await fs.writeFile( phpFilePath, updatedContent, 'utf8' ); console.log( `Version override applied: ${ overrideVersion }` ); } catch ( error ) { console.error( 'Failed to set override version:', error ); throw error; } } }; /** * @function init * @description Initialize the pre-package script to enable feature flags or set override version. * * @returns {Promise<void>} */ const init = async () => { try { await enableFeatureFlags(); await setOverrideVersion(); console.log( 'Ran all pre-package scripts.' ); } catch ( error ) { console.error( 'Failed to run pre-package scripts:', error ); await sendSlackMessage( `Failed to run pre-package scripts during build: ${ error.message }`, 'error' ); process.exit( 1 ); } }; module.exports = init;