build-variations.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. await fs.remove( destDir + '/package.json');
  38. await fs.remove( destDir + '/package-lock.json');
  39. // copy the variation directory.
  40. await fs.copy( srcVariationDir, destDir );
  41. // copy the readme
  42. await fs.copy( localpath + '/variation-readme.md', destDir + '/variation-readme.md' );
  43. // make template modifications
  44. const hasMods = fs.existsSync( `${srcVariationDir}/template-mods.json`);
  45. if(hasMods) {
  46. const srcModsFile = await fs.readFile( `${srcVariationDir}/template-mods.json`, 'utf8' );
  47. const modsJson = JSON.parse( srcModsFile );
  48. modsJson.forEach(mod => {
  49. modifyTemplates(mod.from, mod.to, destDir + '/block-templates');
  50. });
  51. }
  52. // merge the theme.json files
  53. const srcJsonFile = await fs.readFile( srcDir + '/theme.json', 'utf8' );
  54. const srcVariationJsonFile = await fs.readFile( srcVariationDir + '/theme.json', 'utf8' )
  55. const srcJson = JSON.parse( srcJsonFile );
  56. const srcVariationJson = JSON.parse( srcVariationJsonFile );
  57. const mergedJson = deepmerge(srcJson, srcVariationJson, {
  58. arrayMerge: ( dest, source ) => source
  59. });
  60. await fs.writeFile ( destDir + '/theme.json', JSON.stringify( mergedJson, null, '\t' ), 'utf8' );
  61. console.log('Finished sucessfully.\n\n');
  62. }
  63. catch (err){
  64. console.log('ERROR: ', err, '\n\n');
  65. }
  66. }
  67. function getDirectories ( path ) {
  68. return fsorig.readdirSync( path, { withFileTypes: true } )
  69. .filter( item => item.isDirectory() )
  70. .map ( item => item.name )
  71. }
  72. function getTemplates ( path ) {
  73. return fsorig.readdirSync( path, { withFileTypes: true } )
  74. .map ( item => item.name )
  75. }
  76. function modifyTemplates ( from, to, basePath ) {
  77. console.log('modifying templates changing ', from, 'to', to);
  78. const templates = getTemplates(basePath);
  79. for ( let template of templates ) {
  80. let templatePath = basePath + '/' + template;
  81. let srcTemplate = fs.readFileSync( templatePath, 'utf8' );
  82. let resultTemplate = srcTemplate.replace(from, to);
  83. fs.writeFileSync ( templatePath, resultTemplate, 'utf8' );
  84. }
  85. }