-
Notifications
You must be signed in to change notification settings - Fork 229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Import typespec project into a single file #4383
base: main
Are you sure you want to change the base?
Changes from 19 commits
0f3b89c
c22b89d
9ae1564
5c95afe
1ccb4b6
2b8e2aa
1a93dd4
3044f7d
2a6af84
cae4de4
f38faa8
7865753
3eba979
1ada5db
25234d8
a437f30
cc02138
a50b535
d243bf1
198d7ef
af0f383
ec421a2
9120da5
d8a9d00
a247fef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,7 +520,7 @@ export function printComment( | |
case SyntaxKind.BlockComment: | ||
return printBlockComment(commentPath as AstPath<BlockComment>, options); | ||
case SyntaxKind.LineComment: | ||
return `${options.originalText.slice(comment.pos, comment.end).trimEnd()}`; | ||
return `${getRawText(comment, options).trimEnd()}`; | ||
default: | ||
throw new Error(`Not a comment: ${JSON.stringify(comment)}`); | ||
} | ||
|
@@ -1660,7 +1660,7 @@ function printNumberLiteral( | |
options: TypeSpecPrettierOptions | ||
): Doc { | ||
const node = path.node; | ||
return getRawText(node, options); | ||
return node.valueAsString; | ||
} | ||
|
||
function printBooleanLiteral( | ||
|
@@ -1975,7 +1975,10 @@ function printItemList<T extends Node>( | |
* @param options Prettier options | ||
* @returns Raw text in the file for the given node. | ||
*/ | ||
function getRawText(node: TextRange, options: TypeSpecPrettierOptions) { | ||
function getRawText(node: TextRange, options: TypeSpecPrettierOptions): string { | ||
if ("rawText" in node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this way of setting the syntax for basic nodes where they can have |
||
return node.rawText as string; | ||
} | ||
return options.originalText.slice(node.pos, node.end); | ||
} | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this make sense as a separate package and cli? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* File serving as an entrypoint to resolve a local tsp install from a global install. | ||
* DO NOT MOVE or this will create a breaking change for user of global cli. | ||
*/ | ||
import "../dist/cli.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"name": "@typespec/importer", | ||
"private": true, | ||
"version": "0.0.1", | ||
"author": "Microsoft Corporation", | ||
"description": "Package to import TypeSpec files into a single one", | ||
"homepage": "https://typespec.io", | ||
"readme": "https://github.com/microsoft/typespec/blob/main/README.md", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/microsoft/typespec.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/microsoft/typespec/issues" | ||
}, | ||
"keywords": [ | ||
"typespec" | ||
], | ||
"type": "module", | ||
"main": "dist/src/index.js", | ||
"exports": { | ||
".": "./dist/index.js" | ||
}, | ||
"bin": "dist/src/cli.js", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"scripts": { | ||
"bundle": "node ./dist/src/cli.js", | ||
"clean": "rimraf ./dist ./temp", | ||
"build": "tsc -p .", | ||
"watch": "tsc -p . --watch", | ||
"test": "vitest run", | ||
"test:ui": "vitest --ui", | ||
"test:ci": "vitest run --coverage --reporter=junit --reporter=default", | ||
"lint": "eslint . --max-warnings=0", | ||
"lint:fix": "eslint . --fix" | ||
}, | ||
"files": [ | ||
"lib/*.tsp", | ||
"dist/**", | ||
"!dist/test/**" | ||
], | ||
"dependencies": { | ||
"@typespec/compiler": "workspace:~", | ||
"picocolors": "~1.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "~18.11.19", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"@vitest/ui": "^1.6.0", | ||
"c8": "^10.1.2", | ||
"rimraf": "~5.0.7", | ||
"source-map-support": "~0.5.21", | ||
"typescript": "~5.5.3", | ||
"vite": "^5.3.2", | ||
"vitest": "^1.6.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
await import("source-map-support/register.js"); | ||
} catch { | ||
// package only present in dev. | ||
} | ||
|
||
import { getDirectoryPath, logDiagnostics, NodeHost, normalizePath } from "@typespec/compiler"; | ||
import { mkdir, writeFile } from "fs/promises"; | ||
import { resolve } from "path"; | ||
import { parseArgs } from "util"; | ||
import { ImporterHost } from "./importer-host.js"; | ||
import { combineProjectIntoFile } from "./importer.js"; | ||
|
||
function log(...args: any[]) { | ||
// eslint-disable-next-line no-console | ||
console.log(...args); | ||
} | ||
const args = parseArgs({ | ||
options: {}, | ||
args: process.argv.slice(2), | ||
allowPositionals: true, | ||
}); | ||
|
||
const rawEntrypoint = normalizePath(resolve(args.positionals[0])); | ||
|
||
const { content, diagnostics } = await combineProjectIntoFile(ImporterHost, rawEntrypoint); | ||
|
||
if (diagnostics.length > 0) { | ||
logDiagnostics(diagnostics, NodeHost.logSink); | ||
process.exit(1); | ||
} | ||
if (content) { | ||
const outputFile = "tsp-output/main.tsp"; | ||
await mkdir(getDirectoryPath(outputFile), { recursive: true }); | ||
log(`Writing output to ${outputFile}`); | ||
await writeFile(outputFile, content); | ||
process.exit(0); | ||
} else { | ||
process.exit(1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeHost, type CompilerHost } from "@typespec/compiler"; | ||
import { createRemoteHost } from "./remote-host.js"; | ||
|
||
/** | ||
* Special host that tries to load data from additional locations | ||
*/ | ||
export const ImporterHost: CompilerHost = { | ||
...NodeHost, | ||
...createRemoteHost(NodeHost), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not possible to check with urls and doesn't bring too much values apart from figuring out you passed the wrong value when developping.