1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | 1x 1x 1x 1x | /** * This file contains the main WP Make generator base definition. * * The Make Base defines most of the functionality to control the lifecycle of a * generator. Most generators can be defined with little more than an initConfig * method which sends back a lifecycle object controlling the generation * lifecycle. */ // Import dependencies import {Base} from 'extendable-yeoman'; import chalk from 'chalk'; import mkdirp from 'mkdirp'; import ProfileRC from 'yo-profile'; import ASTConfig from './util/ast-config'; import gruntConfig from './util/grunt-config'; // Import mixins import mixinInstaller from './util/installer'; import mixinPrompt from './util/prompt'; import mixinTree from './util/tree'; import mixinTools from './util/tools'; import mixinWriters from './util/writers'; /** * The MakeBase definition for controlling a WP Make generation lifecycle. * * The MakeBase can be exteded in additional generators to allow controlling of * WordPress project generation in a fairly opinionated way. Each project will * define a initConfig method that will return a lifecycle object. * * The lifecycle object controls how a project is generated and consists of two * main sections. The prompts section, which defines what questions will be * asked of the user, and the tree section, which will define the generated * file structure as well as the copies, templates, json objects, and js modules * that will create the project. * * Here is a simple example lifecycle object: * * { * prompts: [ * name: 'projectTitle' * message: 'Project title', * default: 'WP Plugin' * ], * tree: { * templates: { * 'project.php': '_project.php' * }, * json: { * 'package.json': '_package.json' * } * } * } * * The generator will then automatically ask the questions in the prompt * lifecycle, and then write out the templates according to the tree lifecycle. * * Additional methods can be used and will be run in sequence between the prompt * and tree lifecycles. This allows you to further mutate and add to the data * gathered from the user as neede before running into the tree lifecycle. These * methods will be run in sequence unless you specifically use the yeoman * queue framework to move them outside of this basic lifecycle. */ const MakeBase = Base.extend( { /** * This is the default whitespace used when outputting JSON and JS code. * * @type {string} */ defaultPad: '\t', /** * This is the default lifecycle object. * * Don't overwrite this. Instead, define a lifecycle object in your * initConfig method and return a lifecycle object. This returned object * will be run through `Object.assign` with this object as the default. * * @type {Object} */ _lifecycle: { prompts: {}, tree: {}, defaults: {} }, /** * This is where data is stored when gathered from inputs. * * This is what is passed to templates during output. It can be added to * if needed using mutation functions in your generator. * * @type {Object} */ data: {}, /** * Whether or not this project uses grunt and needs a grunt file. * * If you would like a gruntfile to be created for you, simply set this to * true in your generator. It will output a basic Gruntfile.js. You can * modify this using the AST config options in mutation functions. * * @type {boolean} */ grunt: false, /** * Defines which install commands are supported by this generator. * * In your extension, you can define which install commands should be run * using this property. Omit an install entirely if you don't support it. * The key is the installer (npm, bower, etc.) and the value is the default * true it will be installed, false the installer is skipped by default. * * @type {Object} */ installCommands: { npm: true, composer: true }, /** * Sets up the object, registering methods with the Yeoman run loop. * * @return {Object} The resulting MakeBase object. */ constructor: function () { // Run the baser constructor. Base.apply( this, arguments ); // Prepare overall lifecycle. this.env.runLoop.add( 'initializing', this.welcomeMessage.bind( this ), { once: 'wpm:welcome', run: false } ); this.env.runLoop.add( 'initializing', this.initProfiles.bind( this ), { once: 'wpm:initProfiles', run: false } ); this.env.runLoop.add( 'initializing', this.initGrunt.bind( this ), { once: 'wpm:initGrunt', run: false } ); this.env.runLoop.add( 'initializing', this.setLifecycle.bind( this ), { once: 'wpm:setLifecycle', run: false } ); this.env.runLoop.add( 'initializing', this.installers.bind( this ), { once: 'wpm:install', run: false } ); this.env.runLoop.add( 'prompting', this.prompts.bind( this ), { once: 'wpm:prompts', run: false } ); this.env.runLoop.add( 'configuring', this.makeObjects.bind( this ), { once: 'wpm:makeObjects', run: false } ); this.env.runLoop.add( 'writing', this.walkTree.bind( this ), { once: 'wpm:walkTree', run: false } ); this.env.runLoop.add( 'end', this.goodbyeMessage.bind( this ), { once: 'wpm:goodbye', run: false } ); }, /** * Outputs a welcome message to thank users for trying WP Make. * * @param {Function} done The function to continue generation. * @return {void} */ welcomeMessage: function ( done ) { this.log( chalk.magenta( 'Thanks for generating with ', chalk.bold( 'WP Make' ), '!' ) ); done(); }, /** * Outputs a goodbye message to let users know generation is complete. * * @param {Function} done The function to continue generation. * @return {void} */ goodbyeMessage: function ( done ) { this.log( chalk.green.bold( `Your ${this.type || 'item'} has been generated.` ) ); done(); }, /** * Optionally initialize a grunt object if grunt is set to true. * @param {Function} done The function to continue generation. * @return {void} */ initGrunt: function ( done ) { // Optionally intialize grunt config and output. if ( this.grunt ) { // Read in or create the default gruntfile. this.grunt = gruntConfig( this.fs.read( this.destinationPath('Gruntfile.js'), { defaults: this.starter('gruntfile') } ) ); // Set up gruntfile dump on output. this.env.runLoop.add( 'writing', this.writeGruntfile.bind( this ), { once: 'wpm:grunt', run: false } ); } done(); }, /** * Initializes the WP Make prfiles feature from any .wpmakerc files. * @param {Function} done Run to indicate generation should continue. * @return {void} */ initProfiles: function ( done ) { this._makeProfile = new this.ProfileRC().load( { license: 'GPL-2.0+', humanstxt: true, rootNamespace: undefined, phpMin: undefined, wpTested: undefined, wpMin: undefined }, 'wpmake' ).properties; // Convert legacy keys to camelCase. ['root_namespace', 'php_min', 'wp_tested', 'wp_min'].map( key => { if ( this._makeProfile.hasOwnProperty( key ) ) { const camel = key.replace( /(\_\w)/g, m => m[1].toUpperCase() ); this._makeProfile[ camel ] = this._makeProfile[ key ]; delete this._makeProfile[ key ]; } }); // convert legacy prompt to __prompt if ( this._makeProfile.rootNamespace === 'prompt' ) { this._makeProfile.rootNamespace = '__prompt'; } done(); }, /** * Sets the the `this.lifecycle` object using `initConfig` and `_lifecycle`. * * @param {Function} done The function to continue generation. * @return {void} */ setLifecycle: function ( done ) { // Make sure lifecycle is ready. this.lifecycle = Object.assign( {}, this._lifecycle, this.initConfig() ); done(); }, /** * Runs through the prompst defined in `this.lifecycle.prompts`. * * @param {Function} done The function to continue generation. * @return {void} */ prompts: function ( done ) { this.prompt( this.lifecycle.prompts, this.lifecycle.defaults ).then( ( props ) => { this.data = Object.assign( props, { basename: this.basename } ); done(); } ); }, /** * Turns object strings into AST objects for better/safer mutation control. * * @param {Function} done The function to continue generation. * @return {void} */ makeObjects: function ( done ) { this.tree( this.lifecycle.tree, { modules: this.initModule } ); done(); }, /** * Stub function for creating the initial lifecycle object. * * Override this in your generator to define the generation lifecycle. * * @return {Object} Returns a lifecycle object. */ initConfig: () => ({}), /** * Walks the `lifecycle.tree` to output all of the objects defined. * * @param {Function} done The function to continue generation. * @return {void} */ walkTree: function ( done ) { this.tree( this.lifecycle.tree, { _pre: ( tree, dir ) => mkdirp( dir ), json: this.writeJSON, modules: this.writeModule, copies: this.writeCopy, templates: this.writeTemplate }); done(); }, /** * Helper function to turn a specific JS string into and AST object. * * The intended use is to create AST queryable module objects. These are * typically pretty simple objects that are simply passing a config value * back to module.exports. * * @param {string} module The default module value. * @param {string} location The file path to where the module will live. * @param {string} pad Optional. The whitespace to use in output. * Will default to the defined default. * @return {void} */ initModule: function ( module, location, pad = this.defaultPad ) { module = this.fs.read( this.destinationPath( location ), { defaults: module || this.starter( 'module' ) } ); return new ASTConfig( module, { formatOpts: { format: { indent: { style: pad } } } } ); } } ); /** * Extends the MakeBase prototype with other mix-ins. * * These mixins are required for the MakeBase to function properly, but they * are defined separately to help keep the functionality organization a little * cleaner. */ Object.assign( MakeBase.prototype, mixinInstaller, mixinPrompt, mixinTools, mixinTree, mixinWriters, { ProfileRC: ProfileRC } ); // Exports the MakeBase for use. export default MakeBase; |