mirror of
https://github.com/Cameri/nostream.git
synced 2025-07-16 08:42:21 +02:00
fix: invalid type for extname()
Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
This commit is contained in:
@ -9,9 +9,9 @@ import { ISettings } from '../@types/settings'
|
|||||||
|
|
||||||
const debug = createLogger('settings')
|
const debug = createLogger('settings')
|
||||||
|
|
||||||
const FileType = {
|
export enum SettingsFileTypes {
|
||||||
yaml: 'yaml',
|
yaml = 'yaml',
|
||||||
json: 'json',
|
json = 'json',
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingsStatic {
|
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 files: string[] = fs.readdirSync(path)
|
||||||
const filteredFiles = files ? files.filter(fn => fn.startsWith('settings')) : []
|
const filteredFile = files.find(fn => fn.startsWith('settings'))
|
||||||
if (filteredFiles.length) {
|
if (filteredFile) {
|
||||||
const extension = extname(filteredFiles.pop())
|
const extension = extname(filteredFile)
|
||||||
return FileType[extension]
|
return SettingsFileTypes[extension]
|
||||||
} else {
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static loadSettings(path: string, fileType: SettingsFileTypes) {
|
||||||
public static loadSettings(path: string, fileType) {
|
|
||||||
debug('loading settings from %s', path)
|
debug('loading settings from %s', path)
|
||||||
|
|
||||||
switch (fileType) {
|
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')
|
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: {
|
case SettingsFileTypes.yaml: {
|
||||||
return this.loadAndParseYamlFile(path)
|
return SettingsStatic.loadAndParseYamlFile(path)
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
throw new Error('settings file was missing or did not contain .yaml or .json extensions.')
|
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 fileType = SettingsStatic.settingsFileType(basePath)
|
||||||
const settingsFilePath = `${basePath}/settings.${fileType}`
|
const settingsFilePath = `${basePath}/settings.${fileType}`
|
||||||
|
|
||||||
const defaults = SettingsStatic.loadSettings(defaultsFilePath, FileType.yaml)
|
const defaults = SettingsStatic.loadSettings(defaultsFilePath, SettingsFileTypes.yaml)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import fs from 'fs'
|
|||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import Sinon from 'sinon'
|
import Sinon from 'sinon'
|
||||||
|
|
||||||
import { SettingsStatic } from '../../../src/utils/settings'
|
import { SettingsFileTypes, SettingsStatic } from '../../../src/utils/settings'
|
||||||
|
|
||||||
describe('SettingsStatic', () => {
|
describe('SettingsStatic', () => {
|
||||||
describe('.getSettingsFilePath', () => {
|
describe('.getSettingsFilePath', () => {
|
||||||
@ -137,7 +137,7 @@ describe('SettingsStatic', () => {
|
|||||||
it('loads settings from given path', () => {
|
it('loads settings from given path', () => {
|
||||||
readFileSyncStub.returns('"content"')
|
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(
|
expect(readFileSyncStub).to.have.been.calledOnceWithExactly(
|
||||||
'/some/path',
|
'/some/path',
|
||||||
|
Reference in New Issue
Block a user