Skip to content

Commit

Permalink
feat(core/load): gets scripts key and sets options from options key f…
Browse files Browse the repository at this point in the history
…rom kpo scripts file if not a j
  • Loading branch information
rafamel committed Apr 25, 2019
1 parent fcd9122 commit 5c54850
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
22 changes: 17 additions & 5 deletions src/core/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import yaml from 'js-yaml';
import { rejects } from 'errorish';
import { open } from '~/utils/errors';
import { ILoaded, IPaths } from './types';
import { IScripts } from '~/types';
import { IOfType } from '~/types';
import options from './options';

export default async function load(paths: IPaths): Promise<ILoaded> {
return {
Expand All @@ -13,23 +14,34 @@ export default async function load(paths: IPaths): Promise<ILoaded> {
};
}

export async function loadFile(file: string): Promise<IScripts> {
export async function loadFile(file: string): Promise<IOfType<any>> {
const { ext } = path.parse(file);

switch (ext) {
case '.js':
return open.throws(() => require(file));
case '.json':
return fs.readJSON(file).catch(rejects);
return fs
.readJSON(file)
.catch(rejects)
.then(getScripts);
case '.yml':
case '.yaml':
return yaml.safeLoad(
const kpo = yaml.safeLoad(
await fs
.readFile(file)
.then(String)
.catch(rejects)
);
return getScripts(kpo);
default:
throw Error(`Extension not valid`);
throw Error(`Extension not valid for ${file}`);
}
}

export function getScripts(kpo: IOfType<any>): IOfType<any> {
if (!kpo.scripts) throw Error(`Scripts file didn't contain a scripts key`);

if (kpo.options) options.setScope(kpo.options);
return kpo.scripts;
}
30 changes: 0 additions & 30 deletions test/core/load.test.ts

This file was deleted.

0 comments on commit 5c54850

Please sign in to comment.