Skip to content

Commit c15dfd2

Browse files
committed
Refactor APK fetch & terminal script setup to fully use fs utils
1 parent ce6e467 commit c15dfd2

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

src/interceptors/android/fetch-apk.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as fs from 'fs';
21
import * as path from 'path';
32
import * as stream from 'stream';
43
import * as semver from 'semver';
54
import fetch from 'node-fetch';
65

7-
import { readDir, createTmp, moveFile, deleteFile } from '../../util/fs';
6+
import * as fs from '../../util/fs';
87
import { HtkConfig } from '../../config';
98
import { logError } from '../../error-tracking';
109

@@ -30,7 +29,7 @@ async function getLatestRelease(): Promise<{ version: string, url: string } | un
3029
}
3130

3231
async function getAllLocalApks(config: HtkConfig) {
33-
const apks = (await readDir(config.configPath))
32+
const apks = (await fs.readDir(config.configPath))
3433
.map(filename => filename.match(/^httptoolkit-(.*).apk$/))
3534
.filter((match): match is RegExpMatchArray => !!match)
3635
.map((match) => ({
@@ -68,7 +67,7 @@ async function updateLocalApk(
6867
path: tmpApk,
6968
fd: tmpApkFd,
7069
cleanupCallback
71-
} = await createTmp({ keep: true });
70+
} = await fs.createTmp({ keep: true });
7271

7372
const tmpApkStream = fs.createWriteStream(tmpApk, { fd: tmpApkFd });
7473
apkStream.pipe(tmpApkStream);
@@ -88,15 +87,15 @@ async function updateLocalApk(
8887

8988
console.log(`Local APK written to ${tmpApk}`);
9089

91-
await moveFile(tmpApk, path.join(config.configPath, `httptoolkit-${version}.apk`));
90+
await fs.moveFile(tmpApk, path.join(config.configPath, `httptoolkit-${version}.apk`));
9291
console.log(`Local APK moved to ${path.join(config.configPath, `httptoolkit-${version}.apk`)}`);
9392
await cleanupOldApks(config);
9493
}
9594

9695
export async function clearAllApks(config: HtkConfig) {
9796
const apks = await getAllLocalApks(config);
9897
console.log(`Deleting all APKs: ${apks.map(apk => apk.path).join(', ')}`);
99-
return Promise.all(apks.map(apk => deleteFile(apk.path)));
98+
return Promise.all(apks.map(apk => fs.deleteFile(apk.path)));
10099
}
101100

102101
// Delete all but the most recent APK version in the config directory.
@@ -106,7 +105,7 @@ async function cleanupOldApks(config: HtkConfig) {
106105
console.log(`Deleting old APKs: ${apks.slice(1).map(apk => apk.path).join(', ')}`);
107106

108107
return Promise.all(
109-
apks.slice(1).map(apk => deleteFile(apk.path))
108+
apks.slice(1).map(apk => fs.deleteFile(apk.path))
110109
);
111110
}
112111

src/interceptors/terminal/terminal-scripts.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import _ from 'lodash';
2-
import * as fs from 'fs';
32
import * as util from 'util';
43
import * as os from 'os';
54
import * as path from 'path';
65

7-
import { canAccess, writeFile, moveFile, readFile, getRealPath } from '../../util/fs';
6+
import * as fs from '../../util/fs';
87
import { logError } from '../../error-tracking';
98
import { OVERRIDE_BIN_PATH } from './terminal-env-overrides';
109

@@ -15,16 +14,15 @@ const POSIX_OVERRIDE_BIN_PATH = process.platform === 'win32'
1514

1615
const SHELL = (process.env.SHELL || '').split('/').slice(-1)[0];
1716

18-
const appendOrCreateFile = util.promisify(fs.appendFile);
1917
const appendToFirstExisting = async (paths: string[], forceWrite: boolean, contents: string) => {
2018
for (let path of paths) {
2119
// Follow the path through symlinks (relatively common for terminal config):
22-
const realPath = await getRealPath(path);
20+
const realPath = await fs.getRealPath(path);
2321
if (!realPath) continue; // File (or linked file) does not exist
2422

25-
if (await canAccess(realPath)) {
23+
if (await fs.canAccess(realPath)) {
2624
// Found our first valid readable file - append our extra config
27-
return appendOrCreateFile(realPath, contents);
25+
return fs.appendOrCreateFile(realPath, contents);
2826
}
2927

3028
// ^ Small races here, if the file content/perms change between check and write, but
@@ -33,7 +31,7 @@ const appendToFirstExisting = async (paths: string[], forceWrite: boolean, conte
3331

3432
if (forceWrite) {
3533
// If force write is set, write the last file anyway, even though it didn't exist before:
36-
return appendOrCreateFile(paths.slice(-1)[0], contents);
34+
return fs.appendOrCreateFile(paths.slice(-1)[0], contents);
3735
}
3836
};
3937

@@ -196,19 +194,19 @@ export const editShellStartupScripts = async () => {
196194
[
197195
path.join(os.homedir(), '.config', 'fish', 'config.fish'),
198196
],
199-
SHELL === 'fish' || await canAccess(path.join(os.homedir(), '.config', 'fish')),
197+
SHELL === 'fish' || await fs.canAccess(path.join(os.homedir(), '.config', 'fish')),
200198
FISH_SHELL_PATH_CONFIG
201199
).catch(logError);
202200
};
203201

204202
const removeConfigSectionsFromFile = async (path: string) => {
205203
let fileLines: string[];
206204

207-
const targetPath = await getRealPath(path); // Follow symlinks, if present
205+
const targetPath = await fs.getRealPath(path); // Follow symlinks, if present
208206
if (!targetPath) return; // File doesn't exist, no need to clean it up
209207

210208
try {
211-
fileLines = (await readFile(targetPath, 'utf8')).split('\n');
209+
fileLines = (await fs.readFile(targetPath, 'utf8')).split('\n');
212210
} catch (e) {
213211
// Silently skip any files we can't read
214212
return;
@@ -227,8 +225,8 @@ const removeConfigSectionsFromFile = async (path: string) => {
227225
// Write & rename to ensure this is atomic, and avoid races here
228226
// as much as we reasonably can.
229227
const tempFile = targetPath + Date.now() + '.temp';
230-
await writeFile(tempFile, fileLines.join('\n'));
231-
return moveFile(tempFile, targetPath);
228+
await fs.writeFile(tempFile, fileLines.join('\n'));
229+
return fs.moveFile(tempFile, targetPath);
232230
};
233231

234232
// Cleanup: strip our extra config line from all config files

src/util/fs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const chmod = fs.promises.chmod;
1818
export const mkDir = fs.promises.mkdir;
1919
export const writeFile = fs.promises.writeFile;
2020
export const copyFile = fs.promises.copyFile;
21+
export const appendOrCreateFile = fs.promises.appendFile;
22+
23+
export const createReadStream = fs.createReadStream;
24+
export const createWriteStream = fs.createWriteStream;
2125

2226
export const copyRecursive = async (from: string, to: string) => {
2327
// fs.cp is only available in Node 16.7.0+

0 commit comments

Comments
 (0)