Skip to content

Commit d62369f

Browse files
committed
Combine file-manager and file-description
1 parent 978d9e8 commit d62369f

File tree

17 files changed

+81
-20
lines changed

17 files changed

+81
-20
lines changed

source/artifacts/artifacts-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import path from 'node:path';
22
import ssri from 'ssri';
33
import type { BundleContent, BundleDescription } from '../bundler/bundle-description.js';
44
import type { TarballBuilder } from '../tar/tarball-builder.js';
5-
import type { FileDescription } from '../file-description/file-description.js';
6-
import { isExecutableFileMode } from '../file-description/permissions.js';
7-
import type { FileManager } from './file-manager.js';
5+
import type { FileDescription } from '../file-manager/file-description.js';
6+
import { isExecutableFileMode } from '../file-manager/permissions.js';
7+
import type { FileManager } from '../file-manager/file-manager.js';
88

99
export type ArtifactsBuilderDependencies = {
1010
readonly fileManager: FileManager;

source/dependency-scanner/source-map-file-locator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path';
22
import { Maybe } from 'true-myth';
3-
import type { FileManager } from '../artifacts/file-manager.js';
3+
import type { FileManager } from '../file-manager/file-manager.js';
44

55
export type SourceMapFileLocatorDependencies = {
66
readonly fileManager: FileManager;

source/file-description/file-description.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type BaseDescription = {
2+
readonly content: string;
3+
readonly isExecutable: boolean;
4+
};
5+
6+
export type FileDescription = BaseDescription & {
7+
readonly filePath: string;
8+
};
9+
10+
export type TransferableFileDescription = BaseDescription & {
11+
readonly sourceFilePath: string;
12+
readonly targetFilePath: string;
13+
};

source/artifacts/file-manager.test.ts renamed to source/file-manager/file-manager.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,37 @@ test('getFileMode() returns the file mode of the given file path', async (t) =>
101101
t.deepEqual(stat.firstCall.args, ['/foo/bar.txt']);
102102
t.is(result, 42);
103103
});
104+
105+
test('getTransferableFileDescriptionFromPath() returns the file description of the given file paths', async (t) => {
106+
const stat = fake.resolves({ mode: 42 });
107+
const readFile = fake.resolves('the-content');
108+
const fileManager = fileManagerFactory({ stat, readFile });
109+
110+
const result = await fileManager.getTransferableFileDescriptionFromPath('/foo/bar.txt', '/target/path.txt');
111+
112+
t.is(stat.callCount, 1);
113+
t.deepEqual(stat.firstCall.args, ['/foo/bar.txt']);
114+
t.is(readFile.callCount, 1);
115+
t.deepEqual(readFile.firstCall.args, ['/foo/bar.txt']);
116+
t.deepEqual(result, {
117+
sourceFilePath: '/foo/bar.txt',
118+
targetFilePath: '/target/path.txt',
119+
content: 'the-content',
120+
isExecutable: false
121+
});
122+
});
123+
124+
test('getTransferableFileDescriptionFromPath() returns the file description with isExecutable set to true when the file mode indicates this', async (t) => {
125+
const stat = fake.resolves({ mode: 493 });
126+
const readFile = fake.resolves('the-content');
127+
const fileManager = fileManagerFactory({ stat, readFile });
128+
129+
const result = await fileManager.getTransferableFileDescriptionFromPath('/foo/bar.txt', '/target/path.txt');
130+
131+
t.deepEqual(result, {
132+
sourceFilePath: '/foo/bar.txt',
133+
targetFilePath: '/target/path.txt',
134+
content: 'the-content',
135+
isExecutable: true
136+
});
137+
});

source/artifacts/file-manager.ts renamed to source/file-manager/file-manager.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import type { TransferableFileDescription } from './file-description.js';
4+
import { isExecutableFileMode } from './permissions.js';
35

46
export type FileManagerDependencies = {
57
readonly hostFileSystem: typeof fs.promises;
@@ -15,6 +17,10 @@ export type FileManager = {
1517
writeFile(filePath: string, content: string): Promise<void>;
1618
copyFile(from: string, to: string): Promise<void>;
1719
getFileMode(filePath: string): Promise<number>;
20+
getTransferableFileDescriptionFromPath(
21+
sourceFilePath: string,
22+
targetFilePath: string
23+
): Promise<TransferableFileDescription>;
1824
};
1925

2026
export function createFileManager(dependencies: FileManagerDependencies): FileManager {
@@ -44,21 +50,34 @@ export function createFileManager(dependencies: FileManagerDependencies): FileMa
4450
return hostFileSystem.readFile(filePath, { encoding: 'utf8' });
4551
}
4652

53+
async function getFileMode(filePath: string): Promise<number> {
54+
const stats = await hostFileSystem.stat(filePath);
55+
return stats.mode;
56+
}
57+
4758
return {
4859
checkReadability,
4960

5061
writeFile,
5162

5263
readFile,
5364

65+
getFileMode,
66+
67+
async getTransferableFileDescriptionFromPath(sourceFilePath, targetFilePath) {
68+
const mode = await getFileMode(sourceFilePath);
69+
70+
return {
71+
sourceFilePath,
72+
targetFilePath,
73+
content: await readFile(targetFilePath),
74+
isExecutable: isExecutableFileMode(mode)
75+
};
76+
},
77+
5478
async copyFile(from, to) {
5579
const content = await readFile(from);
5680
await writeFile(to, content);
57-
},
58-
59-
async getFileMode(filePath) {
60-
const stats = await hostFileSystem.stat(filePath);
61-
return stats.mode;
6281
}
6382
};
6483
}

0 commit comments

Comments
 (0)