-
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.
cli: adds support for the yaml file format
- Loading branch information
1 parent
b6334f1
commit 1108623
Showing
5 changed files
with
73 additions
and
31 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,32 +1,74 @@ | ||
import {existsSync} from 'fs'; | ||
import getLocalPathConfig from './local-path'; | ||
import {existsSync, readFileSync} from 'fs'; | ||
import {join, extname} from 'path'; | ||
import loadJSON from 'load-json-file'; | ||
import YAML from 'yaml'; | ||
|
||
export function readLocalConfig({error}, prefix) { | ||
const target = getLocalPathConfig(prefix || process.cwd()); | ||
let localConfigExists; | ||
function getConfigPath(prefix) { | ||
const configYaml = join(prefix, 'fleet.yml'); | ||
const configJson = join(prefix, 'fleet.json'); | ||
|
||
if (existsSync(configYaml)) { | ||
return configYaml; | ||
} else if (existsSync(configJson)) { | ||
return configJson; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
function getConfigJson(error, path) { | ||
try { | ||
localConfigExists = existsSync(target); | ||
return loadJSON.sync(path); | ||
} catch (err) { | ||
error('Failed to check if `fleet.json` exists', null); | ||
if (err.name === 'JSONError') { | ||
error(err.message, null); | ||
} else { | ||
const code = err.code ? `(${err.code})` : ''; | ||
error(`Failed to read the \`fleet.json\` file ${code}`, null); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
} | ||
|
||
function getConfigYaml(error, path) { | ||
try { | ||
const file = readFileSync(path, 'utf8'); | ||
const content = YAML.parse(file); | ||
const functions = []; | ||
|
||
if (localConfigExists) { | ||
try { | ||
return loadJSON.sync(target); | ||
} catch (err) { | ||
if (err.name === 'JSONError') { | ||
error(err.message, null); | ||
} else { | ||
const code = err.code ? `(${err.code})` : ''; | ||
error(`Failed to read the \`fleet.json\` file ${code}`, null); | ||
} | ||
|
||
process.exit(1); | ||
if (content.functions) { | ||
Object.keys(content.functions).forEach((key) => { | ||
functions.push({name: key, ...content.functions[key]}); | ||
}); | ||
|
||
return { | ||
...content, | ||
functions, | ||
}; | ||
} | ||
|
||
return content; | ||
} catch (err) { | ||
const code = err.code ? `(${err.code})` : ''; | ||
error(`Failed to parse the \`fleet.yml\` file ${code}`, null); | ||
|
||
process.exit(1); | ||
} | ||
} | ||
|
||
return null; | ||
export function getLocalConfig({error}, prefix) { | ||
const path = getConfigPath(prefix); | ||
const ext = extname(path); | ||
|
||
if (path) { | ||
switch (ext) { | ||
case '.json': | ||
return getConfigJson(error, path); | ||
case '.yml': | ||
return getConfigYaml(error, path); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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