Skip to content

Commit d0a0e1d

Browse files
1 - Initialize Script to auto-version (#2)
* 1 - Initialize script * 1 - Only console log the new version * 1 - Update node version to v23.4.0 to remove warnings * 1 - Update README docs on how to use npm library * 1 - Add initialize-script details * 1 - Update NPM Readme Docs * 1 - error if there's no package.json * 1 - Write tests * 1 - update pr template
1 parent 40bd0e0 commit d0a0e1d

File tree

10 files changed

+324
-20
lines changed

10 files changed

+324
-20
lines changed

.github/pull_request_template.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
# PR
1+
# Pull Request
22

3-
Insert notes here
3+
Add PR details here
4+
5+
## Testing Instructions (or AC)
6+
7+
Add testing instructions or AC here

.github/workflows/deploy.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Publish Package to npmjs
2+
3+
# Trigger the workflow on a new release in Github
4+
on:
5+
release:
6+
types: [published]
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
steps:
15+
- uses: actions/checkout@v4
16+
# Setup .npmrc file to publish to npm
17+
- name: Install Node.js
18+
uses: actions/setup-node@v4
19+
with:
20+
registry-url: "https://registry.npmjs.org"
21+
cache: "npm"
22+
- run: npm ci
23+
- run: npm publish --provenance --access public
24+
env:
25+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Tests Workflow
2+
3+
on:
4+
pull_request:
5+
branches: ["main"]
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Install Node.js
13+
uses: actions/setup-node@v4
14+
with:
15+
cache: "npm"
16+
- run: npm ci
17+
- run: npm run test

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v23.1.0
1+
v23.4.0

README.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
1-
# nodejs
1+
# npm-gitver
22

3-
Nodejs template. Use node version in nvmrc file
3+
`npm-gitver` is an NPM library for managing versioning in your Node.js projects using Git tags.
44

5+
## Installation
6+
7+
Install the library using npm:
8+
9+
```bash
10+
npm install npm-gitver --save-dev
511
```
6-
nvm use
12+
13+
## Usage
14+
15+
### Running via npm script
16+
17+
1. Ensure you are using the correct Node.js version specified in the `.nvmrc` file:
18+
19+
```bash
20+
nvm use
21+
```
22+
23+
2. Add a script in your `package.json` to run `npm-gitver`:
24+
25+
```json
26+
"scripts": {
27+
"version": "npm-gitver"
28+
}
29+
```
30+
31+
3. Run the versioning script:
32+
33+
```bash
34+
npm run version
35+
```
36+
37+
This will automatically update your project version based on Git tags.
38+
39+
### Running directly via CLI
40+
41+
You can also run `npm-gitver` directly using `npx` without adding it to your `package.json`:
42+
43+
```bash
44+
npx npm-gitver
745
```
846

47+
This will execute the package directly from the command line, creating or updating the version based on Git tags.
48+
949
## Linting
1050

11-
https://eslint.org/docs/latest/
51+
Follow best practices for linting your code. Refer to the ESLint documentation for setup and configuration:
52+
53+
[ESLint Documentation](https://eslint.org/docs/latest/)
54+
55+
## Additional Information
56+
57+
For more details on how `npm-gitver` works, refer to the [GitHub repository](https://github.com/your-repo/npm-gitver).

bin/npm-gitver.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env node
2+
3+
const { generateGitVersion } = require('../index');
4+
5+
const version = generateGitVersion();
6+
console.log(version);

index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const fs = require('fs');
2+
const childProcess = require('child_process');
3+
const path = require('path');
4+
5+
function getShortGitSHA() {
6+
return childProcess.execSync('git rev-parse --short HEAD').toString().trim();
7+
}
8+
9+
function readPackageJson() {
10+
const pkgPath = path.resolve(process.cwd(), 'package.json');
11+
if (!fs.existsSync(pkgPath)) {
12+
throw new Error('package.json not found in the current working directory.');
13+
}
14+
const raw = fs.readFileSync(pkgPath);
15+
return JSON.parse(raw);
16+
}
17+
18+
function generateGitVersion() {
19+
const sha = getShortGitSHA();
20+
const pkg = readPackageJson();
21+
22+
if (!pkg.version) {
23+
throw new Error('The "version" key is missing in package.json.');
24+
}
25+
const baseVersion = pkg.version.split('-')[0]; // remove any pre-release suffix
26+
const newVersion = `${baseVersion}-${sha}`;
27+
28+
return newVersion;
29+
}
30+
31+
module.exports = {
32+
generateGitVersion,
33+
};

package-lock.json

Lines changed: 95 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
{
2-
"name": "nodejs",
2+
"name": "npm-gitver",
33
"version": "1.0.0",
4-
"description": "Node template",
4+
"description": "npm gitver is a simple command line tool that allows you to easily manage your git versioning.",
5+
"bin": {
6+
"npm-gitver": "./bin/npm-gitver.js"
7+
},
58
"keywords": [
69
"nodejs",
7-
"javascript"
10+
"javascript",
11+
"git"
812
],
9-
"homepage": "https://github.com/TheBookKnight/nodejs#readme",
13+
"homepage": "https://github.com/TheBookKnight/npm-gitver#readme",
1014
"bugs": {
11-
"url": "https://github.com/TheBookKnight/nodejs/issues"
15+
"url": "https://github.com/TheBookKnight/npm-gitver/issues"
1216
},
1317
"repository": {
1418
"type": "git",
15-
"url": "git+https://github.com/TheBookKnight/nodejs.git"
19+
"url": "git+https://github.com/TheBookKnight/npm-gitver.git"
1620
},
1721
"license": "ISC",
1822
"author": "Joshua Cadavez",
19-
"type": "commonjs",
2023
"main": "index.js",
2124
"scripts": {
2225
"lint": "eslint .",
23-
"test": "echo \"Error: no test specified\" && exit 1"
26+
"test": "node --test"
2427
},
2528
"devDependencies": {
2629
"@eslint/js": "^9.21.0",
2730
"eslint": "^9.21.0",
28-
"globals": "^16.0.0"
31+
"globals": "^16.0.0",
32+
"sinon": "^20.0.0"
2933
}
30-
}
34+
}

0 commit comments

Comments
 (0)