The zipjson CLI uses the json.stringify (code, NULL, 0) method to compress the.json file into a single line. After the…

Writing in the front

The reason is that I went to My JSON Server for no reason. It is easy to use Github repository to store a DB. JSON file to simulate REST Server.

For this I created a repository wxh16144/ I and created a db.json;

But the raw/da.json file is formatted so it’s easy to edit. I need to compress the.json folder into a single line

(It doesn’t do much good… Just drop a few newlines and whitespace and reduce the file size by 1%.

implementation

  1. warehouserawThe branch stores the original directlydb.jsonFiles andgithub workflowFile.
  2. useGithub ActionListen for the PUSH event in the RAW branchdb.jsonfile
  3. Submitted after processingdb.jsonTo the current warehousemainbranch
  4. Open wXh16144 / I/DB to check that REST is running.

During the CI process, I needed to compress the ‘db.json’ file, so I wrote the tool as CLI to run it in github Action.

Zipjson input. Json output.json

The editorindex.js

#! /usr/bin/env node
// Get input/output
const entry = process.argv.slice(2) [0] | |'index.json'
const output = process.argv.slice(2) [1] | |'output.json'

// Reads the input and writes to the output
const json = await readFileSync(entry, 'utf8')
await writeFileSync(output, JSON.stringify(json, null.0), 'utf8')

console.log('SUCCESS');
Copy the code

The editorpackage.json

{
  "name":"zipjson"."version": "1.0.0"."description": "compressed JSON file"."bin": {
    "zipjson": "index.js"}}Copy the code

Publish your NPM package

npm publish
Copy the code

Github Action: NPX zipjson db.json db.json

The last

In the spirit of keeping the package as small as possible, and adding some fault tolerance to the package without considering adding other dependencies, I finally released a minimal available tool to NPM.

npx zipjson [input] [output] --debug
Copy the code

Options

  • input: file or path. default ./index.json
  • output: file or path. default ./dist/index.json
  • –debug: console log. defaule false, alias --log.-l.-d
  • –yes: Agree to all interactions. defaule false, alias --ci.-y