@znemz/build-info 0.1.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devcontainer/devcontainer.json +30 -0
- package/.editorconfig +13 -0
- package/.github/FUNDING.yml +1 -0
- package/.github/workflows/commitlint.yml +32 -0
- package/.github/workflows/publish.yml +27 -0
- package/.github/workflows/release.yml +52 -0
- package/.github/workflows/tests.yml +23 -0
- package/.prettierrc.js +8 -0
- package/.travis.yml +23 -0
- package/CHANGELOG.md +50 -0
- package/README.md +3 -3
- package/commitlint.config.js +1 -0
- package/examples/basic.js +29 -0
- package/package.json +37 -54
- package/src/index.js +60 -52
- package/src/index.spec.js +53 -0
- package/lib/index.js +0 -123
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
|
2
|
+
// README at: https://github.com/devcontainers/templates/tree/main/src/go
|
|
3
|
+
{
|
|
4
|
+
"name": "Go",
|
|
5
|
+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
|
6
|
+
"image": "mcr.microsoft.com/devcontainers/go:1-1.23-bullseye",
|
|
7
|
+
"features": {
|
|
8
|
+
"ghcr.io/devcontainers/features/node:1": {
|
|
9
|
+
"nodeGypDependencies": true,
|
|
10
|
+
"version": "20.18.3",
|
|
11
|
+
"nvmVersion": "latest"
|
|
12
|
+
},
|
|
13
|
+
"ghcr.io/devcontainers/features/github-cli:1": {}
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
// Features to add to the dev container. More info: https://containers.dev/features.
|
|
17
|
+
// "features": {},
|
|
18
|
+
|
|
19
|
+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
|
20
|
+
// "forwardPorts": [],
|
|
21
|
+
|
|
22
|
+
// Use 'postCreateCommand' to run commands after the container is created.
|
|
23
|
+
"postCreateCommand": "npm install -g npm@8"
|
|
24
|
+
|
|
25
|
+
// Configure tool-specific properties.
|
|
26
|
+
// "customizations": {},
|
|
27
|
+
|
|
28
|
+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
|
29
|
+
// "remoteUser": "root"
|
|
30
|
+
}
|
package/.editorconfig
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
github: [nmccready, brickhouse-tech]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: commitlint
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_call:
|
|
5
|
+
push:
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: ["main"]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
commitlint:
|
|
11
|
+
runs-on: ubuntu-22.04
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
with:
|
|
15
|
+
fetch-depth: 0
|
|
16
|
+
- uses: actions/setup-node@v3
|
|
17
|
+
with:
|
|
18
|
+
node-version: 20
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
run: npm install
|
|
21
|
+
- name: Print versions
|
|
22
|
+
run: |
|
|
23
|
+
git --version
|
|
24
|
+
node --version
|
|
25
|
+
npm --version
|
|
26
|
+
npx commitlint --version
|
|
27
|
+
- name: Validate current commit (last commit) with commitlint
|
|
28
|
+
if: github.event_name == 'push'
|
|
29
|
+
run: npx commitlint --last --verbose
|
|
30
|
+
- name: Validate PR commits with commitlint
|
|
31
|
+
if: github.event_name == 'pull_request'
|
|
32
|
+
run: npx commitlint --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
tests:
|
|
10
|
+
uses: ./.github/workflows/tests.yml
|
|
11
|
+
publish-npm:
|
|
12
|
+
needs: [tests]
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
permissions:
|
|
15
|
+
id-token: write
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- name: Use Node.js
|
|
19
|
+
uses: actions/setup-node@v6
|
|
20
|
+
with:
|
|
21
|
+
node-version: '20.x'
|
|
22
|
+
registry-url: 'https://registry.npmjs.org'
|
|
23
|
+
- name: Publish to npm
|
|
24
|
+
run: |
|
|
25
|
+
npm install -g npm@11
|
|
26
|
+
npm ci
|
|
27
|
+
npm publish --access public
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["main"]
|
|
6
|
+
paths-ignore:
|
|
7
|
+
- '.devcontainer/**'
|
|
8
|
+
tags-ignore: ['**']
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
tests:
|
|
12
|
+
uses: ./.github/workflows/tests.yml
|
|
13
|
+
tag-release:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
needs: [tests]
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
with:
|
|
19
|
+
token: ${{ secrets.GH_TOKEN }}
|
|
20
|
+
fetch-depth: 0
|
|
21
|
+
- name: Use Node.js
|
|
22
|
+
uses: actions/setup-node@v3
|
|
23
|
+
with:
|
|
24
|
+
node-version: '20.x'
|
|
25
|
+
- name: tag release
|
|
26
|
+
run: |
|
|
27
|
+
if [[ $(git log -1 --pretty=%B) =~ ^chore\(release\):.* ]]; then
|
|
28
|
+
echo "Commit message starts with 'chore(release):', skipping release"
|
|
29
|
+
exit 0
|
|
30
|
+
fi
|
|
31
|
+
|
|
32
|
+
git config --local user.email "creadbot@github.com"
|
|
33
|
+
git config --local user.name "creadbot_github"
|
|
34
|
+
|
|
35
|
+
npm install
|
|
36
|
+
|
|
37
|
+
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
|
|
38
|
+
if [ -n "$LAST_TAG" ]; then
|
|
39
|
+
RANGE="$LAST_TAG..HEAD"
|
|
40
|
+
else
|
|
41
|
+
RANGE="HEAD"
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
if git log $RANGE --format="%B" | grep -qE "(^[a-z]+!:|BREAKING CHANGE:)"; then
|
|
45
|
+
echo "Breaking change detected, forcing major version bump"
|
|
46
|
+
npx commit-and-tag-version --release-as major
|
|
47
|
+
else
|
|
48
|
+
npx commit-and-tag-version
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
git push
|
|
52
|
+
git push --tags
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
name: tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_call:
|
|
5
|
+
push:
|
|
6
|
+
branches: ["main"]
|
|
7
|
+
pull_request:
|
|
8
|
+
branches: ["main"]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
node-version: ['20.x', '22.x', '24.x']
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
19
|
+
uses: actions/setup-node@v3
|
|
20
|
+
with:
|
|
21
|
+
node-version: ${{ matrix.node-version }}
|
|
22
|
+
- run: npm install
|
|
23
|
+
- run: npm test
|
package/.prettierrc.js
ADDED
package/.travis.yml
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
sudo: false
|
|
2
|
+
|
|
3
|
+
language: node_js
|
|
4
|
+
|
|
5
|
+
node_js:
|
|
6
|
+
- '10'
|
|
7
|
+
- '8'
|
|
8
|
+
|
|
9
|
+
cache:
|
|
10
|
+
directories:
|
|
11
|
+
- node_modules
|
|
12
|
+
|
|
13
|
+
install:
|
|
14
|
+
- npm install -g yarn@1.16
|
|
15
|
+
- yarn
|
|
16
|
+
|
|
17
|
+
after_script:
|
|
18
|
+
- yarn test:ci
|
|
19
|
+
- yarn coveralls
|
|
20
|
+
|
|
21
|
+
branches:
|
|
22
|
+
except:
|
|
23
|
+
- /^v[0-9]/
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
## [1.0.3](https://github.com/brickhouse-tech/build-info/compare/v1.0.2...v1.0.3) (2026-03-15)
|
|
6
|
+
|
|
7
|
+
## [1.0.2](https://github.com/brickhouse-tech/build-info/compare/v1.0.1...v1.0.2) (2026-02-25)
|
|
8
|
+
|
|
9
|
+
## [1.0.1](https://github.com/brickhouse-tech/build-info/compare/v1.0.0...v1.0.1) (2026-02-24)
|
|
10
|
+
|
|
11
|
+
## [1.0.0](https://github.com/brickhouse-tech/build-info/compare/v0.1.1...v1.0.0) (2026-02-24)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### ⚠ BREAKING CHANGES
|
|
15
|
+
|
|
16
|
+
* Requires Node >= 18
|
|
17
|
+
|
|
18
|
+
Removed:
|
|
19
|
+
- esm (replaced by native ES modules via "type": "module")
|
|
20
|
+
- babel (all of it — @babel/cli, core, node, preset-env, runtime, plugin-transform-runtime)
|
|
21
|
+
- gulp + gulp-babel + gulp-eslint (no build step needed)
|
|
22
|
+
- bluebird (replaced by native Promise/async-await)
|
|
23
|
+
- lodash (replaced by native Object methods)
|
|
24
|
+
- macos-release (use os.platform() + os.release() directly)
|
|
25
|
+
- os-name (same)
|
|
26
|
+
- coveralls (eliminated last unfixable CVE)
|
|
27
|
+
- jest + jest-cli + jest-extended + jest-expect-message (replaced by vitest)
|
|
28
|
+
- eslint-watch + babel-eslint + eslint-config-gulp + eslint plugins
|
|
29
|
+
- del, sort-package-json, esm
|
|
30
|
+
- yarn.lock (migrated to npm)
|
|
31
|
+
|
|
32
|
+
Added:
|
|
33
|
+
- simple-git v3 (was v1)
|
|
34
|
+
- vitest (modern test runner with native ESM support)
|
|
35
|
+
|
|
36
|
+
Results:
|
|
37
|
+
- 0 CVEs (was 22)
|
|
38
|
+
- 2 dependencies (was 14 + 24 devDeps)
|
|
39
|
+
- 54 packages total (was 908)
|
|
40
|
+
- 6 tests passing
|
|
41
|
+
- No build step required (ship source directly)
|
|
42
|
+
|
|
43
|
+
### Features
|
|
44
|
+
|
|
45
|
+
* modernize to native ES modules, npm, zero CVEs ([64968a7](https://github.com/brickhouse-tech/build-info/commit/64968a7e096a70fbb2e08a5b73bbdcadea527c64))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Bug Fixes
|
|
49
|
+
|
|
50
|
+
* **security:** resolve 21 of 22 CVE vulnerabilities ([7443e08](https://github.com/brickhouse-tech/build-info/commit/7443e083bffbe47cd536e8f49c05a919f3bb87ad))
|
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ buildInfoAsync({
|
|
|
43
43
|
/*
|
|
44
44
|
{ pack: { name: '@znemz/build-info', version: '0.0.1' },
|
|
45
45
|
git:
|
|
46
|
-
{ branch: '
|
|
46
|
+
{ branch: 'main',
|
|
47
47
|
commit: '7f84cea66d5faf8c14000bd34a130ce730f68bde',
|
|
48
48
|
tag: TagList { latest: undefined, all: [] } },
|
|
49
49
|
build: 2019-06-05T20:54:50.983Z,
|
|
@@ -83,7 +83,7 @@ Returns the git current commit, branch, and tag info.
|
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
85
|
getGitAsync().then(console.log);
|
|
86
|
-
// { branch: '
|
|
86
|
+
// { branch: 'main',
|
|
87
87
|
// commit: '7f84cea66d5faf8c14000bd34a130ce730f68bde',
|
|
88
88
|
// tag: TagList { latest: undefined, all: [] } }
|
|
89
89
|
```
|
|
@@ -118,4 +118,4 @@ getOS();
|
|
|
118
118
|
[travis-image]: https://img.shields.io/travis/nmccready/build-info.svg
|
|
119
119
|
[travis-url]: https://travis-ci.org/nmccready/build-info
|
|
120
120
|
[coveralls-image]: https://coveralls.io/repos/github/nmccready/build-info/badge.svg
|
|
121
|
-
[coveralls-url]: https://coveralls.io/github/nmccready/build-info?branch=
|
|
121
|
+
[coveralls-url]: https://coveralls.io/github/nmccready/build-info?branch=main
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default { extends: ['@commitlint/config-conventional'] };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import buildInfoAsync, { getGitAsync, getOS, getPackage } from '../src/index.js';
|
|
4
|
+
|
|
5
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
|
|
7
|
+
const bar = '--------';
|
|
8
|
+
const barThing = (str) => console.log(`\n${bar} ${str} ${bar}\n`);
|
|
9
|
+
|
|
10
|
+
const main = async () => {
|
|
11
|
+
const result = await buildInfoAsync({
|
|
12
|
+
build: [path.join(__dirname, '..', 'src', 'index.js')],
|
|
13
|
+
pack: [path.join(__dirname, '..', 'package.json')],
|
|
14
|
+
});
|
|
15
|
+
barThing('buildInfoAsync');
|
|
16
|
+
console.log(result);
|
|
17
|
+
|
|
18
|
+
const gitInfo = await getGitAsync();
|
|
19
|
+
barThing('getGitAsync');
|
|
20
|
+
console.log(gitInfo);
|
|
21
|
+
|
|
22
|
+
barThing('getOS');
|
|
23
|
+
console.log(getOS());
|
|
24
|
+
|
|
25
|
+
barThing('getPackage');
|
|
26
|
+
console.log(getPackage(path.join(__dirname, '..', 'package.json')));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
main().catch(console.error);
|
package/package.json
CHANGED
|
@@ -1,63 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@znemz/build-info",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"src/*.js",
|
|
11
|
-
"!src/*.spec.js"
|
|
12
|
-
],
|
|
13
|
-
"main": "lib/index.js",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Get build info (git, os, package) for your app",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
14
10
|
"scripts": {
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"test": "yarn jest",
|
|
25
|
-
"test:ci": "yarn test --coverage",
|
|
26
|
-
"preversion": "yarn test && yarn build"
|
|
11
|
+
"test": "vitest run",
|
|
12
|
+
"test:watch": "vitest",
|
|
13
|
+
"test:coverage": "vitest run --coverage",
|
|
14
|
+
"example": "node examples/basic.js",
|
|
15
|
+
"lint": "eslint src",
|
|
16
|
+
"prepublishOnly": "npm test"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=20"
|
|
27
20
|
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/brickhouse-tech/build-info"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"build",
|
|
27
|
+
"info",
|
|
28
|
+
"git",
|
|
29
|
+
"os",
|
|
30
|
+
"version"
|
|
31
|
+
],
|
|
32
|
+
"author": "Nick McCready",
|
|
33
|
+
"license": "MIT",
|
|
28
34
|
"dependencies": {
|
|
29
|
-
"
|
|
30
|
-
"eslint": "^5.16.0",
|
|
31
|
-
"eslint-config-gulp": "^3.0.1",
|
|
32
|
-
"lodash": "^4.17.11",
|
|
33
|
-
"macos-release": "^2.2.0",
|
|
34
|
-
"os-name": "^3.1.0",
|
|
35
|
-
"simple-git": "^1.113.0"
|
|
35
|
+
"simple-git": "^3.27.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
"babel-eslint": "^10.0.1",
|
|
46
|
-
"coveralls": "^3.0.4",
|
|
47
|
-
"del": "^4.1.1",
|
|
48
|
-
"eslint-config-prettier": "^4.0.0",
|
|
49
|
-
"eslint-import-resolver-jest": "^2.1.1",
|
|
50
|
-
"eslint-plugin-jest": "^22.3.2",
|
|
51
|
-
"eslint-plugin-prettier": "^3.0.1",
|
|
52
|
-
"eslint-watch": "^5.1.2",
|
|
53
|
-
"esm": "^3.2.22",
|
|
54
|
-
"gulp": "4",
|
|
55
|
-
"gulp-babel": "^8",
|
|
56
|
-
"gulp-eslint": "^4",
|
|
57
|
-
"jest": "^24",
|
|
58
|
-
"jest-cli": "^24",
|
|
59
|
-
"jest-expect-message": "^1.0.2",
|
|
60
|
-
"jest-extended": "^0.11.1",
|
|
61
|
-
"sort-package-json": "^1.21.0"
|
|
38
|
+
"@commitlint/cli": "^20.4.2",
|
|
39
|
+
"@commitlint/config-conventional": "^20.4.2",
|
|
40
|
+
"commit-and-tag-version": "^12.6.1",
|
|
41
|
+
"vitest": "^3.0.0"
|
|
42
|
+
},
|
|
43
|
+
"overrides": {
|
|
44
|
+
"minimatch": ">=10.2.1"
|
|
62
45
|
}
|
|
63
46
|
}
|
package/src/index.js
CHANGED
|
@@ -1,68 +1,76 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import _fs from 'fs';
|
|
6
|
-
import os from 'os';
|
|
7
|
-
import macosRelease from 'macos-release';
|
|
1
|
+
import { simpleGit } from 'simple-git';
|
|
2
|
+
import { stat, readFile } from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
8
5
|
|
|
9
|
-
export const git =
|
|
6
|
+
export const git = simpleGit();
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
return `${name} ${release}`;
|
|
8
|
+
/**
|
|
9
|
+
* Get OS name and release info
|
|
10
|
+
*/
|
|
11
|
+
export const getOS = () => {
|
|
12
|
+
const platform = os.platform();
|
|
13
|
+
const release = os.release();
|
|
14
|
+
const arch = os.arch();
|
|
15
|
+
return `${platform} ${release} (${arch})`;
|
|
20
16
|
};
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}).then((all) => {
|
|
33
|
-
const { commit } = all;
|
|
34
|
-
return props({ ...all, tag: git.tagsAsync({ '--contains': commit }) });
|
|
35
|
-
});
|
|
18
|
+
/**
|
|
19
|
+
* Get git branch, commit, and tag info
|
|
20
|
+
*/
|
|
21
|
+
export const getGitAsync = async () => {
|
|
22
|
+
const { current: branch } = await git.branch();
|
|
23
|
+
const log = await git.log({ maxCount: 1 });
|
|
24
|
+
const commit = log.latest?.hash ?? '';
|
|
25
|
+
const tags = await git.tags({ '--contains': commit });
|
|
26
|
+
return { branch, commit, tag: tags };
|
|
27
|
+
};
|
|
36
28
|
|
|
37
29
|
/**
|
|
38
|
-
*
|
|
39
|
-
* @param
|
|
40
|
-
*
|
|
41
|
-
* mind that if you are exposing dependencies it is a security risk and
|
|
42
|
-
* thus that endpoint should only available to admins.
|
|
30
|
+
* Read and pick fields from a package.json file
|
|
31
|
+
* @param {string} filePath - Path to package.json
|
|
32
|
+
* @param {string[]} picks - Fields to include (default: name, version)
|
|
43
33
|
*/
|
|
44
|
-
export const getPackage = (filePath, picks = ['name', 'version']) =>
|
|
45
|
-
|
|
34
|
+
export const getPackage = (filePath, picks = ['name', 'version']) => {
|
|
35
|
+
const require = createRequire(import.meta.url);
|
|
36
|
+
const pkg = require(filePath);
|
|
37
|
+
const result = {};
|
|
38
|
+
for (const key of picks) {
|
|
39
|
+
if (key in pkg) {
|
|
40
|
+
result[key] = pkg[key];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
|
-
*
|
|
47
|
+
* Get the birthtime of a file
|
|
48
|
+
* @param {string} filePath
|
|
49
49
|
*/
|
|
50
|
-
export const getBuildTimeAsync = (filePath) => {
|
|
51
|
-
|
|
50
|
+
export const getBuildTimeAsync = async (filePath) => {
|
|
51
|
+
const stats = await stat(filePath);
|
|
52
|
+
return stats.birthtime;
|
|
52
53
|
};
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @param
|
|
57
|
-
* @param
|
|
58
|
-
* @param
|
|
56
|
+
* Get all build info
|
|
57
|
+
* @param {Object} options
|
|
58
|
+
* @param {Array} options.pack - [filePath, picks] for getPackage
|
|
59
|
+
* @param {Array} options.build - [filePath] for getBuildTimeAsync
|
|
59
60
|
*/
|
|
60
|
-
const buildInfoAsync = ({ pack = [], build = []
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const buildInfoAsync = async ({ pack = [], build = [] }) => {
|
|
62
|
+
const [packResult, gitResult, buildResult] = await Promise.all([
|
|
63
|
+
Promise.resolve(getPackage(...pack)),
|
|
64
|
+
getGitAsync(),
|
|
65
|
+
getBuildTimeAsync(...build),
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
pack: packResult,
|
|
70
|
+
git: gitResult,
|
|
71
|
+
build: buildResult,
|
|
72
|
+
os: getOS(),
|
|
73
|
+
};
|
|
74
|
+
};
|
|
67
75
|
|
|
68
76
|
export default buildInfoAsync;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import buildInfoAsync, { getGitAsync, git, getPackage, getBuildTimeAsync } from './index.js';
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
describe('buildInfoAsync', () => {
|
|
9
|
+
it('should return all build info', async () => {
|
|
10
|
+
const { build, os, git, pack } = await buildInfoAsync({
|
|
11
|
+
pack: [path.join(__dirname, '..', 'package.json')],
|
|
12
|
+
build: [path.join(__dirname, '..', 'package.json')],
|
|
13
|
+
});
|
|
14
|
+
expect(build).toBeDefined();
|
|
15
|
+
expect(os).toBeDefined();
|
|
16
|
+
expect(git).toBeDefined();
|
|
17
|
+
expect(pack).toBeDefined();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should fail with empty options', async () => {
|
|
21
|
+
await expect(buildInfoAsync({})).rejects.toThrow();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('getGitAsync', () => {
|
|
26
|
+
it('should return branch, commit, and tag', async () => {
|
|
27
|
+
const { branch, commit, tag } = await getGitAsync();
|
|
28
|
+
const { current } = await git.branch();
|
|
29
|
+
expect(branch).toEqual(current);
|
|
30
|
+
expect(commit).toBeTruthy();
|
|
31
|
+
expect(tag).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('getPackage', () => {
|
|
36
|
+
it('should return default fields (name, version)', () => {
|
|
37
|
+
const obj = getPackage(path.join(__dirname, '..', 'package.json'));
|
|
38
|
+
expect(Object.keys(obj)).toEqual(['name', 'version']);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should return override fields', () => {
|
|
42
|
+
const picks = ['scripts'];
|
|
43
|
+
const obj = getPackage(path.join(__dirname, '..', 'package.json'), picks);
|
|
44
|
+
expect(Object.keys(obj)).toEqual(picks);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('getBuildTimeAsync', () => {
|
|
49
|
+
it('should return a Date', async () => {
|
|
50
|
+
const ret = await getBuildTimeAsync(path.join(__dirname, '..', 'package.json'));
|
|
51
|
+
expect(ret.toUTCString()).toMatch(/.*GMT/);
|
|
52
|
+
});
|
|
53
|
+
});
|
package/lib/index.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports["default"] = exports.getBuildTimeAsync = exports.getPackage = exports.getGitAsync = exports.getOS = exports.git = void 0;
|
|
9
|
-
|
|
10
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
|
|
11
|
-
|
|
12
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
13
|
-
|
|
14
|
-
var _simpleGit = _interopRequireDefault(require("simple-git"));
|
|
15
|
-
|
|
16
|
-
var _bluebird = require("bluebird");
|
|
17
|
-
|
|
18
|
-
var _osName = _interopRequireDefault(require("os-name"));
|
|
19
|
-
|
|
20
|
-
var _lodash = require("lodash");
|
|
21
|
-
|
|
22
|
-
var _fs2 = _interopRequireDefault(require("fs"));
|
|
23
|
-
|
|
24
|
-
var _os = _interopRequireDefault(require("os"));
|
|
25
|
-
|
|
26
|
-
var _macosRelease = _interopRequireDefault(require("macos-release"));
|
|
27
|
-
|
|
28
|
-
var git = (0, _bluebird.promisifyAll)((0, _simpleGit["default"])());
|
|
29
|
-
exports.git = git;
|
|
30
|
-
var fs = (0, _bluebird.promisifyAll)(_fs2["default"]);
|
|
31
|
-
|
|
32
|
-
var getOS = function getOS() {
|
|
33
|
-
var osNameArgs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
34
|
-
var release = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _os["default"].release();
|
|
35
|
-
|
|
36
|
-
var name = _osName["default"].apply(void 0, (0, _toConsumableArray2["default"])(osNameArgs));
|
|
37
|
-
|
|
38
|
-
var shortName = (0, _lodash.first)(name.split(' '));
|
|
39
|
-
|
|
40
|
-
if (['macOS', 'OS X'].indexOf(shortName) >= 0) {
|
|
41
|
-
release = "".concat((0, _macosRelease["default"])(release).version, ", Darwin: ").concat(release);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return "".concat(name, " ").concat(release);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
exports.getOS = getOS;
|
|
48
|
-
|
|
49
|
-
var getGitAsync = function getGitAsync() {
|
|
50
|
-
return (// get all git, and build info and output it to a static json file
|
|
51
|
-
(0, _bluebird.props)({
|
|
52
|
-
branch: git.branchAsync().then(function (_ref) {
|
|
53
|
-
var current = _ref.current;
|
|
54
|
-
return current;
|
|
55
|
-
}),
|
|
56
|
-
commit: git.showAsync().then(function (ret) {
|
|
57
|
-
return ret.split(' ')[1].replace('commit ', '').replace(/\n.*/g, '');
|
|
58
|
-
})
|
|
59
|
-
}).then(function (all) {
|
|
60
|
-
var commit = all.commit;
|
|
61
|
-
return (0, _bluebird.props)((0, _objectSpread2["default"])({}, all, {
|
|
62
|
-
tag: git.tagsAsync({
|
|
63
|
-
'--contains': commit
|
|
64
|
-
})
|
|
65
|
-
}));
|
|
66
|
-
})
|
|
67
|
-
);
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* @param {String} filePath
|
|
71
|
-
* @param {Array<String>} picks - array of fields allowed to be returned,
|
|
72
|
-
* important for security to return a minimum amount of fields. Keep in
|
|
73
|
-
* mind that if you are exposing dependencies it is a security risk and
|
|
74
|
-
* thus that endpoint should only available to admins.
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
exports.getGitAsync = getGitAsync;
|
|
79
|
-
|
|
80
|
-
var getPackage = function getPackage(filePath) {
|
|
81
|
-
var picks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['name', 'version'];
|
|
82
|
-
return (0, _lodash.pick)(require(filePath), picks);
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* @param {String} filePath
|
|
86
|
-
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
exports.getPackage = getPackage;
|
|
90
|
-
|
|
91
|
-
var getBuildTimeAsync = function getBuildTimeAsync(filePath) {
|
|
92
|
-
return fs.statAsync(filePath).then(function (_ref2) {
|
|
93
|
-
var birthtime = _ref2.birthtime;
|
|
94
|
-
return birthtime;
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* @param {Object} options
|
|
99
|
-
* @param {Array} options.pack - getBuildTimeAsync args
|
|
100
|
-
* @param {Array} options.build - getBuildTimeAsync args
|
|
101
|
-
* @param {Array} options.os - getOs args
|
|
102
|
-
*/
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
exports.getBuildTimeAsync = getBuildTimeAsync;
|
|
106
|
-
|
|
107
|
-
var buildInfoAsync = function buildInfoAsync(_ref3) {
|
|
108
|
-
var _ref3$pack = _ref3.pack,
|
|
109
|
-
pack = _ref3$pack === void 0 ? [] : _ref3$pack,
|
|
110
|
-
_ref3$build = _ref3.build,
|
|
111
|
-
build = _ref3$build === void 0 ? [] : _ref3$build,
|
|
112
|
-
_ref3$os = _ref3.os,
|
|
113
|
-
os = _ref3$os === void 0 ? [] : _ref3$os;
|
|
114
|
-
return (0, _bluebird.props)({
|
|
115
|
-
pack: getPackage.apply(void 0, (0, _toConsumableArray2["default"])(pack)),
|
|
116
|
-
git: getGitAsync(),
|
|
117
|
-
build: getBuildTimeAsync.apply(void 0, (0, _toConsumableArray2["default"])(build)),
|
|
118
|
-
os: getOS.apply(void 0, (0, _toConsumableArray2["default"])(os))
|
|
119
|
-
});
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
var _default = buildInfoAsync;
|
|
123
|
-
exports["default"] = _default;
|