@tui-sandbox/library 9.1.3 → 9.1.5

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 (31) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/src/scripts/tui.js +1 -1
  3. package/dist/src/scripts/tui.js.map +1 -1
  4. package/dist/src/server/dirtree/index.d.ts +2 -2
  5. package/dist/src/server/dirtree/index.js +7 -7
  6. package/dist/src/server/dirtree/index.js.map +1 -1
  7. package/dist/src/server/dirtree/index.test.js +32 -0
  8. package/dist/src/server/dirtree/index.test.js.map +1 -1
  9. package/dist/src/server/neovim/environment/createTempDir.js +4 -2
  10. package/dist/src/server/neovim/environment/createTempDir.js.map +1 -1
  11. package/dist/src/server/neovim/index.d.ts +0 -1
  12. package/dist/src/server/neovim/index.js +1 -6
  13. package/dist/src/server/neovim/index.js.map +1 -1
  14. package/dist/src/server/neovim/prepareNewTestDirectory.d.ts +3 -0
  15. package/dist/src/server/neovim/prepareNewTestDirectory.js +16 -0
  16. package/dist/src/server/neovim/prepareNewTestDirectory.js.map +1 -0
  17. package/dist/src/server/neovim/prepareNewTestDirectory.test.d.ts +1 -0
  18. package/dist/src/server/neovim/prepareNewTestDirectory.test.js +24 -0
  19. package/dist/src/server/neovim/prepareNewTestDirectory.test.js.map +1 -0
  20. package/dist/src/server/terminal/index.js +1 -1
  21. package/dist/src/server/terminal/index.js.map +1 -1
  22. package/dist/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +1 -1
  24. package/src/scripts/tui.ts +1 -1
  25. package/src/server/dirtree/index.test.ts +33 -0
  26. package/src/server/dirtree/index.ts +11 -9
  27. package/src/server/neovim/environment/createTempDir.ts +4 -2
  28. package/src/server/neovim/index.ts +1 -7
  29. package/src/server/neovim/prepareNewTestDirectory.test.ts +24 -0
  30. package/src/server/neovim/prepareNewTestDirectory.ts +18 -0
  31. package/src/server/terminal/index.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tui-sandbox/library",
3
- "version": "9.1.3",
3
+ "version": "9.1.5",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "bin": {
@@ -5,7 +5,7 @@ import type { TestServerConfig } from "../server/index.js"
5
5
  import { startTestServer, updateTestdirectorySchemaFile } from "../server/index.js"
6
6
  import type { StdoutOrStderrMessage } from "../server/neovim/NeovimApplication.js"
7
7
  import { NeovimApplication } from "../server/neovim/NeovimApplication.js"
8
- import { prepareNewTestDirectory } from "../server/neovim/index.js"
8
+ import { prepareNewTestDirectory } from "../server/neovim/prepareNewTestDirectory.js"
9
9
 
10
10
  //
11
11
  // This is the main entrypoint to tui-sandbox
@@ -159,4 +159,37 @@ describe("dirtree", () => {
159
159
  export type MyTestDirectoryFile = z.infer<typeof testDirectoryFiles>"
160
160
  `)
161
161
  })
162
+
163
+ it("creates an empty schema when the directory cannot be read", async () => {
164
+ const tree = getDirectoryTree("nonexistent")
165
+ const result = await buildSchemaForDirectoryTree(tree, "MyDirectoryTree")
166
+ expect(result).toMatchInlineSnapshot(`
167
+ "
168
+ // Note: This file is autogenerated. Do not edit it directly.
169
+ //
170
+ // Describes the contents of the test directory, which is a blueprint for
171
+ // files and directories. Tests can create a unique, safe environment for
172
+ // interacting with the contents of such a directory.
173
+ //
174
+ // Having strong typing for the test directory contents ensures that tests can
175
+ // be written with confidence that the files and directories they expect are
176
+ // actually found. Otherwise the tests are brittle and can break easily.
177
+
178
+ import { z } from "zod"
179
+
180
+ export const MyDirectoryTreeSchema = z.object({
181
+ type: z.literal("directory"),
182
+ name: z.literal("root"),
183
+ contents: z.object({}),
184
+ })
185
+
186
+ export const MyDirectoryTreeContentsSchema = MyDirectoryTreeSchema.shape.contents
187
+ export type MyDirectoryTreeContentsSchemaType = z.infer<typeof MyDirectoryTreeSchema>
188
+
189
+ export type MyDirectoryTree = MyDirectoryTreeContentsSchemaType["contents"]
190
+
191
+ export const testDirectoryFiles = z.enum([])
192
+ export type MyTestDirectoryFile = z.infer<typeof testDirectoryFiles>"
193
+ `)
194
+ })
162
195
  })
@@ -1,11 +1,10 @@
1
- import assert from "assert"
2
1
  import type { Dree } from "dree"
3
2
  import { scan, Type } from "dree"
4
3
  import { format, resolveConfig } from "prettier"
5
4
  import { fileURLToPath } from "url"
6
5
  import { jsonToZod } from "./json-to-zod.js"
7
6
 
8
- type TreeResult = { dree: Dree; allFiles: Dree[] }
7
+ type TreeResult = { dree: Dree | undefined; allFiles: Dree[] }
9
8
 
10
9
  /** Convert a directory tree to a TypeScript type. This is useful for testing
11
10
  * as the initial state of the test directory is fully known in tests. */
@@ -25,9 +24,9 @@ export function getDirectoryTree(path: string): TreeResult {
25
24
  dir => {
26
25
  allFiles.push(dir)
27
26
  }
28
- )
27
+ ) as Dree | null // https://github.com/euberdeveloper/dree/pull/51
29
28
 
30
- return { dree: result, allFiles }
29
+ return { dree: result ?? undefined, allFiles }
31
30
  }
32
31
 
33
32
  type FileNode = {
@@ -42,7 +41,11 @@ type DirectoryNode = {
42
41
 
43
42
  type TreeNode = FileNode | DirectoryNode
44
43
 
45
- export function convertDree(root: Dree): TreeNode {
44
+ export function convertDree(root: Dree | undefined): TreeNode {
45
+ if (!root) {
46
+ return { type: Type.DIRECTORY, name: "root", contents: {} }
47
+ }
48
+
46
49
  if (root.type === Type.FILE) {
47
50
  return {
48
51
  name: root.name,
@@ -63,10 +66,9 @@ export function convertDree(root: Dree): TreeNode {
63
66
  }
64
67
 
65
68
  export async function buildSchemaForDirectoryTree(result: TreeResult, name: string): Promise<string> {
66
- const root = result.dree
67
- assert(root.type === Type.DIRECTORY)
68
- const node = convertDree(root)
69
- const schema = (await jsonToZod(node, `${name}Schema`)).split("\n")
69
+ const root = convertDree(result.dree)
70
+
71
+ const schema = (await jsonToZod(root, `${name}Schema`)).split("\n")
70
72
 
71
73
  const lines = `
72
74
  // Note: This file is autogenerated. Do not edit it directly.
@@ -1,7 +1,7 @@
1
1
  import assert from "assert"
2
2
  import { execSync } from "child_process"
3
3
  import { Type } from "dree"
4
- import { constants, readdirSync } from "fs"
4
+ import { constants, readdirSync, statSync } from "fs"
5
5
  import { access, mkdir, mkdtemp } from "fs/promises"
6
6
  import path from "path"
7
7
  import { convertDree, getDirectoryTree } from "../../dirtree/index.js"
@@ -11,6 +11,8 @@ import { updateTestdirectorySchemaFile } from "../../updateTestdirectorySchemaFi
11
11
 
12
12
  export async function createTempDir(config: DirectoriesConfig): Promise<TestDirectory> {
13
13
  try {
14
+ // before calling this function, the testEnvironmentPath should already exist
15
+ statSync(config.testEnvironmentPath)
14
16
  const dir = await createUniqueDirectory(config.testEnvironmentPath)
15
17
 
16
18
  readdirSync(config.testEnvironmentPath).forEach(entry => {
@@ -58,6 +60,6 @@ export async function removeTestDirectories(testEnvironmentPath: string): Promis
58
60
  await access(testdirs, constants.F_OK)
59
61
  execSync(`rm -rf ${testdirs}/*`)
60
62
  } catch (e) {
61
- console.log("Could not remove test directories, maybe they don't exist yet", e)
63
+ console.debug("Could not remove test directories, maybe they don't exist yet", e)
62
64
  }
63
65
  }
@@ -16,9 +16,9 @@ import type { DirectoriesConfig } from "../updateTestdirectorySchemaFile.js"
16
16
  import { convertEventEmitterToAsyncGenerator } from "../utilities/generator.js"
17
17
  import { Lazy } from "../utilities/Lazy.js"
18
18
  import type { TabId } from "../utilities/tabId.js"
19
- import { createTempDir, removeTestDirectories } from "./environment/createTempDir.js"
20
19
  import type { StdoutOrStderrMessage, TerminalDimensions } from "./NeovimApplication.js"
21
20
  import { NeovimApplication } from "./NeovimApplication.js"
21
+ import { prepareNewTestDirectory } from "./prepareNewTestDirectory.js"
22
22
 
23
23
  const neovims = new Map<TabId["tabId"], NeovimApplication>()
24
24
  const resources: Lazy<AsyncDisposableStack> = new Lazy(() => {
@@ -97,12 +97,6 @@ export async function start(
97
97
  return testDirectory
98
98
  }
99
99
 
100
- export async function prepareNewTestDirectory(config: DirectoriesConfig): Promise<TestDirectory> {
101
- await removeTestDirectories(config.testEnvironmentPath)
102
- const testDirectory = await createTempDir(config)
103
- return testDirectory
104
- }
105
-
106
100
  export async function sendStdin(options: { tabId: TabId; data: string }): Promise<void> {
107
101
  const neovim = neovims.get(options.tabId.tabId)
108
102
  assert(
@@ -0,0 +1,24 @@
1
+ import { rm } from "fs/promises"
2
+ import path from "path"
3
+ import { prepareNewTestDirectory } from "./prepareNewTestDirectory.js"
4
+
5
+ describe("prepareNewTestDirectory when the testEnvironmentPath does not exist", () => {
6
+ it("should be able to create a new test directory", async () => {
7
+ // this happens when starting a new project, and the directory structure
8
+ // has not been created yet
9
+ const testEnvironmentPath = "/tmp/test"
10
+ const testDirectory = await prepareNewTestDirectory({
11
+ outputFilePath: path.join(testEnvironmentPath, "foo.ts"),
12
+ testEnvironmentPath,
13
+ })
14
+
15
+ try {
16
+ expect(testDirectory.contents).toEqual({})
17
+ expect(testDirectory.testEnvironmentPath).toEqual("/tmp/test")
18
+ } finally {
19
+ // for safety, only remove inside /tmp
20
+ assert(testEnvironmentPath.startsWith("/tmp/"))
21
+ await rm(testEnvironmentPath, { recursive: true })
22
+ }
23
+ })
24
+ })
@@ -0,0 +1,18 @@
1
+ import { access, mkdir } from "fs/promises"
2
+ import type { TestDirectory } from "../types.js"
3
+ import type { DirectoriesConfig } from "../updateTestdirectorySchemaFile.js"
4
+ import { createTempDir, removeTestDirectories } from "./environment/createTempDir.js"
5
+
6
+ export async function prepareNewTestDirectory(config: DirectoriesConfig): Promise<TestDirectory> {
7
+ try {
8
+ // if the directory does not exist, create it
9
+ await access(config.testEnvironmentPath)
10
+ } catch {
11
+ console.log(`Creating testEnvironmentPath directory at ${config.testEnvironmentPath}`)
12
+ await mkdir(config.testEnvironmentPath, { recursive: true })
13
+ }
14
+
15
+ await removeTestDirectories(config.testEnvironmentPath)
16
+ const testDirectory = await createTempDir(config)
17
+ return testDirectory
18
+ }
@@ -1,7 +1,7 @@
1
1
  import assert from "assert"
2
2
  import "core-js/proposals/async-explicit-resource-management.js"
3
- import { prepareNewTestDirectory } from "../neovim/index.js"
4
3
  import type { TerminalDimensions } from "../neovim/NeovimApplication.js"
4
+ import { prepareNewTestDirectory } from "../neovim/prepareNewTestDirectory.js"
5
5
  import type { DirectoriesConfig } from "../updateTestdirectorySchemaFile.js"
6
6
  import { convertEventEmitterToAsyncGenerator } from "../utilities/generator.js"
7
7
  import { Lazy } from "../utilities/Lazy.js"