This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.js
73 lines (64 loc) · 1.79 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from 'fs'
import promisify from 'util.promisify'
import pick from 'lodash/pick'
import rename from './object/rename'
const readFile = async (file) => {
try {
return await promisify(fs.readFile)(file || '.lokalise.json', 'utf8')
} catch (err) {
if (file) {
throw err
}
}
}
const bridgeLegacy = (opts) => (
{
...rename(
opts,
['api_token', 'project_id', 'output_path'],
['token', 'project', 'output']
),
...pick(opts, ['token', 'project', 'output'])
}
)
const fetchFileConfig = async (file) => {
const contents = await readFile(file)
if (contents) {
const config = JSON.parse(contents)
return bridgeLegacy(config)
}
return null
}
const fetchEnvironmentConfig = () => {
const config = {}
if (process.env.LOKALISE_TOKEN) {
config.token = process.env.LOKALISE_TOKEN
}
if (process.env.LOKALISE_PROJECT) {
config.project = process.env.LOKALISE_PROJECT
}
if (process.env.LOKALISE_OUTPUT) {
config.output = process.env.LOKALISE_OUTPUT
}
return config
}
const validate = ({ token, project, output }) => {
if (!token) throw Error('"token" is undefined')
if (!project) throw Error('"project" is undefined')
if (!output) throw Error('"output" is undefined')
}
export const build = async (file, args) => {
const envOptions = fetchEnvironmentConfig()
const fileOptions = await fetchFileConfig(file) || {}
const argOptions = pick(args, ['token', 'project', 'output'])
const options = { ...fileOptions, ...envOptions, ...argOptions }
try {
validate(options)
} catch (err) {
if (Object.keys(fileOptions).length === 0) {
console.warn('HINT: It seems that you don\'t have a .lokalise.json file in your directory or you haven\'t specified any options in it.')
}
throw err
}
return options
}