Skip to content

Commit 5585049

Browse files
committed
remove jq and awk dependencies for tool pinning and correct CLI
1 parent 42b0c39 commit 5585049

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

src/scripts/ci-get-version.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// get-version.mjs
2+
import { readFileSync, existsSync } from 'fs';
3+
import { fileURLToPath } from 'url';
4+
import { dirname, join, resolve } from 'path';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
// Function to find repository root (where package.json lives)
10+
function findRepoRoot(startDir) {
11+
let currentDir = startDir;
12+
while (currentDir !== '/') {
13+
const packagePath = join(currentDir, 'package.json');
14+
if (existsSync(packagePath)) {
15+
return currentDir;
16+
}
17+
currentDir = dirname(currentDir);
18+
}
19+
throw new Error('Could not find repository root (no package.json found)');
20+
}
21+
22+
// Get repo root
23+
const repoRoot = findRepoRoot(__dirname);
24+
console.error('Repository root:', repoRoot);
25+
26+
// Get source, name and default version from command line arguments
27+
const [source, name, defaultVersion = '0.5.0'] = process.argv.slice(2);
28+
29+
if (!source || !name) {
30+
console.error('Usage: node get-version.mjs [package|tool] [name] [defaultVersion]');
31+
process.exit(1);
32+
}
33+
34+
try {
35+
if (source === 'package') {
36+
const packagePath = join(repoRoot, 'package.json');
37+
const packageJson = JSON.parse(readFileSync(packagePath, 'utf8'));
38+
const version = packageJson.dependencies?.[name] || defaultVersion;
39+
console.log(version)
40+
} else if (source === 'tool') {
41+
const toolPath = join(repoRoot, '.tool-versions');
42+
const content = readFileSync(toolPath, 'utf8');
43+
const lines = content.split('\n').map(line => line.trim());
44+
const matchingLines = lines.filter(line => line.startsWith(name));
45+
const version = matchingLines[0]?.split(/\s+/)[1] || defaultVersion;
46+
console.log(version)
47+
} else {
48+
console.error('Invalid source. Use "package" or "tool"');
49+
process.exit(1);
50+
}
51+
} catch (error) {
52+
console.log(defaultVersion);
53+
}

src/validations/module.mk

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
# Variables
2-
OSCAL_VERSION = $(shell jq -r .dependencies.oscal package.json)
3-
OSCAL_CLI_VERSION = $(shell awk '/^oscal-cli/ {print $$2}' .tool-versions)
4-
OSCAL_SERVER_VERSION = $(shell awk '/^oscal-server/ {print $$2}' .tool-versions)
1+
2+
# Get tool versions using Node script
3+
OSCAL_VERSION := $(shell node src/scripts/ci-get-version.js package oscal)
4+
OSCAL_CLI_VERSION := $(shell node src/scripts/ci-get-version.js tool oscal-cli)
5+
OSCAL_SERVER_VERSION := $(shell node src/scripts/ci-get-version.js tool oscal-server)
6+
OSCAL_SERVER_PATH := $(shell node -e "console.log(process.cwd())")
7+
8+
# Optional: Add version checking targets
9+
check-versions:
10+
@echo "Using versions:"
11+
@echo "OSCAL CLI: $(OSCAL_CLI_VERSION)"
12+
@echo "OSCAL Server: $(OSCAL_SERVER_VERSION)"
13+
@echo "OSCAL JS: $(OSCAL_VERSION)"
14+
@echo "OSCAL SERVER ALLOWED DIR: $(OSCAL_SERVER_PATH)"
515
OSCAL_CLI = npx oscal@$(OSCAL_VERSION)
616
SRC_DIR = ./src
717
DIST_DIR = ./dist
@@ -14,7 +24,7 @@ init-validations:
1424
@echo "Installing node modules..."
1525
npm install
1626
$(OSCAL_CLI) use $(OSCAL_CLI_VERSION)
17-
$(OSCAL_CLI) server update $(OSCAL_SERVER_VERSION)
27+
$(OSCAL_CLI) server update -t $(OSCAL_SERVER_VERSION)
1828

1929
# Style lint
2030
.PHONY: lint-style
@@ -27,7 +37,7 @@ lint-validations:
2737
build-validations:
2838
@echo "Running Cucumber Tests"
2939
$(OSCAL_CLI) server stop
30-
npx cross-env OSCAL_SERVER_PATH=* $(OSCAL_CLI) server start -bg
40+
npx cross-env OSCAL_SERVER_PATH=$(OSCAL_SERVER_PATH) $(OSCAL_CLI) server start -bg
3141
@npm run test:server
3242
$(OSCAL_CLI) server stop
3343

0 commit comments

Comments
 (0)