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

fix(betterer 🐛): make typescript properly optional #505

Merged
merged 1 commit into from
Feb 17, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .betterer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { eslint } from '@betterer/eslint';
import { regexp } from '@betterer/regexp';

export default {
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ npx @betterer/cli init
To do it yourself, run the following from the root of your project:

```bash
npm install @betterer/cli typescript --save-dev
npm install @betterer/cli --save-dev
```

You'll then need to make your own test file and test commands.

_Note: Typescript is required to execute the `betterer` command. Your code does not have to be written in TS to use Betterer! Referencing it as a peer dependency helps to prevent unexpected behavior._

---

## Why?
Expand Down
3 changes: 3 additions & 0 deletions packages/betterer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"ts-node": "^9.0.0",
"tslib": "^2.0.3"
},
"optionalDependencies": {
"typescript": ">=2.7"
},
"devDependencies": {
"@types/callsite": "^1.0.30",
"@types/prettier": "^1.12.0"
Expand Down
39 changes: 26 additions & 13 deletions packages/betterer/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RegisterOptions, register } from 'ts-node';
import type { RegisterOptions } from 'ts-node';

import { BettererConfig } from './config';

Expand All @@ -9,7 +9,7 @@ export const TS_EXTENSION = '.ts';
type Extensions = typeof require.extensions;

let isRegistered = false;
export function registerExtensions(config: BettererConfig): void {
export async function registerExtensions(config: BettererConfig): Promise<void> {
if (isRegistered) {
return;
}
Expand All @@ -23,21 +23,34 @@ export function registerExtensions(config: BettererConfig): void {
const JS = EXTENSIONS[JS_EXTENSION];

if (!EXTENSIONS[TS_EXTENSION]) {
// Use TS-Node register to allow `.betterer.ts` config files:
const tsRegisterOptions: RegisterOptions = {
transpileOnly: true,
compilerOptions: {
module: 'commonjs'
}
};
if (config.tsconfigPath) {
tsRegisterOptions.project = config.tsconfigPath;
}
register(tsRegisterOptions);
await registerTypeScript(config);
}

// Force `.betterer.results` files to be loaded as JS:
EXTENSIONS[RESULTS_EXTENTION] = (m: NodeModule, filePath: string): void => {
JS(m, filePath);
};
}

async function registerTypeScript(config: BettererConfig): Promise<void> {
let tsNode: typeof import('ts-node');
try {
await import('typescript');
tsNode = await import('ts-node');
} catch {
// Environment doesn't have TypeScript available, move on!
return;
}

// Use TS-Node register to allow `.betterer.ts` config files:
const tsRegisterOptions: RegisterOptions = {
transpileOnly: true,
compilerOptions: {
module: 'commonjs'
}
};
if (config.tsconfigPath) {
tsRegisterOptions.project = config.tsconfigPath;
}
tsNode.register(tsRegisterOptions);
}
2 changes: 1 addition & 1 deletion packages/betterer/src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class BettererRunnerΩ implements BettererRunner {
let reporter = loadReporters([DEFAULT_REPORTER]);
try {
config = await createConfig(partialConfig);
registerExtensions(config);
await registerExtensions(config);
if (config.silent) {
reporter = loadReporters([]);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9280,7 +9280,7 @@ typedarray@^0.0.6:
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@^4.1.3:
typescript@>=2.7, typescript@^4.1.3:
version "4.1.5"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
Expand Down