Skip to content

Commit 28d5384

Browse files
authored
refactor: improve utils code (#1153)
* refactor: improve utils code * refactor: improve code by cr suggest * test: improve getGistId case
1 parent c7f1937 commit 28d5384

File tree

9 files changed

+78
-65
lines changed

9 files changed

+78
-65
lines changed

src/utils/electron-name.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
* @returns {string}
55
*/
66
export function getElectronNameForPlatform(): string {
7-
if (process.platform === 'win32') {
8-
return 'electron.exe';
9-
} else if (process.platform === 'darwin') {
10-
return 'Electron.app';
11-
} else {
12-
return 'electron';
7+
switch (process.platform) {
8+
case 'win32': {
9+
return 'electron.exe';
10+
}
11+
case 'darwin': {
12+
return 'Electron.app';
13+
}
14+
default: {
15+
return 'electron';
16+
}
1317
}
1418
}

src/utils/exec.ts

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
import { exec as cp_exec } from 'child_process';
2+
import { promisify } from 'util';
3+
14
import shellEnv from 'shell-env';
25

3-
// Singleton: We don't want to do this more than once.
4-
let _shellPathCalled = false;
6+
/**
7+
* On macOS & Linux, we need to fix the $PATH environment variable
8+
* so that we can call `npm`.
9+
*
10+
* @returns {Promise<void>}
11+
*/
12+
export const maybeFixPath = (() => {
13+
// Singleton: We don't want to do this more than once.
14+
let _shellPathCalled = false;
15+
16+
return async (): Promise<void> => {
17+
if (_shellPathCalled) {
18+
return;
19+
}
20+
21+
if (process.platform !== 'win32') {
22+
const { PATH } = await shellEnv();
23+
if (PATH) {
24+
process.env.PATH = PATH;
25+
}
26+
}
27+
28+
_shellPathCalled = true;
29+
};
30+
})();
531

632
/**
733
* Execute a command in a directory.
@@ -11,40 +37,12 @@ let _shellPathCalled = false;
1137
* @returns {Promise<string>}
1238
*/
1339
export async function exec(dir: string, cliArgs: string): Promise<string> {
14-
return new Promise<string>(async (resolve, reject) => {
15-
await maybeFixPath();
16-
17-
const { exec } = await import('child_process');
18-
exec(
19-
cliArgs,
20-
{
21-
cwd: dir,
22-
maxBuffer: 200 * 1024 * 100, // 100 times the default
23-
},
24-
(error, result) => {
25-
if (error) {
26-
reject(error);
27-
}
28-
29-
resolve(typeof result === 'string' ? result : `${result}`);
30-
},
31-
);
32-
});
33-
}
40+
await maybeFixPath();
3441

35-
/**
36-
* On macOS & Linux, we need to fix the $PATH environment variable
37-
* so that we can call `npm`.
38-
*
39-
* @returns {Promise<void>}
40-
*/
41-
export async function maybeFixPath(): Promise<void> {
42-
if (!_shellPathCalled && process.platform !== 'win32') {
43-
const { PATH } = await shellEnv();
44-
if (PATH) {
45-
process.env.PATH = PATH;
46-
}
47-
}
42+
const { stdout } = await promisify(cp_exec)(cliArgs, {
43+
cwd: dir,
44+
maxBuffer: 200 * 1024 * 100, // 100 times the default
45+
});
4846

49-
_shellPathCalled = true;
47+
return stdout.trim();
5048
}

src/utils/get-name.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export async function getName(appState: AppState): Promise<string> {
1010
if (appState.localPath) {
1111
const path = await import('path');
1212
return path.basename(appState.localPath);
13-
} else {
14-
const namor = await import('namor');
15-
return namor.generate({ words: 3, numbers: 0 });
1613
}
14+
15+
const namor = await import('namor');
16+
return namor.generate({ words: 3, numbers: 0 });
1717
}

src/utils/get-username.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import * as os from 'os';
22

3-
let username = '';
4-
53
/**
64
* Returns the current username
75
*
86
* @export
97
* @returns {string}
108
*/
11-
export function getUsername(): string {
12-
return (username = username || os.userInfo().username);
13-
}
9+
export const getUsername = (() => {
10+
let username = '';
11+
12+
return (): string => {
13+
if (!username) {
14+
username = os.userInfo().username;
15+
}
16+
17+
return username;
18+
};
19+
})();

src/utils/gist.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@
1313
* @returns {(string | null)}
1414
*/
1515
export function getGistId(rawInput: string): string | null {
16-
let input = rawInput.trim();
16+
const id = rawInput.trim().match(/[0-9A-Fa-f]{32}/);
1717

18-
let id = input;
19-
if (input.startsWith('https://gist.github.com')) {
20-
if (input.endsWith('/')) {
21-
input = input.slice(0, -1);
22-
}
23-
id = input.split('/').pop()!;
24-
}
25-
26-
return id.match(/[0-9A-Fa-f]{32}/) ? id : null;
18+
return id?.[0] || null;
2719
}
2820

2921
/**

src/utils/normalize-version.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
export function normalizeVersion(version = ''): string {
88
if (version.startsWith('v')) {
99
return version.slice(1);
10-
} else {
11-
return version;
1210
}
11+
12+
return version;
1313
}

src/utils/position-for-rect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function isResultOkay(
7575
input: PositionResult,
7676
size: { width: number; height: number },
7777
): boolean {
78-
return !!(
78+
return (
7979
!isTooFarRight(input, size) &&
8080
!isTooFarLeft(input) &&
8181
!isTooFarUp(input) &&

tests/utils/exec-spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ describe('exec', () => {
2525
it('executes a given string', async () => {
2626
const cpExec = require('child_process').exec;
2727
cpExec.mockImplementation((_a: any, _b: any, c: any) =>
28-
c(null, Buffer.from('hi')),
28+
c(null, {
29+
stdout: 'hi',
30+
stderr: '',
31+
}),
2932
);
3033

3134
const result = await execModule.exec('a/dir', 'echo hi');
@@ -38,7 +41,12 @@ describe('exec', () => {
3841

3942
it('handles a returned string', async () => {
4043
const cpExec = require('child_process').exec;
41-
cpExec.mockImplementation((_a: any, _b: any, c: any) => c(null, 'hi'));
44+
cpExec.mockImplementation((_a: any, _b: any, c: any) =>
45+
c(null, {
46+
stdout: 'hi',
47+
stderr: '',
48+
}),
49+
);
4250

4351
const result = await execModule.exec('a/dir', 'echo hi');
4452
expect(result).toBe('hi');

tests/utils/gist-spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,10 @@ describe('gist', () => {
8282
const actual = getGistId(input);
8383
expect(actual).toBe(expected);
8484
});
85+
86+
it('should return null when gist id incorrect', () => {
87+
const actual = getGistId('xx');
88+
expect(actual).toBe(null);
89+
});
8590
});
8691
});

0 commit comments

Comments
 (0)