Skip to content

Commit

Permalink
refactor: only use PlatformTools.load for optional dependencies (#6630)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Sep 3, 2020
1 parent 2b5f139 commit 490ad0d
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 95 deletions.
53 changes: 46 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Expand Up @@ -47,15 +47,20 @@
"oracle-orm"
],
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/chai": "^4.1.2",
"@types/chai-as-promised": "7.1.0",
"@types/debug": "4.1.2",
"@types/debug": "^4.1.2",
"@types/dotenv": "^8.2.0",
"@types/js-yaml": "^3.12.5",
"@types/mkdirp": "^1.0.1",
"@types/mocha": "^5.2.6",
"@types/node": "^9.6.0",
"@types/rimraf": "^2.0.2",
"@types/sha.js": "^2.4.0",
"@types/sinon": "^7.0.8",
"@types/source-map-support": "^0.4.2",
"@types/xml2js": "^0.4.5",
"@types/yargs": "^12.0.9",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
Expand Down Expand Up @@ -103,7 +108,7 @@
"chalk": "^2.4.2",
"cli-highlight": "^2.0.0",
"debug": "^4.1.1",
"dotenv": "^6.2.0",
"dotenv": "^8.2.0",
"glob": "^7.1.2",
"js-yaml": "^3.13.1",
"mkdirp": "^1.0.3",
Expand Down
7 changes: 5 additions & 2 deletions src/cache/RedisQueryResultCache.ts
Expand Up @@ -185,8 +185,11 @@ export class RedisQueryResultCache implements QueryResultCache {
*/
protected loadRedis(): any {
try {
return PlatformTools.load(this.clientType);

if (this.clientType === "ioredis/cluster") {
return PlatformTools.load("ioredis");
} else {
return PlatformTools.load(this.clientType);
}
} catch (e) {
throw new Error(`Cannot use cache because ${this.clientType} is not installed. Please run "npm i ${this.clientType} --save".`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CommandUtils.ts
@@ -1,6 +1,6 @@
import * as fs from "fs";
import * as path from "path";
const mkdirp = require("mkdirp");
import mkdirp from "mkdirp";

/**
* Command line utils functions.
Expand Down
12 changes: 6 additions & 6 deletions src/connection/ConnectionOptionsReader.ts
@@ -1,3 +1,5 @@
import dotenv from "dotenv";
import appRootPath from "app-root-path";
import {ConnectionOptions} from "./ConnectionOptions";
import {PlatformTools} from "../platform/PlatformTools";
import {ConnectionOptionsEnvReader} from "./options-reader/ConnectionOptionsEnvReader";
Expand Down Expand Up @@ -93,10 +95,8 @@ export class ConnectionOptionsReader {

// if .env file found then load all its variables into process.env using dotenv package
if (foundFileFormat === "env") {
const dotenv = PlatformTools.load("dotenv");
dotenv.config({ path: this.baseFilePath });
} else if (PlatformTools.fileExist(".env")) {
const dotenv = PlatformTools.load("dotenv");
dotenv.config({ path: ".env" });
}

Expand All @@ -108,13 +108,13 @@ export class ConnectionOptionsReader {
connectionOptions = new ConnectionOptionsEnvReader().read();

} else if (foundFileFormat === "js" || foundFileFormat === "cjs") {
connectionOptions = await PlatformTools.load(configFile);
connectionOptions = await require(configFile);

} else if (foundFileFormat === "ts") {
connectionOptions = await PlatformTools.load(configFile);
connectionOptions = await require(configFile);

} else if (foundFileFormat === "json") {
connectionOptions = PlatformTools.load(configFile);
connectionOptions = require(configFile);

} else if (foundFileFormat === "yml") {
connectionOptions = new ConnectionOptionsYmlReader().read(configFile);
Expand Down Expand Up @@ -200,7 +200,7 @@ export class ConnectionOptionsReader {
if (this.options && this.options.root)
return this.options.root;

return PlatformTools.load("app-root-path").path;
return appRootPath.path;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/connection/options-reader/ConnectionOptionsXmlReader.ts
@@ -1,3 +1,4 @@
import {parseString as xmlParser} from 'xml2js';
import {PlatformTools} from "../../platform/PlatformTools";
import {ConnectionOptions} from "../ConnectionOptions";

Expand Down Expand Up @@ -43,11 +44,10 @@ export class ConnectionOptionsXmlReader {
* Reads xml file contents and returns them in a promise.
*/
protected readXml(path: string): Promise<any> {
const xmlParser = PlatformTools.load("xml2js").parseString;
const xmlOptions = { trim: true, explicitRoot: false };
return new Promise((ok, fail) => {
xmlParser(PlatformTools.readFileSync(path), xmlOptions, (err: any, result: any) => err ? fail(err) : ok(result));
});
}

}
}
14 changes: 11 additions & 3 deletions src/connection/options-reader/ConnectionOptionsYmlReader.ts
@@ -1,3 +1,4 @@
import ymlParser from 'js-yaml';
import {PlatformTools} from "../../platform/PlatformTools";
import {ConnectionOptions} from "../ConnectionOptions";

Expand All @@ -14,11 +15,18 @@ export class ConnectionOptionsYmlReader {
* Reads connection options from given yml file.
*/
read(path: string): ConnectionOptions[] {
const ymlParser = PlatformTools.load("js-yaml");
const config = ymlParser.safeLoad(PlatformTools.readFileSync(path));
const contentsBuffer = PlatformTools.readFileSync(path);
const contents = contentsBuffer.toString();

const config: undefined | string | {[key: string]: any} = ymlParser.safeLoad(contents);

if (typeof config !== 'object') {
return [];
}

return Object.keys(config).map(connectionName => {
return Object.assign({ name: connectionName }, config[connectionName]);
});
}

}
}
10 changes: 5 additions & 5 deletions src/driver/better-sqlite3/BetterSqlite3Driver.ts
@@ -1,3 +1,5 @@
import mkdirp from 'mkdirp';
import path from 'path';
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError";
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError";
import { PlatformTools } from "../../platform/PlatformTools";
Expand Down Expand Up @@ -88,7 +90,7 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
if (this.options.database !== ":memory:")
await this.createDatabaseDirectory(this.options.database);

const {
const {
database,
readonly = false,
fileMustExist = false,
Expand Down Expand Up @@ -132,10 +134,8 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
/**
* Auto creates database directory if it does not exist.
*/
protected createDatabaseDirectory(fullPath: string): Promise<void> {
const mkdirp = PlatformTools.load("mkdirp");
const path = PlatformTools.load("path");
return mkdirp(path.dirname(fullPath));
protected async createDatabaseDirectory(fullPath: string): Promise<void> {
await mkdirp(path.dirname(fullPath));
}

}
3 changes: 2 additions & 1 deletion src/driver/react-native/ReactNativeDriver.ts
Expand Up @@ -5,6 +5,7 @@ import {QueryRunner} from "../../query-runner/QueryRunner";
import {Connection} from "../../connection/Connection";
import {DriverOptionNotSetError} from "../../error/DriverOptionNotSetError";
import {DriverPackageNotInstalledError} from "../../error/DriverPackageNotInstalledError";
import {PlatformTools} from "../../platform/PlatformTools";

export class ReactNativeDriver extends AbstractSqliteDriver {
options: ReactNativeConnectionOptions;
Expand Down Expand Up @@ -89,7 +90,7 @@ export class ReactNativeDriver extends AbstractSqliteDriver {
*/
protected loadDependencies(): void {
try {
this.sqlite = require("react-native-sqlite-storage");
this.sqlite = PlatformTools.load("react-native-sqlite-storage");

} catch (e) {
throw new DriverPackageNotInstalledError("React-Native", "react-native-sqlite-storage");
Expand Down
8 changes: 4 additions & 4 deletions src/driver/sqlite/SqliteDriver.ts
@@ -1,3 +1,5 @@
import mkdirp from 'mkdirp';
import path from 'path';
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError";
import { SqliteQueryRunner } from "./SqliteQueryRunner";
import { DriverOptionNotSetError } from "../../error/DriverOptionNotSetError";
Expand Down Expand Up @@ -132,10 +134,8 @@ export class SqliteDriver extends AbstractSqliteDriver {
/**
* Auto creates database directory if it does not exist.
*/
protected createDatabaseDirectory(fullPath: string): Promise<void> {
const mkdirp = PlatformTools.load("mkdirp");
const path = PlatformTools.load("path");
return mkdirp(path.dirname(fullPath));
protected async createDatabaseDirectory(fullPath: string): Promise<void> {
await mkdirp(path.dirname(fullPath));
}

}

0 comments on commit 490ad0d

Please sign in to comment.