@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
155 lines (131 loc) • 5.31 kB
JavaScript
const simpleGit = require( 'simple-git' );
const fs = require( 'fs' );
const fsp = require( 'fs' ).promises;
const path = require( 'path' );
const marked = require( 'marked' );
const { exec } = require( 'child_process' );
const { promisify } = require( 'util' );
const sendSlackMessage = require( '../utils/send-slack-message' );
const execAsync = promisify( exec );
const { loadEnv } = require( '../utils/tools' );
const getConfig = require('../../config');
const { config } = getConfig();
loadEnv( config.paths.root );
const rootPath = config?.paths?.root || '';
const fileName = config?.settings?.rootPluginFile || '';
const cloneRepository = async ( repoUrl, directoryPath ) => {
const git = simpleGit();
try {
await git.clone( repoUrl, directoryPath, [ '--branch', 'main', '--single-branch' ] );
console.log( 'Repo cloned successfully.' );
} catch ( err ) {
console.error( 'Error cloning repo:', err );
throw err; // Rethrow the error to be caught in the caller function
}
};
const setupFiles = async () => {
const token = process.env.GITHUB_TOKEN;
if ( ! token ) {
console.error( 'GitHub token not found. Cannot proceed with the setup of vendor environments.' );
return;
}
const directoryPath = path.join( rootPath, 'vendor-environments' );
const repoUrl = `https://${ token }@github.com/gravityforms/vendor-environments.git`;
// Check if the directory exists
if ( fs.existsSync( directoryPath ) ) {
console.log( 'Vendor environments directory exists. Deleting...' );
// Delete the directory and all of its contents
fs.rmSync( directoryPath, { recursive: true, force: true } );
}
// Clone the repository into the directory
console.log( 'Cloning vendor environments repository...' );
await cloneRepository( repoUrl, directoryPath );
};
const copyFilesAndUpdate = async () => {
const slug = config?.settings?.slug || '';
if ( ! slug || ! fileName ) {
console.error( 'Slug or filename not found. Cannot proceed with the setup.' );
return;
}
try {
// 1. Copy environment-parser.php
await fsp.copyFile( path.join( rootPath, 'vendor-environments', 'environment-parser.php' ), path.join( rootPath, 'environment-parser.php' ) );
// 2. Remove the environment directory if it exists
try {
await fsp.rm( path.join( rootPath, 'environment' ), { recursive: true, force: true } );
} catch ( error ) {
console.log( 'Error removing environment directory:', error.message );
}
// 3. Copy the environment directory
await fsp.cp( path.join( rootPath, 'vendor-environments', 'wpcom' ), path.join( rootPath, 'environment' ), { recursive: true } );
const rootFilePath = path.join( rootPath, fileName );
const backupFilePath = `${ rootFilePath }.old`;
// 4. Copy the original file to an .old file
await fsp.copyFile( rootFilePath, backupFilePath );
console.log( `Backup created at ${ backupFilePath }.` );
let content = await fsp.readFile( rootFilePath, 'utf8' );
// 5. Append require_once to rootfile if not present
const requireString = "require_once( plugin_dir_path( __FILE__ ) . '/environment-parser.php' );";
if ( ! content.includes( requireString ) ) {
console.log( 'Appending require_once to root file.' );
content += `\n${ requireString }`; // Append to the content variable
}
// 6. Update domain path
const searchPattern = /Domain Path: \/languages/g;
const replaceWith = 'Domain Path: /languages\rWoo: 18734000846909:1433d1511ab85009eab4f998d86c8cc9';
if ( searchPattern.test( content ) ) {
content = content.replace( searchPattern, replaceWith );
}
// 7. Write all changes back to the file
await fsp.writeFile( rootFilePath, content );
console.log( `${ rootFilePath } modified successfully.` );
} catch ( error ) {
console.error( 'An error occurred:', error );
}
};
const convertChangelog = async () => {
const lexer = new marked.Lexer();
const markdown = await fsp.readFile( path.join( rootPath, 'change_log.txt' ), 'utf8' );
const tokens = lexer.lex( markdown );
let newContent = `*** ${ config?.settings?.friendlyName || 'Gravity Forms' } changelog ***\r\n\r\n`;
tokens.forEach( ( token ) => {
switch ( token.type ) {
case 'heading':
const headingText = token.text;
const parts = headingText.split( '|' );
if ( parts.length < 2 ) {
newContent += `Version ${ parts[ 0 ].trim() }` + "\r\n";
} else {
newContent += `${ parts[ 1 ].trim() } - version ${ parts[ 0 ].trim() }` + "\r\n";
}
break;
case 'list':
token.items.forEach( ( item ) => {
newContent += `* ${ item.text }` + "\r\n";
} );
newContent += "\r\n";
break;
}
} );
try {
await fsp.writeFile( path.join( rootPath, 'changelog.txt' ), newContent );
} catch ( err ) {
console.log( 'Error updating changelog:', err );
}
};
// Usage
( async () => {
try {
await setupFiles();
console.log( 'Cloned vendor environments into project.' );
await copyFilesAndUpdate();
await convertChangelog();
await execAsync( 'gulp zip:packageWpCom' );
console.log( 'Package for wpcom created successfully.' );
} catch ( err ) {
console.error( 'An error occurred during setup:', err );
await sendSlackMessage( `Failed to create wpcom vendor environment during build: ${ err.message }`, 'error' );
process.exit( 1 );
}
} )();