Skip to content

Commit 025c7ca

Browse files
renovate[bot]favnakyranet
authored
fix(deps): update dependency @sapphire/result to v2 (#135)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jeroen Claassens <support@favware.tech> Co-authored-by: A. Román <kyradiscord@gmail.com>
1 parent b7ba03c commit 025c7ca

File tree

6 files changed

+163
-451
lines changed

6 files changed

+163
-451
lines changed

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"dependencies": {
4141
"@favware/colorette-spinner": "^1.0.0",
42-
"@sapphire/result": "^1.1.1",
42+
"@sapphire/result": "^2.0.1",
4343
"colorette": "^2.0.19",
4444
"commander": "^9.3.0",
4545
"execa": "^6.1.0",
@@ -57,10 +57,10 @@
5757
"@sapphire/prettier-config": "^1.4.3",
5858
"@sapphire/ts-config": "^3.3.4",
5959
"@types/js-yaml": "^4.0.5",
60-
"@types/node": "^17.0.33",
61-
"@types/prompts": "^2.0.14",
62-
"@typescript-eslint/eslint-plugin": "^5.30.3",
63-
"@typescript-eslint/parser": "^5.30.3",
60+
"@types/node": "^18.0.1",
61+
"@types/prompts": "^2.4.0",
62+
"@typescript-eslint/eslint-plugin": "^5.30.4",
63+
"@typescript-eslint/parser": "^5.30.4",
6464
"cz-conventional-changelog": "^3.3.0",
6565
"eslint": "^8.19.0",
6666
"eslint-config-prettier": "^8.5.0",

src/commands/generate.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { componentsFolder } from '#constants';
22
import { CreateFileFromTemplate } from '#functions/CreateFileFromTemplate';
33
import { fileExists } from '#functions/FileExists';
44
import { Spinner } from '@favware/colorette-spinner';
5-
import { fromAsync, isErr } from '@sapphire/result';
5+
import { Result } from '@sapphire/result';
66
import { blueBright, red } from 'colorette';
77
import findUp from 'find-up';
88
import { load } from 'js-yaml';
@@ -48,7 +48,7 @@ export default async (component: string, name: string) => {
4848

4949
const fail = (error: string, additionalExecution?: () => void) => {
5050
spinner.error({ text: error });
51-
if (additionalExecution) additionalExecution();
51+
additionalExecution?.();
5252
process.exit(1);
5353
};
5454

@@ -64,14 +64,17 @@ export default async (component: string, name: string) => {
6464
return fail("Can't parse the Sapphire CLI config.");
6565
}
6666

67-
const result = await fromAsync(async () => createComponent(component, name, config, configLoc.replace(/.sapphirerc.(json|yml)/g, '')));
67+
const result = await Result.fromAsync<boolean, Error>(() =>
68+
createComponent(component, name, config, configLoc.replace(/.sapphirerc.(json|yml)/g, ''))
69+
);
6870

69-
if (isErr(result)) {
70-
return fail((result.error as Error).message, () => console.log(red((result.error as Error).message)));
71-
}
72-
73-
spinner.success();
71+
return result.match({
72+
ok: () => {
73+
spinner.success();
7474

75-
console.log(blueBright('Done!'));
76-
process.exit(0);
75+
console.log(blueBright('Done!'));
76+
process.exit(0);
77+
},
78+
err: (error) => fail(error.message, () => console.log(red(error.message)))
79+
});
7780
};

src/commands/new.ts

+27-49
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CreateFileFromTemplate } from '#functions/CreateFileFromTemplate';
44
import { fileExists } from '#functions/FileExists';
55
import { PromptNew, PromptNewObjectKeys } from '#prompts/PromptNew';
66
import { Spinner } from '@favware/colorette-spinner';
7-
import { fromAsync, isErr, isOk } from '@sapphire/result';
7+
import { Result } from '@sapphire/result';
88
import { blueBright, red } from 'colorette';
99
import { execa } from 'execa';
1010
import { cp, readFile, rm, writeFile } from 'node:fs/promises';
@@ -18,24 +18,18 @@ async function editPackageJson(location: string, name: string) {
1818

1919
output.name = name;
2020

21-
const result = await fromAsync(() => writeFile(pjLocation, JSON.stringify(output, null, 2)));
21+
const result = await Result.fromAsync(() => writeFile(pjLocation, JSON.stringify(output, null, 2)));
2222

23-
return isOk(result);
23+
return result.isOk();
2424
}
2525

2626
async function installDeps(location: string, pm: string, verbose: boolean) {
27-
const result = await fromAsync(() =>
28-
execa(pm.toLowerCase(), ['install'], {
29-
stdio: verbose ? 'inherit' : undefined,
30-
cwd: `./${location}/`
31-
})
32-
);
33-
34-
if (isErr(result)) {
35-
throw result.error;
36-
}
27+
const value = await execa(pm.toLowerCase(), ['install'], {
28+
stdio: verbose ? 'inherit' : undefined,
29+
cwd: `./${location}/`
30+
});
3731

38-
if (result.value.exitCode !== 0) {
32+
if (value.exitCode !== 0) {
3933
throw new Error('An unknown error occurred while installing the dependencies. Try running Sapphire CLI with "--verbose" flag.');
4034
}
4135

@@ -50,23 +44,16 @@ async function installDeps(location: string, pm: string, verbose: boolean) {
5044

5145
async function configureYarnRc(location: string, name: string, value: string) {
5246
await execa('yarn', ['config', 'set', name, value], { cwd: `./${location}/` });
53-
5447
return true;
5548
}
5649

5750
async function installYarnV3(location: string, verbose: boolean) {
58-
const result = await fromAsync(() =>
59-
execa('yarn', ['set', 'version', 'berry'], {
60-
stdio: verbose ? 'inherit' : undefined,
61-
cwd: `./${location}/`
62-
})
63-
);
64-
65-
if (isErr(result)) {
66-
throw result.error;
67-
}
51+
const value = await execa('yarn', ['set', 'version', 'berry'], {
52+
stdio: verbose ? 'inherit' : undefined,
53+
cwd: `./${location}/`
54+
});
6855

69-
if (result.value.exitCode !== 0) {
56+
if (value.exitCode !== 0) {
7057
throw new Error('An unknown error occurred while installing Yarn v3. Try running Sapphire CLI with "--verbose" flag.');
7158
}
7259

@@ -81,43 +68,34 @@ async function installYarnV3(location: string, verbose: boolean) {
8168

8269
async function installYarnTypescriptPlugin(location: string) {
8370
await execa('yarn', ['plugin', 'import', 'typescript'], { cwd: `./${location}/` });
84-
8571
return true;
8672
}
8773

8874
async function initializeGitRepo(location: string) {
8975
await execa('git', ['init'], { cwd: `./${location}/` });
90-
9176
return true;
9277
}
9378

9479
async function runJob(job: () => Promise<any>, name: string) {
9580
const spinner = new Spinner(name).start();
9681

97-
const result = await fromAsync(async () => job());
98-
99-
if (isErr(result)) {
100-
spinner.error({ text: red((result.error as Error).message) });
101-
console.error(red((result.error as Error).message));
102-
throw result.error;
103-
}
104-
105-
spinner.success();
106-
return true;
82+
const result = await Result.fromAsync<any, Error>(() => job());
83+
return result.match({
84+
ok: () => {
85+
spinner.success();
86+
return true;
87+
},
88+
err: (error) => {
89+
spinner.error({ text: red(error.message) });
90+
console.error(red(error.message));
91+
throw error;
92+
}
93+
});
10794
}
10895

10996
async function cloneRepo(location: string, verbose: boolean) {
110-
const result = await fromAsync(async () =>
111-
execa('git', ['clone', repoUrl, `${location}/ghr`], {
112-
stdio: verbose ? 'inherit' : undefined
113-
})
114-
);
115-
116-
if (isErr(result)) {
117-
throw result.error;
118-
}
119-
120-
if (result.value.exitCode !== 0) {
97+
const value = await execa('git', ['clone', repoUrl, `${location}/ghr`], { stdio: verbose ? 'inherit' : undefined });
98+
if (value.exitCode !== 0) {
12199
throw new Error('An unknown error occurred while cloning the repository. Try running Sapphire CLI with "--verbose" flag.');
122100
}
123101

src/functions/CommandExists.ts

+14-18
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
*/
2424

2525
import { fileExists } from '#functions/FileExists';
26-
import { fromAsync, isErr } from '@sapphire/result';
27-
import { execa } from 'execa';
26+
import { Result } from '@sapphire/result';
27+
import { execa, ExecaReturnValue } from 'execa';
2828
import { constants } from 'node:fs';
2929
import { access } from 'node:fs/promises';
3030

3131
const windows = process.platform === 'win32';
3232

3333
async function isExecutable(command: string): Promise<boolean> {
34-
const result = await fromAsync(() => access(command, constants.X_OK));
34+
const result = await Result.fromAsync(() => access(command, constants.X_OK));
3535

36-
return isErr(result);
36+
return result.isErr();
3737
}
3838

3939
function cleanWindowsCommand(input: string) {
@@ -52,13 +52,11 @@ async function commandExistsUnix(command: string): Promise<boolean> {
5252
}
5353
}
5454

55-
const result = await fromAsync(() => execa('which', [command]));
56-
57-
if (isErr(result)) {
58-
return false;
59-
}
60-
61-
return Boolean(result.value.stdout);
55+
const result = await Result.fromAsync(() => execa('which', [command]));
56+
return result.match({
57+
err: () => false,
58+
ok: (value: ExecaReturnValue<string>) => Boolean(value.stdout)
59+
});
6260
}
6361

6462
const invalidWindowsCommandNameRegex = /[\x00-\x1f<>:"|?*]/;
@@ -68,13 +66,11 @@ async function commandExistsWindows(command: string): Promise<boolean> {
6866
return false;
6967
}
7068

71-
const result = await fromAsync(async () => execa('where', [cleanWindowsCommand(command)]));
72-
73-
if (isErr(result)) {
74-
return fileExists(command);
75-
}
76-
77-
return true;
69+
const result = await Result.fromAsync(async () => execa('where', [cleanWindowsCommand(command)]));
70+
return result.match({
71+
err: () => fileExists(command),
72+
ok: () => true
73+
});
7874
}
7975

8076
export async function CommandExists(command: string): Promise<boolean> {

src/functions/FileExists.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { fromAsync, isOk } from '@sapphire/result';
1+
import { Result } from '@sapphire/result';
22
import { access } from 'node:fs/promises';
33

44
export async function fileExists(filePath: string): Promise<boolean> {
5-
const result = await fromAsync(() => access(filePath));
5+
const result = await Result.fromAsync(() => access(filePath));
66

7-
return isOk(result);
7+
return result.isOk();
88
}

0 commit comments

Comments
 (0)