Skip to content

Instantly share code, notes, and snippets.

@JoshuaKGoldberg
Created May 10, 2020 17:37
Show Gist options
  • Save JoshuaKGoldberg/b8cec1c6d30e37a30c8a6ce8495a1d7c to your computer and use it in GitHub Desktop.
Save JoshuaKGoldberg/b8cec1c6d30e37a30c8a6ce8495a1d7c to your computer and use it in GitHub Desktop.
Basic Angular tslint-to-eslint-config converter generator
const { promisify } = require("util");
const { appendFile, writeFile } = require("fs").promises;
const inquirer = require("inquirer");
const exec = promisify(require("child_process").exec);
const execAndPrint = async (command) => {
console.log(`> ${command}`);
const { stdout, stderr } = await exec(command);
if (stderr) {
console.error(stderr);
}
if (stdout) {
console.log(stdout);
}
};
const main = async (ruleNameKebabCase) => {
const ruleNamePascalCase = ruleNameKebabCase
.split("-")
.map((text) => text[0].toUpperCase() + text.slice(1))
.join("");
const ruleNameCamelCase =
ruleNamePascalCase[0].toLowerCase() + ruleNamePascalCase.slice(1);
console.log(`Converting ${ruleNameKebabCase} (${ruleNameCamelCase})`);
await execAndPrint(`git checkout master`);
await execAndPrint(`git pull`);
await execAndPrint(`git checkout -b ${ruleNameKebabCase}`);
await writeFile(
`src/rules/converters/codelyzer/${ruleNameKebabCase}.ts`,
`import { RuleConverter } from "../../converter";
export const convert${ruleNamePascalCase}: RuleConverter = () => {
return {
rules: [
{
ruleName: "@angular-eslint/${ruleNameKebabCase}",
},
],
plugins: ["@angular-eslint/eslint-plugin"],
};
};
`
);
await writeFile(
`src/rules/converters/codelyzer/tests/${ruleNameKebabCase}.test.ts`,
`import { convert${ruleNamePascalCase} } from "../${ruleNameKebabCase}";
describe(convert${ruleNamePascalCase}, () => {
test("conversion without arguments", () => {
const result = convert${ruleNamePascalCase}({
ruleArguments: [],
});
expect(result).toEqual({
rules: [
{
ruleName: "@angular-eslint/${ruleNameKebabCase}",
},
],
plugins: ["@angular-eslint/eslint-plugin"],
});
});
});
`
);
await appendFile(
`src/rules/rulesConverters.ts`,
`
import { convert${ruleNamePascalCase} } from "./converters/codelyzer/${ruleNameKebabCase}";
["${ruleNameKebabCase}", convert${ruleNamePascalCase}],
`
);
await inquirer.prompt([
{
name: "_",
type: "input",
message: `Hit enter here once ${ruleNameKebabCase} is moved up in ruleConverters.`,
},
]);
await execAndPrint("git add src/");
await execAndPrint(
[
`git commit`,
`"Added Codelyzer ${ruleNameKebabCase} converter"`,
`"Another non-configurable rule. ⚡"`,
`"http://codelyzer.com/rules/${ruleNameKebabCase} / https://github.com/angular-eslint/angular-eslint/blob/master/packages/eslint-plugin/src/rules/${ruleNameKebabCase}.ts"`,
].join(" -m ")
);
await execAndPrint(`git push --set-upstream origin ${ruleNameKebabCase}`);
await execAndPrint(
`start https://github.com/typescript-eslint/tslint-to-eslint-config/pull/new/${ruleNameKebabCase}`
);
};
main(process.argv[2])
.then(() => console.log("Done ✔"))
.catch((error) => console.error({ error }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment