@tinyrack/devsync 1.0.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.
@@ -0,0 +1,421 @@
1
+ import { lstat, mkdir, mkdtemp, rm, symlink } from "node:fs/promises";
2
+ import { basename, dirname, join, posix } from "node:path";
3
+
4
+ import {
5
+ type ResolvedSyncConfig,
6
+ type ResolvedSyncConfigEntry,
7
+ resolveManagedSyncMode,
8
+ } from "#app/config/sync.ts";
9
+ import { DevsyncError } from "./error.ts";
10
+ import {
11
+ copyFilesystemNode,
12
+ getPathStats,
13
+ listDirectoryEntries,
14
+ removePathAtomically,
15
+ replacePathAtomically,
16
+ writeFileNode,
17
+ writeSymlinkNode,
18
+ } from "./filesystem.ts";
19
+ import type { FileLikeSnapshotNode, SnapshotNode } from "./local-snapshot.ts";
20
+ import { buildDirectoryKey } from "./paths.ts";
21
+
22
+ type EntryMaterialization =
23
+ | Readonly<{
24
+ desiredKeys: ReadonlySet<string>;
25
+ type: "absent";
26
+ }>
27
+ | Readonly<{
28
+ desiredKeys: ReadonlySet<string>;
29
+ node: FileLikeSnapshotNode;
30
+ type: "file";
31
+ }>
32
+ | Readonly<{
33
+ desiredKeys: ReadonlySet<string>;
34
+ nodes: ReadonlyMap<string, FileLikeSnapshotNode>;
35
+ type: "directory";
36
+ }>;
37
+
38
+ const copyIgnoredLocalNodesToDirectory = async (
39
+ sourceDirectory: string,
40
+ targetDirectory: string,
41
+ config: ResolvedSyncConfig,
42
+ repoPathPrefix: string,
43
+ ): Promise<number> => {
44
+ const stats = await getPathStats(sourceDirectory);
45
+
46
+ if (stats === undefined || !stats.isDirectory()) {
47
+ return 0;
48
+ }
49
+
50
+ let copiedNodeCount = 0;
51
+ const entries = await listDirectoryEntries(sourceDirectory);
52
+ const directoryMode = resolveManagedSyncMode(config, repoPathPrefix);
53
+
54
+ if (directoryMode === "ignore") {
55
+ await mkdir(targetDirectory, { recursive: true });
56
+ copiedNodeCount += 1;
57
+ }
58
+
59
+ for (const entry of entries) {
60
+ const sourcePath = join(sourceDirectory, entry.name);
61
+ const targetPath = join(targetDirectory, entry.name);
62
+ const repoPath = posix.join(repoPathPrefix, entry.name);
63
+ const entryStats = await lstat(sourcePath);
64
+
65
+ if (entryStats.isDirectory()) {
66
+ copiedNodeCount += await copyIgnoredLocalNodesToDirectory(
67
+ sourcePath,
68
+ targetPath,
69
+ config,
70
+ repoPath,
71
+ );
72
+ continue;
73
+ }
74
+
75
+ if (resolveManagedSyncMode(config, repoPath) !== "ignore") {
76
+ continue;
77
+ }
78
+
79
+ await mkdir(dirname(targetPath), { recursive: true });
80
+ await copyFilesystemNode(sourcePath, targetPath, entryStats);
81
+ copiedNodeCount += 1;
82
+ }
83
+
84
+ return copiedNodeCount;
85
+ };
86
+
87
+ const stageAndReplaceFilePath = async (
88
+ targetPath: string,
89
+ node: FileLikeSnapshotNode,
90
+ ) => {
91
+ await mkdir(dirname(targetPath), { recursive: true });
92
+ const stagingDirectory = await mkdtemp(
93
+ join(dirname(targetPath), `.${basename(targetPath)}.devsync-sync-`),
94
+ );
95
+ const stagedPath = join(stagingDirectory, basename(targetPath));
96
+
97
+ try {
98
+ if (node.type === "symlink") {
99
+ await symlink(node.linkTarget, stagedPath);
100
+ } else {
101
+ await writeFileNode(stagedPath, node);
102
+ }
103
+
104
+ await replacePathAtomically(targetPath, stagedPath);
105
+ } finally {
106
+ await rm(stagingDirectory, { force: true, recursive: true });
107
+ }
108
+ };
109
+
110
+ const stageAndReplaceMergedDirectoryPath = async (
111
+ entry: ResolvedSyncConfigEntry,
112
+ config: ResolvedSyncConfig,
113
+ desiredNodes: ReadonlyMap<string, FileLikeSnapshotNode>,
114
+ ) => {
115
+ await mkdir(dirname(entry.localPath), { recursive: true });
116
+ const stagingDirectory = await mkdtemp(
117
+ join(
118
+ dirname(entry.localPath),
119
+ `.${basename(entry.localPath)}.devsync-sync-`,
120
+ ),
121
+ );
122
+
123
+ try {
124
+ const preservedIgnoredNodeCount = await copyIgnoredLocalNodesToDirectory(
125
+ entry.localPath,
126
+ stagingDirectory,
127
+ config,
128
+ entry.repoPath,
129
+ );
130
+
131
+ for (const relativePath of [...desiredNodes.keys()].sort((left, right) => {
132
+ return left.localeCompare(right);
133
+ })) {
134
+ const node = desiredNodes.get(relativePath);
135
+
136
+ if (node === undefined) {
137
+ continue;
138
+ }
139
+
140
+ const targetNodePath = join(stagingDirectory, ...relativePath.split("/"));
141
+
142
+ if (node.type === "symlink") {
143
+ await writeSymlinkNode(targetNodePath, node.linkTarget);
144
+ } else {
145
+ await writeFileNode(targetNodePath, node);
146
+ }
147
+ }
148
+
149
+ if (preservedIgnoredNodeCount === 0 && desiredNodes.size === 0) {
150
+ await removePathAtomically(entry.localPath);
151
+
152
+ return;
153
+ }
154
+
155
+ await replacePathAtomically(entry.localPath, stagingDirectory);
156
+ } finally {
157
+ await rm(stagingDirectory, { force: true, recursive: true });
158
+ }
159
+ };
160
+
161
+ export const buildEntryMaterialization = (
162
+ entry: ResolvedSyncConfigEntry,
163
+ snapshot: ReadonlyMap<string, SnapshotNode>,
164
+ ): EntryMaterialization => {
165
+ if (entry.kind === "file") {
166
+ const node = snapshot.get(entry.repoPath);
167
+
168
+ if (node === undefined) {
169
+ return {
170
+ desiredKeys: new Set<string>(),
171
+ type: "absent",
172
+ };
173
+ }
174
+
175
+ if (node.type === "directory") {
176
+ throw new DevsyncError(
177
+ `File sync entry resolves to a directory in the repository: ${entry.repoPath}`,
178
+ );
179
+ }
180
+
181
+ return {
182
+ desiredKeys: new Set<string>([entry.repoPath]),
183
+ node,
184
+ type: "file",
185
+ };
186
+ }
187
+
188
+ const rootNode = snapshot.get(entry.repoPath);
189
+
190
+ if (rootNode !== undefined && rootNode.type !== "directory") {
191
+ throw new DevsyncError(
192
+ `Directory sync entry resolves to a file in the repository: ${entry.repoPath}`,
193
+ );
194
+ }
195
+
196
+ const nodes = new Map<string, FileLikeSnapshotNode>();
197
+ const desiredKeys = new Set<string>();
198
+
199
+ for (const [repoPath, node] of snapshot.entries()) {
200
+ if (!repoPath.startsWith(`${entry.repoPath}/`)) {
201
+ continue;
202
+ }
203
+
204
+ if (node.type === "directory") {
205
+ continue;
206
+ }
207
+
208
+ const relativePath = repoPath.slice(entry.repoPath.length + 1);
209
+
210
+ nodes.set(relativePath, node);
211
+ desiredKeys.add(repoPath);
212
+ }
213
+
214
+ if (rootNode === undefined && nodes.size === 0) {
215
+ return {
216
+ desiredKeys,
217
+ type: "absent",
218
+ };
219
+ }
220
+
221
+ desiredKeys.add(buildDirectoryKey(entry.repoPath));
222
+
223
+ return {
224
+ desiredKeys,
225
+ nodes,
226
+ type: "directory",
227
+ };
228
+ };
229
+
230
+ const collectLocalLeafKeys = async (
231
+ targetPath: string,
232
+ repoPathPrefix: string,
233
+ keys: Set<string>,
234
+ prefix?: string,
235
+ ) => {
236
+ const stats = await getPathStats(targetPath);
237
+
238
+ if (stats === undefined) {
239
+ return;
240
+ }
241
+
242
+ if (!stats.isDirectory()) {
243
+ keys.add(repoPathPrefix);
244
+
245
+ return;
246
+ }
247
+
248
+ keys.add(buildDirectoryKey(repoPathPrefix));
249
+
250
+ const entries = await listDirectoryEntries(targetPath);
251
+
252
+ for (const entry of entries) {
253
+ const absolutePath = join(targetPath, entry.name);
254
+ const relativePath =
255
+ prefix === undefined ? entry.name : `${prefix}/${entry.name}`;
256
+ const childStats = await lstat(absolutePath);
257
+
258
+ if (childStats?.isDirectory()) {
259
+ await collectLocalLeafKeys(
260
+ absolutePath,
261
+ repoPathPrefix,
262
+ keys,
263
+ relativePath,
264
+ );
265
+ continue;
266
+ }
267
+
268
+ keys.add(posix.join(repoPathPrefix, relativePath));
269
+ }
270
+ };
271
+
272
+ const collectIgnoredLocalKeys = async (
273
+ targetPath: string,
274
+ repoPath: string,
275
+ config: ResolvedSyncConfig,
276
+ keys: Set<string>,
277
+ ): Promise<boolean> => {
278
+ const stats = await getPathStats(targetPath);
279
+
280
+ if (stats === undefined) {
281
+ return false;
282
+ }
283
+
284
+ const mode = resolveManagedSyncMode(config, repoPath);
285
+
286
+ if (!stats.isDirectory()) {
287
+ if (mode !== "ignore") {
288
+ return false;
289
+ }
290
+
291
+ keys.add(repoPath);
292
+
293
+ return true;
294
+ }
295
+
296
+ let preservedIgnoredChildren = mode === "ignore";
297
+ const entries = await listDirectoryEntries(targetPath);
298
+
299
+ for (const entry of entries) {
300
+ const childPath = join(targetPath, entry.name);
301
+ const childRepoPath = posix.join(repoPath, entry.name);
302
+
303
+ preservedIgnoredChildren =
304
+ (await collectIgnoredLocalKeys(childPath, childRepoPath, config, keys)) ||
305
+ preservedIgnoredChildren;
306
+ }
307
+
308
+ if (mode === "ignore" || preservedIgnoredChildren) {
309
+ keys.add(buildDirectoryKey(repoPath));
310
+ }
311
+
312
+ return mode === "ignore" || preservedIgnoredChildren;
313
+ };
314
+
315
+ export const countDeletedLocalNodes = async (
316
+ entry: ResolvedSyncConfigEntry,
317
+ desiredKeys: ReadonlySet<string>,
318
+ config: ResolvedSyncConfig,
319
+ ) => {
320
+ const existingKeys = new Set<string>();
321
+ const preservedIgnoredKeys = new Set<string>();
322
+
323
+ await collectLocalLeafKeys(entry.localPath, entry.repoPath, existingKeys);
324
+ await collectIgnoredLocalKeys(
325
+ entry.localPath,
326
+ entry.repoPath,
327
+ config,
328
+ preservedIgnoredKeys,
329
+ );
330
+
331
+ return [...existingKeys].filter((key) => {
332
+ return !desiredKeys.has(key) && !preservedIgnoredKeys.has(key);
333
+ }).length;
334
+ };
335
+
336
+ export const applyEntryMaterialization = async (
337
+ entry: ResolvedSyncConfigEntry,
338
+ materialization: EntryMaterialization,
339
+ config: ResolvedSyncConfig,
340
+ ) => {
341
+ if (
342
+ entry.kind === "file" &&
343
+ resolveManagedSyncMode(config, entry.repoPath) === "ignore"
344
+ ) {
345
+ return;
346
+ }
347
+
348
+ if (materialization.type === "absent") {
349
+ if (entry.kind === "directory") {
350
+ await stageAndReplaceMergedDirectoryPath(entry, config, new Map());
351
+
352
+ return;
353
+ }
354
+
355
+ await removePathAtomically(entry.localPath);
356
+
357
+ return;
358
+ }
359
+
360
+ if (materialization.type === "file") {
361
+ await stageAndReplaceFilePath(entry.localPath, materialization.node);
362
+
363
+ return;
364
+ }
365
+
366
+ await stageAndReplaceMergedDirectoryPath(
367
+ entry,
368
+ config,
369
+ materialization.nodes,
370
+ );
371
+ };
372
+
373
+ export const buildPullCounts = (
374
+ materializations: readonly EntryMaterialization[],
375
+ ) => {
376
+ let decryptedFileCount = 0;
377
+ let directoryCount = 0;
378
+ let plainFileCount = 0;
379
+ let symlinkCount = 0;
380
+
381
+ for (const materialization of materializations) {
382
+ if (materialization === undefined) {
383
+ continue;
384
+ }
385
+
386
+ if (materialization.type === "file") {
387
+ if (materialization.node.type === "symlink") {
388
+ symlinkCount += 1;
389
+ } else if (materialization.node.secret) {
390
+ decryptedFileCount += 1;
391
+ } else {
392
+ plainFileCount += 1;
393
+ }
394
+
395
+ continue;
396
+ }
397
+
398
+ if (materialization.type !== "directory") {
399
+ continue;
400
+ }
401
+
402
+ directoryCount += 1;
403
+
404
+ for (const node of materialization.nodes.values()) {
405
+ if (node.type === "symlink") {
406
+ symlinkCount += 1;
407
+ } else if (node.secret) {
408
+ decryptedFileCount += 1;
409
+ } else {
410
+ plainFileCount += 1;
411
+ }
412
+ }
413
+ }
414
+
415
+ return {
416
+ decryptedFileCount,
417
+ directoryCount,
418
+ plainFileCount,
419
+ symlinkCount,
420
+ };
421
+ };
@@ -0,0 +1,173 @@
1
+ import { lstat, readFile, readlink } from "node:fs/promises";
2
+ import { join, posix } from "node:path";
3
+
4
+ import {
5
+ type ResolvedSyncConfig,
6
+ resolveManagedSyncMode,
7
+ } from "#app/config/sync.ts";
8
+ import { DevsyncError } from "./error.ts";
9
+ import {
10
+ getPathStats,
11
+ isExecutableMode,
12
+ listDirectoryEntries,
13
+ } from "./filesystem.ts";
14
+ import { assertStorageSafeRepoPath } from "./repo-artifacts.ts";
15
+
16
+ export type SnapshotNode =
17
+ | Readonly<{
18
+ type: "directory";
19
+ }>
20
+ | Readonly<{
21
+ executable: boolean;
22
+ secret: boolean;
23
+ type: "file";
24
+ contents: Uint8Array;
25
+ }>
26
+ | Readonly<{
27
+ linkTarget: string;
28
+ type: "symlink";
29
+ }>;
30
+
31
+ export type FileSnapshotNode = Extract<
32
+ SnapshotNode,
33
+ Readonly<{ type: "file" }>
34
+ >;
35
+
36
+ export type FileLikeSnapshotNode = Extract<
37
+ SnapshotNode,
38
+ Readonly<{ type: "file" | "symlink" }>
39
+ >;
40
+
41
+ export const addSnapshotNode = (
42
+ snapshot: Map<string, SnapshotNode>,
43
+ repoPath: string,
44
+ node: SnapshotNode,
45
+ ) => {
46
+ if (snapshot.has(repoPath)) {
47
+ throw new DevsyncError(`Duplicate sync path generated for ${repoPath}`);
48
+ }
49
+
50
+ snapshot.set(repoPath, node);
51
+ };
52
+
53
+ const addLocalNode = async (
54
+ snapshot: Map<string, SnapshotNode>,
55
+ config: ResolvedSyncConfig,
56
+ repoPath: string,
57
+ path: string,
58
+ stats: Awaited<ReturnType<typeof lstat>>,
59
+ ) => {
60
+ assertStorageSafeRepoPath(repoPath);
61
+ const mode = resolveManagedSyncMode(config, repoPath);
62
+
63
+ if (mode === "ignore") {
64
+ return;
65
+ }
66
+
67
+ if (stats.isDirectory()) {
68
+ throw new DevsyncError(
69
+ `Expected a file-like path but found a directory: ${path}`,
70
+ );
71
+ }
72
+
73
+ if (stats.isSymbolicLink()) {
74
+ if (mode === "secret") {
75
+ throw new DevsyncError(
76
+ `Secret sync paths must be regular files, not symlinks: ${repoPath}`,
77
+ );
78
+ }
79
+
80
+ addSnapshotNode(snapshot, repoPath, {
81
+ linkTarget: await readlink(path),
82
+ type: "symlink",
83
+ });
84
+
85
+ return;
86
+ }
87
+
88
+ if (!stats.isFile()) {
89
+ throw new DevsyncError(`Unsupported filesystem entry: ${path}`);
90
+ }
91
+
92
+ addSnapshotNode(snapshot, repoPath, {
93
+ contents: await readFile(path),
94
+ executable: isExecutableMode(stats.mode),
95
+ secret: mode === "secret",
96
+ type: "file",
97
+ });
98
+ };
99
+
100
+ const walkLocalDirectory = async (
101
+ snapshot: Map<string, SnapshotNode>,
102
+ config: ResolvedSyncConfig,
103
+ localDirectory: string,
104
+ repoPathPrefix: string,
105
+ ) => {
106
+ const entries = await listDirectoryEntries(localDirectory);
107
+
108
+ for (const entry of entries) {
109
+ const localPath = join(localDirectory, entry.name);
110
+ const repoPath = posix.join(repoPathPrefix, entry.name);
111
+ const stats = await lstat(localPath);
112
+
113
+ if (stats.isDirectory()) {
114
+ assertStorageSafeRepoPath(repoPath);
115
+ await walkLocalDirectory(snapshot, config, localPath, repoPath);
116
+ continue;
117
+ }
118
+
119
+ await addLocalNode(snapshot, config, repoPath, localPath, stats);
120
+ }
121
+ };
122
+
123
+ export const buildLocalSnapshot = async (config: ResolvedSyncConfig) => {
124
+ const snapshot = new Map<string, SnapshotNode>();
125
+
126
+ for (const entry of config.entries) {
127
+ const stats = await getPathStats(entry.localPath);
128
+
129
+ if (stats === undefined) {
130
+ continue;
131
+ }
132
+
133
+ const entryMode = resolveManagedSyncMode(config, entry.repoPath);
134
+
135
+ if (entry.kind === "file") {
136
+ if (entryMode === "ignore") {
137
+ continue;
138
+ }
139
+
140
+ if (stats.isDirectory()) {
141
+ throw new DevsyncError(
142
+ `Sync entry ${entry.name} expects a file, but found a directory: ${entry.localPath}`,
143
+ );
144
+ }
145
+
146
+ await addLocalNode(
147
+ snapshot,
148
+ config,
149
+ entry.repoPath,
150
+ entry.localPath,
151
+ stats,
152
+ );
153
+ continue;
154
+ }
155
+
156
+ if (!stats.isDirectory()) {
157
+ throw new DevsyncError(
158
+ `Sync entry ${entry.name} expects a directory: ${entry.localPath}`,
159
+ );
160
+ }
161
+
162
+ const snapshotSizeBeforeWalk = snapshot.size;
163
+ await walkLocalDirectory(snapshot, config, entry.localPath, entry.repoPath);
164
+
165
+ if (entryMode !== "ignore" || snapshot.size > snapshotSizeBeforeWalk) {
166
+ addSnapshotNode(snapshot, entry.repoPath, {
167
+ type: "directory",
168
+ });
169
+ }
170
+ }
171
+
172
+ return snapshot;
173
+ };
@@ -0,0 +1,98 @@
1
+ import { isAbsolute, relative, resolve } from "node:path";
2
+
3
+ import { normalizeSyncRepoPath } from "#app/config/sync.ts";
4
+ import { expandHomePath } from "#app/config/xdg.ts";
5
+
6
+ import { DevsyncError } from "./error.ts";
7
+
8
+ export const buildDirectoryKey = (repoPath: string) => {
9
+ return `${repoPath}/`;
10
+ };
11
+
12
+ export const isPathEqualOrNested = (path: string, rootPath: string) => {
13
+ const rootToPath = relative(rootPath, path);
14
+
15
+ return (
16
+ rootToPath === "" ||
17
+ (!isAbsolute(rootToPath) &&
18
+ !rootToPath.startsWith("..") &&
19
+ rootToPath !== "..")
20
+ );
21
+ };
22
+
23
+ export const doPathsOverlap = (leftPath: string, rightPath: string) => {
24
+ return (
25
+ isPathEqualOrNested(leftPath, rightPath) ||
26
+ isPathEqualOrNested(rightPath, leftPath)
27
+ );
28
+ };
29
+
30
+ export const isExplicitLocalPath = (target: string) => {
31
+ return (
32
+ target === "." ||
33
+ target === ".." ||
34
+ target === "~" ||
35
+ target.startsWith("./") ||
36
+ target.startsWith("../") ||
37
+ target.startsWith("~/") ||
38
+ isAbsolute(target)
39
+ );
40
+ };
41
+
42
+ export const resolveCommandTargetPath = (
43
+ target: string,
44
+ environment: NodeJS.ProcessEnv,
45
+ cwd: string,
46
+ ) => {
47
+ return resolve(cwd, expandHomePath(target, environment));
48
+ };
49
+
50
+ export const buildRepoPathWithinRoot = (
51
+ absolutePath: string,
52
+ rootPath: string,
53
+ description: string,
54
+ ) => {
55
+ const relativePath = relative(rootPath, absolutePath);
56
+
57
+ if (relativePath === "") {
58
+ throw new DevsyncError(
59
+ `${description} must be inside ${rootPath}, not the root itself: ${absolutePath}`,
60
+ );
61
+ }
62
+
63
+ if (
64
+ isAbsolute(relativePath) ||
65
+ relativePath.startsWith("..") ||
66
+ relativePath === ".."
67
+ ) {
68
+ throw new DevsyncError(
69
+ `${description} must be inside ${rootPath}: ${absolutePath}`,
70
+ );
71
+ }
72
+
73
+ return normalizeSyncRepoPath(relativePath);
74
+ };
75
+
76
+ export const buildConfiguredHomeLocalPath = (repoPath: string) => {
77
+ return `~/${repoPath}`;
78
+ };
79
+
80
+ export const tryBuildRepoPathWithinRoot = (
81
+ absolutePath: string,
82
+ rootPath: string,
83
+ description: string,
84
+ ) => {
85
+ try {
86
+ return buildRepoPathWithinRoot(absolutePath, rootPath, description);
87
+ } catch {
88
+ return undefined;
89
+ }
90
+ };
91
+
92
+ export const tryNormalizeRepoPathInput = (value: string) => {
93
+ try {
94
+ return normalizeSyncRepoPath(value);
95
+ } catch {
96
+ return undefined;
97
+ }
98
+ };