Skip to content

Commit 93e5624

Browse files
Attempt to retrieve config from package.json if codehawk.json doesnt exist (#42)
1 parent e9c4787 commit 93e5624

File tree

5 files changed

+98
-14
lines changed

5 files changed

+98
-14
lines changed

src/codehawk.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from 'fs'
21
import * as path from 'path'
32
import slash from 'slash'
43
import { getCoverage } from './coverage'
@@ -15,7 +14,7 @@ import {
1514
FullyAnalyzedFile,
1615
FullyAnalyzedDirectory
1716
} from './types'
18-
import { buildOptions } from './options'
17+
import { buildOptions, getConfiguration } from './options'
1918
import { formatResultsAsTable } from './cli-util'
2019
import { flattenEntireTree } from './util'
2120

@@ -32,17 +31,7 @@ export { calculateComplexity }
3231
export const analyzeProject = (rawPath: string, isCliContext?: boolean): Results => {
3332
// When using CLI, execute from the cwd rather than a relative path
3433
const actualRoot = (isCliContext) ? cwd : rawPath;
35-
const optionsPath = path.resolve(`${actualRoot}/codehawk.json`)
36-
37-
let projectOptionsFile = null
38-
try {
39-
projectOptionsFile = fs.readFileSync(optionsPath, 'utf8')
40-
} catch (e) {
41-
console.log(e)
42-
throw new Error('Please ensure you have a codehawk.json file in your project root.')
43-
}
44-
45-
const projectOptions = JSON.parse(projectOptionsFile)
34+
const projectOptions = getConfiguration(actualRoot)
4635
const options = buildOptions(projectOptions)
4736
const dirPath = path.resolve(`${actualRoot}/`)
4837
const projectCoverage = getCoverage(dirPath)

src/consts/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const NO_CONFIGURATION_FOUND = "Please ensure your configuration is inside a codehawk.json file in your project root, or in your package.json.";

src/options.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import {
2+
existsSync,
3+
readFileSync,
4+
} from 'fs'
5+
import { resolve as pathResolve } from 'path'
6+
7+
import { NO_CONFIGURATION_FOUND } from './consts/errors'
18
import { CodehawkOptions, AllOptionKeys, AssembledOptions } from './types'
29

310
const baseOptions: CodehawkOptions = {
@@ -56,4 +63,28 @@ export const buildOptions = (
5663
})
5764

5865
return assembledOptions
59-
}
66+
}
67+
68+
export const getConfiguration = (rootDirectory: string): AssembledOptions => {
69+
try {
70+
if (existsSync(pathResolve(`${rootDirectory}/codehawk.json`))) {
71+
const configContents = readFileSync(
72+
pathResolve(`${rootDirectory}/codehawk.json`),
73+
"utf8",
74+
);
75+
return JSON.parse(configContents);
76+
}
77+
const packageConfig = readFileSync(
78+
pathResolve(`${rootDirectory}/package.json`),
79+
"utf-8",
80+
);
81+
const parsedPackageConfig = JSON.parse(packageConfig);
82+
if ("codehawk" in parsedPackageConfig) {
83+
return parsedPackageConfig.codehawk;
84+
}
85+
} catch (e) {
86+
console.log(e);
87+
}
88+
89+
throw new Error(NO_CONFIGURATION_FOUND);
90+
};

test/__mocks__/fs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = jest.createMockFromModule('fs');
2+
3+
let fileFound = false;
4+
let readFileSyncData = null;
5+
6+
function __codehawkConfigFound(found, params) {
7+
fileFound = found;
8+
readFileSyncData = params
9+
}
10+
11+
fs.existsSync = () => fileFound;
12+
fs.readFileSync = () => JSON.stringify(readFileSyncData);
13+
14+
fs.__codehawkConfigFound = __codehawkConfigFound
15+
16+
module.exports = fs;

test/options.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { getConfiguration } = require('../build/options')
2+
3+
jest.mock('fs');
4+
5+
describe('when attempting to get the configuration for codehawk', () => {
6+
afterEach(() => {
7+
jest.resetAllMocks()
8+
})
9+
10+
describe('and codehawk.json can be found', () => {
11+
it('should use the configuration defined in codehawk.json', () => {
12+
const mockCodehawkJson = {
13+
thing: true,
14+
good: 'yes',
15+
}
16+
17+
require('fs').__codehawkConfigFound(true, mockCodehawkJson)
18+
19+
const config = getConfiguration('/home/')
20+
21+
expect(config).toEqual(mockCodehawkJson);
22+
})
23+
})
24+
25+
describe('and codehawk.json can not be found', () => {
26+
it('should use the configuration defined in package.json', () => {
27+
const mockCodehawkJson = {
28+
boo: 'spooked',
29+
happiness: 5,
30+
}
31+
32+
require('fs').__codehawkConfigFound(false, {
33+
codehawk: mockCodehawkJson,
34+
})
35+
36+
const config = getConfiguration('/home/')
37+
38+
expect(config).toEqual(mockCodehawkJson)
39+
})
40+
41+
it('should throw an error when package.json contains no codehawk config' , () => {
42+
require('fs').__codehawkConfigFound(false, {})
43+
44+
expect(() => getConfiguration('/home/')).toThrow()
45+
})
46+
})
47+
});

0 commit comments

Comments
 (0)