Writing pre commit with nodejs

reference resources https://segmentfault.com/a/1190000007820338

Next, I will start from my actual project (a) and write a pre commit. This is mainly to solve the problem. You want to lock other package versions. However, for the b-kit comp package developed by your team, you are allowed to install the latest package all the time.

If you lock the version in the yarn.lock mode, you need to manually change the package.json of project a after upgrading package B each time, so that you can update yarn.lock during yarn install. Therefore, the method adopted here is to delete the b-kit-comp package lock in yarn.lock. When packaging online, yarn detects that there is no b-kit-comp package lock in yarn.lock every time yarn install is run, and the latest package will be installed every time.

However, when local development is started, the yarn.lock must have a corresponding package lock, so sometimes you forget to delete the b-kit-comp package lock. Therefore, the following pre commit is available.

The directory structure is as follows: (. git is at the same level as doc and editor, in the outermost layer, and start.js is in the scripts directory)


The summary involves three documents:

1. The first file that triggers hooks: pre commit. Put it in the. git/hooks folder. When git commit is executed, this file will be executed first. If non-0 is returned (it can be written in different script languages, such as nodejs returns through: process.exit(1)), the commit will be stopped-----< Pre commit File >

#!/usr/bin/env node
const execSync = require('child_process').execSync;

const path = require('path');
# 2> It is used to output the error.log file (if any error is reported) in the editor folder
# Note: the running directory of the code here is under the project root directory
execSync("node ./editor/pre-commit.js 2> error.log" );

2. The second is the file written through nodejs: pre-commit.js, which is placed in the root directory of the project. The logic is: when the requirements are met, process.exit(0) is returned; when the requirements are not met, process.exit (1) is returned-----< Pre commit.js File >

#!/usr/bin/env node
const fs = require('fs');

// Check whether the cucc package is deleted before commit
// The running file path here is the root directory. Here, editor/yarn.lock can also be replaced by. / editor/yarn.lock
const file = fs.readFileSync('editor/yarn.lock', 'utf-8');
if (file.indexOf('b-kite-comp@npm') > -1) {
  console.error('Please delete it first yarn.lock Medium b-kite-comp Packet lock');

3. The third file is the start.js file. Because normally, the. git filer will not be submitted to the project management, it is impossible to share the pre commit with others in the project. To do this, you need to put the pre commit file under the project, which can be placed in the same directory as start.js for the time being. As everyone who has used react knows, start.js is usually started when running a project. When running, you can copy the pre commit in this directory to. git/hooks.

// Judge whether pre commit already exists. If it does not exist, read pre commit.sh and write it
if (!fs.existsSync('.git/hooks/pre-commit')) {
  if (!fs.existsSync('../.git/hooks/')) {

  const preCommitFile = fs.readFileSync('./scripts/pre-commit');
  // The running directory is the current directory, i.e. editor
  fs.writeFileSync('../.git/hooks/pre-commit', preCommitFile, {
    encoding: 'utf8',
    mode: 0o777

Finally, when submitting code, if the verification fails, the code cannot be submitted. Of course, if -- no verify is added through the command line, git commit is executed  -- No verify, you can still submit.

Submit test:

1. When the yarn.lock file has b-kit-comp, it cannot be submitted successfully, and an error.log file is generated


  2. After deleting the b-kit-comp package lock in yarn.lock, the error.log does not output an error message, and the commit succeeds


Tags: node.js git

Posted on Tue, 09 Nov 2021 00:58:22 -0500 by birwin