Skip to content

Commit

Permalink
feat(tasks): adds write task
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Feb 18, 2021
1 parent fb53a87 commit 86a44ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tasks/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './print';
export * from './raises';
export * from './remove';
export * from './sleep';
export * from './write';
33 changes: 33 additions & 0 deletions src/tasks/create/write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Task, Context } from '../../definitions';
import { useDestination } from '../../helpers/paths';
import { log } from './log';
import { Serial } from 'type-core';
import { into } from 'pipettes';
import fs from 'fs-extra';

export interface WriteOptions {
exists?: 'error' | 'ignore' | 'overwrite';
}

export function write(
path: string,
content: Buffer | Serial.Type,
options?: WriteOptions
): Task.Async {
return async (ctx: Context): Promise<void> => {
into(ctx, log('debug', 'Write:', path));

const data = Buffer.isBuffer(content)
? content
: typeof content === 'object'
? JSON.stringify(content, null, 2)
: String(content);

await useDestination(
path,
ctx,
Object.assign({ exists: 'error' }, options),
(dest) => fs.writeFile(dest, data)
);
};
}

0 comments on commit 86a44ed

Please sign in to comment.