background

The previous project team of the author encountered a small problem in the management of NPM version, how to manage THE NPM package version in the small project. Recently, we encountered a small problem in packaging. The packages of both the test environment and production environment were developed by ourselves and submitted to SVN. However, everyone’s SVN local address is different (for example, A’s SVN address is in drive D, but B’s is in drive E), so I want to write A script to help me deal with this matter

My company does not allow me to publish on the company computer, so I wrote this article on my own computer at home

Encounter analysis

What do I need first

  • The package format you need to generate is*.tar.gz
  • Everyone’s localsvnDifferent address

And then think about what do I have

  • Safe computers are standard, so the software configuration for developers is the same
  • Computers are available on Windows and MAC
  • After everyone is hiredgitAre required to configure themselves into a secure work email[email protected]
  • When everyone is in the companygitUser names must be configured as their own namesXXX such as Vincent is me
  • A: The computer with the standard installation has7-Zip.nodejs.xmind.gitSuch as software

Break down problems encountered

How do I generate the *.tar.gz package

There is a plugin called Filemanager-webpack-plugin is can directly generate *.tar.gz package, but can not do personalized input directory, but we look at its source found it relies on archiver package to generate compressed files, but in line with the idea of being able to do something about it is not comfortable, So let’s see if we can generate *.tar.gz from something we already have

I can use a graphical interface to do this, but I can also use a command line to do this.

After some googling, I did find a command

7z.exe a *.tar . -ttar Tar the contents of the current folder

So I need to gzip the tar packageExe a *.tar.gz. -tgzipTar the package into gzip
Copy the code

On a MAC, it’s easy. Go straight to the dist directory

tar -zxcf xx.tar.gz . 
Copy the code

How do I put each person on a different SVN address

Scenario 1: Each person’s package address is entered from the terminal: as a result, each input command will be tedious

Solution 2: Declare a variable in a packaged script file, and every time someone else’s comment needs to be packaged: as a result, sometimes the file will be submitted, and someone else may use your path directly when packaged, resulting in an error, but this is a better solution

Is there a better plan?

And what do I have

I also have git configuration, and it is mandatory for new recruits to configure it according to our requirements, and the tutor will check it, so can it be used?

How do YOU maintain an address object in which script file

// Personnel address map, key is name pinyin, value is address
const peopleAddressMap = {
    vincent:'/users/vincent/project/svn'.susan:'D:\\project\\svn'
}
Copy the code

So there’s a better solution

Scenario 3: Get the mailbox by child_process and get the address from the staff address map as follows


const c = require("child_process");
const path = require("path");
const fs = require("fs");
const os = require("os");
const projectName = `payroll`;// Project name
let targetPath = ""; // Destination address
let person = ""; / / user name
const zipPath = "xxxx/7z.exe";
// The personnel's local SVN path
const peopleAddressMap = {
  vincent: "/Users/vincent/Documents/study/vue-study"};// Get the git user name
person = c
  .execSync("git config user.email")
  .toString()
  .trim()
  .toUpperCase()
  .replace("@PINGAN.COM.CN"."");
targetPath = peopleAddressMap[person];
// If there is no such name
if(! (personin peopleAddressMap)) {
  throw new Error(
    'Git config --global user.email${person}Pingan.com.cn then add it according to the format${person}The path of the `
  );
}
// If the name is specified but the address is not specified
if(! targetPath) {throw new Error('Please add according to the format${person}The path of the `);
}
try {
  removeFile(path.resolve(__dirname, ".. /dist".`${projectName}.tar`));
  removeFile(path.resolve(__dirname, ".. /dist".`${projectName}.tar.gz`));
  removeFile(path.resolve(targetPath, `${projectName}.tar`));
  removeFile(path.resolve(targetPath, `${projectName}.tar.gz`));
  / / generated gzip
  if (os.type() === "Windows_NT") {
    // windows
    c.execSync(
      `cd dist && ${zipPath} a ${projectName}.tar . -ttar && ${zipPath} a ${projectName}.tar.gz ${projectName}.tar -tgzip `
    );
  } else if (os.type() === "Darwin") {
    // mac
    c.execSync(`cd dist && tar -zvcf ${projectName}.tar.gz .`);
  }
  // Move the file
  copy(
    path.resolve(__dirname, ".. /dist".`${projectName}.tar.gz`),
    path.resolve(targetPath, `${projectName}.tar.gz`)); }catch (error) {
  throw new Error(Unknown error please contact Vincent);
}

// Remove the file
function removeFile(target) {
  if(fs.existsSync(target)) { fs.unlinkSync(target); }}// Copy the file
function copy(src, target) {
  fs.createReadStream(src).pipe(fs.createWriteStream(target));
}

Copy the code

conclusion

The way this article generated *.tar.gz has a lot of limitations. I would have used archiver as a cross-platform NPM library if I didn’t happen to work for a company that has a 7-Zip installed on all standard machines. For example, when I interviewed people recently, I found that many people were proficient in using Webpack on their resumes, but when I asked them carefully, they all answered that they used vue or React scaffolding to generate projects, but it was not clear which plugin or loader they used or how to configure and configure them. So what’s the point of having such a highlight on your resume? The purpose of writing this article is to wake up yourself in your comfort zone.

I hope you can have your own harvest after reading this article !!!!