|
| 1 | +# API |
| 2 | + |
| 3 | +You can use `tslint-to-eslint-config` programmatically in your Node apps. |
| 4 | +It provides a **[`convertTSLintConfig`](#convertTSLintConfig)** function to find relevant configurations on disk and output the generated ESLint configuration. |
| 5 | + |
| 6 | +## `convertTSLintConfig` |
| 7 | + |
| 8 | +```ts |
| 9 | +import { convertTSLintConfig } from "tslint-to-eslint-config"; |
| 10 | + |
| 11 | +const result = await convertTSLintConfig(); |
| 12 | +``` |
| 13 | + |
| 14 | +Finds relevant configurations on disk and outputs the generated ESLint configuration. |
| 15 | + |
| 16 | +Optionally takes in the same settings you can provide via the CLI: |
| 17 | + |
| 18 | +* `config`: Output ESLint configuration file path _(default: `.eslintrc.js`)_. |
| 19 | +* `eslint`: Original ESLint configuration file path _(default: `.eslintrc.js`)_. |
| 20 | +* `package`: Original packages configuration file path _(default: `package.json`)_. |
| 21 | +* `prettier`: Whether to add `eslint-config-prettier` to the plugins list. |
| 22 | +* `tslint`: Original TSLint configuration file path _(default: `tslint.json`)_. |
| 23 | +* `typescript`: Original TypeScript configuration file path _(default: `tsconfig.json`)_. |
| 24 | + |
| 25 | +```ts |
| 26 | +import { convertTSLintConfig } from "tslint-to-eslint-config"; |
| 27 | + |
| 28 | +const result = await convertTSLintConfig({ |
| 29 | + config: "./path/to/output/eslintrc.js", |
| 30 | + eslint: "./path/to/input/eslintrc.js", |
| 31 | + package: "./path/to/package.json", |
| 32 | + prettier: true, // Prettier: highly recommended! |
| 33 | + tslint: "./path/to/tslint.json", |
| 34 | + typescript: "./path/to/tsconfig.json", |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +If the TSLint configuration or any manually specified configurations fail to read from disk, the result will contain: |
| 39 | + |
| 40 | +* `complaints`: String complaints describing the errors. |
| 41 | +* `status`: `ResultStatus.ConfigurationError` (`2`). |
| 42 | + |
| 43 | +If no error is detected, the result will contain: |
| 44 | + |
| 45 | +* `data`: Resultant ESLint configuration as: |
| 46 | + * `formatted`: Stringified result per the output config path's file type. |
| 47 | + * `raw`: Plain old JavaScript object. |
| 48 | +* `status`: `ResultStatus.Succeeded` (`0`). |
| 49 | + |
| 50 | +```ts |
| 51 | +import { convertTSLintConfig, ResultStatus } from "tslint-to-eslint-config"; |
| 52 | + |
| 53 | +const result = await convertTSLintConfig({ /* ... */ }); |
| 54 | + |
| 55 | +if (result.status !== ResultStatus.Succeeded) { |
| 56 | + console.info("Oh no!"); |
| 57 | + console.error(result.complaints.join("\n")); |
| 58 | +} else { |
| 59 | + console.info("Hooray!"); |
| 60 | + console.log(result.data.formatted); |
| 61 | + console.log(result.data.raw); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +> See the provided `.d.ts` TypeScript typings for full descriptions of inputs and outputs. |
| 66 | +
|
| 67 | +## Standalone API |
| 68 | + |
| 69 | +> ⚠ This area of code is still considered experimental. |
| 70 | +> Use at your own risk. |
| 71 | +> Please file an issue on GitHub if you'd like to see changes. |
| 72 | +
|
| 73 | +Portions of the individual steps within `convertTSLintConfig` are each available as exported functions as well. |
| 74 | + |
| 75 | +* **[`findOriginalConfigurations`](#findOriginalConfigurations)** takes in an object of original configuration locations and retrieves their raw and computed contents. |
| 76 | + * **[`findReportedConfiguration`](#findReportedConfiguration)** runs a config print command and parses its output as JSON. |
| 77 | +* **[`createESLintConfiguration`](#createESLintConfiguration)** creates an raw output ESLint configuration summary from those input configuration values. |
| 78 | + * `joinConfigConversionResults` turns a raw ESLint configuration summary into ESLint's configuration shape. |
| 79 | + * `formatOutput` prints that formatted output into a string per the output file extension. |
| 80 | + |
| 81 | +### `findOriginalConfigurations` |
| 82 | + |
| 83 | +Reading in from the default file locations, including `.eslintrc.js`: |
| 84 | + |
| 85 | +```ts |
| 86 | +import { findOriginalConfigurations } from "tslint-to-eslint-config"; |
| 87 | + |
| 88 | +const originalConfigurations = await findOriginalConfigurations(); |
| 89 | +``` |
| 90 | + |
| 91 | +Overriding some configuration file locations to read from: |
| 92 | + |
| 93 | +```ts |
| 94 | +import { findOriginalConfigurations } from "tslint-to-eslint-config"; |
| 95 | + |
| 96 | +const originalConfigurations = await findOriginalConfigurations({ |
| 97 | + config: "./path/to/.eslintrc.json", |
| 98 | + tslint: "./another/path/to/tslint.custom.json", |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +#### `findReportedConfiguration` |
| 103 | + |
| 104 | +Retrieving the reported contents of a TSLint configuration: |
| 105 | + |
| 106 | +```ts |
| 107 | +import { findReportedConfiguration } from "tslint-to-eslint-config"; |
| 108 | + |
| 109 | +const full = await findReportedConfiguration("npx tslint --print-config", "./tslint.json"); |
| 110 | +``` |
| 111 | + |
| 112 | +### `createESLintConfiguration` |
| 113 | + |
| 114 | +Generating an ESLint configuration from the contents of a local `tslint.json`: |
| 115 | + |
| 116 | +```ts |
| 117 | +import { createESLintConfiguration, findReportedConfiguration } from "tslint-to-eslint-config"; |
| 118 | + |
| 119 | +const summarizedConfiguration = await createESLintConfiguration({ |
| 120 | + tslint: { |
| 121 | + full: await findReportedConfiguration("npx tslint --print-config", "./tslint.json"), |
| 122 | + raw: require("./tslint.json"), |
| 123 | + }, |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +Using the full configuration values from disk: |
| 128 | + |
| 129 | +```ts |
| 130 | +import { createESLintConfiguration, findOriginalConfigurations } from "tslint-to-eslint-config"; |
| 131 | + |
| 132 | +const originalConfigurations = await findOriginalConfigurations(); |
| 133 | +const summarizedConfiguration = await createESLintConfiguration(originalConfigurations); |
| 134 | + |
| 135 | +const raw = joinConfigConversionResults(summarizedConfiguration, originalConfigurations.data); |
| 136 | + |
| 137 | +const formatted = formatOutput("eslintrc.js", raw); |
| 138 | +``` |
0 commit comments