Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Feb 11, 2025
1 parent 6807109 commit 0f540cd
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
npx lint-staged
npx lint-staged
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import util from 'node:util';
import semver from 'semver';
import sumchecker from 'sumchecker';

Expand Down Expand Up @@ -71,7 +72,7 @@ async function validateArtifact(
const generatedChecksums = fileNames
.map((fileName) => `${checksums[fileName]} *${fileName}`)
.join('\n');
await fs.promises.writeFile(shasumPath, generatedChecksums);
await util.promisify(fs.writeFile)(shasumPath, generatedChecksums);
} else {
shasumPath = await _downloadArtifact({
isGeneric: true,
Expand Down Expand Up @@ -173,7 +174,7 @@ export async function downloadArtifact(
// that the caller can take ownership of the returned file
const tempDir = await mkdtemp(artifactDetails.tempDirectory);
artifactPath = path.resolve(tempDir, fileName);
await fs.promises.copyFile(cachedPath, artifactPath);
await util.promisify(fs.copyFile)(cachedPath, artifactPath);
}
try {
await validateArtifact(details, artifactPath, downloadArtifact);
Expand Down
15 changes: 8 additions & 7 deletions test/Cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'graceful-fs';
import os from 'node:os';
import path from 'node:path';
import util from 'node:util';

import { afterEach, beforeEach, describe, expect, it } from 'vitest';

Expand Down Expand Up @@ -41,7 +42,7 @@ describe('Cache', () => {
const cacheFolder = path.resolve(cacheDir, sanitizedDummyUrl);
await fs.promises.mkdir(cacheFolder, { recursive: true });
const cachePath = path.resolve(cacheFolder, 'test.txt');
await fs.promises.writeFile(cachePath, 'dummy data');
await util.promisify(fs.writeFile)(cachePath, 'dummy data');
expect(cache.getPathForFileInCache(dummyUrl, 'test.txt')).toEqual(cachePath);
});
});
Expand All @@ -59,7 +60,7 @@ describe('Cache', () => {
const originalFolder = path.resolve(cacheDir, sanitizedDummyUrl);
await fs.promises.mkdir(originalFolder, { recursive: true });
const originalPath = path.resolve(originalFolder, 'original.txt');
await fs.promises.writeFile(originalPath, 'dummy data');
await util.promisify(fs.writeFile)(originalPath, 'dummy data');
await cache.putFileInCache(dummyUrl, originalPath, 'test.txt');
expect(fs.existsSync(originalPath)).toEqual(false);
});
Expand All @@ -68,24 +69,24 @@ describe('Cache', () => {
const originalFolder = path.resolve(cacheDir, sanitizedDummyUrl);
await fs.promises.mkdir(originalFolder, { recursive: true });
const originalPath = path.resolve(originalFolder, 'original.txt');
await fs.promises.writeFile(originalPath, 'example content');
await util.promisify(fs.writeFile)(originalPath, 'example content');
const cachePath = await cache.putFileInCache(dummyUrl, originalPath, 'test.txt');
expect(cachePath.startsWith(cacheDir)).toEqual(true);
expect(await fs.promises.readFile(cachePath, 'utf8')).toEqual('example content');
expect(await util.promisify(fs.readFile)(cachePath, 'utf8')).toEqual('example content');
});

it('should overwrite the file if it already exists in cache', async () => {
const originalFolder = path.resolve(cacheDir, sanitizedDummyUrl);
await fs.promises.mkdir(originalFolder, { recursive: true });
const originalPath = path.resolve(cacheDir, 'original.txt');
await fs.promises.writeFile(originalPath, 'example content');
await fs.promises.writeFile(
await util.promisify(fs.writeFile)(originalPath, 'example content');
await util.promisify(fs.writeFile)(
path.resolve(cacheDir, sanitizedDummyUrl, 'test.txt'),
'bad content',
);
const cachePath = await cache.putFileInCache(dummyUrl, originalPath, 'test.txt');
expect(cachePath.startsWith(cacheDir)).toEqual(true);
expect(await fs.promises.readFile(cachePath, 'utf8')).toEqual('example content');
expect(await util.promisify(fs.readFile)(cachePath, 'utf8')).toEqual('example content');
});
});
});
3 changes: 2 additions & 1 deletion test/FixtureDownloader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'graceful-fs';
import path from 'node:path';
import util from 'node:util';

import { DownloadOptions } from '../src/types';
import { Downloader } from '../src/Downloader';
Expand All @@ -14,6 +15,6 @@ export class FixtureDownloader implements Downloader<DownloadOptions> {
throw new Error(`Cannot find the fixture '${fixtureFile}'`);
}

await fs.promises.copyFile(fixtureFile, targetFilePath);
await util.promisify(fs.copyFile)(fixtureFile, targetFilePath);
}
}
5 changes: 3 additions & 2 deletions test/GotDownloader.network.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'graceful-fs';
import path from 'node:path';
import util from 'node:util';

import { describe, expect, it, vi } from 'vitest';

Expand All @@ -26,7 +27,7 @@ describe('GotDownloader', () => {
},
);
expect(fs.existsSync(testFile)).toEqual(true);
expect(await fs.promises.readFile(testFile, 'utf8')).toMatchSnapshot();
expect(await util.promisify(fs.readFile)(testFile, 'utf8')).toMatchSnapshot();
expect(progressCallbackCalled).toEqual(true);
}, TempDirCleanUpMode.CLEAN);
});
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('GotDownloader', () => {
),
).resolves.toBeUndefined();
expect(fs.existsSync(testFile)).toEqual(true);
expect(await fs.promises.readFile(testFile, 'utf8')).toMatchSnapshot();
expect(await util.promisify(fs.readFile)(testFile, 'utf8')).toMatchSnapshot();
}, TempDirCleanUpMode.CLEAN);
});
});
Expand Down
19 changes: 11 additions & 8 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from 'node:os';
import path from 'node:path';
import util from 'node:util';

import fs from 'graceful-fs';
import sumchecker from 'sumchecker';
Expand Down Expand Up @@ -55,11 +56,11 @@ describe('Public API', () => {
expect(
url.replace(process.platform, 'platform').replace(process.arch, 'arch'),
).toMatchSnapshot();
await fs.promises.writeFile(targetPath, 'faked from downloader');
await util.promisify(fs.writeFile)(targetPath, 'faked from downloader');
},
},
});
expect(await fs.promises.readFile(zipPath, 'utf8')).toEqual('faked from downloader');
expect(await util.promisify(fs.readFile)(zipPath, 'utf8')).toEqual('faked from downloader');
});

it('should pass download options to a custom downloader', async () => {
Expand All @@ -73,7 +74,7 @@ describe('Public API', () => {
downloader: {
async download(url: string, targetPath: string, opts?: DownloadOptions): Promise<void> {
expect(opts).toStrictEqual(downloadOpts);
await fs.promises.writeFile(targetPath, 'file');
await util.promisify(fs.writeFile)(targetPath, 'file');
},
},
downloadOptions: downloadOpts,
Expand Down Expand Up @@ -111,15 +112,15 @@ describe('Public API', () => {
cacheRoot,
downloader,
});
await fs.promises.writeFile(zipPath, 'cached content');
await util.promisify(fs.writeFile)(zipPath, 'cached content');
const zipPath2 = await download('2.0.9', {
cacheRoot,
downloader,
cacheMode: ElectronDownloadCacheMode.ReadOnly,
});
expect(zipPath2).not.toEqual(zipPath);
expect(path.dirname(zipPath2).startsWith(cacheRoot)).toEqual(false);
expect(await fs.promises.readFile(zipPath2, 'utf8')).toEqual('cached content');
expect(await util.promisify(fs.readFile)(zipPath2, 'utf8')).toEqual('cached content');
});
});

Expand All @@ -134,7 +135,9 @@ describe('Public API', () => {
});
expect(fs.existsSync(dtsPath)).toEqual(true);
expect(path.basename(dtsPath)).toEqual('electron.d.ts');
expect(await fs.promises.readFile(dtsPath, 'utf8')).toContain('declare namespace Electron');
expect(await util.promisify(fs.readFile)(dtsPath, 'utf8')).toContain(
'declare namespace Electron',
);
expect(path.dirname(dtsPath).startsWith(cacheRoot)).toEqual(true);
expect(fs.readdirSync(cacheRoot).length).toBeGreaterThan(0);
});
Expand Down Expand Up @@ -253,7 +256,7 @@ describe('Public API', () => {
platform: 'darwin',
arch: 'x64',
});
await fs.promises.writeFile(driverPath, 'cached content');
await util.promisify(fs.writeFile)(driverPath, 'cached content');
const driverPath2 = await downloadArtifact({
cacheRoot,
downloader,
Expand All @@ -265,7 +268,7 @@ describe('Public API', () => {
});
expect(driverPath2).not.toEqual(driverPath);
expect(path.dirname(driverPath2).startsWith(cacheRoot)).toEqual(false);
expect(await fs.promises.readFile(driverPath2, 'utf8')).toEqual('cached content');
expect(await util.promisify(fs.readFile)(driverPath2, 'utf8')).toEqual('cached content');
});

describe('sumchecker', () => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"sourceMap": true,
"outDir": "dist",
"types": ["node"],
"declaration": true
},
"include": ["src"]
}

0 comments on commit 0f540cd

Please sign in to comment.