Last active
January 25, 2025 20:13
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
}, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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