Skip to content

Instantly share code, notes, and snippets.

@0aveRyan
Last active January 25, 2025 20:13
Show Gist options
  • Save 0aveRyan/36910299d620473792207c56ef3c7a80 to your computer and use it in GitHub Desktop.
Save 0aveRyan/36910299d620473792207c56ef3c7a80 to your computer and use it in GitHub Desktop.
Simple node rig for using code-split javscript files to generate a theme.json
#!/usr/bin/env node
const { writeFileSync } = require('fs');
const { join } = require('path');
const themeConfig = require('../.source/theme');
console.log('🧱 Building theme.json...');
try {
writeFileSync(
join(__dirname, '../theme.json'),
JSON.stringify(themeConfig, null, 4)
);
console.log('βœ… theme.json built');
} catch (error) {
console.log('πŸ›‘ failed to create theme.json file.\n\nError:');
console.dir(error);
}
const customTemplates = require('./custom-templates');
const settings = require('./settings');
const styles = require('./styles');
const templateParts = require('./template-parts');
/**
* πŸŽ™ Theme Configuration πŸŽ™
*
* Generates the theme.json file.
*/
module.exports = {
$schema: `https://schemas.wp.org/trunk/theme.json`,
version: 2,
customTemplates,
settings,
styles,
templateParts,
};
{
"devDependencies": {
"npm-watch": "^0.11.0",
"rimraf": "^4.1.1",
},
"scripts": {
"theme": "rimraf ./theme.json && node .scripts/theme.js",
"theme:watch": "npm-watch theme"
},
"watch": {
"theme": {
"patterns": [
".source/theme"
],
"extensions": "js"
},
}
}
#!/usr/bin/env node
const { writeFileSync, mkdirSync } = require('fs');
const { join, basename } = require('path');
const { sync } = require('require-glob');
const srcDir = join(__dirname, '../.source/theme-styles');
const stylesDir = join(__dirname, '../styles');
const makeStyleJSON = (file) => {
const filename = basename(file.path, '.js') + '.json';
console.log('🎨 Making', filename);
try {
writeFileSync(
join(stylesDir, filename),
JSON.stringify(file.exports, null, 4)
);
} catch (err) {
console.log('⚠️ Issue making', basename(file.path, '.js'));
}
};
console.log('🌈 Building Style Variations...');
mkdirSync(stylesDir, { recursive: true });
sync(
'./*js', // relative to cwd arg not this file
{
cwd: srcDir,
reducer: (options, result, fileObject) => {
makeStyleJSON(fileObject);
},
}
);
console.log('βœ… Theme Style Variations built');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment