mirror of https://github.com/status-im/metro.git
RN packager: wrap-worker-fn.js: syncify
Reviewed By: davidaurelio Differential Revision: D5803796 fbshipit-source-id: d6b7d169a2c17864e8ce68d82df8e898d966e404
This commit is contained in:
parent
69eb3430b6
commit
36b2f8d67f
|
@ -10,20 +10,15 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
const asyncify: Asyncify = require('async/asyncify');
|
||||
const optimizeModule = require('./worker/optimize-module');
|
||||
const transformModule = require('./worker/transform-module');
|
||||
const wrapWorkerFn = require('./worker/wrap-worker-fn');
|
||||
|
||||
import type {Callback} from './types.flow';
|
||||
import type {OptimizationOptions} from './worker/optimize-module';
|
||||
import type {TransformOptions} from './worker/transform-module';
|
||||
import type {WorkerFnWithIO} from './worker/wrap-worker-fn';
|
||||
|
||||
type Asyncify = <A, B, C>((A, B) => C) => (A, B, Callback<C>) => void;
|
||||
|
||||
|
||||
exports.optimizeModule =
|
||||
(wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO<OptimizationOptions>);
|
||||
(wrapWorkerFn(optimizeModule): WorkerFnWithIO<OptimizationOptions>);
|
||||
exports.transformModule =
|
||||
(wrapWorkerFn(asyncify(transformModule)): WorkerFnWithIO<TransformOptions>);
|
||||
(wrapWorkerFn(transformModule): WorkerFnWithIO<TransformOptions>);
|
||||
|
|
|
@ -27,72 +27,71 @@ describe('wrapWorkerFn:', () => {
|
|||
let workerFn, wrapped;
|
||||
beforeEach(() => {
|
||||
workerFn = fn();
|
||||
workerFn.stub.yields();
|
||||
wrapped = wrapWorkerFn(workerFn);
|
||||
});
|
||||
|
||||
const fs = require('fs');
|
||||
const mkdirp = require('mkdirp');
|
||||
|
||||
it('reads the passed-in file synchronously as buffer', done => {
|
||||
wrapped(infile, outfile, {}, () => {
|
||||
expect(fs.readFileSync).toBeCalledWith(infile);
|
||||
done();
|
||||
});
|
||||
it('reads the passed-in file synchronously as buffer', () => {
|
||||
wrapped(infile, outfile, {});
|
||||
expect(fs.readFileSync).toBeCalledWith(infile);
|
||||
});
|
||||
|
||||
it('calls the worker function with file contents and options', done => {
|
||||
it('calls the worker function with file contents and options', () => {
|
||||
const contents = 'arbitrary(contents);';
|
||||
const options = {arbitrary: 'options'};
|
||||
fs.readFileSync.mockReturnValue(contents);
|
||||
wrapped(infile, outfile, options, () => {
|
||||
expect(workerFn).toBeCalledWith(contents, options, any(Function));
|
||||
done();
|
||||
});
|
||||
wrapped(infile, outfile, options);
|
||||
expect(workerFn).toBeCalledWith(contents, options);
|
||||
});
|
||||
|
||||
it('passes through any error that the worker function calls back with', done => {
|
||||
it('passes through any error that the worker function calls back with', () => {
|
||||
const error = new Error();
|
||||
workerFn.stub.yields(error);
|
||||
wrapped(infile, outfile, {}, e => {
|
||||
workerFn.stub.throws(error);
|
||||
try {
|
||||
wrapped(infile, outfile, {});
|
||||
throw new Error('should not reach');
|
||||
} catch (e) {
|
||||
expect(e).toBe(error);
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('writes the result to disk', done => {
|
||||
it('writes the result to disk', () => {
|
||||
const result = {arbitrary: 'result'};
|
||||
workerFn.stub.yields(null, result);
|
||||
wrapped(infile, outfile, {}, () => {
|
||||
expect(mkdirp.sync).toBeCalledWith(dirname(outfile));
|
||||
expect(fs.writeFileSync).toBeCalledWith(
|
||||
outfile,
|
||||
JSON.stringify(result),
|
||||
'utf8',
|
||||
);
|
||||
done();
|
||||
});
|
||||
workerFn.stub.returns(result);
|
||||
wrapped(infile, outfile, {});
|
||||
expect(mkdirp.sync).toBeCalledWith(dirname(outfile));
|
||||
expect(fs.writeFileSync).toBeCalledWith(
|
||||
outfile,
|
||||
JSON.stringify(result),
|
||||
'utf8',
|
||||
);
|
||||
});
|
||||
|
||||
it('calls back with any error thrown by `mkdirp.sync`', done => {
|
||||
it('calls back with any error thrown by `mkdirp.sync`', () => {
|
||||
const error = new Error();
|
||||
mkdirp.sync.mockImplementationOnce(() => {
|
||||
throw error;
|
||||
});
|
||||
wrapped(infile, outfile, {}, e => {
|
||||
try {
|
||||
wrapped(infile, outfile, {});
|
||||
throw new Error('should not reach');
|
||||
} catch (e) {
|
||||
expect(e).toBe(error);
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('calls back with any error thrown by `fs.writeFileSync`', done => {
|
||||
it('calls back with any error thrown by `fs.writeFileSync`', () => {
|
||||
const error = new Error();
|
||||
fs.writeFileSync.mockImplementationOnce(() => {
|
||||
throw error;
|
||||
});
|
||||
wrapped(infile, outfile, {}, e => {
|
||||
try {
|
||||
wrapped(infile, outfile, {});
|
||||
throw new Error('should not reach');
|
||||
} catch (e) {
|
||||
expect(e).toBe(error);
|
||||
done();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,47 +17,22 @@ const mkdirp = require('mkdirp');
|
|||
|
||||
const {dirname} = require('path');
|
||||
|
||||
import type {Callback} from '../types.flow';
|
||||
|
||||
type Path = string;
|
||||
type WorkerFn<Options> = (
|
||||
fileContents: Buffer,
|
||||
options: Options,
|
||||
callback: Callback<Object>,
|
||||
) => void;
|
||||
type WorkerFn<Options> = (fileContents: Buffer, options: Options) => mixed;
|
||||
export type WorkerFnWithIO<Options> = (
|
||||
infile: Path,
|
||||
outfile: Path,
|
||||
options: Options,
|
||||
callback: Callback<>,
|
||||
) => void;
|
||||
|
||||
function wrapWorkerFn<Options>(
|
||||
workerFunction: WorkerFn<Options>,
|
||||
): WorkerFnWithIO<Options> {
|
||||
return (
|
||||
infile: Path,
|
||||
outfile: Path,
|
||||
options: Options,
|
||||
callback: Callback<>,
|
||||
) => {
|
||||
return (infile: Path, outfile: Path, options: Options) => {
|
||||
const contents = fs.readFileSync(infile);
|
||||
workerFunction(contents, options, (error, result) => {
|
||||
if (error) {
|
||||
callback(error);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
mkdirp.sync(dirname(outfile));
|
||||
fs.writeFileSync(outfile, JSON.stringify(result), 'utf8');
|
||||
} catch (writeError) {
|
||||
callback(writeError);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null);
|
||||
});
|
||||
const result = workerFunction(contents, options);
|
||||
mkdirp.sync(dirname(outfile));
|
||||
fs.writeFileSync(outfile, JSON.stringify(result), 'utf8');
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue