-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb8e0bc
commit 2e27d0b
Showing
10 changed files
with
153 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
import { join } from 'node:path'; | ||
import { cwd } from 'node:process'; | ||
|
||
import { RC_FILE_NAME } from './constants.js'; | ||
import { Logger } from './logger.js'; | ||
import { messages } from './messages.js'; | ||
|
||
/** | ||
* @typedef {import('../types/types.d.js').Config} Config | ||
*/ | ||
|
||
/** | ||
* Get the config from the config file. | ||
* @returns {Config | null} The config object. | ||
*/ | ||
export function getConfigFromFile() { | ||
const filePath = join(cwd(), RC_FILE_NAME); | ||
if (!existsSync(filePath)) { | ||
Logger.warn(messages.CONFIG_FILE_NOT_FOUND, RC_FILE_NAME); | ||
return null; | ||
} | ||
const fileContent = readFileSync(filePath, 'utf-8'); | ||
try { | ||
return /** @type {Config} */ (JSON.parse(fileContent)) || null; | ||
} catch (error) { | ||
Logger.error(messages.CONFIG_FILE_INVALID(RC_FILE_NAME)); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Save runtime config into rc file. | ||
* @param {Config} config Config object to write. | ||
*/ | ||
export function saveConfigToFile(config) { | ||
const filePath = join(cwd(), RC_FILE_NAME); | ||
const fileContent = JSON.stringify(config, null, '\t'); | ||
writeFileSync(filePath, fileContent); | ||
Logger.info(messages.SAVED_CONFIG(RC_FILE_NAME), config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { expect, use } from 'chai'; | ||
import esmock from 'esmock'; | ||
import { createSandbox } from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
|
||
import { RC_FILE_NAME } from '../../../src/helpers/constants.js'; | ||
import { messages } from '../../../src/helpers/messages.js'; | ||
import { globalMocksFactory } from '../../helpers/global-mocks-factory.js'; | ||
|
||
use(sinonChai); | ||
const sandbox = createSandbox(); | ||
|
||
class Logger { | ||
static info = sandbox.stub(); | ||
static warn = sandbox.stub(); | ||
static error = sandbox.stub(); | ||
} | ||
|
||
const mocks = { | ||
'./logger.js': { Logger }, | ||
}; | ||
const globalMocks = globalMocksFactory(sandbox); | ||
const { fs, path } = globalMocks; | ||
const fileToTest = '../../../src/helpers/config-file.js'; | ||
const absolutePath = new URL(fileToTest, import.meta.url).pathname; | ||
const { getConfigFromFile, saveConfigToFile } = await esmock(absolutePath, absolutePath, mocks, globalMocks); | ||
|
||
describe('unit: config-file', () => { | ||
afterEach(() => { | ||
sandbox.reset(); | ||
}); | ||
|
||
describe('getConfigFromFile', () => { | ||
it('should return null if the config file does not exist', () => { | ||
expect(getConfigFromFile()).to.be.null; | ||
expect(Logger.warn).to.have.been.calledOnceWithExactly(messages.CONFIG_FILE_NOT_FOUND, RC_FILE_NAME); | ||
}); | ||
|
||
it('should return null if the config file is invalid', () => { | ||
fs.existsSync.returns(true); | ||
fs.readFileSync.returns('invalid json'); | ||
expect(getConfigFromFile()).to.be.null; | ||
expect(Logger.error).to.have.been.calledOnceWithExactly(messages.CONFIG_FILE_INVALID(RC_FILE_NAME)); | ||
}); | ||
|
||
it('should return null if the config file is null', () => { | ||
fs.existsSync.returns(true); | ||
fs.readFileSync.returns(null); | ||
expect(getConfigFromFile()).to.be.null; | ||
}); | ||
|
||
it('should return the config object', () => { | ||
const config = { foo: 'bar' }; | ||
fs.existsSync.returns(true); | ||
fs.readFileSync.returns(JSON.stringify(config)); | ||
expect(getConfigFromFile()).to.deep.equal(config); | ||
}); | ||
}); | ||
|
||
describe('saveConfigToFile', () => { | ||
it('should write the config to the file', () => { | ||
const configFilePath = '/path/to/config/file'; | ||
const config = { foo: 'bar' }; | ||
path.join.returns(configFilePath); | ||
saveConfigToFile(config); | ||
expect(fs.writeFileSync).to.have.been.calledOnceWithExactly(configFilePath, JSON.stringify(config, null, '\t')); | ||
expect(Logger.info).to.have.been.calledOnceWithExactly(messages.SAVED_CONFIG(RC_FILE_NAME), config); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.