这就是我在 python 脚本中修改 tsconfig.json 文件的方法。但正如您所看到的,大部分内容是完全相同的,因此这不是一个非常优雅的解决方案。 由于我是 python 新手,我需要一些帮助如何更有效地编写此代码。
还有另一种
if
情况,我必须添加单个参数(例如"noUncheckedIndexedAccess": False
)或将现有参数设置为不同的值(False
)。
with open('/'.join([directory, "tsconfig.json"]),'r+') as file:
file_data = json.load(file)
if plugin == "node":
file_data["compilerOptions"] = {
"module": "commonjs",
"strict": True,
"alwaysStrict": True,
"noImplicitAny": True,
"noImplicitThis": True,
"strictNullChecks": True,
"strictBindCallApply": True,
"strictFunctionTypes": True,
"strictPropertyInitialization": True,
"useUnknownInCatchVariables": True,
"noUnusedLocals": True,
"noUnusedParameters": True,
"allowUnusedLabels": True,
"allowUnreachableCode": True,
"noImplicitOverride": True,
"noImplicitReturns": True,
"noUncheckedIndexedAccess": True,
"noPropertyAccessFromIndexSignature": True,
"noFallthroughCasesInSwitch": True,
"exactOptionalPropertyTypes": True,
"forceConsistentCasingInFileNames": True
}
elif plugin == 'react':
file_data["compilerOptions"] = {
"jsx": "react-jsx",
"allowJs": False,
"esModuleInterop": False,
"allowSyntheticDefaultImports": True,
"strict": True,
"alwaysStrict": True,
"noImplicitAny": True,
"noImplicitThis": True,
"strictNullChecks": True,
"strictBindCallApply": True,
"strictFunctionTypes": True,
"strictPropertyInitialization": True,
"useUnknownInCatchVariables": True,
"noUnusedLocals": True,
"noUnusedParameters": True,
"allowUnusedLabels": True,
"allowUnreachableCode": True,
"noImplicitOverride": True,
"noImplicitReturns": True,
"noUncheckedIndexedAccess": True,
"noPropertyAccessFromIndexSignature": True,
"noFallthroughCasesInSwitch": True,
"exactOptionalPropertyTypes": True,
"forceConsistentCasingInFileNames": True
}
file.seek(0)
json.dump(file_data, file, indent = 2)
你应该弄清楚什么是共同点,什么是独特的。然后你可以拥有三个“静态”字典 - 一个用于公共数据,一个用于“react”特有的数据,另一个用于“node”特有的数据。
另请注意,当重写以 r+ 模式打开的文件时,您应该确保调用 truncate() 除非您绝对确定该文件的大小只会增加。即使大小确实增加,调用 truncate() 也是良性的,因此是最佳实践。
import json
COMMON = {
"strict": True,
"alwaysStrict": True,
"noImplicitAny": True,
"noImplicitThis": True,
"strictNullChecks": True,
"strictBindCallApply": True,
"strictFunctionTypes": True,
"strictPropertyInitialization": True,
"useUnknownInCatchVariables": True,
"noUnusedLocals": True,
"noUnusedParameters": True,
"allowUnusedLabels": True,
"allowUnreachableCode": True,
"noImplicitOverride": True,
"noImplicitReturns": True,
"noUncheckedIndexedAccess": True,
"noPropertyAccessFromIndexSignature": True,
"noFallthroughCasesInSwitch": True,
"exactOptionalPropertyTypes": True,
"forceConsistentCasingInFileNames": True,
}
NODE = {"module": "commonjs"}
REACT = {
"jsx": "react-jsx",
"allowJs": False,
"esModuleInterop": False,
"allowSyntheticDefaultImports": True,
}
def update(filename, plugin):
with open(filename, "r+") as file:
data = json.load(file)
data["compilerOptions"] = COMMON
match plugin:
case "node":
data["compilerOptions"].update(NODE)
case "react":
data["compilerOptions"].update(REACT)
case _:
pass
file.seek(0)
json.dump(data, file, indent=4)
file.truncate()
update("tsconfig.json", "node")