Skip to content

Commit 3b0571d

Browse files
committed
cleanup denodeify from git
1 parent fc704d9 commit 3b0571d

3 files changed

Lines changed: 12 additions & 27 deletions

File tree

extensions/git/src/git.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as fs from 'fs';
6+
import { promises as fs, exists } from 'fs';
77
import * as path from 'path';
88
import * as os from 'os';
99
import * as cp from 'child_process';
1010
import * as which from 'which';
1111
import { EventEmitter } from 'events';
1212
import iconv = require('iconv-lite');
1313
import * as filetype from 'file-type';
14-
import { assign, groupBy, denodeify, IDisposable, toDisposable, dispose, mkdirp, readBytes, detectUnicodeEncoding, Encoding, onceEvent, splitInChunks, Limiter } from './util';
14+
import { assign, groupBy, IDisposable, toDisposable, dispose, mkdirp, readBytes, detectUnicodeEncoding, Encoding, onceEvent, splitInChunks, Limiter } from './util';
1515
import { CancellationToken, Progress } from 'vscode';
1616
import { URI } from 'vscode-uri';
1717
import { detectEncoding } from './encoding';
@@ -22,8 +22,6 @@ import { StringDecoder } from 'string_decoder';
2222
// https://github.com/microsoft/vscode/issues/65693
2323
const MAX_CLI_LENGTH = 30000;
2424

25-
const readfile = denodeify<string, string | null, string>(fs.readFile);
26-
2725
export interface IGit {
2826
path: string;
2927
version: string;
@@ -350,7 +348,7 @@ export class Git {
350348
let folderPath = path.join(parentPath, folderName);
351349
let count = 1;
352350

353-
while (count < 20 && await new Promise(c => fs.exists(folderPath, c))) {
351+
while (count < 20 && await new Promise(c => exists(folderPath, c))) {
354352
folderName = `${baseFolderName}-${count++}`;
355353
folderPath = path.join(parentPath, folderName);
356354
}
@@ -1830,7 +1828,7 @@ export class Repository {
18301828
const mergeMsgPath = path.join(this.repositoryRoot, '.git', 'MERGE_MSG');
18311829

18321830
try {
1833-
const raw = await readfile(mergeMsgPath, 'utf8');
1831+
const raw = await fs.readFile(mergeMsgPath, 'utf8');
18341832
return raw.trim();
18351833
} catch {
18361834
return undefined;
@@ -1854,7 +1852,7 @@ export class Repository {
18541852
templatePath = path.join(this.repositoryRoot, templatePath);
18551853
}
18561854

1857-
const raw = await readfile(templatePath, 'utf8');
1855+
const raw = await fs.readFile(templatePath, 'utf8');
18581856
return raw.trim();
18591857

18601858
} catch (err) {
@@ -1879,7 +1877,7 @@ export class Repository {
18791877
const gitmodulesPath = path.join(this.root, '.gitmodules');
18801878

18811879
try {
1882-
const gitmodulesRaw = await readfile(gitmodulesPath, 'utf8');
1880+
const gitmodulesRaw = await fs.readFile(gitmodulesPath, 'utf8');
18831881
return parseGitmodules(gitmodulesRaw);
18841882
} catch (err) {
18851883
if (/ENOENT/.test(err.message)) {

extensions/git/src/ipc/ipcServer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { Disposable } from 'vscode';
7-
import { denodeify, toDisposable } from '../util';
7+
import { toDisposable } from '../util';
88
import * as path from 'path';
99
import * as http from 'http';
1010
import * as os from 'os';
1111
import * as fs from 'fs';
1212
import * as crypto from 'crypto';
1313

14-
const randomBytes = denodeify<Buffer>(crypto.randomBytes);
15-
1614
function getIPCHandlePath(nonce: string): string {
1715
if (process.platform === 'win32') {
1816
return `\\\\.\\pipe\\vscode-git-ipc-${nonce}-sock`;
@@ -31,7 +29,7 @@ export interface IIPCHandler {
3129

3230
export async function createIPCServer(): Promise<IIPCServer> {
3331
const server = http.createServer();
34-
const buffer = await randomBytes(20);
32+
const buffer = await new Promise<Buffer>((c, e) => crypto.randomBytes(20, (err, buf) => err ? e(err) : c(buf)));
3533
const nonce = buffer.toString('hex');
3634
const ipcHandlePath = getIPCHandlePath(nonce);
3735

extensions/git/src/util.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { Event } from 'vscode';
77
import { dirname, sep } from 'path';
88
import { Readable } from 'stream';
9-
import * as fs from 'fs';
9+
import { promises as fs, createReadStream } from 'fs';
1010
import * as byline from 'byline';
1111

1212
export function log(...args: any[]): void {
@@ -140,25 +140,14 @@ export function groupBy<T>(arr: T[], fn: (el: T) => string): { [key: string]: T[
140140
}, Object.create(null));
141141
}
142142

143-
export function denodeify<A, B, C, R>(fn: Function): (a: A, b: B, c: C) => Promise<R>;
144-
export function denodeify<A, B, R>(fn: Function): (a: A, b: B) => Promise<R>;
145-
export function denodeify<A, R>(fn: Function): (a: A) => Promise<R>;
146-
export function denodeify<R>(fn: Function): (...args: any[]) => Promise<R>;
147-
export function denodeify<R>(fn: Function): (...args: any[]) => Promise<R> {
148-
return (...args) => new Promise<R>((c, e) => fn(...args, (err: any, r: any) => err ? e(err) : c(r)));
149-
}
150-
151-
export function nfcall<R>(fn: Function, ...args: any[]): Promise<R> {
152-
return new Promise<R>((c, e) => fn(...args, (err: any, r: any) => err ? e(err) : c(r)));
153-
}
154143

155144
export async function mkdirp(path: string, mode?: number): Promise<boolean> {
156145
const mkdir = async () => {
157146
try {
158-
await nfcall(fs.mkdir, path, mode);
147+
await fs.mkdir(path, mode);
159148
} catch (err) {
160149
if (err.code === 'EEXIST') {
161-
const stat = await nfcall<fs.Stats>(fs.stat, path);
150+
const stat = await fs.stat(path);
162151

163152
if (stat.isDirectory()) {
164153
return;
@@ -232,7 +221,7 @@ export function find<T>(array: T[], fn: (t: T) => boolean): T | undefined {
232221

233222
export async function grep(filename: string, pattern: RegExp): Promise<boolean> {
234223
return new Promise<boolean>((c, e) => {
235-
const fileStream = fs.createReadStream(filename, { encoding: 'utf8' });
224+
const fileStream = createReadStream(filename, { encoding: 'utf8' });
236225
const stream = byline(fileStream);
237226
stream.on('data', (line: string) => {
238227
if (pattern.test(line)) {

0 commit comments

Comments
 (0)