-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create TSConfig in CLI (#1065)
- Loading branch information
1 parent
1031fb8
commit 6d66193
Showing
10 changed files
with
177 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
import { writeFile } from "mz/fs"; | ||
|
||
import { ProjectDescription } from "../initializeProject/shared"; | ||
import { initializeSources } from "../sources"; | ||
import { initializeImports } from "./imports"; | ||
import { initializeRenames } from "./renames"; | ||
import { createJavaScriptConfig } from "./createJavaScriptConfig"; | ||
|
||
export interface InitializeJavaScriptSettings { | ||
fileName: string; | ||
project: string; | ||
project: ProjectDescription; | ||
} | ||
|
||
export const initializeJavaScript = async ({ fileName, project }: InitializeJavaScriptSettings) => { | ||
const sourceFiles = await initializeSources({ fromJavaScript: true }); | ||
const sourceFiles = await initializeSources({ fromJavaScript: true, project }); | ||
const renames = await initializeRenames(); | ||
const imports = await initializeImports(); | ||
|
||
const settings = await createJavaScriptConfig({ imports, project, sourceFiles, renames }); | ||
const settings = createJavaScriptConfig({ imports, project, sourceFiles, renames }); | ||
await writeFile(fileName, JSON.stringify(settings, undefined, 4)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/initialization/initializeProject/initializeNewProject.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { fs } from "mz"; | ||
import { prompt } from "enquirer"; | ||
import { ProjectDescription } from "./shared"; | ||
|
||
const filePath = "./tsconfig.json"; | ||
|
||
export const initializeNewProject = async (): Promise<ProjectDescription> => { | ||
const { emit } = await prompt<{ emit: string }>([ | ||
{ | ||
choices: ["yes", "no"], | ||
message: "Should TypeScript output .js files from .ts sources?", | ||
name: "emit", | ||
type: "select", | ||
}, | ||
]); | ||
const { inBrowser } = await prompt<{ inBrowser: string }>([ | ||
{ | ||
choices: ["yes", "no"], | ||
message: "Does your code run in browser?", | ||
name: "inBrowser", | ||
type: "select", | ||
}, | ||
]); | ||
const jsx = | ||
inBrowser && | ||
( | ||
await prompt<{ jsx: string }>([ | ||
{ | ||
choices: ["yes", "no"], | ||
message: "Does your project use JSX?", | ||
name: "jsx", | ||
type: "select", | ||
}, | ||
]) | ||
).jsx; | ||
const { target } = await prompt<{ target: string }>([ | ||
{ | ||
choices: ["es2020", "es2021", "es2022", "esnext"], | ||
message: "What minimum runtime does your code run on?", | ||
name: "target", | ||
type: "select", | ||
}, | ||
]); | ||
const { strict } = await prompt<{ strict: string }>([ | ||
{ | ||
choices: ["yes", "no"], | ||
message: "Would you like to enable TypeScript's strict compiler options? (recommended)", | ||
name: "strict", | ||
type: "select", | ||
}, | ||
]); | ||
|
||
await fs.writeFile( | ||
filePath, | ||
JSON.stringify( | ||
{ | ||
compilerOptions: { | ||
declaration: true, | ||
declarationMap: true, | ||
...(emit === "no" && { noEmit: true }), | ||
esModuleInterop: true, | ||
...(inBrowser === "no" && { lib: [target] }), | ||
...(jsx === "yes" && { jsx: "react" }), | ||
sourceMap: true, | ||
moduleResolution: "node", | ||
module: "nodenext", | ||
skipLibCheck: true, | ||
...(strict === "yes" ? { strict: true } : {}), | ||
target, | ||
}, | ||
}, | ||
null, | ||
4, | ||
), | ||
); | ||
|
||
return { created: true, filePath }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export enum TSConfigLocationSuggestion { | ||
Custom = "custom", | ||
DoesNotExist = "I don't have one yet", | ||
} | ||
|
||
export enum TSConfigLocation { | ||
Root = "./tsconfig.json", | ||
UnderSrc = "./src/tsconfig.json", | ||
} | ||
|
||
export interface ProjectDescription { | ||
created?: boolean; | ||
filePath: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.