build-variations.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Then copy the source directory.
  41. await fs.copy( srcDir, destDir );
  42. // remove unneeded resources
  43. await fs.remove( destDir + '/sass');
  44. await fs.remove( destDir + '/node_modules' );
  45. await fs.remove( destDir + '/package.json');
  46. await fs.remove( destDir + '/package-lock.json');
  47. // copy the variation directory.
  48. await fs.copy( srcVariationDir, destDir );
  49. // copy the readme
  50. await fs.copy( localpath + '/variation-readme.md', destDir + '/variation-readme.md' );
  51. // merge the theme.json files
  52. const srcJsonFile = await fs.readFile( srcDir + '/theme.json', 'utf8' );
  53. const srcVariationJsonFile = await fs.readFile( srcVariationDir + '/theme.json', 'utf8' )
  54. const srcJson = JSON.parse( srcJsonFile );
  55. const srcVariationJson = JSON.parse( srcVariationJsonFile );
  56. const mergedJson = deepmerge(srcJson, srcVariationJson, {
  57. arrayMerge: ( dest, source ) => source
  58. });
  59. await fs.writeFile ( destDir + '/theme.json', JSON.stringify( mergedJson, null, '\t' ), 'utf8' );
  60. // replace the with current version
  61. if ( currentVersion != null ) {
  62. await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${destDir}/style.css`);
  63. }
  64. if ( args[0] == 'git-add-changes') {
  65. await executeCommand(`git add ${destDir}`, true);
  66. }
  67. console.log('Finished sucessfully.\n\n');
  68. }
  69. catch (err){
  70. console.log('ERROR: ', err, '\n\n');
  71. }
  72. }
  73. function getDirectories ( path ) {
  74. return fsorig.readdirSync( path, { withFileTypes: true } )
  75. .filter( item => item.isDirectory() )
  76. .map ( item => item.name )
  77. }
  78. function getTemplates ( path ) {
  79. return fsorig.readdirSync( path, { withFileTypes: true } )
  80. .map ( item => item.name )
  81. }
  82. function modifyTemplates ( from, to, basePath ) {
  83. console.log('modifying templates changing ', from, 'to', to);
  84. const templates = getTemplates(basePath);
  85. for ( let template of templates ) {
  86. let templatePath = basePath + '/' + template;
  87. let srcTemplate = fs.readFileSync( templatePath, 'utf8' );
  88. let resultTemplate = srcTemplate.replace(from, to);
  89. fs.writeFileSync ( templatePath, resultTemplate, 'utf8' );
  90. }
  91. }