Skip to content

Commit

Permalink
wrap fs methods with promisify for node@12 compat (#328)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <[email protected]>
  • Loading branch information
Spencer and spalger authored Jun 29, 2021
1 parent f3f6fed commit 17fee5e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import { once, EventEmitter } from 'events';
import { join } from 'path';
import { mkdir, rm, writeFile } from 'fs/promises';
import { Journey } from '../dsl/journey';
import { Step } from '../dsl/step';
import { reporters, Reporter } from '../reporters';
Expand All @@ -35,6 +34,9 @@ import {
getTimestamp,
runParallel,
generateUniqueId,
mkdirAsync,
rmAsync,
writeFileAsync,
} from '../helpers';
import {
StatusValue,
Expand Down Expand Up @@ -146,7 +148,7 @@ export default class Runner extends EventEmitter {
* For each journey we create the screenshots folder for
* caching all screenshots and clear them at end of each journey
*/
await mkdir(this.screenshotPath, { recursive: true });
await mkdirAsync(this.screenshotPath, { recursive: true });
return {
start,
params: options.params,
Expand All @@ -170,7 +172,7 @@ export default class Runner extends EventEmitter {
*/
if (buffer) {
const fileName = `${generateUniqueId()}.json`;
await writeFile(
await writeFileAsync(
join(Runner.screenshotPath, fileName),
JSON.stringify({
step,
Expand Down Expand Up @@ -322,7 +324,7 @@ export default class Runner extends EventEmitter {
await once(this, 'journey:end:reported');
}
// clear screenshots cache after each journey
await rm(Runner.screenshotPath, { recursive: true, force: true });
await rmAsync(Runner.screenshotPath, { recursive: true, force: true });
}

/**
Expand Down Expand Up @@ -401,7 +403,7 @@ export default class Runner extends EventEmitter {
/**
* Set up the directory for caching screenshots
*/
await mkdir(CACHE_PATH, { recursive: true });
await mkdirAsync(CACHE_PATH, { recursive: true });
}

async run(options: RunOptions) {
Expand Down Expand Up @@ -448,7 +450,7 @@ export default class Runner extends EventEmitter {
* Clear all cache data stored for post processing by
* the current synthetic agent run
*/
await rm(CACHE_PATH, { recursive: true, force: true });
await rmAsync(CACHE_PATH, { recursive: true, force: true });
this.currentJourney = null;
this.journeys = [];
this.active = false;
Expand Down
14 changes: 10 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ import { promisify } from 'util';
import { performance } from 'perf_hooks';
import { HooksArgs, HooksCallback } from './common_types';

const statAsync = promisify(fs.lstat);
const readAsync = promisify(fs.readdir);
const lstatAsync = promisify(fs.lstat);
const readdirAsync = promisify(fs.readdir);

export const readFileAsync = promisify(fs.readFile);
export const writeFileAsync = promisify(fs.writeFile);
export const rmAsync = promisify(fs.rm);
export const mkdirAsync = promisify(fs.mkdir);

const SEPARATOR = '\n';

export function noop() {}
Expand Down Expand Up @@ -164,11 +170,11 @@ export async function totalist(
pre = ''
) {
dir = resolve('.', dir);
await readAsync(dir).then(arr => {
await readdirAsync(dir).then(arr => {
return Promise.all(
arr.map(str => {
const abs = join(dir, str);
return statAsync(abs).then(stats =>
return lstatAsync(abs).then(stats =>
stats.isDirectory()
? totalist(abs, callback, join(pre, str))
: callback(join(pre, str), abs)
Expand Down

0 comments on commit 17fee5e

Please sign in to comment.