What is ploP? Plop is a lightweight project builder with a fairly simple API

Clear requirements

Why is PLOP needed? In theory, plop can do a lot of things, but mostly template files. For me, it’s just to generate some module files

Install the plop

Use global installation, not local, local installation is a cancer. npm install -g plop

Create a new file in the current project root directoryplopfile.js

There’s no need to create a new project, plop is a very lightweight tool with a solid and simple API, and we don’t even need to touch the CLI

const reducerGenerator = require('./plop-templates/reduce/prompt.js')

module.exports=function (plop) {
    plop.setGenerator('reducer', reducerGenerator);
};
Copy the code

Remember the reducer here so that you can use it later

Write a correspondingreducerGeneratorTemplate file of

So the next HBS file looks like this

let initialState = {} const{{name}}Reducer = (state = initialState, action) => { switch (action.type) { default: return state; } } export default{{name}}Reducer
Copy the code

The code that calls the template and generates the file

In my daily development process, REDUCER is generally placed here:

That is, SRC /reducers/{{name}}Reducer.js, where name corresponds to a Reducer name.

So our code looks like this:

module.exports = {
    description: 'Generate the Reducer template in redux'.// Describe the effect of generate
    prompts: [{
        type: 'input'.name: 'name'.message: 'reducers name'.// A problem at the command line}].actions: function(data){
        data.name=data.name.replace(data.name[0],data.name[0].toLowerCase());
        const actions =  [{
            type:'add'.path:'src/reducers/{{name}}Reducer.js'.templateFile:"plop-templates/reduce/index.hbs".data: {name:data.name
            }
        }]
        returnactions; }};Copy the code

With plop, we can enforce specifications such that our reducer file name must begin with a lowercase letter.

So our basic directory structure looks like this

call

We used global installation previously, and we can directly plop here. Remember the reducer parameter mentioned earlier? We need to use plop reducer and then the installation logic pops up a name query. After input, we find that the generation is complete


Plop is pretty darn good; Next, try husky