const path = require('path'); const Generator = require('yeoman-generator'); // const exec = require('child_process').exec; const baseDir = path.join(__dirname, '../../'); module.exports = class extends Generator { async prompting() { this.data = await this.prompt([ { type: "input", name: "name", message: "Block Name", validate: (str) => { const matches = str.match(/\d+/g); if (matches != null) { return false; } return !!str; } }, // { // type: "input", // name: "name", // message: "Project ID" // }, { type: "list", name: "baseView", message: "View Template", default: 'container', choices: ['container', 'alignfull'], }, // { // type: "confirm", // name: "include_script", // default: false, // message: "Include script.js File?" // }, ]); } writing() { const title = capitalize(this.data.name); const data = Object.assign(this.data, { title, blockFilename: title.toLowerCase().replace(/ /ig, '-'), blockClassName: title.toLowerCase().replace(/ /ig, '_'), }); const pathDist = path.join(baseDir, 'blocks', data.blockFilename); this.fs.copyTpl( this.templatePath('src/template.template.hbs'), this.destinationPath(path.join(pathDist, 'src', data.blockFilename + '.template.hbs')), data ); this.fs.copyTpl( this.templatePath('src/styles/template.scss'), this.destinationPath(path.join(pathDist, 'src', 'styles', data.blockFilename + '.scss')), data ); this.fs.copyTpl( this.templatePath('src/scripts/template.mjs'), this.destinationPath(path.join(pathDist, 'src', 'scripts', data.blockFilename + '.mjs')), data ); this.fs.copyTpl( this.templatePath('src/images/demo.jpeg'), this.destinationPath(path.join(pathDist, 'src', 'images', 'demo.jpeg')), data ); this.fs.copyTpl( this.templatePath('config/default.cjs'), this.destinationPath(path.join(pathDist, 'config', 'default.cjs')), data ); this.fs.copyTpl( this.templatePath('package.json'), this.destinationPath(path.join(pathDist, 'package.json')), data ); this.fs.copyTpl( this.templatePath('data/default.json'), this.destinationPath(path.join(pathDist, 'data', 'default.json')), data ); this.fs.copyTpl( this.templatePath('data/advanced.json'), this.destinationPath(path.join(pathDist, 'data', 'advanced.json')), data ); this.fs.copyTpl( this.templatePath('README.md'), this.destinationPath(path.join(pathDist, 'README.md')), data ); this.fs.copyTpl( this.templatePath('.editorconfig'), this.destinationPath(path.join(pathDist, '.editorconfig')), data ); this.fs.copyTpl( this.templatePath('.gitignore'), this.destinationPath(path.join(pathDist, '.gitignore')), data ); // Run BUILD script // var cmd = exec("npm run build", function (err, stdout, stderr) { // if (err) { // console.log('Issue with running - "npm run build"\n\n', err); // } // }); // console.log(`\n\nDon't forget to connect the Component in your functions.php file ;)\n\n`); } }; function capitalize(str) { if (typeof str !== 'string') { return ''; } return str .toLowerCase() .split(/[ -_]/g) .filter((word) => !!word) .map((word) => { return word.charAt(0).toUpperCase() + word.slice(1); }) .join(' '); }