This is the 8th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Scenario: The front end is separated from the back end, and the front end is deployed to the back end, requiring the code to be pushed to the git directory on the back end after each front end packaging. So I wrote the following scenario.

Install toolkit

npm i cpx shelljs --save-dev
Copy the code

See the Cloud shellJS Chinese document

CPX NPM document

Add js file to the project root directory

// Introduce the required toolkits
const cpx = require("cpx");
const shell = require('shelljs')
// Add the fs module to read files
const fs = require('fs');
/* fs.existssync (path), check if the second file exists, and return true shell.which('git') to check if git is available */
if (fs.existsSync('./package.json') && shell.which('git')) {
  /** fs.readfilesync () is the way to read files synchronously, and it will block
  var package = fs.readFileSync('./package.json');
  if (typeof package == "object") {
    var result = JSON.parse(package);
    if (result.gitcopy) {
      if(! fs.existsSync(result.gitcopy.dir)) {/** exec() exec(command [, options] [, callback]) command 
      
       : complete command to be executed on the command line options 
       : optional parameter, JSON Object async: Asynchronous execution. If you provide a callback method, this value must be true, Any other arguments to the child_process.exec() method of node.js can be called with callback:
        
         : when the process terminates, with the output, regardless of how you set silent: to print information to no output. The error "error > stdout < String > | < Buffer > stderr < String > | < Buffer > return values: In synchronous mode, returns a ShellString (shelljs v0.6.xf returns a ShellString of the form {code:... , stdout:... , stderr:... } object); In asynchronous mode, an object */ of child_process is returned
        
      
        shell.exec('git clone ' + result.gitcopy.git + ' ' + result.gitcopy.dir);
      }
      shell.exec('cd ' + result.gitcopy.dir + ' && git checkout ' + result.gitcopy.checkout + ' && git pull '.function (err, stdout, stderr) {
        if(! err) {/** cpx.copy(source, dest, options, callback) source {string} -- copy glob of the target file. Dest {string} -- The file path of the destination directory. Options {object} options.clean {Boolean} -- Remove flags for files copied before copying. Default value :false. Options. Dereference {Boolean} -- Flags that follow symbolic links when copied from symbolic links. Default value :false. Options. includeEmptyDirs {Boolean} -- Copies the flag of an empty directory that matches glob. Default value :false. Options. initialCopy {Boolean} -- Flag for not copying at the initial time of monitoring. This is CPX. Watch (). Default value: True. Options. Preserve {Boolean} -- Copies the uid, GID, atime, and mtime flags of the file. Default value :false. options.Transform {((filepath: string) => Stream.transform)[]} -- a function that creates a stream. Convert objects to convert each copy file. Options. update {Boolean} -- If the source file is old, the flag of the target file is not overwritten. Default value :false. The callback {(err: Error | null) = > void} - the function called when done. Copy the files matching the source glob to the dest directory. * /
          cpx.copy("dist/**", result.gitcopy.dir + result.gitcopy.webapp, {}, function () {
            shell.exec('cd ' + result.gitcopy.dir + '&& git pull && git add * && git commit -m "automatic update" && git push'.function (err, stdout, stderr) {
              if (err) {
                console.log("No Update!");
              } else {
                console.log("Successful!"); }}); })}else {
          console.log(err); }}); }}else {
    console.log("Configuration file is not a valid object"); }}else {
  console.log('Please check that Git has been installed successfully and that the configuration file exists! ');
}
Copy the code

Modify the package.json file

Add the following code for tier 1 nodes:

{
  "gitcopy": {
    "git": "https://git.jd.com/bt/jd-bt-middle-template.git"."dir": ".. /bt-middle-template-main"."webapp": "/bt-middle-template-main/src/main/webapp"."checkout": "master_jmm_default_images"}}Copy the code

Praise support, hand stay fragrance, and have glory yan, move your small hand to make a fortune yo, thank you for leaving your footprints.

Past excellent recommendation

Front ten thousand literal classics – basic

Front swastika area – advanced chapter

Let’s talk about two of the most common administrative tools used in front-end development

Talk about regular expressions

Hand touch hand take your liver Nodejs (1)

Obtain the file BLOB stream address to download the function

Hand touch hand take your liver NodeJS (2)