Skip to content

Commit 4f8aec5

Browse files
committed
Move the language options to a shared module
1 parent af0c4e7 commit 4f8aec5

3 files changed

Lines changed: 68 additions & 54 deletions

File tree

‎src/cli.ts‎

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,16 @@
11
#!/usr/bin/env node
22

33
import { defineCommand, runMain } from "citty";
4-
import { BuiltInParserName, format as prettierFormat } from "prettier";
4+
import { format as prettierFormat } from "prettier";
55
import { format as sqlFormat, SqlLanguage, supportedDialects } from "sql-formatter";
6-
7-
type PrettierLanguage =
8-
| "angular"
9-
| "css"
10-
| "glimmer"
11-
| "graphql"
12-
| "html"
13-
| "javascript"
14-
| "json"
15-
| "json5"
16-
| "less"
17-
| "lwc"
18-
| "markdown"
19-
| "mdx"
20-
| "scss"
21-
| "typescript"
22-
| "vue"
23-
| "yaml";
24-
25-
type Language = PrettierLanguage | "sql" | "sql-output";
26-
27-
const prettierParsers: Record<PrettierLanguage, BuiltInParserName> = {
28-
angular: "angular",
29-
css: "css",
30-
glimmer: "glimmer",
31-
graphql: "graphql",
32-
html: "html",
33-
javascript: "babel",
34-
json5: "json5",
35-
json: "json",
36-
less: "less",
37-
lwc: "lwc",
38-
markdown: "markdown",
39-
mdx: "mdx",
40-
scss: "scss",
41-
typescript: "typescript",
42-
vue: "vue",
43-
yaml: "yaml",
44-
};
45-
46-
const languages: Language[] = [...(Object.keys(prettierParsers) as PrettierLanguage[]), "sql", "sql-output"];
47-
48-
function isSqlLanguage(value: string): value is SqlLanguage {
49-
return supportedDialects.includes(value);
50-
}
51-
52-
function parsePrintWidth(value: string): number | undefined {
53-
const printWidth = Number(value);
54-
return Number.isInteger(printWidth) && printWidth > 0 ? printWidth : undefined;
55-
}
6+
import {
7+
isLanguage,
8+
isSqlLanguage,
9+
Language,
10+
languages,
11+
parsePrintWidth,
12+
prettierParsers,
13+
} from "./options.js";
5614

5715
function formatSQL(input: string, sqlDialect: SqlLanguage): string {
5816
return sqlFormat(input, {
@@ -136,11 +94,11 @@ const main = defineCommand({
13694
},
13795
},
13896
async run({ args }) {
139-
const language = args.language as Language;
97+
const language = args.language;
14098
const printWidth = parsePrintWidth(args["print-width"]);
14199
const sqlDialect = args["sql-dialect"];
142100

143-
if (!languages.includes(language)) {
101+
if (!isLanguage(language)) {
144102
console.error(`Unknown language: ${language}`);
145103
console.error(`Languages: ${languages.join(", ")}`);
146104
process.exit(1);

‎src/options.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { BuiltInParserName } from "prettier";
2+
import { SqlLanguage, supportedDialects } from "sql-formatter";
3+
4+
export type PrettierLanguage =
5+
| "angular"
6+
| "css"
7+
| "glimmer"
8+
| "graphql"
9+
| "html"
10+
| "javascript"
11+
| "json"
12+
| "json5"
13+
| "less"
14+
| "lwc"
15+
| "markdown"
16+
| "mdx"
17+
| "scss"
18+
| "typescript"
19+
| "vue"
20+
| "yaml";
21+
22+
export type Language = PrettierLanguage | "sql" | "sql-output";
23+
24+
export const prettierParsers: Record<PrettierLanguage, BuiltInParserName> = {
25+
angular: "angular",
26+
css: "css",
27+
glimmer: "glimmer",
28+
graphql: "graphql",
29+
html: "html",
30+
javascript: "babel",
31+
json5: "json5",
32+
json: "json",
33+
less: "less",
34+
lwc: "lwc",
35+
markdown: "markdown",
36+
mdx: "mdx",
37+
scss: "scss",
38+
typescript: "typescript",
39+
vue: "vue",
40+
yaml: "yaml",
41+
};
42+
43+
export const languages: Language[] = [...(Object.keys(prettierParsers) as PrettierLanguage[]), "sql", "sql-output"];
44+
45+
export function isLanguage(value: string): value is Language {
46+
return (languages as string[]).includes(value);
47+
}
48+
49+
export function isSqlLanguage(value: string): value is SqlLanguage {
50+
return supportedDialects.includes(value);
51+
}
52+
53+
export function parsePrintWidth(value: string): number | undefined {
54+
const printWidth = Number(value);
55+
return Number.isInteger(printWidth) && printWidth > 0 ? printWidth : undefined;
56+
}

‎tsconfig.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://json.schemastore.org/tsconfig",
3-
"include": ["src/cli.ts"],
3+
"include": ["src"],
44
"compilerOptions": {
55
"lib": ["es2021"],
66
"module": "NodeNext",

0 commit comments

Comments
 (0)