UNPKG

@gravityforms/gulp-tasks

Version:
48 lines (40 loc) 1.19 kB
import fs from 'node:fs'; import path from 'node:path'; /** * @description Extract a plugin version string from plugin file contents. * * @since 8.4.0 * * @param {string} fileContent Plugin file contents. * * @return {string} Returns the discovered version string. */ const extractVersion = ( fileContent ) => { const versionRegex = /Version:\s*(\S+)/; const match = fileContent.match( versionRegex ); if ( match && match[ 1 ] ) { return match[ 1 ]; } throw new Error( 'Version not found for the plugin.' ); }; /** * @description Read a plugin file and return its declared version. * * @since 8.4.0 * * @param {string} rootPath Root path containing the plugin file. * @param {string} fileName Plugin file name. * * @return {string|undefined} Returns the plugin version when found. */ const getPluginVersion = ( rootPath, fileName ) => { const filePath = path.join( rootPath, fileName ); try { const content = fs.readFileSync( filePath, { encoding: 'utf8' } ); return extractVersion( content ); } catch ( error ) { console.error( `Error processing ${ filePath }: ${ error.message }` ); } }; export { extractVersion }; export default getPluginVersion;