-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using typescript itselft to write tsconfig for more reliability
- Loading branch information
1 parent
ef64590
commit dfcdee6
Showing
7 changed files
with
146 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,93 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import ts from 'typescript' | ||
|
||
export function findTSConfigPath(targetPath: string) { | ||
return ts.findConfigFile(targetPath, ts.sys.fileExists, 'tsconfig.json') | ||
} | ||
|
||
export function readTSConfig(targetPath: string) { | ||
const configFilePath = findTSConfigPath(targetPath) | ||
if (!configFilePath) return null | ||
|
||
const configJsonString = ts.readConfigFile(configFilePath, ts.sys.readFile) | ||
if (configJsonString.error) { | ||
throw new Error( | ||
ts.formatDiagnostic(configJsonString.error, ts.createCompilerHost({})) | ||
) | ||
} | ||
|
||
const configObj = ts.parseJsonConfigFileContent( | ||
configJsonString.config, | ||
ts.sys, | ||
path.dirname(configFilePath) | ||
) | ||
|
||
return { | ||
...configObj.options, | ||
configDirPath: path.dirname(configFilePath), | ||
configFilePath, | ||
} | ||
} | ||
|
||
export function updateTSConfig( | ||
configFilePath: string, | ||
newConfig: { | ||
compilerOptions?: ts.CompilerOptions | ||
include?: string[] | ||
} = {}, | ||
|
||
force?: true | ||
) { | ||
if (force && !fs.existsSync(configFilePath)) { | ||
fs.writeFileSync(configFilePath, '{}', 'utf-8') | ||
} | ||
|
||
const configFileContent = fs.readFileSync(configFilePath, 'utf-8') | ||
const configObj = ts.parseConfigFileTextToJson( | ||
configFilePath, | ||
configFileContent | ||
) | ||
|
||
if (configObj.error) { | ||
throw new Error( | ||
ts.formatDiagnostic(configObj.error, ts.createCompilerHost({})) | ||
) | ||
} | ||
|
||
const updatedConfig = { | ||
...configObj.config, | ||
|
||
compilerOptions: { | ||
...configObj.config.compilerOptions, | ||
...newConfig.compilerOptions, | ||
}, | ||
|
||
include: [ | ||
...(configObj.config.include ?? []), | ||
...(newConfig.include ?? []), | ||
], | ||
} | ||
|
||
if (updatedConfig.include?.length) { | ||
const basePath = path.resolve( | ||
path.dirname(configFilePath), | ||
updatedConfig.compilerOptions.baseUrl ?? '' | ||
) | ||
|
||
const fullIncludePaths = updatedConfig.include.map((includePath: string) => | ||
path.resolve(basePath, includePath) | ||
) | ||
|
||
updatedConfig.include = [...new Set<string>(fullIncludePaths)] | ||
.map((includePath) => path.relative(basePath, includePath)) | ||
.filter(Boolean) | ||
} | ||
|
||
if (updatedConfig.include?.length === 0) { | ||
delete updatedConfig.include | ||
} | ||
|
||
const updatedConfigString = JSON.stringify(updatedConfig, null, 2) | ||
fs.writeFileSync(configFilePath, updatedConfigString, 'utf-8') | ||
} |
This file was deleted.
Oops, something went wrong.