Skip to content

Commit

Permalink
feat(exposed/tags): adds ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 26, 2019
1 parent 063fcba commit 28adb76
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/exposed/tags/ensure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from 'path';
import fs from 'fs-extra';
import core from '~/core';
import { TScript } from '~/types';
import asTag from '~/utils/as-tag';
import { rejects } from 'errorish';

export default ensure;

function ensure(directory: string): TScript;
function ensure(
literals: TemplateStringsArray,
...placeholders: any[]
): TScript;
/**
* String tag; ensures `directory` exists -if it doesn't it creates it. `directory` can be relative to the project's directory.
* @returns A `TScript`, as a function, that won't be executed until called by `kpo` -hence, calling `ensure` won't have any effect until the returned function is called.
*/
function ensure(...args: any[]): TScript {
return async function ensure(): Promise<void> {
let directory = asTag(args.shift(), ...args);
if (!path.isAbsolute(directory)) {
const paths = await core.paths();
directory = path.join(paths.directory, directory);
}

await fs.ensureDir(directory).catch(rejects);
};
}
1 change: 1 addition & 0 deletions src/exposed/tags/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as ensure } from './ensure';
export { default as line } from './line';
export { default as silent } from './silent';

0 comments on commit 28adb76

Please sign in to comment.