1

Every time I compress my project, I need to manually sync version with the generated file name like CCCC007.tgz
In my package.json, compress-files script like below:

{
  "name": "MyBuild",
  "version": "0.0.7",
  "scripts": {
    "dev": "nodemon --legacy-watch ./src",
    "start": "node src/",
    "compress-files": "tar --exclude='./node_modules' --exclude='./myBuild.pem' --exclude='./config/local.json' -zcvf ~/CCCC007.tgz . ",
  },
}

I am wondering whether I can make the version as variable and use it in the script compress-files with something like:

  "name": "MyBuild",
  "version": "0.0.7",
  "scripts": {
    "dev": "nodemon --legacy-watch ./src",
    "start": "node src/",
    "compress-files": "tar --exclude='./node_modules' --exclude='./myBuild.pem' --exclude='./config/local.json' -zcvf ~/CCCC${this.version}.tgz . ",
  },
Codebling
  • 6,340
  • 2
  • 28
  • 52
CCCC
  • 1,702
  • 4
  • 12
  • This might help you **"Automating versioning and publication"** https://itnext.io/how-to-automate-versioning-and-publication-of-an-npm-package-233e8757a526 – Nishant S Vispute May 27 '21 at 02:44
  • @Nishant S Vispute `You’ve read all of your member-only stories this month.` – CCCC May 27 '21 at 02:45
  • sorry, I don't understand your comment is that msg you see on the link or is it a question – Nishant S Vispute May 27 '21 at 02:49
  • @Nishant S Vispute I cannot view the post in your link because I am not a member – CCCC May 27 '21 at 02:53
  • neither am i a member on that site but somehow it does shows me the article anyways nevermind that link here is a link from StackOverflow for the same https://stackoverflow.com/questions/13059991/update-package-json-version-automatically – Nishant S Vispute May 27 '21 at 02:58

1 Answers1

1

You can't refer to another object in JSON, and certainly not inside a value.

From the / direction, it looks like you're on linux. You can probaly do something like this:

"get-version": "node -e \"console.log(require('./package.json').version)\"",
"compress-files": "tar [...] `npm run get-version`",

That being said, there are tools to releases and versioning that should make life easier. You could give Release-It a try. But...if a package.json script with tar is all you need, don't let anyone tell you that you should be doing it a different way.

Codebling
  • 6,340
  • 2
  • 28
  • 52