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

feature/ts-support: Add new loader #19

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ It will check these files and return config file if found it.
- `.your-applicationrc.yml`
- `.your-applicationrc.yaml`
- `.your-applicationrc.js`
- `.your-applicationrc.cjs`
- `.your-applicationrc.ts`
- [optional] `package.json`
- if `packageJSON` option is enabled

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"debug": "^4.3.4",
"js-yaml": "^4.1.0",
"json5": "^2.2.1",
"require-from-string": "^2.0.2"
"require-from-string": "^2.0.2",
"typescript": "^4.8.4"
},
"devDependencies": {
"@types/mocha": "^9.1.0",
Expand All @@ -70,7 +71,6 @@
"mocha": "^9.2.2",
"prettier": "^2.6.2",
"ts-node": "^10.7.0",
"ts-node-test-register": "^10.0.0",
"typescript": "^4.6.3"
"ts-node-test-register": "^10.0.0"
}
}
24 changes: 23 additions & 1 deletion src/rc-config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import path from "path";
import fs from "fs";
import requireFromString from "require-from-string";
import JSON5 from "json5";
import ts from "typescript";

const debug = require("debug")("rc-config-loader");
const defaultLoaderByExt = {
".cjs": loadJSConfigFile,
".js": loadJSConfigFile,
".ts": loadTSConfigFile,
".json": loadJSONConfigFile,
".yaml": loadYAMLConfigFile,
".yml": loadYAMLConfigFile
Expand All @@ -19,7 +21,7 @@ const defaultOptions = {
// does look for `package.json`
packageJSON: false,
// treat default(no ext file) as some extension
defaultExtension: [".json", ".yaml", ".yml", ".js", ".cjs"],
defaultExtension: [".json", ".yaml", ".yml", ".ts", ".js", ".cjs"],
cwd: process.cwd()
};

Expand Down Expand Up @@ -192,6 +194,26 @@ function loadJSConfigFile(filePath: string, suppress: boolean) {
}
}

function loadTSConfigFile(filePath: string, supperes: boolean) {
debug(`Loading TypeScript config file: ${filePath}`);

try {
const code = fs.readFileSync(filePath, "utf-8");
const content = ts.transpile(code, {
module: ts.ModuleKind.Node16,
target: ts.ScriptTarget.ES5
});

return requireFromString(content, filePath).default;
} catch (error: any) {
debug(`Error reading TypeScript file: ${filePath}`);
if (!supperes) {
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
throw error;
}
}
}

function loadJSONConfigFile(filePath: string, suppress: boolean) {
debug(`Loading JSON config file: ${filePath}`);

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/.tsrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type Config = {
foo: "foo";
};

export default <Config>{
foo: "foo"
};
10 changes: 10 additions & 0 deletions test/rc-config-loader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ describe("rc-config-loader", () => {
assert.deepStrictEqual(config, { bar: "bar" });
});

it("should read ts config in current directory", () => {
const results = rcFile("ts", { cwd: path.join(__dirname, "fixtures") });

if (!results) {
throw new Error("not found");
}
const { config } = results;
assert.deepStrictEqual(config, { foo: "foo" });
});

it("should read cjs config in current directory", () => {
const results = rcFile("baz", { cwd: path.join(__dirname, "fixtures") });
if (!results) {
Expand Down