Skip to content

Commit

Permalink
feat: support cjs extension for ormconfig (#6285)
Browse files Browse the repository at this point in the history
* support cjs extension for ormconfig

* fix linting errors

* handle cjs extensions in class loader
  • Loading branch information
dpickett committed Jul 7, 2020
1 parent 0ba2a48 commit 6eeb03a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/connection/ConnectionOptionsReader.ts
Expand Up @@ -80,7 +80,7 @@ export class ConnectionOptionsReader {
protected async load(): Promise<ConnectionOptions[]|undefined> {
let connectionOptions: ConnectionOptions|ConnectionOptions[]|undefined = undefined;

const fileFormats = ["env", "js", "ts", "json", "yml", "yaml", "xml"];
const fileFormats = ["env", "js", "cjs", "ts", "json", "yml", "yaml", "xml"];

// Detect if baseFilePath contains file extension
const possibleExtension = this.baseFilePath.substr(this.baseFilePath.lastIndexOf("."));
Expand All @@ -107,7 +107,7 @@ export class ConnectionOptionsReader {
if (PlatformTools.getEnvVariable("TYPEORM_CONNECTION") || PlatformTools.getEnvVariable("TYPEORM_URL")) {
connectionOptions = new ConnectionOptionsEnvReader().read();

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

} else if (foundFileFormat === "ts") {
Expand Down
4 changes: 2 additions & 2 deletions src/util/DirectoryExportedClassesLoader.ts
Expand Up @@ -4,7 +4,7 @@ import {Logger} from "../logger/Logger";
/**
* Loads all exported classes from the given directory.
*/
export function importClassesFromDirectories(logger: Logger, directories: string[], formats = [".js", ".ts"]): Function[] {
export function importClassesFromDirectories(logger: Logger, directories: string[], formats = [".js", ".cjs", ".ts"]): Function[] {

const logLevel = "info";
const classesNotFoundMessage = "No classes were found using the provided glob pattern: ";
Expand Down Expand Up @@ -54,4 +54,4 @@ export function importJsonsFromDirectories(directories: string[], format = ".jso
return allFiles
.filter(file => PlatformTools.pathExtname(file) === format)
.map(file => PlatformTools.load(PlatformTools.pathResolve(file)));
}
}
36 changes: 36 additions & 0 deletions test/github-issues/6284/issue-6284.ts
@@ -0,0 +1,36 @@
import {expect} from "chai";
import { writeFileSync, unlinkSync } from "fs";
import { ConnectionOptionsReader } from "../../../src/connection/ConnectionOptionsReader";
import { importClassesFromDirectories } from "../../../src/util/DirectoryExportedClassesLoader";
import { LoggerFactory } from "../../../src/logger/LoggerFactory";

describe("cli support for cjs extension", () => {
it("will load a cjs file", async () => {
const cjsConfigPath = [__dirname, "ormconfig.cjs"].join("/");
const databaseType = "postgres";
const config = `module.exports = {"type": "${databaseType}"};`;

writeFileSync(cjsConfigPath, config);
const reader = new ConnectionOptionsReader({root: __dirname });

const results = await reader.all();
expect(results).to.be.an("Array");
expect(results[0]).to.be.an("Object");
expect(results[0].type).to.equal(databaseType);


unlinkSync(cjsConfigPath);
});

it("loads cjs files via DirectoryExportedClassesloader", () => {
const klassPath = [__dirname, "klass.cjs"].join("/");
const klass = `module.exports.Widget = class Widget {};`;
writeFileSync(klassPath, klass);

const classes = importClassesFromDirectories(new LoggerFactory().create(), [`${__dirname}/*.cjs`]);
expect(classes).to.be.an("Array");
expect(classes.length).to.eq(1);

unlinkSync(klassPath);
});
});

0 comments on commit 6eeb03a

Please sign in to comment.