本文整理汇总了TypeScript中ts-simple-ast.Project类的典型用法代码示例。如果您正苦于以下问题:TypeScript Project类的具体用法?TypeScript Project怎么用?TypeScript Project使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Project类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: verifyDeclarationFile
export function verifyDeclarationFile() {
const project = new Project();
const declarationFile = project.addExistingSourceFile("ts-nameof.d.ts");
const declarationFileTests = project.addExistingSourceFile("lib/declarationFileTests.ts");
const diagnostics = [...declarationFile.getPreEmitDiagnostics(), ...declarationFileTests.getPreEmitDiagnostics()];
if (diagnostics.length > 0)
console.error(project.formatDiagnosticsWithColorAndContext(diagnostics));
}
示例2: createDeclarationFile
export function createDeclarationFile(project: Project) {
const mainFile = project.getSourceFileOrThrow("src/main.ts");
const outputFiles = mainFile.getEmitOutput({ emitOnlyDtsFiles: true }).getOutputFiles();
if (outputFiles.length !== 1)
throw new Error(`Expected 1 file when emitting, but had ${outputFiles.length}`);
const declarationFile = project.createSourceFile("ts-nameof.d.ts", outputFiles[0].getText(), { overwrite: true });
removePreceedingCommentReference();
commentExternalTypes();
removeTypeScriptImport();
wrapInGlobalModule();
addGlobalDeclarations();
function removePreceedingCommentReference() {
const firstChild = declarationFile.getFirstChildOrThrow();
declarationFile.removeText(0, firstChild.getStart());
}
function commentExternalTypes() {
// these types are made to be any so that this library will work when included in
// web projects and NodeJS does not exist. See issue #22.
const typesToComment = [
"ts.TransformerFactory<ts.SourceFile>",
"NodeJS.ErrnoException"
];
declarationFile.forEachDescendant(descendant => {
if (typesToComment.indexOf(descendant.getText()) >= 0)
descendant.replaceWithText(`any /* ${descendant.getText()} */`);
});
}
function removeTypeScriptImport() {
declarationFile.getImportDeclarationOrThrow("typescript").remove();
}
function wrapInGlobalModule() {
const fileText = declarationFile.getText();
declarationFile.removeText();
const apiModule = declarationFile.addNamespace({
hasDeclareKeyword: true,
declarationKind: NamespaceDeclarationKind.Module,
name: `"ts-nameof"`
});
apiModule.setBodyText(fileText);
apiModule.getVariableStatementOrThrow(s => s.getDeclarations().some(d => d.getName() === "api"))
.setHasDeclareKeyword(false);
}
function addGlobalDeclarations() {
const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
declarationFile.addStatements(writer => {
writer.newLine();
writer.write(globalFile.getText().replace(/\r?\n$/, ""));
});
}
}
示例3: loadFiles
export function loadFiles(project: Project, filePaths: string[]) {
const fileSystem = project.getFileSystem();
for (const filePath of filePaths) {
const fileText = readFileSync(filePath, {
encoding: "utf8"
});
fileSystem.writeFileSync(filePath, fileText);
}
}
示例4: createDeclarationFile
export function createDeclarationFile(project: Project) {
const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
const declarationFile = project.createSourceFile("ts-nameof.macro.d.ts", "", { overwrite: true });
const namespaceDec = declarationFile.addNamespace({
name: `"ts-nameof.macro"`,
declarationKind: NamespaceDeclarationKind.Module,
hasDeclareKeyword: true
});
namespaceDec.setBodyText(globalFile.getFullText());
for (const statement of namespaceDec.getStatements()) {
if (TypeGuards.isAmbientableNode(statement))
statement.setHasDeclareKeyword(false);
}
namespaceDec.addExportAssignment({
expression: "nameof",
isExportEquals: false
});
}
示例5: setupFixtures
/** setups and returns the fixtures for testing */
function setupFixtures() {
const basePath = process.cwd();
const buildPath = `${basePath}/tools/ts_library_builder/testdata`;
const outputFile = `${buildPath}/lib.output.d.ts`;
const inputProject = new Project({
compilerOptions: {
baseUrl: basePath,
declaration: true,
emitDeclarationOnly: true,
module: ModuleKind.AMD,
moduleResolution: ModuleResolutionKind.NodeJs,
strict: true,
stripInternal: true,
target: ScriptTarget.ESNext
}
});
inputProject.addExistingSourceFiles([
`${buildPath}/globals.ts`,
`${buildPath}/api.ts`
]);
const declarationProject = new Project({
compilerOptions: {},
useVirtualFileSystem: true
});
loadDtsFiles(declarationProject);
for (const { filePath, text } of inputProject.emitToMemory().getFiles()) {
declarationProject.createSourceFile(filePath, text);
}
const outputProject = new Project({
compilerOptions: {},
useVirtualFileSystem: true
});
loadDtsFiles(outputProject);
const outputSourceFile = outputProject.createSourceFile(outputFile);
const debug = true;
return {
basePath,
buildPath,
inputProject,
outputFile,
declarationProject,
outputProject,
outputSourceFile,
debug
};
}
示例6: checkDiagnostics
export function checkDiagnostics(project: Project, onlyFor?: string[]) {
const program = project.getProgram();
const diagnostics = [
...program.getGlobalDiagnostics(),
...program.getSyntacticDiagnostics(),
...program.getSemanticDiagnostics(),
...program.getDeclarationDiagnostics()
]
.filter(diagnostic => {
const sourceFile = diagnostic.getSourceFile();
return onlyFor && sourceFile
? onlyFor.includes(sourceFile.getFilePath())
: true;
})
.map(diagnostic => diagnostic.compilerObject);
logDiagnostics(diagnostics);
if (diagnostics.length) {
process.exit(1);
}
}
示例7: createProject
export function createProject(files: { fileName: string, content: string }[]): Project {
if (!project) {
project = new Project({
useVirtualFileSystem: true,
compilerOptions: {
target: ts.ScriptTarget.ES2016,
moduleResolution: ModuleResolutionKind.NodeJs,
// module: ModuleKind.CommonJS,
// noEmit: true,
// strict: true,
jsx: JsxEmit.React,
jsxFactory: 'JSXAlone.createElement',
libs: ["es2015", "dom"],
// typeRoots: ['node_modules/@types'],
},
})
const fs: FileSystemHost = project.getFileSystem();
// fs.writeFileSync(`node_modules/typescript/lib/lib.d.ts`, lib_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.d.ts`, lib_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es5.d.ts`, lib_es5_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.core.d.ts`, lib_es2015_core_d_ts);
fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.symbol.d.ts`, lib_es2015_symbol_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.iterable.d.ts`, lib_es2015_iterable_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.collection.d.ts`, lib_es2015_collection_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.d.ts`, lib_es2015_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.promise.d.ts`, lib_es2015_promise_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.proxy.d.ts`, lib_es2015_proxy_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.reflect.d.ts`, lib_es2015_reflect_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts`, lib_es2015_symbol_wellknown_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.scripthost.d.ts`, lib_scripthost_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.webworker.d.ts`, lib_webworker_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.webworker.importscripts.d.ts`, lib_webworker_importscripts_d_ts);
// fs.writeFileSync(`node_modules/typescript/lib/lib.dom.d.ts`, lib_dom_d_ts);
// fs.writeFileSync(`node_modules/jsx-alone/index.d.ts`, jsx_alone_core_d_ts);
// project.createSourceFile('lib.d.ts', lib_d_ts)
project.createSourceFile('lib.es5.d.ts', lib_es5_d_ts)
project.createSourceFile('lib.es2015.core.d.ts', lib_es2015_core_d_ts)
// project.createSourceFile('lib.es2015.symbol.d.ts', lib_es2015_symbol_d_ts)
project.createSourceFile('lib.es2015.iterable.d.ts', lib_es2015_iterable_d_ts)
project.createSourceFile('lib.dom.d.ts', lib_dom_d_ts)
// fs.writeFileSync(`node_modules/typescript/lib/lib.es2015.iterable.d.ts`, lib_es2015_iterable_d_ts);
// project.createSourceFile('node_modules/typescript/lib/lib.es5.d.ts', lib_es5_d_ts)
// project.createSourceFile('node_modules/typescript/lib/lib.es2015.core.d.ts', lib_es2015_core_d_ts)
// project.createSourceFile('node_modules/typescript/lib/lib.es2015.symbol.d.ts', lib_es2015_symbol_d_ts)
// project.createSourceFile('node_modules/typescript/lib/lib.es2015.iterable.d.ts', lib_es2015_iterable_d_ts)
// project.createSourceFile('node_modules/typescript/lib/lib.dom.d.ts', lib_dom_d_ts)
project.createSourceFile('index.d.ts', jsx_alone_core_d_ts)
files.forEach(f => project!.createSourceFile(f.fileName, f.content, { overwrite: true }))
}
else {
files.forEach(f => {
let sf = project!.getSourceFile(f.fileName)
if (!sf) {
// this only happens in tests
sf = project!.createSourceFile(f.fileName, f.content)
}
else if (sf!.getText() !== f.content) {
sf.replaceWithText(f.content)
}
})
}
project.saveSync()
return project
}
示例8: main
export function main({
basePath,
buildPath,
inline,
debug,
outFile,
silent
}: BuildLibraryOptions): void {
if (!silent) {
console.log("-----");
console.log("build_lib");
console.log();
console.log(`basePath: "${basePath}"`);
console.log(`buildPath: "${buildPath}"`);
if (inline && inline.length) {
console.log(`inline:`);
for (const filename of inline) {
console.log(` "${filename}"`);
}
}
console.log(`debug: ${!!debug}`);
console.log(`outFile: "${outFile}"`);
console.log();
}
// the inputProject will take in the TypeScript files that are internal
// to Deno to be used to generate the library
const inputProject = new Project({
compilerOptions: {
baseUrl: basePath,
declaration: true,
emitDeclarationOnly: true,
lib: [],
module: ModuleKind.AMD,
moduleResolution: ModuleResolutionKind.NodeJs,
noLib: true,
paths: {
"*": ["*", `${buildPath}/*`]
},
preserveConstEnums: true,
strict: true,
stripInternal: true,
target: ScriptTarget.ESNext
}
});
// Add the input files we will need to generate the declarations, `globals`
// plus any modules that are importable in the runtime need to be added here
// plus the `lib.esnext` which is used as the base library
inputProject.addExistingSourceFiles([
`${basePath}/node_modules/typescript/lib/lib.esnext.d.ts`,
`${basePath}/js/deno.ts`,
`${basePath}/js/globals.ts`
]);
// emit the project, which will be only the declaration files
const inputEmitResult = inputProject.emitToMemory();
const inputDiagnostics = inputEmitResult
.getDiagnostics()
.map(d => d.compilerObject);
logDiagnostics(inputDiagnostics);
if (inputDiagnostics.length) {
process.exit(1);
}
// the declaration project will be the target for the emitted files from
// the input project, these will be used to transfer information over to
// the final library file
const declarationProject = new Project({
compilerOptions: {
baseUrl: basePath,
moduleResolution: ModuleResolutionKind.NodeJs,
noLib: true,
paths: {
"*": ["*", `${buildPath}/*`]
},
strict: true,
target: ScriptTarget.ESNext
},
useVirtualFileSystem: true
});
// we don't want to add to the declaration project any of the original
// `.ts` source files, so we need to filter those out
const jsPath = normalizeSlashes(`${basePath}/js`);
const inputProjectFiles = inputProject
.getSourceFiles()
.map(sourceFile => sourceFile.getFilePath())
.filter(filePath => !filePath.startsWith(jsPath));
loadFiles(declarationProject, inputProjectFiles);
// now we add the emitted declaration files from the input project
for (const { filePath, text } of inputEmitResult.getFiles()) {
declarationProject.createSourceFile(filePath, text);
}
// the outputProject will contain the final library file we are looking to
// build
const outputProject = new Project({
//.........这里部分代码省略.........
示例9: addGlobalDeclarations
function addGlobalDeclarations() {
const globalFile = project.addExistingSourceFile("../../shared/lib/global.d.ts");
declarationFile.addStatements(writer => {
writer.newLine();
writer.write(globalFile.getText().replace(/\r?\n$/, ""));
});
}