Skip to content

Commit ac4e542

Browse files
committed
refactor(cli): extract duplicated getVersion() to shared utility
1 parent dd9c9f9 commit ac4e542

22 files changed

Lines changed: 39 additions & 287 deletions

packages/cli/src/commands/breaking.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { readFileSync } from 'node:fs';
21
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import { extract } from '@openpkg-ts/sdk';
53
import { categorizeBreakingChanges, diffSpec, normalize } from '@openpkg-ts/spec';
64
import type { Command } from 'commander';
@@ -10,20 +8,9 @@ import { extractSpecFromRef } from '../utils/git-extract';
108
import { formatError, formatOutput } from '../utils/output';
119
import { shouldRenderHuman } from '../utils/render';
1210
import { resolveSpecs } from '../utils/resolve-specs';
11+
import { getVersion } from '../utils/version';
1312
import { discoverPackages, filterPublic } from '../utils/workspaces';
1413

15-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
16-
17-
function getVersion(): string {
18-
try {
19-
return (
20-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
21-
);
22-
} catch {
23-
return '0.0.0';
24-
}
25-
}
26-
2714
export function registerBreakingCommand(program: Command): void {
2815
program
2916
.command('breaking [old] [new]')

packages/cli/src/commands/cache.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
import { readFileSync } from 'node:fs';
2-
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
41
import type { Command } from 'commander';
52
import { clearCache, getCacheStatus } from '../cache/spec-cache';
63
import { renderCacheClear, renderCacheStatus } from '../formatters/cache';
74
import { formatOutput } from '../utils/output';
8-
9-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
10-
11-
function getVersion(): string {
12-
try {
13-
return (
14-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
15-
);
16-
} catch {
17-
return '0.0.0';
18-
}
19-
}
5+
import { getVersion } from '../utils/version';
206

217
export function registerCacheCommand(program: Command): void {
228
const cache = program.command('cache').description('Manage spec extraction cache');

packages/cli/src/commands/changelog.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
import { readFileSync } from 'node:fs';
2-
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
41
import { categorizeBreakingChanges, diffSpec, recommendSemverBump } from '@openpkg-ts/spec';
52
import type { Command } from 'commander';
63
import { renderChangelog } from '../formatters/changelog';
74
import { formatError, formatOutput } from '../utils/output';
85
import { shouldRenderHuman } from '../utils/render';
96
import { resolveSpecs } from '../utils/resolve-specs';
10-
11-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
12-
13-
function getVersion(): string {
14-
try {
15-
return (
16-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
17-
);
18-
} catch {
19-
return '0.0.0';
20-
}
21-
}
7+
import { getVersion } from '../utils/version';
228

239
function generateMarkdown(
2410
breaking: { name: string; reason: string; severity: string }[],

packages/cli/src/commands/ci.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { execSync } from 'node:child_process';
22
import { existsSync, readFileSync } from 'node:fs';
33
import * as path from 'node:path';
4-
import { fileURLToPath } from 'node:url';
54
import { computeDrift } from '@driftdev/sdk';
65
import { categorizeBreakingChanges, diffSpec } from '@openpkg-ts/spec';
76
import type { Command } from 'commander';
@@ -20,20 +19,9 @@ import {
2019
import { appendHistory, readHistory } from '../utils/history';
2120
import { formatError, formatOutput, type OutputNext } from '../utils/output';
2221
import { computeRatchetMin } from '../utils/ratchet';
22+
import { getVersion } from '../utils/version';
2323
import { detectWorkspaces, resolveGlobs } from '../utils/workspaces';
2424

25-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
26-
27-
function getVersion(): string {
28-
try {
29-
return (
30-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
31-
);
32-
} catch {
33-
return '0.0.0';
34-
}
35-
}
36-
3725
interface PackageResult {
3826
name: string;
3927
coverage: number;

packages/cli/src/commands/config.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
22
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
43
import type { Command } from 'commander';
54
import { getGlobalConfigPath, getGlobalDir } from '../config/global';
65
import { loadConfig } from '../config/loader';
76
import { renderConfigGet, renderConfigList } from '../formatters/config';
87
import { formatError, formatOutput } from '../utils/output';
9-
10-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
11-
12-
function getVersion(): string {
13-
try {
14-
return (
15-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
16-
);
17-
} catch {
18-
return '0.0.0';
19-
}
20-
}
8+
import { getVersion } from '../utils/version';
219

2210
function getNestedValue(obj: Record<string, unknown>, keyPath: string): unknown {
2311
const keys = keyPath.split('.');

packages/cli/src/commands/context.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { execSync } from 'node:child_process';
22
import { readFileSync } from 'node:fs';
33
import * as path from 'node:path';
4-
import { fileURLToPath } from 'node:url';
54
import { computeDrift } from '@driftdev/sdk';
65
import type { Command } from 'commander';
76
import { cachedExtract } from '../cache/cached-extract';
@@ -16,20 +15,9 @@ import {
1615
import { detectEntry } from '../utils/detect-entry';
1716
import { readHistory } from '../utils/history';
1817
import { formatError, formatOutput } from '../utils/output';
18+
import { getVersion } from '../utils/version';
1919
import { discoverPackages, filterPublic } from '../utils/workspaces';
2020

21-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
22-
23-
function getVersion(): string {
24-
try {
25-
return (
26-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
27-
);
28-
} catch {
29-
return '0.0.0';
30-
}
31-
}
32-
3321
function getCommitSha(): string | null {
3422
try {
3523
return execSync('git rev-parse --short HEAD', { encoding: 'utf-8', timeout: 5000 }).trim();

packages/cli/src/commands/coverage.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { readFileSync } from 'node:fs';
21
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import type { Command } from 'commander';
53
import { cachedExtract } from '../cache/cached-extract';
64
import { loadConfig } from '../config/loader';
@@ -10,20 +8,9 @@ import { detectEntry } from '../utils/detect-entry';
108
import { formatError, formatOutput, type OutputNext } from '../utils/output';
119
import { computeRatchetMin } from '../utils/ratchet';
1210
import { shouldRenderHuman } from '../utils/render';
11+
import { getVersion } from '../utils/version';
1312
import { discoverPackages, filterPublic } from '../utils/workspaces';
1413

15-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
16-
17-
function getVersion(): string {
18-
try {
19-
return (
20-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
21-
);
22-
} catch {
23-
return '0.0.0';
24-
}
25-
}
26-
2714
export function registerCoverageCommand(program: Command): void {
2815
program
2916
.command('coverage [entry]')

packages/cli/src/commands/diff.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { readFileSync } from 'node:fs';
21
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import { extract } from '@openpkg-ts/sdk';
53
import { categorizeBreakingChanges, diffSpec, normalize } from '@openpkg-ts/spec';
64
import type { Command } from 'commander';
@@ -10,20 +8,9 @@ import { extractSpecFromRef } from '../utils/git-extract';
108
import { formatError, formatOutput } from '../utils/output';
119
import { shouldRenderHuman } from '../utils/render';
1210
import { resolveSpecs } from '../utils/resolve-specs';
11+
import { getVersion } from '../utils/version';
1312
import { discoverPackages, filterPublic } from '../utils/workspaces';
1413

15-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
16-
17-
function getVersion(): string {
18-
try {
19-
return (
20-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
21-
);
22-
} catch {
23-
return '0.0.0';
24-
}
25-
}
26-
2714
export function registerDiffCommand(program: Command): void {
2815
program
2916
.command('diff [old] [new]')

packages/cli/src/commands/examples.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { readFileSync } from 'node:fs';
22
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
43
import type { ExampleValidation, ExampleValidationResult } from '@driftdev/sdk';
54
import { validateExamples } from '@driftdev/sdk';
65
import type { Command } from 'commander';
@@ -10,20 +9,9 @@ import { renderBatchExamples } from '../formatters/batch';
109
import { renderExamples } from '../formatters/examples';
1110
import { detectEntry } from '../utils/detect-entry';
1211
import { formatError, formatOutput, type OutputNext } from '../utils/output';
12+
import { getVersion } from '../utils/version';
1313
import { discoverPackages, filterPublic } from '../utils/workspaces';
1414

15-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
16-
17-
function getVersion(): string {
18-
try {
19-
return (
20-
JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8')).version ?? '0.0.0'
21-
);
22-
} catch {
23-
return '0.0.0';
24-
}
25-
}
26-
2715
function findPackagePath(entryFile: string): string {
2816
let dir = path.dirname(entryFile);
2917
while (dir !== path.dirname(dir)) {

packages/cli/src/commands/extract.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
import { readFileSync } from 'node:fs';
21
import * as path from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import { Drift } from '@driftdev/sdk';
53
import { normalize } from '@openpkg-ts/spec';
64
import type { Command } from 'commander';
75
import { cachedExtract } from '../cache/cached-extract';
86
import { renderExtract } from '../formatters/extract';
97
import { detectEntry } from '../utils/detect-entry';
108
import { formatError, formatOutput } from '../utils/output';
9+
import { getVersion } from '../utils/version';
1110
import { discoverPackages, filterPublic } from '../utils/workspaces';
1211

13-
const __filename = fileURLToPath(import.meta.url);
14-
const __dirname = path.dirname(__filename);
15-
16-
function getVersion(): string {
17-
try {
18-
const pkg = JSON.parse(readFileSync(path.join(__dirname, '../package.json'), 'utf-8'));
19-
return pkg.version ?? '0.0.0';
20-
} catch {
21-
return '0.0.0';
22-
}
23-
}
24-
2512
export function registerExtractCommand(program: Command): void {
2613
program
2714
.command('extract [entry]')

0 commit comments

Comments
 (0)