create-child.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const child_theme_json = `{
  2. "settings": {
  3. "custom": {}
  4. },
  5. "styles": {
  6. "blocks": {}
  7. }
  8. }`;
  9. const child_package_json = `{
  10. "name": "{newtheme}",
  11. "version": "0.0.1",
  12. "description": "",
  13. "main": "style.css",
  14. "dependencies": {},
  15. "devDependencies": {
  16. "@wordpress/base-styles": "^3.4.0",
  17. "chokidar-cli": "^2.1.0",
  18. "node-sass": "^5.0.0",
  19. "node-sass-package-importer": "^5.3.2"
  20. },
  21. "scripts": {
  22. "start": "chokidar \\"**/*.scss\\" child-theme.json ../blockbase/theme.json -c \\"npm run build\\" --initial",
  23. "build": "npm run build:theme && npm run build:scss",
  24. "build:theme": "node ../blockbase/build.js {newtheme}",
  25. "build:scss": "node-sass --importer node_modules/node-sass-package-importer/dist/cli.js sass/theme.scss assets/theme.css --output-style expanded --indent-type tab --indent-width 1 --source-map true"
  26. },
  27. "author": "",
  28. "license": "GPLv2"
  29. }`;
  30. const style_css = `/*
  31. Theme Name: {newtheme}
  32. Theme URI:
  33. Author:
  34. Author URI:
  35. Description:
  36. Requires at least: 5.7
  37. Tested up to: 5.7
  38. Requires PHP: 5.7
  39. Version: 0.0.1
  40. License: GNU General Public License v2 or later
  41. License URI:
  42. Template: blockbase
  43. Text Domain: {newtheme}
  44. Tags:
  45. */`;
  46. const theme_scss = `
  47. // Custom CSS should be added here. It will be compiled to /assets/theme.css.
  48. `;
  49. const functions_php = `<?php
  50. /**
  51. * Add Editor Styles
  52. */
  53. function newtheme_editor_styles() {
  54. // Enqueue editor styles.
  55. add_editor_style(
  56. array(
  57. '/assets/theme.css',
  58. )
  59. );
  60. }
  61. add_action( 'after_setup_theme', 'newtheme_editor_styles' );
  62. /**
  63. *
  64. * Enqueue scripts and styles.
  65. */
  66. function newtheme_scripts() {
  67. wp_enqueue_style( 'newtheme-styles', get_stylesheet_directory_uri() . '/assets/theme.css', array('blockbase-ponyfill'), wp_get_theme()->get( 'Version' ) );
  68. }
  69. add_action( 'wp_enqueue_scripts', 'newtheme_scripts' );
  70. `;
  71. const block_templates_index_html = `
  72. <!-- wp:template-part {"slug":"header","tagName":"header"} /-->
  73. <!-- wp:paragraph -->
  74. <p>Populate this /block-templates/INDEX.html template (and other templates) with your block markup.</p>
  75. <!-- /wp:paragraph -->
  76. `;
  77. const block_template_parts_header = `
  78. <!-- wp:paragraph -->
  79. <p>Populate this /block-template-parts/header.html template part (and other template parts) with your block markup.</p>
  80. <!-- /wp:paragraph -->
  81. `;
  82. const success_message = `
  83. Success creating new theme {newtheme}!
  84. `;
  85. const fs = require( 'fs' );
  86. const execSync = require( 'child_process' ).execSync;
  87. const childThemeName = process.argv[ 2 ];
  88. if ( ! childThemeName ) {
  89. console.log( 'Please provide the slug of the child theme to create.' );
  90. } else {
  91. createChild( childThemeName );
  92. }
  93. function createChild( name ) {
  94. //TODO: Santatize theme name/slug
  95. try {
  96. fs.mkdirSync( `../${ name }` );
  97. fs.mkdirSync( `../${ name }/sass` );
  98. fs.mkdirSync( `../${ name }/block-templates` );
  99. fs.mkdirSync( `../${ name }/block-template-parts` );
  100. fs.writeFileSync(
  101. `../${ name }/child-theme.json`,
  102. child_theme_json.replace( /{newtheme}/g, name )
  103. );
  104. fs.writeFileSync(
  105. `../${ name }/package.json`,
  106. child_package_json.replace( /{newtheme}/g, name )
  107. );
  108. fs.writeFileSync(
  109. `../${ name }/style.css`,
  110. style_css.replace( /{newtheme}/g, name )
  111. );
  112. fs.writeFileSync(
  113. `../${ name }/functions.php`,
  114. functions_php.replace( /{newtheme}/g, name )
  115. );
  116. fs.writeFileSync(
  117. `../${ name }/sass/theme.scss`,
  118. theme_scss.replace( /{newtheme}/g, name )
  119. );
  120. fs.writeFileSync(
  121. `../${ name }/block-templates/index.html`,
  122. block_templates_index_html.replace( /{newtheme}/g, name )
  123. );
  124. fs.writeFileSync(
  125. `../${ name }/block-template-parts/header.html`,
  126. block_template_parts_header.replace( /{newtheme}/g, name )
  127. );
  128. execSync( `cd ../${ name }/ && npm install && npm run build`, {
  129. stdio: 'inherit',
  130. } );
  131. console.log( success_message.replace( /{newtheme}/g, name ) );
  132. } catch ( err ) {
  133. console.log( 'child theme creation failed: ' + err );
  134. }
  135. }