Skip to content

Commit ac0d9a1

Browse files
committed
Refactoring.
1 parent ac07661 commit ac0d9a1

9 files changed

Lines changed: 795 additions & 669 deletions

File tree

tools/tsp-client/package-lock.json

Lines changed: 233 additions & 266 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/tsp-client/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"clean": "rimraf ./dist ./types",
1818
"example": "npx ts-node src/index.ts update",
1919
"prepack": "npm run build",
20-
"test": "mocha --exclude **/examples/**"
20+
"test": "mocha --exclude **/examples/**",
21+
"tsp-client": "node cmd/tsp-client.js",
22+
"watch": "tsc -p . --watch"
2123
},
2224
"author": "Microsoft Corporation",
2325
"license": "MIT",
@@ -47,11 +49,13 @@
4749
},
4850
"dependencies": {
4951
"@azure/core-rest-pipeline": "^1.12.0",
52+
"@types/yargs": "^17.0.32",
5053
"chalk": "^5.3.0",
5154
"dotenv": "^16.4.5",
5255
"prompt-sync": "^4.2.0",
5356
"simple-git": "^3.20.0",
54-
"yaml": "^2.3.1"
57+
"yaml": "^2.3.1",
58+
"yargs": "^17.2.1"
5559
},
5660
"peerDependencies": {
5761
"@typespec/compiler": ">=0.48.1 <1.0.0"

tools/tsp-client/src/fs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { mkdir, rm, stat, readFile, access } from "node:fs/promises";
22
import { Logger } from "./log.js";
33
import { parse as parseYaml } from "yaml";
4-
import { TspLocation } from "./typespec.js";
54
import { joinPaths, normalizePath, resolvePath } from "@typespec/compiler";
5+
import { TspLocation } from "./typespec.js";
66

77
export async function ensureDirectory(path: string) {
88
await mkdir(path, { recursive: true });

tools/tsp-client/src/index.ts

Lines changed: 375 additions & 159 deletions
Large diffs are not rendered by default.

tools/tsp-client/src/log.ts

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from "chalk";
2-
import { getPackageVersion } from "./npm.js";
32

43
const logSink = {
54
log: console.log,
@@ -31,8 +30,11 @@ function createLogger(): Logger {
3130
return direct;
3231
}
3332

34-
export function enableDebug(): void {
35-
logSink.debug = console.debug;
33+
export function checkDebugLogging(argv: any): void {
34+
const debug = argv.debug;
35+
if (debug) {
36+
logSink.debug = console.debug;
37+
}
3638
}
3739

3840
export const Logger = createLogger();
@@ -54,46 +56,7 @@ export function printBanner() {
5456
Logger.info(bannerText);
5557
}
5658

57-
const usageText = `
58-
Usage: tsp-client <command> [options]
59+
export const usageText = `
60+
Usage: tsp-client <COMMAND> [OPTIONS]
5961
60-
Use one of the supported commands to get started generating clients from a TypeSpec project.
61-
This tool will default to using your current working directory to generate clients in and will
62-
use it to look for relevant configuration files. To specify a different directory, use
63-
the -o or --output-dir option.
64-
65-
Commands:
66-
init Initialize the SDK project folder from a tspconfig.yaml [string]
67-
sync Sync TypeSpec project specified in tsp-location.yaml [string]
68-
generate Generate from a TypeSpec project [string]
69-
update Sync and generate from a TypeSpec project [string]
70-
convert Convert a swagger specification to TypeSpec [string]
71-
72-
Options:
73-
--arm Convert ARM swagger specification to TypeSpec [boolean]
74-
-c, --tsp-config The tspconfig.yaml file to use [string]
75-
--commit Commit to be used for project init or update [string]
76-
-d, --debug Enable debug logging [boolean]
77-
--emitter-options The options to pass to the emitter [string]
78-
--generate-lock-file Generate a lock file under the eng/ directory from
79-
an existing emitter-package.json [boolean]
80-
-h, --help Show help [boolean]
81-
--local-spec-repo Path to local repository with the TypeSpec project [string]
82-
--no-prompt Skip prompting for output directory confirmation [boolean]
83-
--save-inputs Don't clean up the temp directory after generation [boolean]
84-
--skip-sync-and-generate Skip sync and generate during project init [boolean]
85-
--swagger-readme Path or url to swagger readme file [string]
86-
-o, --output-dir Specify an alternate output directory for the
87-
generated files. Default is your current directory [string]
88-
--repo Repository where the project is defined for init
89-
or update [string]
90-
-v, --version Show version number [boolean]
91-
`;
92-
export function printUsage() {
93-
Logger(usageText);
94-
}
95-
96-
export async function printVersion() {
97-
const version = await getPackageVersion();
98-
Logger(`tsp-client version: ${version}`);
99-
}
62+
Use one of the supported commands to get started generating clients from a TypeSpec project. This tool will default to using your current working directory to generate clients in and will use it to look for relevant configuration files. To specify a different directory, use the -o or --output-dir option.`;

tools/tsp-client/src/npm.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import * as path from "node:path";
2-
import { readFile, writeFile } from "node:fs/promises";
1+
import { writeFile } from "node:fs/promises";
32
import { spawn } from "node:child_process";
4-
import { fileURLToPath } from "node:url";
53
import { joinPaths } from "@typespec/compiler";
64

75
export async function createPackageJson(rootPath: string, deps: Set<string>): Promise<void> {
@@ -58,15 +56,3 @@ export async function npxCommand(workingDir: string, args: string[]): Promise<vo
5856
});
5957
});
6058
}
61-
62-
let packageVersion: string;
63-
export async function getPackageVersion(): Promise<string> {
64-
if (!packageVersion) {
65-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
66-
const packageJson = JSON.parse(
67-
await readFile(joinPaths(__dirname, "..", "package.json"), "utf-8"),
68-
);
69-
packageVersion = packageJson.version ?? "unknown";
70-
}
71-
return packageVersion;
72-
}

0 commit comments

Comments
 (0)