-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from devreluije/master
New environment system (using fs-recursive@1.1.7)
- Loading branch information
Showing
5 changed files
with
105 additions
and
113 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
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 |
---|---|---|
@@ -1,106 +1,93 @@ | ||
import YAML from 'js-yaml' | ||
import { config } from 'dotenv' | ||
import { fetch } from 'fs-recursive' | ||
import { EnvironmentFactory } from '../types/Factory' | ||
import { fetchSortedExpression, File } from 'fs-recursive' | ||
import { | ||
EnvironmentFactory, | ||
Environment, | ||
EnvironmentElement, | ||
} from 'types/Factory' | ||
|
||
config() | ||
|
||
export default class EnvironmentManager { | ||
public $env: EnvironmentFactory = { | ||
public $env: Environment = { | ||
type: '', | ||
path: '', | ||
content: '', | ||
content: {}, | ||
} | ||
|
||
public async load () { | ||
const environment = await fetch(process.cwd(), | ||
['env', 'json', 'yaml', 'yml'], | ||
'utf-8', | ||
['node_modules']) | ||
|
||
const environments = Array.from(environment.entries()) | ||
.filter(([_, file]) => file.filename === 'environment' || file.extension === 'env') | ||
.map(([_, file]) => file) | ||
|
||
const env = environments.find(file => file.extension === 'env') | ||
if (env) { | ||
return this.$env = { | ||
type: env.extension, | ||
path: env.path, | ||
content: '', | ||
} | ||
} | ||
public async load() { | ||
const environment = await fetchSortedExpression( | ||
process.cwd(), | ||
/^environment\.(json|yml|yaml)|\.env$/, | ||
['env', 'json', 'yml', 'yaml'], | ||
'utf-8', | ||
['node_modules'] | ||
) | ||
|
||
const json = environments.find(file => file.extension === 'json') | ||
if (json) { | ||
const content = await json.getContent('utf-8') | ||
return this.$env = { | ||
type: json.extension, | ||
path: json.path, | ||
content: content!.toString(), | ||
} | ||
} | ||
if (!environment.length) | ||
throw new Error('Environment file is missing, please create one.') | ||
|
||
const yaml = environments.find(file => file.extension === 'yaml' || file.extension === 'yml') | ||
if (yaml) { | ||
const content = await yaml.getContent('utf-8') | ||
return this.$env = { | ||
type: yaml.extension, | ||
path: yaml.path, | ||
content: content!.toString(), | ||
} | ||
const types: EnvironmentFactory = { | ||
env: this.getFromEnv, | ||
json: this.getFromJSON, | ||
yml: this.getFromYAML, | ||
yaml: this.getFromYAML, | ||
} | ||
const file = environment[0] | ||
const factory = types[file.extension] | ||
|
||
throw new Error('Environment file is missing, please create one.') | ||
} | ||
if (!factory) throw new Error(`Factory not found for file: ${file.path}`) | ||
|
||
public get (key: string) { | ||
const environments = { | ||
env: () => this.getFromEnv(key), | ||
json: () => this.getFromJSON(key), | ||
yaml: () => this.getFromYAML(key), | ||
yml: () => this.getFromYAML(key), | ||
} | ||
return environments[this.$env.type]() | ||
return (this.$env = { | ||
type: file.extension, | ||
path: file.path, | ||
content: await factory(file), | ||
}) | ||
} | ||
|
||
private filterEnvironment (identifier) { | ||
private filterEnvironment(identifier: string) { | ||
return Object.entries(process.env) | ||
.map(([key, value]) => key.toLowerCase().startsWith(identifier) ? { [key.replace(`${identifier.toUpperCase()}_`, '')]: value } : null) | ||
.filter(rule => rule).reduce((acc, t) => ({ ...acc, ...t })) | ||
.filter(([key]) => key.toLowerCase().startsWith(identifier)) | ||
.map(([key, value]) => ({ | ||
[key.replace(`${identifier.toUpperCase()}_`, '')]: value!, | ||
})) | ||
.reduce((acc, t) => ({ ...acc, ...t })) | ||
} | ||
|
||
private getFromEnv (key: string) { | ||
private async getFromEnv(_file: File): Promise<EnvironmentElement> { | ||
const messages = this.filterEnvironment('message') | ||
const presets = this.filterEnvironment('preset') | ||
const partials = Object.entries(process.env) | ||
.flatMap(([key, value]: any) => { | ||
return key.toLowerCase().startsWith('partial') ? { [key]: value } : null | ||
}) | ||
.filter(preset => preset) | ||
.flatMap((preset: any) => { | ||
return Object.entries(preset).flatMap((item) => { | ||
return item[1] === 'true' && item[0].replace('PARTIAL_', '') | ||
}).filter(item => item) | ||
}) as Array<string> | ||
const partials = Object.keys( | ||
Object.entries(process.env) | ||
.filter(([key]) => key.toLowerCase().startsWith('partial')) | ||
.filter(([_, value]) => value === 'true') | ||
.map(([key, value]) => ({ [key]: value })) | ||
.keys() | ||
) | ||
|
||
const env = { | ||
APP_TOKEN: process.env.APP_TOKEN, | ||
APP_PREFIX: process.env.APP_PREFIX, | ||
return { | ||
APP_TOKEN: process.env.APP_TOKEN!, | ||
APP_PREFIX: process.env.APP_PREFIX!, | ||
PARTIALS: partials, | ||
MESSAGES: messages, | ||
PRESETS: presets, | ||
} | ||
} | ||
|
||
return env[key] | ||
private async getFromJSON(file: File) { | ||
const content = await file.getContent('utf-8') | ||
|
||
return JSON.parse(content!.toString()) | ||
} | ||
|
||
private getFromJSON (key: string) { | ||
return JSON.parse(this.$env.content)[key] | ||
private async getFromYAML(file: File) { | ||
const content = await file.getContent('utf-8') | ||
|
||
return YAML.load(content!.toString()) | ||
} | ||
|
||
private getFromYAML (key: string) { | ||
const env = YAML.load(this.$env.content) | ||
return env[key] | ||
public get(key: string) { | ||
return this.$env.content[key] | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,20 @@ | ||
export type EnvironmentFactory = { | ||
import { File } from 'fs-recursive' | ||
|
||
export type EnvironmentElement = { | ||
[key: string]: string | string[] | EnvironmentElement | ||
} | ||
|
||
export type Environment = { | ||
type: string | ||
path: string | ||
content: string | ||
content: EnvironmentElement | ||
} | ||
|
||
export type EnvironmentType = 'env' | 'json' | 'yaml' | ||
export type EnvironmentType = 'env' | 'json' | 'yml' | 'yaml' | ||
|
||
export interface EnvironmentFactory { | ||
env: (file: File) => Promise<EnvironmentElement> | ||
json: (file: File) => Promise<EnvironmentElement> | ||
yml: (file: File) => Promise<EnvironmentElement> | ||
yaml: (file: File) => Promise<EnvironmentElement> | ||
} |
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