Daily work will encounter the release, management of NPM package situation, here record the standard process, to share with you.
Write code and submit it
We use Git to manage versions of our code. Once a feature is complete, you need to commit a COMMIT and write the content feed for the update in the commit information. If it is a major or significant release change, it is best to label the current commit.
Tagging is equivalent to creating a label for a submission. The hash is still the commit hash. Tagging can take place at any time, and the current file is not committed.
Git tag -a v0.2-m git tag -a v0.2-m"git tag for version 0.0.2"
Copy the code
You can also tag previous commits, simply by providing a commit hash:
Git tag-a v0.0.34ahHsd837HF9q8u34RFCopy the code
Change the package version number
After committing git, you need to ensure that the current working directory of Git is clean, that is, there are no changed files and no temporary changes. Then run the NPM version command based on the required change version.
Parameter of NPM version
The following parameters can be used.
npm version <new version>
You can specify a version number directly, such as NPM Version 1.0.2.
npm version major | minor | patch
Major, Minor, and Patch indicate the major version, minor version, and patch version respectively. Running the corresponding command adds a version to the corresponding version number. If the version in package.json is version:1.2.3, the version changes to Version: 1.3.3 after you run the NPM version minor command.
npm version from-git
The from-git keyword indicates the version number to get the change from the git commit of the current repository. First, NPM prints out the latest tag using Git Describe. Then get the tag-version-prefix from the NPM configuration, which defaults to V, such as V0.0.5. After removing the prefix from the tag, it is checked, and if it is a valid semver (semantic version), it is the latest version number. Otherwise, the read fails. An error is also reported if the version obtained from the git tag is the same as the existing version, unless NPM version from-git –allow-same-version is used to run it.
The little knowledge
- The Git describe command prints out the current tag.
- If the latest tag refers to the latest commit, the tag is printed directly.
- If the latest tag refers to a non-latest commit, the tag is printed along with the latest Commit hash, and a number is inserted to indicate the number of commit differences between the latest tag and the last commit, as in
1 - gcfe7dff v0.0.7 -
, indicating that the latest tag refers to a commit that is two different from the latest commit.- Tag-version-prefix in NPM config indicates the prefix of the git tag version number. The default value is git tag
v
, can be accessed throughnpm config get tag-version-prefix
Look at it.
After running NPM Version successfully, NPM changes the version number as required and commits the changes to Git.
Publish a package
After the version number is changed, the next step can be released. Run the command:
npm publish
Copy the code
Releasable versions. After publishing, you can use NPM View to view package information.
Add a tag to the package version
In addition to being marked with version, versions of NPM packages are marked with tags. For example, after each package release, NPM will point the latest tag to the latest version, so that the tag can be used to download a version:
npm i package-name@latest
Copy the code
If we want to release a new version with a label other than the default Lastest, but something else, such as alpha, beta, or RC, we can specify a label at release time:
# Release an alpha version
npm publish --tag=alpha
Copy the code
If we can also tag a version after release.
Add a beta tag to version 1.0.0NPM dist- Tag Add [email protected] betaCopy the code
To delete a tag, just run:
npm dist-tag rm package-name beta
Copy the code
Look at all the labels
npm dist-tag ls package-name
Copy the code
At this point, the process of publishing an NPM package is complete.
Cancel the release
If you want to unpublish a package after it has been published, you can run unpublish, generally with the –force option:
npm unpublish --force
Copy the code
NPM version running process
This part is divided into extended content.
When running the version command, NPM looks for three commands in the script property of package.json:
preversion
version
postversion
If corresponding commands exist, they are executed at appropriate times throughout NPM version
execution. With our custom script command, the whole process of changing the version number is as follows:
- Make sure your current Git workspace is clean, otherwise an error message will appear. If you want to skip the check and force a version change, add this parameter
--force
Parameters. - perform
preversion
Command. In this command, you can make some changes to the current file. Note thatYou need to add the changed files to git staging. - Change the version number in package.json based on the current version number parameter, for example, change the version number in package.json
1.0.0
Changed to:1.0.1
。 - Then perform
version
Command. In this command, you can also make some changes to the current file. Note thatYou need to add the changed files to git staging. - run
git add package.json
Command and commit. - Label the current submission with a label name
npm.config.tagVersionPrefix + version
Note that in the above steps, if you make changes to local files in your script, you need to add them to the staging area using git add so that NPM will commit them during the commit operation.
Managing Git Tags
This part is divided into extended content.
Here are the basics of Git tag manipulation.
Add tags
Add lightweight labels:
git tag <tag name>
Copy the code
Add label
Git tag -a <tag name> -mCopy the code
Push the label to the remote end
git push origin <tag name>
Copy the code
Push all labels to the remote end
git push origin --tags
Copy the code
Remove the label
Delete a local label:
git tag -d <tag name>
Copy the code
Delete the label for the remote repository
git push origin :refs/tags/<tag name>
Copy the code
This command pushes an empty pointer to the specified label on the remote end.
View a specific label
git show <tag name>
Copy the code
Thank you for reading to the end, and I’m glad if the article was of any help to you. Feel useless or write well, request direct criticism ~
If you have any questions, welcome to discuss ~~
This post was first posted on my blog