mirror of
https://github.com/Cameri/nostream.git
synced 2025-03-17 21:31:48 +01:00
fix: invalid type for extname()
Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
This commit is contained in:
parent
9f8a25cdc4
commit
f5782473eb
@ -9,9 +9,9 @@ import { ISettings } from '../@types/settings'
|
||||
|
||||
const debug = createLogger('settings')
|
||||
|
||||
const FileType = {
|
||||
yaml: 'yaml',
|
||||
json: 'json',
|
||||
export enum SettingsFileTypes {
|
||||
yaml = 'yaml',
|
||||
json = 'json',
|
||||
}
|
||||
|
||||
export class SettingsStatic {
|
||||
@ -40,28 +40,25 @@ export class SettingsStatic {
|
||||
)
|
||||
}
|
||||
|
||||
public static settingsFileType(path) {
|
||||
public static settingsFileType(path: string): SettingsFileTypes | undefined {
|
||||
const files: string[] = fs.readdirSync(path)
|
||||
const filteredFiles = files ? files.filter(fn => fn.startsWith('settings')) : []
|
||||
if (filteredFiles.length) {
|
||||
const extension = extname(filteredFiles.pop())
|
||||
return FileType[extension]
|
||||
} else {
|
||||
return null
|
||||
const filteredFile = files.find(fn => fn.startsWith('settings'))
|
||||
if (filteredFile) {
|
||||
const extension = extname(filteredFile)
|
||||
return SettingsFileTypes[extension]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static loadSettings(path: string, fileType) {
|
||||
public static loadSettings(path: string, fileType: SettingsFileTypes) {
|
||||
debug('loading settings from %s', path)
|
||||
|
||||
switch (fileType) {
|
||||
case FileType.json: {
|
||||
case SettingsFileTypes.json: {
|
||||
console.warn('settings.json is deprecated, please use a yaml file based on resources/default-settings.yaml')
|
||||
return this.loadAndParseJsonFile(path)
|
||||
return SettingsStatic.loadAndParseJsonFile(path)
|
||||
}
|
||||
case FileType.yaml: {
|
||||
return this.loadAndParseYamlFile(path)
|
||||
case SettingsFileTypes.yaml: {
|
||||
return SettingsStatic.loadAndParseYamlFile(path)
|
||||
}
|
||||
default: {
|
||||
throw new Error('settings file was missing or did not contain .yaml or .json extensions.')
|
||||
@ -83,7 +80,7 @@ export class SettingsStatic {
|
||||
const fileType = SettingsStatic.settingsFileType(basePath)
|
||||
const settingsFilePath = `${basePath}/settings.${fileType}`
|
||||
|
||||
const defaults = SettingsStatic.loadSettings(defaultsFilePath, FileType.yaml)
|
||||
const defaults = SettingsStatic.loadSettings(defaultsFilePath, SettingsFileTypes.yaml)
|
||||
|
||||
try {
|
||||
|
||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
||||
import { join } from 'path'
|
||||
import Sinon from 'sinon'
|
||||
|
||||
import { SettingsStatic } from '../../../src/utils/settings'
|
||||
import { SettingsFileTypes, SettingsStatic } from '../../../src/utils/settings'
|
||||
|
||||
describe('SettingsStatic', () => {
|
||||
describe('.getSettingsFilePath', () => {
|
||||
@ -137,7 +137,7 @@ describe('SettingsStatic', () => {
|
||||
it('loads settings from given path', () => {
|
||||
readFileSyncStub.returns('"content"')
|
||||
|
||||
expect(SettingsStatic.loadSettings('/some/path', 'yaml')).to.equal('content')
|
||||
expect(SettingsStatic.loadSettings('/some/path', SettingsFileTypes.yaml)).to.equal('content')
|
||||
|
||||
expect(readFileSyncStub).to.have.been.calledOnceWithExactly(
|
||||
'/some/path',
|
||||
@ -221,7 +221,7 @@ describe('SettingsStatic', () => {
|
||||
existsSyncStub.returns(true)
|
||||
readdirSyncStub.returns(['settings.yaml'])
|
||||
settingsFileTypeStub.returns('yaml')
|
||||
|
||||
|
||||
|
||||
expect(SettingsStatic.createSettings()).to.be.an('object')
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user