Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package-lock.json version not updated correctly #257

Open
sdankel opened this issue Apr 17, 2024 · 6 comments
Open

package-lock.json version not updated correctly #257

sdankel opened this issue Apr 17, 2024 · 6 comments

Comments

@sdankel
Copy link

sdankel commented Apr 17, 2024

Newer versions of npm require updating package-lock.json in two places, like this:

--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "my-pkg",
-  "version": "0.3.1",
+  "version": "0.3.2",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "my-pkg",
-      "version": "0.3.1",
+      "version": "0.3.2",
       "license": "Apache-2.0",
       "dependencies": {

however, adding package-lock.json to the version-file property only results in the version being updated on one line:

--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "my-pkg",
-  "version": "0.3.1",
+  "version": "0.3.2",
   "lockfileVersion": 2,

What would be better is if there was a pre-commit hook that runs a bash command (npm i in this case) to update the package-lock.json. It looks like the pre-commit hook only accept a js script right now.

@TriPSs
Copy link
Owner

TriPSs commented Apr 17, 2024

You could ofc inside that pre-commit hook script run that command

@dhaakchg
Copy link

dhaakchg commented May 1, 2024

I am interested in this behavior too, could you elaborate on what you mean by "ofc" ?

@TriPSs
Copy link
Owner

TriPSs commented May 2, 2024

That you can do anything you want inside the pre-commit script, so if you want to run a command you can do that there.

@dhaakchg
Copy link

dhaakchg commented May 2, 2024

@sdankel setting the following as a pre-commit script is working fine for me. Obviously change the cwd line to suit your needs. I have it as pre-commit: .github/workflows/version-pre-commit.js

const { execSync } = require('child_process')
const path = require('path')

exports.preCommit = (props) => {
    const command = 'HUSKY=0 npm i'
    try {
        console.log(`Bumping package-lock.json to ${props.version} by execing: npm i in ${path.join(__dirname, '../..')}`)
        const stdout = execSync(command, { cwd: path.join(__dirname, '../..') })
        console.log(stdout.toString())
    } catch (err) {
        console.log(`${command} failed, error: ${err.message}`)
        process.exit(1)
    }
}

@sdankel
Copy link
Author

sdankel commented May 2, 2024

@sdankel setting the following as a pre-commit script is working fine for me. Obviously change the cwd line to suit your needs. I have it as pre-commit: .github/workflows/version-pre-commit.js

const { execSync } = require('child_process')
const path = require('path')

exports.preCommit = (props) => {
    const command = 'HUSKY=0 npm i'
    try {
        console.log(`Bumping package-lock.json to ${props.version} by execing: npm i in ${path.join(__dirname, '../..')}`)
        const stdout = execSync(command, { cwd: path.join(__dirname, '../..') })
        console.log(stdout.toString())
    } catch (err) {
        console.log(`${command} failed, error: ${err.message}`)
        process.exit(1)
    }
}

Where do you install child-process for this script? Does the script have to have a package.json?

In my opinion, it would be better if the script could be written in bash and executed.

@TriPSs
Copy link
Owner

TriPSs commented May 3, 2024

child_process is something from node it self, no need to install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants