preface

Recently the development is not busy, handy learn how to write a scaffold. Well, let’s look at the definition of scaffolding.

Scaffolding must be in accordance with the floor and structure, garbage solid, garbage point vertical distance should not exceed 4 meters, horizontal distance should not exceed 6 meters. The material used for scaffolding waste should not be less than the strength of two no.8 steel wires, and tall shelves should not be connected with flexible materials. The operating surface of the scaffold must be covered with feet, hand plate from the wall can not be more than 20 cm, can not have a gap and probe board non-springboard. Foot plate to set up the lower level of the net operation surface is provided with two protective railings and a foot plate or set up a protective railings, hang up the safety net. Scaffolding must ensure that the overall structure is not deformed, the height of more than 20 meters of scaffold longitudinal must be set up cross cover, width can not exceed 7, pole from the horizontal Angle of the best 45 degrees to 60 degrees.

Emmm, I think there’s something wrong. Although WE are migrant workers, but the type of work is not quite the same.

Scaffolding used in computers refers to one of two techniques: code generation techniques associated with database access in some MVC frameworks; The second is project generation techniques supported by various tools.

That’s probably what most of our cognitive scaffolding means.

How easy it is to write a scaffold

Next, get to the point: how easy it is to write a scaffold.

Create a file, index.js, and do NPM init.

We can see the value -bin added to package.json.Then add it at the beginning of the index.js file

#! /usr/bin/env node
Copy the code

So what is it?

#!This symbol usually appears at the beginning of the first line to indicate the interpreter for the script file./usr/bin/envIt tells the system to look in the PATH directory, so configure#! /usr/bin/env node, is to solve the problem of different user node path, can let the system to dynamically find node to execute your script file.

We use NPM link for local debugging.

Now it’s time to witness the miracle.

Oh ho ~ is this the most basic scaffolding?!

Scaffolding related tripartite dependency library (part)

The name of the Introduction to the
commander Command line custom instructions are also used by most scaffolds
inquirer Provides a question-and-answer command line interaction method to collect and obtain the answers from the command line, and then perform operations
chalk Command line multicolored print, who doesn’t want to own a slutty flying scaffolding
cross-spawn A cross-platform command execution solution

There are also many excellent tripartite libraries, which I won’t go into here, but will be used later in this article.

Implement a scaffold that initializes the project

See can be so simple to achieve a scaffold, workers have no itching ah. So let’s start with one of the most basic functions of scaffolding, initialization.

Accept parameters

First of all, we will implement the command line custom instruction, using the three-party library is Commander.

yarn add commander
Copy the code
/ / into the commander
const program = require('commander')
// Declare a command line directive
program.command('create <project-name>').alias('c')
       .description('create a new project')
       .action((name) = > {
           console.log(name)
       })
// Enable command line parsing
program.parse(process.argv)
Copy the code

Remotely Downloading templates

In the remote download template section, we use cross-spawn to implement a run command method. Git clone command is used for remote download.

function runCommand({ cmd, args, cwd = process.cwd() }) { try { const res = spawn.sync(cmd, args, { cwd, stdio: 'inherit' }); } catch (error) { console.error(error); process.exit(1); }}Copy the code

Then add the following code to the previously defined command line event

. // Build address const path = '${process.cwd()}/${name}'; / / run the download instructions runCommand ({CMD: 'git', args: [' clone 'gitUrl, ` ${path} / `]})...Copy the code

Let’s run it and see what happens.

At this point, we have roughly implemented a scaffolding for downloading remote templates.

conclusion

Writing a scaffold is really not that difficult, and the difficulty of development is mostly in the accumulation of code, but the clever use of three-party libraries, can get twice the result with half the effort. A scaffold that suits you and knows you well is definitely a sharp edge for development.

Finally, I left a scaffold I just wrote – able to quickly locate and switch projects, which recorded a little bit of their own file templates and project templates, there are workers who want to use can look.

Portal groove – the cli