Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove fs-extra usage #319

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
},
"dependencies": {
"async-mutex": "^0.5.0",
"fs-extra": "^11.1.1",
"global-agent": "^3.0.0",
"probot": "^12.3.3",
"prom-client": "^14.2.0",
Expand All @@ -27,7 +26,6 @@
"yaml": "^2.3.1"
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.0.0",
"@types/node": "^20.11.8",
"@types/pino-std-serializers": "^4.0.0",
Expand Down
16 changes: 8 additions & 8 deletions spec/operations.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawnSync } from 'child_process';
import * as fs from 'fs-extra';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import simpleGit from 'simple-git';
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('runner', () => {

afterEach(async () => {
if (dirObject && dirObject.dir) {
await fs.remove(dirObject.dir);
await fs.promises.rm(dirObject.dir, { force: true, recursive: true });
}
});

Expand All @@ -51,8 +51,8 @@ describe('runner', () => {
accessToken: '',
}),
);
expect(await fs.pathExists(dir)).toBe(true);
expect(await fs.pathExists(path.resolve(dir, '.git'))).toBe(true);
expect(fs.existsSync(dir)).toBe(true);
expect(fs.existsSync(path.resolve(dir, '.git'))).toBe(true);
});

it('should fail if the github repository does not exist', async () => {
Expand All @@ -69,14 +69,14 @@ describe('runner', () => {
let dir: string;

beforeEach(async () => {
dir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'trop-spec-'));
await fs.mkdirp(dir);
dir = await fs.promises.mkdtemp(path.resolve(os.tmpdir(), 'trop-spec-'));
await fs.promises.mkdir(dir, { recursive: true });
spawnSync('git', ['init'], { cwd: dir });
});

afterEach(async () => {
if (await fs.pathExists(dir)) {
await fs.remove(dir);
if (fs.existsSync(dir)) {
await fs.promises.rm(dir, { force: true, recursive: true });
}
});

Expand Down
14 changes: 7 additions & 7 deletions src/operations/backport-commits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from 'fs-extra';
import * as fs from 'fs';
import * as path from 'path';
import simpleGit from 'simple-git';
import { BackportOptions } from '../interfaces';
Expand Down Expand Up @@ -77,7 +77,7 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {

for (const patch of options.patches) {
try {
await fs.writeFile(patchPath, patch, 'utf8');
await fs.promises.writeFile(patchPath, patch, 'utf8');
await git.raw(['am', '-3', '--keep-cr', patchPath]);
} catch (error) {
log(
Expand All @@ -89,8 +89,8 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {

return false;
} finally {
if (await fs.pathExists(patchPath)) {
await fs.remove(patchPath);
if (fs.existsSync(patchPath)) {
await fs.promises.rm(patchPath, { force: true, recursive: true });
}
}
}
Expand Down Expand Up @@ -131,16 +131,16 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {
tree: await Promise.all(
changedFiles.map(async (changedFile) => {
const onDiskPath = path.resolve(options.dir, changedFile);
if (!(await fs.pathExists(onDiskPath))) {
if (!fs.existsSync(onDiskPath)) {
return {
path: changedFile,
mode: '100644',
type: 'blob',
sha: null,
};
}
const fileContents = await fs.readFile(onDiskPath);
const stat = await fs.stat(onDiskPath);
const fileContents = await fs.promises.readFile(onDiskPath);
const stat = await fs.promises.stat(onDiskPath);
const userMode = (stat.mode & parseInt('777', 8)).toString(8)[0];
if (isUtf8(fileContents)) {
return {
Expand Down
12 changes: 6 additions & 6 deletions src/operations/init-repo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from 'yaml';
import * as fs from 'fs-extra';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import simpleGit, { CheckRepoActions } from 'simple-git';
Expand All @@ -24,7 +24,7 @@ function mutexForRepoCache(slug: string) {
async function updateRepoCache({ slug, accessToken }: InitRepoOptions) {
const cacheDir = path.resolve(baseDir, slug, 'git-cache');

await fs.mkdirp(cacheDir);
await fs.promises.mkdir(cacheDir, { recursive: true });
const git = simpleGit(cacheDir);
if (!(await git.checkIsRepo(CheckRepoActions.BARE))) {
// The repo might be missing, or otherwise somehow corrupt. Re-clone it.
Expand All @@ -33,8 +33,8 @@ async function updateRepoCache({ slug, accessToken }: InitRepoOptions) {
LogLevel.INFO,
`${cacheDir} was not a git repo, cloning...`,
);
await fs.remove(cacheDir);
await fs.mkdirp(cacheDir);
await fs.promises.rm(cacheDir, { recursive: true, force: true });
await fs.promises.mkdir(cacheDir, { recursive: true });
await git.clone(githubUrl({ slug, accessToken }), '.', ['--bare']);
}
await git.fetch();
Expand All @@ -54,9 +54,9 @@ export const initRepo = async ({
}: InitRepoOptions): Promise<{ dir: string }> => {
log('initRepo', LogLevel.INFO, 'Setting up local repository');

await fs.mkdirp(path.resolve(baseDir, slug));
await fs.promises.mkdir(path.resolve(baseDir, slug), { recursive: true });
const prefix = path.resolve(baseDir, slug, 'job-');
const dir = await fs.mkdtemp(prefix);
const dir = await fs.promises.mkdtemp(prefix);
const git = simpleGit(dir);

// Concurrent access to the repo cache has the potential to mess things up.
Expand Down
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from 'fs-extra';
import * as fs from 'fs';
import { execSync } from 'child_process';
import Queue from 'queue';
import simpleGit from 'simple-git';
Expand Down Expand Up @@ -717,7 +717,7 @@ export const backportImpl = async (
}),
);

await fs.remove(createdDir);
await fs.promises.rm(createdDir, { force: true, recursive: true });
},
async () => {
let annotations: unknown[] | null = null;
Expand Down Expand Up @@ -758,7 +758,7 @@ export const backportImpl = async (
}
}

await fs.remove(createdDir);
await fs.promises.rm(createdDir, { force: true, recursive: true });
}

if (purpose === BackportPurpose.ExecuteBackport) {
Expand Down
45 changes: 1 addition & 44 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1200,14 +1200,6 @@
"@types/qs" "*"
"@types/serve-static" "*"

"@types/fs-extra@^11.0.1":
version "11.0.1"
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-11.0.1.tgz#f542ec47810532a8a252127e6e105f487e0a6ea5"
integrity sha512-MxObHvNl4A69ofaTRU8DFqvgzzv8s9yRtaPPm5gud9HDNvpB3GPQFvNuTWAI59B9huVGV5jXYJwbCsmBsOGYWA==
dependencies:
"@types/jsonfile" "*"
"@types/node" "*"

"@types/graceful-fs@^4.1.3":
version "4.1.5"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
Expand Down Expand Up @@ -1269,13 +1261,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==

"@types/jsonfile@*":
version "6.1.1"
resolved "https://registry.yarnpkg.com/@types/jsonfile/-/jsonfile-6.1.1.tgz#ac84e9aefa74a2425a0fb3012bdea44f58970f1b"
integrity sha512-GSgiRCVeapDN+3pqA35IkQwasaCh/0YFH5dEF6S88iDvEn901DjOeH3/QPY+XYP1DFzDZPvIvfeEgk+7br5png==
dependencies:
"@types/node" "*"

"@types/jsonwebtoken@^9.0.0":
version "9.0.1"
resolved "https://registry.yarnpkg.com/@types/jsonwebtoken/-/jsonwebtoken-9.0.1.tgz#29b1369c4774200d6d6f63135bf3d1ba3ef997a4"
Expand Down Expand Up @@ -2832,15 +2817,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"
integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==

fs-extra@^11.1.1:
version "11.1.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.1.1.tgz#da69f7c39f3b002378b0954bb6ae7efdc0876e2d"
integrity sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -3042,16 +3018,11 @@ gopd@^1.0.1:
dependencies:
get-intrinsic "^1.1.3"

graceful-fs@^4.1.15, graceful-fs@^4.1.6:
graceful-fs@^4.1.15:
version "4.2.6"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.6.tgz#ff040b2b0853b23c3d31027523706f1885d76bee"
integrity sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ==

graceful-fs@^4.2.0:
version "4.2.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==

graceful-fs@^4.2.10, graceful-fs@^4.2.9:
version "4.2.10"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.10.tgz#147d3a006da4ca3ce14728c7aefc287c367d7a6c"
Expand Down Expand Up @@ -3919,15 +3890,6 @@ json5@^2.2.1:
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

jsonfile@^6.0.1:
version "6.1.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==
dependencies:
universalify "^2.0.0"
optionalDependencies:
graceful-fs "^4.1.6"

jsonwebtoken@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-9.0.0.tgz#d0faf9ba1cc3a56255fe49c0961a67e520c1926d"
Expand Down Expand Up @@ -5514,11 +5476,6 @@ universal-user-agent@^6.0.0:
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.0.tgz#3381f8503b251c0d9cd21bc1de939ec9df5480ee"
integrity sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==

universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==

[email protected], unpipe@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unpipe/-/unpipe-1.0.0.tgz#b2bf4ee8514aae6165b4817829d21b2ef49904ec"
Expand Down