|
| 1 | +import path from 'path'; |
| 2 | +import pkg from 'fs-extra'; |
| 3 | +const { readFileSync, writeFileSync, existsSync, mkdirp, copy, chmodSync } = |
| 4 | + pkg; |
| 5 | +import { camelcase, spinalcase } from 'stringcase'; |
| 6 | + |
| 7 | +// Create an async function to handle the replace operations |
| 8 | +async function replaceInFile( options ) { |
| 9 | + const { replaceInFile } = await import( 'replace-in-file' ); |
| 10 | + return replaceInFile( options ); |
| 11 | +} |
| 12 | + |
| 13 | +function buildIndexPhp( { |
| 14 | + blocks, |
| 15 | + version, |
| 16 | + name, |
| 17 | + description, |
| 18 | + resource, |
| 19 | + locale, |
| 20 | +} ) { |
| 21 | + let contents = readFileSync( |
| 22 | + path.join( 'bundler', 'template', 'index.php' ), |
| 23 | + 'utf-8' |
| 24 | + ); |
| 25 | + |
| 26 | + contents = contents.replace( /\[VERSION\]/g, version ); |
| 27 | + contents = contents.replace( /\[NAME\]/g, name ); |
| 28 | + contents = contents.replace( /\[DESCRIPTION\]/g, description ); |
| 29 | + contents = contents.replace( /\[RESOURCE\]/g, spinalcase( resource ) ); |
| 30 | + contents = contents.replace( |
| 31 | + /\[LOCALE\]/g, |
| 32 | + spinalcase( locale || resource ) |
| 33 | + ); |
| 34 | + contents += '\n'; |
| 35 | + |
| 36 | + for ( let index = 0; index < blocks.length; index++ ) { |
| 37 | + contents += |
| 38 | + "include_once __DIR__ . '/blocks/" + blocks[ index ] + ".php';\n"; |
| 39 | + |
| 40 | + if ( |
| 41 | + existsSync( path.join( 'blocks', blocks[ index ], 'rest-api.php' ) ) |
| 42 | + ) { |
| 43 | + contents += "include_once __DIR__ . '/blocks/rest-api.php';\n"; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return contents; |
| 48 | +} |
| 49 | + |
| 50 | +function buildIndexJs( blocks, isLabs ) { |
| 51 | + let contents = readFileSync( |
| 52 | + path.join( 'bundler', 'template', 'index.js' ), |
| 53 | + 'utf-8' |
| 54 | + ); |
| 55 | + |
| 56 | + contents += '\n'; |
| 57 | + |
| 58 | + for ( let index = 0; index < blocks.length; index++ ) { |
| 59 | + contents += |
| 60 | + 'import * as ' + |
| 61 | + camelcase( blocks[ index ] ) + |
| 62 | + " from '../../blocks/" + |
| 63 | + blocks[ index ] + |
| 64 | + "/src';\n"; |
| 65 | + } |
| 66 | + |
| 67 | + contents += |
| 68 | + '\n// Instantiate the blocks, adding them to our block category\n'; |
| 69 | + |
| 70 | + for ( let index = 0; index < blocks.length; index++ ) { |
| 71 | + contents += camelcase( blocks[ index ] ) + '.registerBlock();\n'; |
| 72 | + } |
| 73 | + |
| 74 | + return contents; |
| 75 | +} |
| 76 | + |
| 77 | +function buildStyle( blocks, title, fileType ) { |
| 78 | + let contents = '// ' + title + '\n'; |
| 79 | + |
| 80 | + for ( let index = 0; index < blocks.length; index++ ) { |
| 81 | + const styleName = `./blocks/${ blocks[ index ] }/${ fileType }.scss`; |
| 82 | + |
| 83 | + if ( existsSync( styleName ) ) { |
| 84 | + contents += `@import '${ styleName }';\n`; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return contents; |
| 89 | +} |
| 90 | + |
| 91 | +async function storeFile( contents, fileName ) { |
| 92 | + try { |
| 93 | + await mkdirp( path.dirname( fileName ) ); |
| 94 | + writeFileSync( fileName, contents ); |
| 95 | + } catch ( error ) { |
| 96 | + console.error( |
| 97 | + `storeFile: Unable to create directory: ${ fileName }. Error: ${ error.message }` |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +async function copyExtra( sourceDir, targetDir ) { |
| 103 | + const manifest = path.join( sourceDir, 'index.json' ); |
| 104 | + |
| 105 | + if ( existsSync( manifest ) ) { |
| 106 | + const json = JSON.parse( readFileSync( manifest, 'utf8' ) ); |
| 107 | + |
| 108 | + for ( let index = 0; index < json.length; index++ ) { |
| 109 | + await copy( |
| 110 | + path.join( sourceDir, json[ index ] ), |
| 111 | + path.join( targetDir, json[ index ] ) |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +async function copyBlocks( { blocks, resource }, targetDir ) { |
| 118 | + for ( let index = 0; index < blocks.length; index++ ) { |
| 119 | + const sourceFile = path.join( 'blocks', blocks[ index ], 'index.php' ); |
| 120 | + const targetFile = path.join( |
| 121 | + targetDir, |
| 122 | + 'blocks', |
| 123 | + blocks[ index ] + '.php' |
| 124 | + ); |
| 125 | + const manifest = path.join( 'blocks', blocks[ index ] ); |
| 126 | + |
| 127 | + try { |
| 128 | + await mkdirp( path.dirname( targetFile ) ); |
| 129 | + if ( ! existsSync( sourceFile ) ) { |
| 130 | + console.error( `Source file does not exist: ${ sourceFile }` ); |
| 131 | + continue; |
| 132 | + } |
| 133 | + await copy( sourceFile, targetFile ); |
| 134 | + await replaceInFile( { |
| 135 | + files: targetFile, |
| 136 | + from: /block-experiments/g, |
| 137 | + to: spinalcase( resource ), |
| 138 | + } ); |
| 139 | + await copyExtra( manifest, path.join( targetDir, 'blocks' ) ); |
| 140 | + } catch ( error ) { |
| 141 | + console.error( |
| 142 | + `copyBlocks: Unable to create directory: ${ targetFile }. Error: ${ error.message }` |
| 143 | + ); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function packageBundle( { resource, version } ) { |
| 149 | + const resourceDir = path.join( 'bundler', 'resources', resource ); |
| 150 | + const licenseFile = path.join( 'bundler', 'resources', 'license.txt' ); |
| 151 | + const file = spinalcase( resource ); |
| 152 | + const versioned = file; |
| 153 | + const output = `plugin/${ file }`; |
| 154 | + const assets = `plugin/assets`; |
| 155 | + const lines = [ |
| 156 | + '#!/bin/sh', |
| 157 | + `./node_modules/.bin/wp-scripts build ./build/src --output-path=${ assets } --config ./bundler/webpack.config.js`, |
| 158 | + `cp ${ assets }/* ${ output }`, |
| 159 | + `./node_modules/.bin/node-sass ./build/editor.scss -o ${ output }`, |
| 160 | + `./node_modules/.bin/node-sass ./build/style.scss -o ${ output }`, |
| 161 | + `if [ -d ${ resourceDir } ]; then cp -R ${ resourceDir }/* ${ output }; fi;`, |
| 162 | + `cp ${ licenseFile } ${ output }`, |
| 163 | + `(cd plugin; zip ${ versioned }.zip -r ${ file })`, |
| 164 | + `mkdir -p bundles`, |
| 165 | + `mv plugin/${ versioned }.zip bundles/${ versioned }.zip`, |
| 166 | + `rm -rf ${ assets }`, |
| 167 | + ]; |
| 168 | + |
| 169 | + mkdirp( 'build' ).then( function () { |
| 170 | + writeFileSync( './build/bundle.sh', lines.join( '\n' ) ); |
| 171 | + chmodSync( './build/bundle.sh', 0o755 ); |
| 172 | + } ); |
| 173 | +} |
| 174 | + |
| 175 | +export { |
| 176 | + storeFile, |
| 177 | + buildStyle, |
| 178 | + buildIndexJs, |
| 179 | + buildIndexPhp, |
| 180 | + copyBlocks, |
| 181 | + packageBundle, |
| 182 | +}; |
0 commit comments