@tinyrack/devsync 1.1.0 → 1.3.0

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 (220) hide show
  1. package/README.md +230 -62
  2. package/dist/cli/base-command.d.ts +14 -0
  3. package/dist/cli/base-command.d.ts.map +1 -0
  4. package/dist/cli/base-command.js +22 -0
  5. package/dist/cli/base-command.js.map +1 -0
  6. package/dist/cli/commands/dir.d.ts +8 -0
  7. package/dist/cli/commands/dir.d.ts.map +1 -0
  8. package/dist/cli/commands/dir.js +16 -0
  9. package/dist/cli/commands/dir.js.map +1 -0
  10. package/dist/cli/commands/doctor.d.ts +8 -0
  11. package/dist/cli/commands/doctor.d.ts.map +1 -0
  12. package/dist/cli/commands/doctor.js +18 -0
  13. package/dist/cli/commands/doctor.js.map +1 -0
  14. package/dist/cli/commands/index.d.ts +23 -0
  15. package/dist/cli/commands/index.d.ts.map +1 -0
  16. package/dist/cli/commands/index.js +23 -0
  17. package/dist/cli/commands/index.js.map +1 -0
  18. package/dist/cli/commands/init.d.ts +15 -0
  19. package/dist/cli/commands/init.d.ts.map +1 -0
  20. package/dist/cli/commands/init.js +43 -0
  21. package/dist/cli/commands/init.js.map +1 -0
  22. package/dist/cli/commands/machine/list.d.ts +7 -0
  23. package/dist/cli/commands/machine/list.d.ts.map +1 -0
  24. package/dist/cli/commands/machine/list.js +12 -0
  25. package/dist/cli/commands/machine/list.js.map +1 -0
  26. package/dist/cli/commands/machine/use.d.ts +11 -0
  27. package/dist/cli/commands/machine/use.d.ts.map +1 -0
  28. package/dist/cli/commands/machine/use.js +28 -0
  29. package/dist/cli/commands/machine/use.js.map +1 -0
  30. package/dist/cli/commands/pull.d.ts +12 -0
  31. package/dist/cli/commands/pull.d.ts.map +1 -0
  32. package/dist/cli/commands/pull.js +34 -0
  33. package/dist/cli/commands/pull.js.map +1 -0
  34. package/dist/cli/commands/push.d.ts +12 -0
  35. package/dist/cli/commands/push.d.ts.map +1 -0
  36. package/dist/cli/commands/push.js +34 -0
  37. package/dist/cli/commands/push.js.map +1 -0
  38. package/dist/cli/commands/status.d.ts +11 -0
  39. package/dist/cli/commands/status.d.ts.map +1 -0
  40. package/dist/cli/commands/status.js +27 -0
  41. package/dist/cli/commands/status.js.map +1 -0
  42. package/dist/cli/commands/track.d.ts +16 -0
  43. package/dist/cli/commands/track.d.ts.map +1 -0
  44. package/dist/cli/commands/track.js +82 -0
  45. package/dist/cli/commands/track.js.map +1 -0
  46. package/dist/cli/commands/untrack.d.ts +11 -0
  47. package/dist/cli/commands/untrack.d.ts.map +1 -0
  48. package/dist/cli/commands/untrack.js +28 -0
  49. package/dist/cli/commands/untrack.js.map +1 -0
  50. package/dist/config/global-config.d.ts +21 -0
  51. package/dist/config/global-config.d.ts.map +1 -0
  52. package/dist/config/global-config.js +106 -0
  53. package/dist/config/global-config.js.map +1 -0
  54. package/dist/config/platform.d.ts +11 -0
  55. package/dist/config/platform.d.ts.map +1 -0
  56. package/dist/config/platform.js +19 -0
  57. package/dist/config/platform.js.map +1 -0
  58. package/dist/config/sync.d.ts +107 -0
  59. package/dist/config/sync.d.ts.map +1 -0
  60. package/dist/config/sync.js +424 -0
  61. package/dist/config/sync.js.map +1 -0
  62. package/dist/config/xdg.d.ts +14 -0
  63. package/dist/config/xdg.d.ts.map +1 -0
  64. package/dist/config/xdg.js +102 -0
  65. package/dist/config/xdg.js.map +1 -0
  66. package/dist/index.d.ts +3 -0
  67. package/dist/index.d.ts.map +1 -0
  68. package/{src/index.ts → dist/index.js} +1 -1
  69. package/dist/index.js.map +1 -0
  70. package/dist/lib/file-mode.d.ts +3 -0
  71. package/dist/lib/file-mode.d.ts.map +1 -0
  72. package/dist/lib/file-mode.js +7 -0
  73. package/dist/lib/file-mode.js.map +1 -0
  74. package/dist/lib/output.d.ts +31 -0
  75. package/dist/lib/output.d.ts.map +1 -0
  76. package/dist/lib/output.js +198 -0
  77. package/dist/lib/output.js.map +1 -0
  78. package/dist/lib/path.d.ts +5 -0
  79. package/dist/lib/path.d.ts.map +1 -0
  80. package/dist/lib/path.js +25 -0
  81. package/dist/lib/path.js.map +1 -0
  82. package/dist/lib/string.d.ts +2 -0
  83. package/dist/lib/string.d.ts.map +1 -0
  84. package/dist/lib/string.js +4 -0
  85. package/dist/lib/string.js.map +1 -0
  86. package/dist/lib/validation.d.ts +3 -0
  87. package/dist/lib/validation.d.ts.map +1 -0
  88. package/dist/lib/validation.js +9 -0
  89. package/dist/lib/validation.js.map +1 -0
  90. package/dist/services/add.d.ts +20 -0
  91. package/dist/services/add.d.ts.map +1 -0
  92. package/dist/services/add.js +161 -0
  93. package/dist/services/add.js.map +1 -0
  94. package/dist/services/config-file.d.ts +45 -0
  95. package/dist/services/config-file.d.ts.map +1 -0
  96. package/dist/services/config-file.js +35 -0
  97. package/dist/services/config-file.js.map +1 -0
  98. package/dist/services/crypto.d.ts +9 -0
  99. package/dist/services/crypto.d.ts.map +1 -0
  100. package/dist/services/crypto.js +75 -0
  101. package/dist/services/crypto.js.map +1 -0
  102. package/dist/services/doctor.d.ts +16 -0
  103. package/dist/services/doctor.d.ts.map +1 -0
  104. package/dist/services/doctor.js +84 -0
  105. package/dist/services/doctor.js.map +1 -0
  106. package/dist/services/error.d.ts +14 -0
  107. package/dist/services/error.d.ts.map +1 -0
  108. package/dist/services/error.js +38 -0
  109. package/dist/services/error.js.map +1 -0
  110. package/dist/services/filesystem.d.ts +15 -0
  111. package/dist/services/filesystem.d.ts.map +1 -0
  112. package/dist/services/filesystem.js +113 -0
  113. package/dist/services/filesystem.js.map +1 -0
  114. package/dist/services/forget.d.ts +14 -0
  115. package/dist/services/forget.d.ts.map +1 -0
  116. package/dist/services/forget.js +124 -0
  117. package/dist/services/forget.js.map +1 -0
  118. package/dist/services/git.d.ts +10 -0
  119. package/dist/services/git.d.ts.map +1 -0
  120. package/dist/services/git.js +57 -0
  121. package/dist/services/git.js.map +1 -0
  122. package/dist/services/init.d.ts +19 -0
  123. package/dist/services/init.d.ts.map +1 -0
  124. package/dist/services/init.js +203 -0
  125. package/dist/services/init.js.map +1 -0
  126. package/dist/services/local-materialization.d.ts +28 -0
  127. package/dist/services/local-materialization.d.ts.map +1 -0
  128. package/dist/services/local-materialization.js +262 -0
  129. package/dist/services/local-materialization.js.map +1 -0
  130. package/dist/services/local-snapshot.d.ts +25 -0
  131. package/dist/services/local-snapshot.d.ts.map +1 -0
  132. package/dist/services/local-snapshot.js +93 -0
  133. package/dist/services/local-snapshot.js.map +1 -0
  134. package/dist/services/machine.d.ts +40 -0
  135. package/dist/services/machine.d.ts.map +1 -0
  136. package/dist/services/machine.js +113 -0
  137. package/dist/services/machine.js.map +1 -0
  138. package/dist/services/paths.d.ts +13 -0
  139. package/dist/services/paths.d.ts.map +1 -0
  140. package/dist/services/paths.js +71 -0
  141. package/dist/services/paths.js.map +1 -0
  142. package/dist/services/pull.d.ts +28 -0
  143. package/dist/services/pull.d.ts.map +1 -0
  144. package/dist/services/pull.js +67 -0
  145. package/dist/services/pull.js.map +1 -0
  146. package/dist/services/push.d.ts +35 -0
  147. package/dist/services/push.d.ts.map +1 -0
  148. package/dist/services/push.js +96 -0
  149. package/dist/services/push.js.map +1 -0
  150. package/dist/services/repo-artifacts.d.ts +52 -0
  151. package/dist/services/repo-artifacts.d.ts.map +1 -0
  152. package/dist/services/repo-artifacts.js +251 -0
  153. package/dist/services/repo-artifacts.js.map +1 -0
  154. package/dist/services/repo-snapshot.d.ts +6 -0
  155. package/dist/services/repo-snapshot.d.ts.map +1 -0
  156. package/dist/services/repo-snapshot.js +163 -0
  157. package/dist/services/repo-snapshot.js.map +1 -0
  158. package/dist/services/runtime.d.ts +40 -0
  159. package/dist/services/runtime.d.ts.map +1 -0
  160. package/dist/services/runtime.js +71 -0
  161. package/dist/services/runtime.js.map +1 -0
  162. package/dist/services/set.d.ts +38 -0
  163. package/dist/services/set.d.ts.map +1 -0
  164. package/dist/services/set.js +184 -0
  165. package/dist/services/set.js.map +1 -0
  166. package/dist/services/status.d.ts +30 -0
  167. package/dist/services/status.d.ts.map +1 -0
  168. package/dist/services/status.js +35 -0
  169. package/dist/services/status.js.map +1 -0
  170. package/package.json +15 -7
  171. package/src/cli/commands/add.ts +0 -40
  172. package/src/cli/commands/cd.ts +0 -80
  173. package/src/cli/commands/doctor.ts +0 -20
  174. package/src/cli/commands/forget.ts +0 -32
  175. package/src/cli/commands/index.ts +0 -23
  176. package/src/cli/commands/init.ts +0 -43
  177. package/src/cli/commands/list.ts +0 -17
  178. package/src/cli/commands/pull.ts +0 -31
  179. package/src/cli/commands/push.ts +0 -31
  180. package/src/cli/commands/set.ts +0 -47
  181. package/src/cli/commands/status.ts +0 -18
  182. package/src/cli/sync-output.test.ts +0 -173
  183. package/src/cli/sync-output.ts +0 -200
  184. package/src/config/sync.test.ts +0 -609
  185. package/src/config/sync.ts +0 -572
  186. package/src/config/xdg.ts +0 -138
  187. package/src/lib/string.test.ts +0 -13
  188. package/src/lib/string.ts +0 -3
  189. package/src/lib/validation.test.ts +0 -32
  190. package/src/lib/validation.ts +0 -11
  191. package/src/services/add.ts +0 -178
  192. package/src/services/config-file.test.ts +0 -161
  193. package/src/services/config-file.ts +0 -101
  194. package/src/services/crypto.test.ts +0 -132
  195. package/src/services/crypto.ts +0 -83
  196. package/src/services/doctor.ts +0 -142
  197. package/src/services/error.ts +0 -6
  198. package/src/services/filesystem.test.ts +0 -171
  199. package/src/services/filesystem.ts +0 -183
  200. package/src/services/forget.ts +0 -261
  201. package/src/services/git.test.ts +0 -83
  202. package/src/services/git.ts +0 -74
  203. package/src/services/init.test.ts +0 -109
  204. package/src/services/init.ts +0 -244
  205. package/src/services/list.ts +0 -63
  206. package/src/services/local-materialization.ts +0 -421
  207. package/src/services/local-snapshot.ts +0 -173
  208. package/src/services/paths.test.ts +0 -74
  209. package/src/services/paths.ts +0 -98
  210. package/src/services/pull.ts +0 -144
  211. package/src/services/push.ts +0 -168
  212. package/src/services/repo-artifacts.ts +0 -262
  213. package/src/services/repo-snapshot.ts +0 -197
  214. package/src/services/runtime.ts +0 -57
  215. package/src/services/set.ts +0 -383
  216. package/src/services/status.ts +0 -57
  217. package/src/services/sync.dry-run.test.ts +0 -179
  218. package/src/services/sync.runtime.test.ts +0 -756
  219. package/src/services/sync.service.test.ts +0 -1169
  220. package/src/test/helpers/sync-fixture.ts +0 -47
@@ -1,142 +0,0 @@
1
- import { readSyncConfig } from "#app/config/sync.ts";
2
-
3
- import { countConfiguredRules } from "./config-file.ts";
4
- import { pathExists } from "./filesystem.ts";
5
- import { ensureRepository } from "./git.ts";
6
- import type { SyncContext } from "./runtime.ts";
7
-
8
- export type DoctorCheckLevel = "fail" | "ok" | "warn";
9
-
10
- export type DoctorCheck = Readonly<{
11
- detail: string;
12
- level: DoctorCheckLevel;
13
- name: string;
14
- }>;
15
-
16
- export type SyncDoctorResult = Readonly<{
17
- checks: readonly DoctorCheck[];
18
- configPath: string;
19
- hasFailures: boolean;
20
- hasWarnings: boolean;
21
- syncDirectory: string;
22
- }>;
23
-
24
- const ok = (name: string, detail: string): DoctorCheck => ({
25
- detail,
26
- level: "ok",
27
- name,
28
- });
29
-
30
- const warn = (name: string, detail: string): DoctorCheck => ({
31
- detail,
32
- level: "warn",
33
- name,
34
- });
35
-
36
- const fail = (name: string, detail: string): DoctorCheck => ({
37
- detail,
38
- level: "fail",
39
- name,
40
- });
41
-
42
- export const runSyncDoctor = async (
43
- context: SyncContext,
44
- ): Promise<SyncDoctorResult> => {
45
- const checks: DoctorCheck[] = [];
46
-
47
- try {
48
- await ensureRepository(context.paths.syncDirectory);
49
- checks.push(ok("git", "Sync directory is a git repository."));
50
- } catch (error: unknown) {
51
- checks.push(
52
- fail(
53
- "git",
54
- error instanceof Error ? error.message : "Git repository check failed.",
55
- ),
56
- );
57
-
58
- return {
59
- checks,
60
- configPath: context.paths.configPath,
61
- hasFailures: true,
62
- hasWarnings: false,
63
- syncDirectory: context.paths.syncDirectory,
64
- };
65
- }
66
-
67
- let config: Awaited<ReturnType<typeof readSyncConfig>>;
68
-
69
- try {
70
- config = await readSyncConfig(
71
- context.paths.syncDirectory,
72
- context.environment,
73
- );
74
- checks.push(
75
- ok(
76
- "config",
77
- `Loaded config with ${config.entries.length} entries, ${countConfiguredRules(config)} rules, and ${config.age.recipients.length} recipients.`,
78
- ),
79
- );
80
- } catch (error: unknown) {
81
- checks.push(
82
- fail(
83
- "config",
84
- error instanceof Error
85
- ? error.message
86
- : "Sync configuration could not be read.",
87
- ),
88
- );
89
-
90
- return {
91
- checks,
92
- configPath: context.paths.configPath,
93
- hasFailures: true,
94
- hasWarnings: false,
95
- syncDirectory: context.paths.syncDirectory,
96
- };
97
- }
98
-
99
- checks.push(
100
- (await pathExists(config.age.identityFile))
101
- ? ok("age", `Age identity file exists at ${config.age.identityFile}.`)
102
- : fail("age", `Age identity file is missing: ${config.age.identityFile}`),
103
- );
104
-
105
- checks.push(
106
- config.entries.length === 0
107
- ? warn("entries", "No sync entries are configured yet.")
108
- : ok("entries", `Tracked ${config.entries.length} sync entries.`),
109
- );
110
-
111
- const missingEntries = config.entries.filter((entry) => {
112
- return !context.environment || entry.localPath.length > 0;
113
- });
114
-
115
- let missingCount = 0;
116
-
117
- for (const entry of missingEntries) {
118
- if (!(await pathExists(entry.localPath))) {
119
- missingCount += 1;
120
- }
121
- }
122
-
123
- checks.push(
124
- missingCount === 0
125
- ? ok("local-paths", "All tracked local paths currently exist.")
126
- : warn(
127
- "local-paths",
128
- `${missingCount} tracked local path${missingCount === 1 ? " is" : "s are"} missing.`,
129
- ),
130
- );
131
-
132
- const hasFailures = checks.some((check) => check.level === "fail");
133
- const hasWarnings = checks.some((check) => check.level === "warn");
134
-
135
- return {
136
- checks,
137
- configPath: context.paths.configPath,
138
- hasFailures,
139
- hasWarnings,
140
- syncDirectory: context.paths.syncDirectory,
141
- };
142
- };
@@ -1,6 +0,0 @@
1
- export class DevsyncError extends Error {
2
- public constructor(message: string) {
3
- super(message);
4
- this.name = "DevsyncError";
5
- }
6
- }
@@ -1,171 +0,0 @@
1
- import {
2
- chmod,
3
- lstat,
4
- mkdir,
5
- readFile,
6
- readlink,
7
- rm,
8
- symlink,
9
- writeFile,
10
- } from "node:fs/promises";
11
- import { join } from "node:path";
12
-
13
- import { afterEach, describe, expect, it } from "vitest";
14
-
15
- import {
16
- buildExecutableMode,
17
- copyFilesystemNode,
18
- getPathStats,
19
- isExecutableMode,
20
- listDirectoryEntries,
21
- pathExists,
22
- removePathAtomically,
23
- replacePathAtomically,
24
- writeFileNode,
25
- writeSymlinkNode,
26
- writeTextFileAtomically,
27
- } from "#app/services/filesystem.ts";
28
- import { createTemporaryDirectory } from "../test/helpers/sync-fixture.ts";
29
-
30
- const temporaryDirectories: string[] = [];
31
-
32
- const createWorkspace = async () => {
33
- const directory = await createTemporaryDirectory("devsync-filesystem-");
34
-
35
- temporaryDirectories.push(directory);
36
-
37
- return directory;
38
- };
39
-
40
- afterEach(async () => {
41
- while (temporaryDirectories.length > 0) {
42
- const directory = temporaryDirectories.pop();
43
-
44
- if (directory !== undefined) {
45
- await rm(directory, { force: true, recursive: true });
46
- }
47
- }
48
- });
49
-
50
- describe("filesystem helpers", () => {
51
- it("checks path existence and missing stats", async () => {
52
- const workspace = await createWorkspace();
53
- const filePath = join(workspace, "value.txt");
54
-
55
- expect(await pathExists(filePath)).toBe(false);
56
- expect(await getPathStats(filePath)).toBeUndefined();
57
-
58
- await writeFile(filePath, "value\n", "utf8");
59
-
60
- expect(await pathExists(filePath)).toBe(true);
61
- expect((await getPathStats(filePath))?.isFile()).toBe(true);
62
- });
63
-
64
- it("lists directory entries in sorted order", async () => {
65
- const workspace = await createWorkspace();
66
-
67
- await mkdir(join(workspace, "b"), { recursive: true });
68
- await writeFile(join(workspace, "c.txt"), "c\n", "utf8");
69
- await writeFile(join(workspace, "a.txt"), "a\n", "utf8");
70
-
71
- const entries = await listDirectoryEntries(workspace);
72
-
73
- expect(entries.map((entry) => entry.name)).toEqual(["a.txt", "b", "c.txt"]);
74
- });
75
-
76
- it("builds and detects executable modes", async () => {
77
- expect(buildExecutableMode(true)).toBe(0o755);
78
- expect(buildExecutableMode(false)).toBe(0o644);
79
- expect(isExecutableMode(0o100755)).toBe(true);
80
- expect(isExecutableMode(0o100644)).toBe(false);
81
- });
82
-
83
- it("writes regular files and preserves executable bits", async () => {
84
- if (process.platform === "win32") {
85
- return;
86
- }
87
-
88
- const workspace = await createWorkspace();
89
- const filePath = join(workspace, "bin", "tool.sh");
90
-
91
- await writeFileNode(filePath, {
92
- contents: "#!/bin/sh\nexit 0\n",
93
- executable: true,
94
- });
95
-
96
- expect(await readFile(filePath, "utf8")).toContain("#!/bin/sh");
97
- expect(isExecutableMode((await lstat(filePath)).mode)).toBe(true);
98
- });
99
-
100
- it("writes symlinks after removing existing content", async () => {
101
- if (process.platform === "win32") {
102
- return;
103
- }
104
-
105
- const workspace = await createWorkspace();
106
- const linkPath = join(workspace, "links", "current");
107
-
108
- await mkdir(join(workspace, "links"), { recursive: true });
109
- await writeFile(linkPath, "old\n", "utf8");
110
- await writeSymlinkNode(linkPath, "../target.txt");
111
-
112
- expect(await readlink(linkPath)).toBe("../target.txt");
113
- });
114
-
115
- it("copies regular files and symlinks", async () => {
116
- if (process.platform === "win32") {
117
- return;
118
- }
119
-
120
- const workspace = await createWorkspace();
121
- const sourceDirectory = join(workspace, "source");
122
- const targetDirectory = join(workspace, "target");
123
- const filePath = join(sourceDirectory, "nested", "value.txt");
124
- const linkPath = join(sourceDirectory, "nested", "value-link");
125
-
126
- await mkdir(join(sourceDirectory, "nested"), { recursive: true });
127
- await writeFile(filePath, "payload\n", "utf8");
128
- await chmod(filePath, 0o755);
129
- await symlink("value.txt", linkPath);
130
-
131
- await copyFilesystemNode(sourceDirectory, targetDirectory);
132
-
133
- expect(
134
- await readFile(join(targetDirectory, "nested", "value.txt"), "utf8"),
135
- ).toBe("payload\n");
136
- expect(await readlink(join(targetDirectory, "nested", "value-link"))).toBe(
137
- "value.txt",
138
- );
139
- });
140
-
141
- it("replaces and removes paths atomically", async () => {
142
- const workspace = await createWorkspace();
143
- const targetPath = join(workspace, "config.json");
144
- const stagedPath = join(workspace, "next.json");
145
-
146
- await writeFile(targetPath, "old\n", "utf8");
147
- await writeFile(stagedPath, "new\n", "utf8");
148
-
149
- await replacePathAtomically(targetPath, stagedPath);
150
-
151
- expect(await readFile(targetPath, "utf8")).toBe("new\n");
152
- expect(await pathExists(stagedPath)).toBe(false);
153
-
154
- await removePathAtomically(targetPath);
155
-
156
- expect(await pathExists(targetPath)).toBe(false);
157
- await removePathAtomically(targetPath);
158
- expect(await pathExists(targetPath)).toBe(false);
159
- });
160
-
161
- it("writes text files atomically for create and overwrite flows", async () => {
162
- const workspace = await createWorkspace();
163
- const targetPath = join(workspace, "nested", "config.json");
164
-
165
- await writeTextFileAtomically(targetPath, "first\n");
166
- expect(await readFile(targetPath, "utf8")).toBe("first\n");
167
-
168
- await writeTextFileAtomically(targetPath, "second\n");
169
- expect(await readFile(targetPath, "utf8")).toBe("second\n");
170
- });
171
- });
@@ -1,183 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import {
3
- access,
4
- chmod,
5
- lstat,
6
- mkdir,
7
- mkdtemp,
8
- readdir,
9
- readFile,
10
- readlink,
11
- rename,
12
- rm,
13
- symlink,
14
- writeFile,
15
- } from "node:fs/promises";
16
- import { basename, dirname, join } from "node:path";
17
-
18
- import { DevsyncError } from "./error.ts";
19
-
20
- export const pathExists = async (path: string) => {
21
- try {
22
- await access(path);
23
-
24
- return true;
25
- } catch (error: unknown) {
26
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
27
- return false;
28
- }
29
-
30
- throw error;
31
- }
32
- };
33
-
34
- export const getPathStats = async (path: string) => {
35
- try {
36
- return await lstat(path);
37
- } catch (error: unknown) {
38
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
39
- return undefined;
40
- }
41
-
42
- throw error;
43
- }
44
- };
45
-
46
- export const listDirectoryEntries = async (path: string) => {
47
- const entries = await readdir(path, { withFileTypes: true });
48
-
49
- return entries.sort((left, right) => {
50
- return left.name.localeCompare(right.name);
51
- });
52
- };
53
-
54
- export const buildExecutableMode = (executable: boolean) => {
55
- return executable ? 0o755 : 0o644;
56
- };
57
-
58
- export const isExecutableMode = (mode: number | bigint) => {
59
- return (Number(mode) & 0o111) !== 0;
60
- };
61
-
62
- export const writeFileNode = async (
63
- path: string,
64
- node: Readonly<{
65
- contents: string | Uint8Array;
66
- executable: boolean;
67
- }>,
68
- ) => {
69
- await mkdir(dirname(path), { recursive: true });
70
- await writeFile(path, node.contents);
71
- await chmod(path, buildExecutableMode(node.executable));
72
- };
73
-
74
- export const writeSymlinkNode = async (path: string, linkTarget: string) => {
75
- await mkdir(dirname(path), { recursive: true });
76
- await rm(path, { force: true, recursive: true });
77
- await symlink(linkTarget, path);
78
- };
79
-
80
- export const copyFilesystemNode = async (
81
- sourcePath: string,
82
- targetPath: string,
83
- stats?: Awaited<ReturnType<typeof lstat>>,
84
- ) => {
85
- const sourceStats = stats ?? (await lstat(sourcePath));
86
-
87
- if (sourceStats.isDirectory()) {
88
- await mkdir(targetPath, { recursive: true });
89
-
90
- const entries = await listDirectoryEntries(sourcePath);
91
-
92
- for (const entry of entries) {
93
- await copyFilesystemNode(
94
- join(sourcePath, entry.name),
95
- join(targetPath, entry.name),
96
- );
97
- }
98
-
99
- return;
100
- }
101
-
102
- if (sourceStats.isSymbolicLink()) {
103
- await writeSymlinkNode(targetPath, await readlink(sourcePath));
104
-
105
- return;
106
- }
107
-
108
- if (!sourceStats.isFile()) {
109
- throw new DevsyncError(`Unsupported filesystem entry: ${sourcePath}`);
110
- }
111
-
112
- await writeFileNode(targetPath, {
113
- contents: await readFile(sourcePath),
114
- executable: isExecutableMode(sourceStats.mode),
115
- });
116
- };
117
-
118
- export const replacePathAtomically = async (
119
- targetPath: string,
120
- nextPath: string,
121
- ) => {
122
- const backupPath = join(
123
- dirname(targetPath),
124
- `.${basename(targetPath)}.devsync-sync-backup-${randomUUID()}`,
125
- );
126
- const existingStats = await getPathStats(targetPath);
127
- let targetMoved = false;
128
-
129
- try {
130
- if (existingStats !== undefined) {
131
- await rename(targetPath, backupPath);
132
- targetMoved = true;
133
- }
134
-
135
- await rename(nextPath, targetPath);
136
-
137
- if (targetMoved) {
138
- await rm(backupPath, { force: true, recursive: true });
139
- }
140
- } catch (error: unknown) {
141
- if (targetMoved && !(await pathExists(targetPath))) {
142
- await rename(backupPath, targetPath).catch(() => {});
143
- }
144
-
145
- throw error;
146
- } finally {
147
- await rm(backupPath, { force: true, recursive: true }).catch(() => {});
148
- }
149
- };
150
-
151
- export const removePathAtomically = async (targetPath: string) => {
152
- const stats = await getPathStats(targetPath);
153
-
154
- if (stats === undefined) {
155
- return;
156
- }
157
-
158
- const backupPath = join(
159
- dirname(targetPath),
160
- `.${basename(targetPath)}.devsync-sync-remove-${randomUUID()}`,
161
- );
162
-
163
- await rename(targetPath, backupPath);
164
- await rm(backupPath, { force: true, recursive: true });
165
- };
166
-
167
- export const writeTextFileAtomically = async (
168
- targetPath: string,
169
- contents: string,
170
- ) => {
171
- await mkdir(dirname(targetPath), { recursive: true });
172
- const stagingDirectory = await mkdtemp(
173
- join(dirname(targetPath), `.${basename(targetPath)}.devsync-sync-`),
174
- );
175
- const stagedPath = join(stagingDirectory, basename(targetPath));
176
-
177
- try {
178
- await writeFile(stagedPath, contents, "utf8");
179
- await replacePathAtomically(targetPath, stagedPath);
180
- } finally {
181
- await rm(stagingDirectory, { force: true, recursive: true });
182
- }
183
- };