@azure-tools/typespec-ts 0.55.0-dev.2 → 0.55.0-dev.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. package/README.md +96 -24
  2. package/dist/src/framework/hooks/binder.d.ts.map +1 -1
  3. package/dist/src/framework/hooks/binder.js +3 -4
  4. package/dist/src/framework/hooks/binder.js.map +1 -1
  5. package/dist/src/framework/load-static-helpers.d.ts +4 -1
  6. package/dist/src/framework/load-static-helpers.d.ts.map +1 -1
  7. package/dist/src/framework/load-static-helpers.js +19 -15
  8. package/dist/src/framework/load-static-helpers.js.map +1 -1
  9. package/dist/src/framework/sample.js +1 -1
  10. package/dist/src/framework/sample.js.map +1 -1
  11. package/dist/src/index.d.ts.map +1 -1
  12. package/dist/src/index.js +47 -28
  13. package/dist/src/index.js.map +1 -1
  14. package/dist/src/lib.js +30 -31
  15. package/dist/src/lib.js.map +1 -1
  16. package/dist/src/modular/build-project-files.d.ts.map +1 -1
  17. package/dist/src/modular/build-project-files.js +4 -4
  18. package/dist/src/modular/build-project-files.js.map +1 -1
  19. package/dist/src/modular/build-restore-poller.js +2 -2
  20. package/dist/src/modular/build-restore-poller.js.map +1 -1
  21. package/dist/src/modular/build-root-index.d.ts.map +1 -1
  22. package/dist/src/modular/build-root-index.js +5 -6
  23. package/dist/src/modular/build-root-index.js.map +1 -1
  24. package/dist/src/modular/build-subpath-index.d.ts.map +1 -1
  25. package/dist/src/modular/build-subpath-index.js +4 -4
  26. package/dist/src/modular/build-subpath-index.js.map +1 -1
  27. package/dist/src/modular/emit-models-options.js +2 -2
  28. package/dist/src/modular/emit-models-options.js.map +1 -1
  29. package/dist/src/modular/emit-models.d.ts.map +1 -1
  30. package/dist/src/modular/emit-models.js +2 -3
  31. package/dist/src/modular/emit-models.js.map +1 -1
  32. package/dist/src/modular/emit-samples.d.ts.map +1 -1
  33. package/dist/src/modular/emit-samples.js +3 -4
  34. package/dist/src/modular/emit-samples.js.map +1 -1
  35. package/dist/src/modular/emit-tests.d.ts +2 -1
  36. package/dist/src/modular/emit-tests.d.ts.map +1 -1
  37. package/dist/src/modular/emit-tests.js +21 -11
  38. package/dist/src/modular/emit-tests.js.map +1 -1
  39. package/dist/src/modular/helpers/example-value-helpers.js +4 -4
  40. package/dist/src/modular/helpers/example-value-helpers.js.map +1 -1
  41. package/dist/src/rlc-common/build-client-definitions.js +3 -3
  42. package/dist/src/rlc-common/build-client-definitions.js.map +1 -1
  43. package/dist/src/rlc-common/build-client.js +4 -4
  44. package/dist/src/rlc-common/build-client.js.map +1 -1
  45. package/dist/src/rlc-common/build-index-file.js +3 -3
  46. package/dist/src/rlc-common/build-index-file.js.map +1 -1
  47. package/dist/src/rlc-common/build-is-unexpected-helper.js +3 -3
  48. package/dist/src/rlc-common/build-is-unexpected-helper.js.map +1 -1
  49. package/dist/src/rlc-common/build-logger.js +3 -3
  50. package/dist/src/rlc-common/build-logger.js.map +1 -1
  51. package/dist/src/rlc-common/build-paginate-helper.js +2 -2
  52. package/dist/src/rlc-common/build-paginate-helper.js.map +1 -1
  53. package/dist/src/rlc-common/build-parameter-types.js +3 -3
  54. package/dist/src/rlc-common/build-parameter-types.js.map +1 -1
  55. package/dist/src/rlc-common/build-polling-helper.js +2 -2
  56. package/dist/src/rlc-common/build-polling-helper.js.map +1 -1
  57. package/dist/src/rlc-common/build-response-types.js +3 -3
  58. package/dist/src/rlc-common/build-response-types.js.map +1 -1
  59. package/dist/src/rlc-common/build-samples.js +2 -2
  60. package/dist/src/rlc-common/build-samples.js.map +1 -1
  61. package/dist/src/rlc-common/build-schema-type.js +4 -4
  62. package/dist/src/rlc-common/build-schema-type.js.map +1 -1
  63. package/dist/src/rlc-common/build-serialize-helper.js +2 -2
  64. package/dist/src/rlc-common/build-serialize-helper.js.map +1 -1
  65. package/dist/src/rlc-common/build-top-level-index-file.js +3 -3
  66. package/dist/src/rlc-common/build-top-level-index-file.js.map +1 -1
  67. package/dist/src/rlc-common/helpers/path-utils.d.ts.map +1 -1
  68. package/dist/src/rlc-common/helpers/path-utils.js +1 -2
  69. package/dist/src/rlc-common/helpers/path-utils.js.map +1 -1
  70. package/dist/src/rlc-common/metadata/build-api-extractor-config.js +1 -1
  71. package/dist/src/rlc-common/metadata/build-api-extractor-config.js.map +1 -1
  72. package/dist/src/rlc-common/metadata/build-es-lint-config.js +1 -1
  73. package/dist/src/rlc-common/metadata/build-es-lint-config.js.map +1 -1
  74. package/dist/src/rlc-common/metadata/build-package-file.js +1 -1
  75. package/dist/src/rlc-common/metadata/build-package-file.js.map +1 -1
  76. package/dist/src/rlc-common/metadata/build-readme-file.d.ts +2 -2
  77. package/dist/src/rlc-common/metadata/build-readme-file.d.ts.map +1 -1
  78. package/dist/src/rlc-common/metadata/build-readme-file.js +11 -14
  79. package/dist/src/rlc-common/metadata/build-readme-file.js.map +1 -1
  80. package/dist/src/rlc-common/metadata/build-rollup-config.js +1 -1
  81. package/dist/src/rlc-common/metadata/build-rollup-config.js.map +1 -1
  82. package/dist/src/rlc-common/metadata/build-ts-config.js +1 -1
  83. package/dist/src/rlc-common/metadata/build-ts-config.js.map +1 -1
  84. package/dist/src/transform/transform.d.ts.map +1 -1
  85. package/dist/src/transform/transform.js +2 -3
  86. package/dist/src/transform/transform.js.map +1 -1
  87. package/dist/src/utils/emit-util.d.ts.map +1 -1
  88. package/dist/src/utils/emit-util.js +10 -5
  89. package/dist/src/utils/emit-util.js.map +1 -1
  90. package/dist/src/utils/file-system-utils.d.ts +4 -4
  91. package/dist/src/utils/file-system-utils.d.ts.map +1 -1
  92. package/dist/src/utils/file-system-utils.js +14 -16
  93. package/dist/src/utils/file-system-utils.js.map +1 -1
  94. package/dist/src/utils/import-helper.js +3 -3
  95. package/dist/src/utils/import-helper.js.map +1 -1
  96. package/dist/src/utils/resolve-project-root.d.ts +6 -4
  97. package/dist/src/utils/resolve-project-root.d.ts.map +1 -1
  98. package/dist/src/utils/resolve-project-root.js +11 -18
  99. package/dist/src/utils/resolve-project-root.js.map +1 -1
  100. package/dist/tsconfig.tsbuildinfo +1 -1
  101. package/package.json +26 -26
  102. package/src/framework/hooks/binder.ts +3 -4
  103. package/src/framework/load-static-helpers.ts +29 -18
  104. package/src/framework/sample.ts +1 -1
  105. package/src/index.ts +61 -28
  106. package/src/lib.ts +31 -31
  107. package/src/modular/build-project-files.ts +11 -4
  108. package/src/modular/build-restore-poller.ts +2 -2
  109. package/src/modular/build-root-index.ts +5 -6
  110. package/src/modular/build-subpath-index.ts +4 -4
  111. package/src/modular/emit-models-options.ts +2 -2
  112. package/src/modular/emit-models.ts +2 -3
  113. package/src/modular/emit-samples.ts +3 -4
  114. package/src/modular/emit-tests.ts +28 -11
  115. package/src/modular/helpers/example-value-helpers.ts +4 -4
  116. package/src/rlc-common/build-client-definitions.ts +3 -3
  117. package/src/rlc-common/build-client.ts +4 -4
  118. package/src/rlc-common/build-index-file.ts +3 -3
  119. package/src/rlc-common/build-is-unexpected-helper.ts +3 -3
  120. package/src/rlc-common/build-logger.ts +3 -3
  121. package/src/rlc-common/build-paginate-helper.ts +2 -2
  122. package/src/rlc-common/build-parameter-types.ts +3 -3
  123. package/src/rlc-common/build-polling-helper.ts +2 -2
  124. package/src/rlc-common/build-response-types.ts +3 -3
  125. package/src/rlc-common/build-samples.ts +2 -2
  126. package/src/rlc-common/build-schema-type.ts +4 -4
  127. package/src/rlc-common/build-serialize-helper.ts +2 -2
  128. package/src/rlc-common/build-top-level-index-file.ts +3 -3
  129. package/src/rlc-common/helpers/path-utils.ts +1 -3
  130. package/src/rlc-common/metadata/build-api-extractor-config.ts +1 -1
  131. package/src/rlc-common/metadata/build-es-lint-config.ts +1 -1
  132. package/src/rlc-common/metadata/build-package-file.ts +1 -1
  133. package/src/rlc-common/metadata/build-readme-file.ts +12 -15
  134. package/src/rlc-common/metadata/build-rollup-config.ts +1 -1
  135. package/src/rlc-common/metadata/build-ts-config.ts +1 -1
  136. package/src/transform/transform.ts +2 -3
  137. package/src/utils/emit-util.ts +10 -8
  138. package/src/utils/file-system-utils.ts +14 -15
  139. package/src/utils/import-helper.ts +3 -3
  140. package/src/utils/resolve-project-root.ts +11 -24
  141. package/dist/src/utils/dirname.d.ts +0 -9
  142. package/dist/src/utils/dirname.d.ts.map +0 -1
  143. package/dist/src/utils/dirname.js +0 -12
  144. package/dist/src/utils/dirname.js.map +0 -1
  145. package/src/utils/dirname.ts +0 -12
@@ -1,6 +1,8 @@
1
- import { CompilerHost, NoTarget, Program } from "@typespec/compiler";
2
- import { dirname, join } from "path";
1
+ import { CompilerHost, getDirectoryPath, joinPaths, NoTarget, Program } from "@typespec/compiler";
3
2
  import { format } from "prettier";
3
+ import prettierPluginBabel from "prettier/plugins/babel";
4
+ import prettierPluginEstree from "prettier/plugins/estree";
5
+ import prettierPluginTypescript from "prettier/plugins/typescript";
4
6
  import { prettierJSONOptions, prettierTypeScriptOptions, reportDiagnostic } from "../lib.js";
5
7
  import {
6
8
  buildSchemaTypes,
@@ -58,7 +60,7 @@ async function emitFile(
58
60
  return;
59
61
  }
60
62
  const host: CompilerHost = program.host;
61
- const filePath = join(emitterOutputDir ?? "", file.path);
63
+ const filePath = joinPaths(emitterOutputDir ?? "", file.path);
62
64
  const isJson = /\.json$/gi.test(filePath);
63
65
  const isSourceCode = /\.(ts|js)$/gi.test(filePath);
64
66
  const microsoftHeader = isAzureFlavor ? `// Copyright (c) Microsoft Corporation.\n` : "";
@@ -71,10 +73,10 @@ async function emitFile(
71
73
  // Format the contents if necessary
72
74
  if (isJson || isSourceCode) {
73
75
  try {
74
- prettierFileContent = await format(
75
- prettierFileContent,
76
- isJson ? prettierJSONOptions : prettierTypeScriptOptions,
77
- );
76
+ prettierFileContent = await format(prettierFileContent, {
77
+ ...(isJson ? prettierJSONOptions : prettierTypeScriptOptions),
78
+ plugins: [prettierPluginTypescript, prettierPluginEstree, prettierPluginBabel],
79
+ });
78
80
  } catch (e) {
79
81
  reportDiagnostic(program, {
80
82
  code: "file-formatting-error",
@@ -87,6 +89,6 @@ async function emitFile(
87
89
  // Continue with unformatted content rather than crashing
88
90
  }
89
91
  }
90
- await host.mkdirp(dirname(filePath));
92
+ await host.mkdirp(getDirectoryPath(filePath));
91
93
  await host.writeFile(filePath, prettierFileContent);
92
94
  }
@@ -1,49 +1,48 @@
1
- import { NoTarget, Program } from "@typespec/compiler";
2
- import { mkdir, readdir, rm, stat } from "fs/promises";
3
- import { resolve } from "path";
1
+ import { NoTarget, Program, resolvePath, type CompilerHost } from "@typespec/compiler";
4
2
  import { reportDiagnostic } from "../lib.js";
5
3
 
6
- export async function pathExists(targetPath: string): Promise<boolean> {
4
+ export async function pathExists(host: CompilerHost, targetPath: string): Promise<boolean> {
7
5
  try {
8
- await stat(targetPath);
6
+ await host.stat(targetPath);
9
7
  return true;
10
8
  } catch {
11
9
  return false;
12
10
  }
13
11
  }
14
12
 
15
- export async function emptyDir(dirPath: string): Promise<void> {
13
+ export async function emptyDir(host: CompilerHost, dirPath: string): Promise<void> {
16
14
  let entries: string[];
17
15
  try {
18
- entries = await readdir(dirPath);
16
+ entries = await host.readDir(dirPath);
19
17
  } catch {
20
- await mkdir(dirPath, { recursive: true });
18
+ await host.mkdirp(dirPath);
21
19
  return;
22
20
  }
23
21
 
24
22
  await Promise.all(
25
- entries.map((entry) => rm(resolve(dirPath, entry), { recursive: true, force: true })),
23
+ entries.map((entry) => host.rm(resolvePath(dirPath, entry), { recursive: true })),
26
24
  );
27
25
  }
28
26
 
29
27
  export async function clearDirectory(
28
+ host: CompilerHost,
30
29
  dirPath: string,
31
30
  excludeNames: string[] = [],
32
31
  program?: Program,
33
32
  ): Promise<void> {
34
- if (!(await pathExists(dirPath))) {
33
+ if (!(await pathExists(host, dirPath))) {
35
34
  return;
36
35
  }
37
36
 
38
37
  // If no exclude names, just use regular emptyDir for efficiency
39
38
  if (excludeNames.length === 0) {
40
- await emptyDir(dirPath);
39
+ await emptyDir(host, dirPath);
41
40
  return;
42
41
  }
43
42
 
44
43
  try {
45
44
  // Get all subdirectories and files
46
- const entries = await readdir(dirPath);
45
+ const entries = await host.readDir(dirPath);
47
46
 
48
47
  // Filter entries to exclude those that should be preserved
49
48
  const filteredEntries = entries.filter((entry) => {
@@ -52,8 +51,8 @@ export async function clearDirectory(
52
51
 
53
52
  // Process each entry
54
53
  for (const entry of filteredEntries) {
55
- const entryPath = resolve(dirPath, entry);
56
- await rm(entryPath, { recursive: true, force: true });
54
+ const entryPath = resolvePath(dirPath, entry);
55
+ await host.rm(entryPath, { recursive: true });
57
56
  }
58
57
  } catch (error) {
59
58
  // If there's an error, fall back to regular emptyDir
@@ -64,6 +63,6 @@ export async function clearDirectory(
64
63
  target: NoTarget,
65
64
  });
66
65
  }
67
- await emptyDir(dirPath);
66
+ await emptyDir(host, dirPath);
68
67
  }
69
68
  }
@@ -1,4 +1,4 @@
1
- import { dirname, relative, resolve } from "path";
1
+ import { getDirectoryPath, getRelativePathFromDirectory, resolvePath } from "@typespec/compiler";
2
2
  import { SourceFile } from "ts-morph";
3
3
  import { useContext } from "../context-manager.js";
4
4
 
@@ -20,7 +20,7 @@ export function addImportBySymbol(symbol: string, currentFile: SourceFile) {
20
20
  }
21
21
 
22
22
  // Resolve absolute path and compute a relative path from the current file to the module.
23
- const moduleAbsolutePath = resolve(modulePath);
23
+ const moduleAbsolutePath = resolvePath(modulePath);
24
24
  const relativeImportPath = getRelativeImportPath(currentFile.getFilePath(), moduleAbsolutePath)
25
25
  .replace(/\\/g, "/")
26
26
  .replace(/\.ts$/, ".js");
@@ -53,7 +53,7 @@ export function addImportBySymbol(symbol: string, currentFile: SourceFile) {
53
53
  * @returns {string} The relative path adjusted for import syntax.
54
54
  */
55
55
  function getRelativeImportPath(from: string, to: string) {
56
- const relativePath = relative(dirname(from), to);
56
+ const relativePath = getRelativePathFromDirectory(getDirectoryPath(from), to, false);
57
57
  // Adjust the path format to TypeScript module syntax
58
58
  return relativePath.startsWith(".") ? relativePath : "./" + relativePath;
59
59
  }
@@ -1,28 +1,15 @@
1
- import { existsSync } from "fs";
2
- import { dirname, resolve } from "path";
3
- import { fileURLToPath } from "url";
1
+ import { resolvePath } from "@typespec/compiler";
4
2
 
5
3
  /**
6
- * Recursively finds the nearest package.json file starting from the specified directory.
7
- * @param {string} currentDir - The directory to start searching from. Defaults to the directory of the current module.
8
- * @returns {string} path to the directory containing the package.json file.
4
+ * Returns the package root directory for the emitter.
5
+ *
6
+ * The compiled output lives at `dist/src/utils/resolve-project-root.js`,
7
+ * so the package root is 3 levels up. This avoids filesystem access,
8
+ * making it compatible with virtual file systems (e.g., the playground).
9
9
  */
10
- export function resolveProjectRoot(
11
- currentDir: string = dirname(fileURLToPath(import.meta.url)),
12
- ): string {
13
- const packageJsonPath = resolve(currentDir, "package.json");
14
-
15
- if (existsSync(packageJsonPath)) {
16
- return currentDir; // Return the directory containing the package.json
17
- }
18
-
19
- const parentDir = resolve(currentDir, "..");
20
-
21
- if (parentDir === currentDir) {
22
- // If we've reached the root directory and haven't found package.json
23
- throw new Error("Could not find package.json");
24
- }
25
-
26
- // Recursively search the parent directory
27
- return resolveProjectRoot(parentDir);
10
+ export function resolveProjectRoot(): string {
11
+ // Use the global URL constructor (works in both Node.js and browsers)
12
+ const currentDir = new URL(".", import.meta.url).pathname;
13
+ // From dist/src/utils/ -> package root (3 levels: utils -> src -> dist -> root)
14
+ return resolvePath(currentDir, "../../..");
28
15
  }
@@ -1,9 +0,0 @@
1
- /**
2
- *
3
- * @param metaUrl usually import.meta.url
4
- */
5
- export declare function getDirname(metaUrl: string): {
6
- __dirname: string;
7
- __filename: string;
8
- };
9
- //# sourceMappingURL=dirname.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dirname.d.ts","sourceRoot":"","sources":["../../../src/utils/dirname.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM;;;EAIzC"}
@@ -1,12 +0,0 @@
1
- import path from "path";
2
- import { fileURLToPath } from "url";
3
- /**
4
- *
5
- * @param metaUrl usually import.meta.url
6
- */
7
- export function getDirname(metaUrl) {
8
- const __filename = fileURLToPath(metaUrl);
9
- const __dirname = path.dirname(__filename);
10
- return { __dirname, __filename };
11
- }
12
- //# sourceMappingURL=dirname.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dirname.js","sourceRoot":"","sources":["../../../src/utils/dirname.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AAEpC;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,OAAe;IACxC,MAAM,UAAU,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC;AACnC,CAAC"}
@@ -1,12 +0,0 @@
1
- import path from "path";
2
- import { fileURLToPath } from "url";
3
-
4
- /**
5
- *
6
- * @param metaUrl usually import.meta.url
7
- */
8
- export function getDirname(metaUrl: string) {
9
- const __filename = fileURLToPath(metaUrl);
10
- const __dirname = path.dirname(__filename);
11
- return { __dirname, __filename };
12
- }