build-variations.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import fs from 'fs-extra';
  2. import fsorig from 'fs';
  3. import deepmerge from 'deepmerge';
  4. import { dirname } from 'path';
  5. import { fileURLToPath } from 'url';
  6. import { executeCommand, getThemeMetadata } from '../theme-utils.mjs';
  7. const localpath = dirname( fileURLToPath( import.meta.url ) );
  8. const args = process.argv.slice(2);
  9. async function start() {
  10. let source = args?.[0];
  11. let variation = args?.[1];
  12. if ( source && variation ) {
  13. return await buildVariation(source, variation);
  14. }
  15. return await buildAllVariations();
  16. }
  17. start();
  18. async function buildAllVariations(){
  19. for (let source of getDirectories( localpath )){
  20. for (let variation of getDirectories( `${localpath}/${source}` )){
  21. await buildVariation(source, variation);
  22. }
  23. }
  24. }
  25. async function buildVariation(source, variation) {
  26. const srcDir = localpath + '/../' + source;
  27. const srcVariationDir = localpath + '/' + source + '/' + variation;
  28. const destDir = localpath + '/../' + variation;
  29. console.log( `Copying the source ${source} to the variation ${variation}` );
  30. try {
  31. // First grab the existing version if the variation exists already
  32. let variationExists = fs.existsSync(`${destDir}/style.css`);
  33. let currentVersion = null;
  34. if( variationExists ) {
  35. let styleCss = fs.readFileSync(`${destDir}/style.css`, 'utf8');
  36. currentVersion = getThemeMetadata(styleCss, 'Version');
  37. }
  38. // then empty the old directory.
  39. await fs.emptyDir( destDir );
  40. const exclude = [ 'node_modules', 'sass', 'package.json', 'package-lock.json' ];
  41. // Then copy the source directory.
  42. await fs.copy( srcDir, destDir, {
  43. filter: (fileName) => {
  44. if (exclude.some( ignore => fileName.includes( ignore ) )){
  45. return false;
  46. }
  47. return true;
  48. }
  49. });
  50. // copy the variation directory.
  51. await fs.copy( srcVariationDir, destDir );
  52. // copy the readme
  53. await fs.copy( localpath + '/variation-readme.md', destDir + '/variation-readme.md' );
  54. // merge the theme.json files
  55. const srcJsonFile = await fs.readFile( srcDir + '/theme.json', 'utf8' );
  56. const srcVariationJsonFile = await fs.readFile( srcVariationDir + '/theme.json', 'utf8' )
  57. const srcJson = JSON.parse( srcJsonFile );
  58. const srcVariationJson = JSON.parse( srcVariationJsonFile );
  59. const mergedJson = deepmerge(srcJson, srcVariationJson, {
  60. arrayMerge: ( dest, source ) => source
  61. });
  62. await fs.writeFile ( destDir + '/theme.json', JSON.stringify( mergedJson, null, '\t' ), 'utf8' );
  63. // replace the with current version
  64. if ( currentVersion != null ) {
  65. await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${destDir}/style.css`);
  66. }
  67. if ( args[0] == 'git-add-changes') {
  68. await executeCommand(`git add ${destDir}`, true);
  69. }
  70. console.log('Finished sucessfully.\n\n');
  71. }
  72. catch (err){
  73. console.log('ERROR: ', err, '\n\n');
  74. }
  75. }
  76. function getDirectories ( path ) {
  77. return fsorig.readdirSync( path, { withFileTypes: true } )
  78. .filter( item => item.isDirectory() )
  79. .map ( item => item.name )
  80. }
  81. function getTemplates ( path ) {
  82. return fsorig.readdirSync( path, { withFileTypes: true } )
  83. .map ( item => item.name )
  84. }
  85. function modifyTemplates ( from, to, basePath ) {
  86. console.log('modifying templates changing ', from, 'to', to);
  87. const templates = getTemplates(basePath);
  88. for ( let template of templates ) {
  89. let templatePath = basePath + '/' + template;
  90. let srcTemplate = fs.readFileSync( templatePath, 'utf8' );
  91. let resultTemplate = srcTemplate.replace(from, to);
  92. fs.writeFileSync ( templatePath, resultTemplate, 'utf8' );
  93. }
  94. }