@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
75 lines (66 loc) • 1.86 kB
JavaScript
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createRequire } from 'node:module';
const require = createRequire( import.meta.url );
/**
* @description Determine whether a config belongs to a Gravity Forms addon.
*
* @since 8.4.0
*
* @param {Object} config Project config object.
*
* @return {boolean} Returns true when the slug represents an addon.
*/
function isGravityFormsAddon( config ) {
return ( config.settings.slug.includes( 'gravityforms' ) || config.settings.slug.includes( 'gravityflow' ) ) && config.settings.slug !== 'gravityforms' && config.settings.slug !== 'gravityflow';
}
/**
* @description Remove a trailing slash from a string.
*
* @since 8.4.0
*
* @param {string} str String to normalize.
*
* @return {string} Returns the string without a trailing slash.
*/
function untrailingSlashIt( str ) {
return str.replace( /\/$/, '' );
}
/**
* @description Ensure a string has a trailing slash.
*
* @since 8.4.0
*
* @param {string} str String to normalize.
*
* @return {string} Returns the string with a trailing slash.
*/
function trailingSlashIt( str ) {
return untrailingSlashIt( str ) + '/';
}
/**
* @description Load local and global dotenv files when present.
*
* @since 8.4.0
*
* @param {string} [projectEnvPath=''] Project root used to resolve the local .env file.
*
* @return {void}
*/
function loadEnv( projectEnvPath = '' ) {
const globalEnvPath = path.join( os.homedir(), '.config', 'gravity', '.env' );
const localEnvPath = path.resolve( projectEnvPath, '.env' );
if ( fs.existsSync( localEnvPath ) ) {
require( 'dotenv' ).config( { path: localEnvPath } );
}
if ( fs.existsSync( globalEnvPath ) ) {
require( 'dotenv' ).config( { path: globalEnvPath } );
}
}
export {
isGravityFormsAddon,
loadEnv,
trailingSlashIt,
untrailingSlashIt,
};