2

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 . ",
  },
5
  • This might help you "Automating versioning and publication" itnext.io/… Commented May 27, 2021 at 2:44
  • 1
    @Nishant S Vispute You’ve read all of your member-only stories this month. Commented May 27, 2021 at 2:45
  • sorry, I don't understand your comment is that msg you see on the link or is it a question Commented May 27, 2021 at 2:49
  • @Nishant S Vispute I cannot view the post in your link because I am not a member Commented May 27, 2021 at 2: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 stackoverflow.com/questions/13059991/… Commented May 27, 2021 at 2:58

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know, you can use this variables inside the package.json:

${npm_package_name} ${npm_package_version}

Readed from this post

Comments

0

You can access package.json variables inside scripts section:

  "scripts": {
    "inst": "code --install-extension ${npm_package_name}-${npm_package_version}.vsix",
    "unist": "code --uninstall-extension ${npm_package_name}-${npm_package_version}.vsix"
  },

Also you can access them as environment variables via process.env:

http.createServer(...).listen(process.env.npm_package_config_port);

The package.json fields are tacked onto the npm_package_ prefix. So, for instance, if you had {"name":"foo", "version":"1.2.5"} in your package.json file, then your package scripts would have the npm_package_name environment variable set to "foo", and the npm_package_version set to "1.2.5". You can access these variables in your code with process.env.npm_package_name and process.env.npm_package_version, and so on for other fields.

Comments

0

The correct syntax for using package.json values in scripts is: (you need to escape in the string "...")

%npm_package_name%

%npm_package_version%

"scripts": {
  "serve": "ng serve -o --ssl --project \"%npm_package_name%\""
}

here have another example (by: Hadar Dashty): https://medium.com/@hadards/change-default-ng-serve-proxy-in-net7-project-with-angular-launch-998b1cdcaea2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.