build-variations.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. const localpath = dirname( fileURLToPath( import.meta.url ) );
  7. (async function start() {
  8. let args = process.argv.slice(2);
  9. let source = args?.[0];
  10. let variation = args?.[1];
  11. if ( source && variation ) {
  12. return await buildVariation(source, variation);
  13. }
  14. return await buildAllVariations();
  15. })();
  16. async function buildAllVariations(){
  17. for (let source of getDirectories( localpath )){
  18. for (let variation of getDirectories( `${localpath}/${source}` )){
  19. await buildVariation(source, variation);
  20. }
  21. }
  22. }
  23. async function buildVariation(source, variation) {
  24. const srcDir = localpath + '/../' + source;
  25. const srcVariationDir = localpath + '/' + source + '/' + variation;
  26. const destDir = localpath + '/../' + variation;
  27. console.log( `Copying the source ${source} to the variation ${variation}` );
  28. try {
  29. // First empty the old directory.
  30. await fs.emptyDir( destDir );
  31. // Then copy the source directory.
  32. await fs.copy( srcDir, destDir );
  33. // remove unneeded resources
  34. await fs.remove( destDir + '/sass');
  35. await fs.remove( destDir + '/node_modules' );
  36. await fs.remove( destDir + '/template-mods.json');
  37. // copy the variation directory.
  38. await fs.copy( srcVariationDir, destDir );
  39. // copy the readme
  40. await fs.copy( localpath + '/variation-readme.md', destDir + '/variation-readme.md' );
  41. // make template modifications
  42. const hasMods = fs.existsSync( `${srcVariationDir}/template-mods.json`);
  43. if(hasMods) {
  44. const srcModsFile = await fs.readFile( `${srcVariationDir}/template-mods.json`, 'utf8' );
  45. const modsJson = JSON.parse( srcModsFile );
  46. modsJson.forEach(mod => {
  47. modifyTemplates(mod.from, mod.to, destDir + '/block-templates');
  48. });
  49. }
  50. // merge the theme.json files
  51. const srcJsonFile = await fs.readFile( srcDir + '/theme.json', 'utf8' );
  52. const srcVariationJsonFile = await fs.readFile( srcVariationDir + '/theme.json', 'utf8' )
  53. const srcJson = JSON.parse( srcJsonFile );
  54. const srcVariationJson = JSON.parse( srcVariationJsonFile );
  55. const mergedJson = deepmerge(srcJson, srcVariationJson, {
  56. arrayMerge: ( dest, source ) => source
  57. });
  58. await fs.writeFile ( destDir + '/theme.json', JSON.stringify( mergedJson, null, '\t' ), 'utf8' );
  59. console.log('Finished sucessfully.\n\n');
  60. }
  61. catch (err){
  62. console.log('ERROR: ', err, '\n\n');
  63. }
  64. }
  65. function getDirectories ( path ) {
  66. return fsorig.readdirSync( path, { withFileTypes: true } )
  67. .filter( item => item.isDirectory() )
  68. .map ( item => item.name )
  69. }
  70. function getTemplates ( path ) {
  71. return fsorig.readdirSync( path, { withFileTypes: true } )
  72. .map ( item => item.name )
  73. }
  74. function modifyTemplates ( from, to, basePath ) {
  75. console.log('modifying templates changing ', from, 'to', to);
  76. const templates = getTemplates(basePath);
  77. for ( let template of templates ) {
  78. let templatePath = basePath + '/' + template;
  79. let srcTemplate = fs.readFileSync( templatePath, 'utf8' );
  80. let resultTemplate = srcTemplate.replace(from, to);
  81. fs.writeFileSync ( templatePath, resultTemplate, 'utf8' );
  82. }
  83. }