Skip to content

Commit

Permalink
Add readJson fallback (#51)
Browse files Browse the repository at this point in the history
* Enable strict null checks

* Allow baseUrl and context to be undefined

* Swap nulls for undefined (as define in Callback type)

* Add test for custom file system

* Update ResolverFileSystem to have optional json methods

* Reuse typeof fs for ResolverFileSystem

* Fallback to standard readFile and JSON.parse if readJson is unavailable

* Fix bad console errors
  • Loading branch information
JakeSidSmith authored Jul 29, 2020
1 parent 092e791 commit 27728f7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 38 deletions.
4 changes: 2 additions & 2 deletions base-tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true
// "strictNullChecks": true
"noUnusedParameters": true,
"strictNullChecks": true
}
}
25 changes: 25 additions & 0 deletions example/custom-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require('fs');
const webpack = require('webpack');

const config = require('./webpack.config');

const compiler = webpack(config);

compiler.inputFileSystem = fs;

compiler.run(function (error, stats) {
if (error) {
console.error(error);
return process.exit(1);
}

if (stats.compilation.errors.length) {
stats.compilation.errors.forEach((compilationError) => {
console.error(compilationError);
});

return process.exit(1);
}

console.log('Successfully compiled');
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"compile": "tsc -p src",
"compile:tests": "tsc -p tests",
"compile:example": "tsc -p example",
"example": "yarn build && cd example && webpack",
"example": "yarn build && cd example && webpack && node custom-fs.js",
"build": "rimraf lib && tsc -p src",
"lint":
"tslint -t msbuild './src/**/*.ts{,x}' -e './src/node_modules/**/*'",
Expand Down
4 changes: 2 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export type LogLevel = "INFO" | "WARN" | "ERROR";
export interface Options {
readonly configFile: string;
readonly extensions: ReadonlyArray<string>;
readonly baseUrl: string;
readonly baseUrl: string | undefined;
readonly silent: boolean;
readonly logLevel: LogLevel;
readonly logInfoToStdOut: boolean;
readonly context: string;
readonly context: string | undefined;
readonly colors: boolean;
readonly mainFields: string[];
}
Expand Down
70 changes: 37 additions & 33 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as TsconfigPaths from "tsconfig-paths";
import * as path from "path";
import * as Options from "./options";
import * as Logger from "./logger";
import { Stats } from "fs";
import * as fs from "fs";

export interface ResolverPlugin {
readonly apply: (resolver: Resolver) => void;
Expand Down Expand Up @@ -33,33 +33,11 @@ export type doResolve = (
callback: Callback
) => void;

export interface ResolverFileSystem {
readonly stat: (
path: string,
callback: (err: Error, stats: Stats) => void
) => void;
readonly readdir: (
path: string,
callback: (err: Error, files: ReadonlyArray<string>) => void
) => void;
readonly readFile: (
path: string,
callback: (err: Error, data: {}) => void
) => void;
readonly readlink: (
path: string,
callback: (err: Error, linkString: string) => void
) => void;
readonly readJson: (
path: string,
callback: (err: Error, json: {}) => void
) => void;
readonly statSync: (path: string) => Stats;
readonly readdirSync: (path: string) => ReadonlyArray<string>;
readonly readFileSync: (path: string) => {};
readonly readlinkSync: (path: string) => string;
readonly readJsonSync: (path: string) => {};
}
export type ReadJsonCallback = (error: Error | undefined, result?: {}) => void;

export type ReadJson = (path2: string, callback: ReadJsonCallback) => void;

export type ResolverFileSystem = typeof fs & { readJson?: ReadJson };

export interface ResolveContext {
log?: string;
Expand Down Expand Up @@ -286,10 +264,10 @@ function createPluginCallback(

// Don't allow other aliasing or raw request
if (result2 === undefined) {
return callback(null, null);
return callback(undefined, undefined);
}

callback(null, result2);
callback(undefined, result2);
}
);
}
Expand Down Expand Up @@ -356,20 +334,46 @@ function createPluginLegacy(
}

// don't allow other aliasing or raw request
callback(null, null);
callback(undefined, undefined);
}, callback)
);
}
);
};
}

function readJson(
fileSystem: ResolverFileSystem,
path2: string,
callback: ReadJsonCallback
): void {
if ("readJson" in fileSystem && fileSystem.readJson) {
return fileSystem.readJson(path2, callback);
}

fileSystem.readFile(path2, (err, buf) => {
if (err) {
return callback(err);
}

let data;

try {
data = JSON.parse(buf.toString("utf-8"));
} catch (e) {
return callback(e);
}

return callback(undefined, data);
});
}

function createReadJsonAsync(
filesystem: ResolverFileSystem
): TsconfigPaths.ReadJsonAsync {
// tslint:disable-next-line:no-any
return (path2: string, callback2: (err?: Error, content?: any) => void) => {
filesystem.readJson(path2, (err, json) => {
readJson(filesystem, path2, (err, json) => {
// If error assume file does not exist
if (err || !json) {
callback2();
Expand All @@ -387,7 +391,7 @@ function createFileExistAsync(
path2: string,
callback2: (err?: Error, exists?: boolean) => void
) => {
filesystem.stat(path2, (err: Error, stats: Stats) => {
filesystem.stat(path2, (err: Error, stats: fs.Stats) => {
// If error assume file does not exist
if (err) {
callback2(undefined, false);
Expand Down

0 comments on commit 27728f7

Please sign in to comment.