Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap fs methods with promisify for node@12 compat #328

Merged
merged 1 commit into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -159,11 +165,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