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

support cjs extension for ormconfig #6285

Merged
merged 3 commits into from Jul 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});