Skip to content

Commit

Permalink
feat(parse): adds readFile and IScripts type
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 11, 2019
1 parent 78676b4 commit 1cd195c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/parse/read-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from 'fs';
import path from 'path';
import pify from 'pify';
import yaml from 'js-yaml';
import { IScripts } from '~/types';
import ensure from '~/utils/ensure';

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

switch (ext) {
case '.js':
return require(file);
case '.json':
return JSON.parse(await ensure.rejection(() => pify(fs.readFile)(file)));
case '.yml':
case '.yaml':
return yaml.safeLoad(
await ensure.rejection(() => pify(fs.readFile)(file))
);
default:
throw Error(`Extension not valid`);
}
}
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { LogLevelDesc } from 'loglevel';

export interface IOfType<T> {
[key: string]: T;
}

export type TLogger = LogLevelDesc;

export type TScript = string | (() => Promise<void> | void);

export interface IScripts {
[key: string]: TScript | TScript[] | IScripts;
}

0 comments on commit 1cd195c

Please sign in to comment.