@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
149 lines (123 loc) • 4.86 kB
JavaScript
import { exec } from 'node:child_process';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import path from 'node:path';
import { createRequire } from 'node:module';
import { promisify } from 'node:util';
import getConfig from '../../config.js';
import sendSlackMessage from '../utils/send-slack-message.js';
import { loadEnv } from '../utils/tools.js';
const cjsRequire = createRequire( import.meta.url );
const simpleGit = cjsRequire( 'simple-git' );
const marked = cjsRequire( 'marked' );
const execAsync = promisify( exec );
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;
}
};
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`;
if ( fs.existsSync( directoryPath ) ) {
console.log( 'Vendor environments directory exists. Deleting...' );
fs.rmSync( directoryPath, { recursive: true, force: true } );
}
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 {
await fsp.copyFile( path.join( rootPath, 'vendor-environments', 'environment-parser.php' ), path.join( rootPath, 'environment-parser.php' ) );
try {
await fsp.rm( path.join( rootPath, 'environment' ), { recursive: true, force: true } );
} catch ( error ) {
console.log( 'Error removing environment directory:', error.message );
}
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`;
await fsp.copyFile( rootFilePath, backupFilePath );
console.log( `Backup created at ${ backupFilePath }.` );
let content = await fsp.readFile( rootFilePath, 'utf8' );
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 }`;
}
const searchPattern = /Domain Path: \/languages/g;
const replaceWith = 'Domain Path: /languages\rWoo: 18734000846909:1433d1511ab85009eab4f998d86c8cc9';
if ( searchPattern.test( content ) ) {
content = content.replace( searchPattern, replaceWith );
}
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 );
}
};
( 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 );
}
} )();