-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit VSCode settings for TypeScript (#41710)
This makes sure that the TypeScript in the workspace can be enabled. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import path from 'path' | ||
import { promises as fs } from 'fs' | ||
|
||
// Write .vscode settings to enable Next.js typescript plugin. | ||
export async function writeVscodeConfigurations( | ||
baseDir: string | ||
): Promise<void> { | ||
const vscodeSettings = path.join(baseDir, '.vscode', 'settings.json') | ||
let settings: any = {} | ||
let currentContent: string = '' | ||
|
||
try { | ||
currentContent = await fs.readFile(vscodeSettings, 'utf8') | ||
settings = JSON.parse(currentContent) | ||
} catch (err) {} | ||
|
||
const libPath = | ||
'.' + path.sep + path.join('node_modules', 'typescript', 'lib') | ||
if ( | ||
settings['typescript.tsdk'] === libPath && | ||
settings['typescript.enablePromptUseWorkspaceTsdk'] | ||
) { | ||
return | ||
} | ||
|
||
settings['typescript.tsdk'] = libPath | ||
settings['typescript.enablePromptUseWorkspaceTsdk'] = true | ||
|
||
const content = JSON.stringify(settings, null, 2) | ||
|
||
const vscodeFolder = path.join(baseDir, '.vscode') | ||
try { | ||
await fs.lstat(vscodeFolder) | ||
} catch (e) { | ||
await fs.mkdir(vscodeFolder, { recursive: true }) | ||
} | ||
|
||
await fs.writeFile(vscodeSettings, content) | ||
|
||
// Write to .gitignore if it exists | ||
const gitIgnore = path.join(baseDir, '.gitignore') | ||
try { | ||
const gitIgnoreContent = await fs.readFile(gitIgnore, 'utf8') | ||
if (!gitIgnoreContent.includes('.vscode')) { | ||
await fs.writeFile(gitIgnore, `${gitIgnoreContent}\n.vscode\n`) | ||
} | ||
} catch (e) { | ||
await fs.writeFile(gitIgnore, `vscode\n`) | ||
} | ||
} |
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