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

fix(deps): update dependency @sapphire/result to v2 #135

Merged
merged 3 commits into from
Jul 3, 2022
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"@favware/colorette-spinner": "^1.0.0",
"@sapphire/result": "^1.1.1",
"@sapphire/result": "^2.0.1",
"colorette": "^2.0.19",
"commander": "^9.3.0",
"execa": "^6.1.0",
Expand All @@ -57,10 +57,10 @@
"@sapphire/prettier-config": "^1.4.3",
"@sapphire/ts-config": "^3.3.4",
"@types/js-yaml": "^4.0.5",
"@types/node": "^17.0.33",
"@types/prompts": "^2.0.14",
"@typescript-eslint/eslint-plugin": "^5.30.3",
"@typescript-eslint/parser": "^5.30.3",
"@types/node": "^18.0.1",
"@types/prompts": "^2.4.0",
"@typescript-eslint/eslint-plugin": "^5.30.4",
"@typescript-eslint/parser": "^5.30.4",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.19.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
23 changes: 13 additions & 10 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { componentsFolder } from '#constants';
import { CreateFileFromTemplate } from '#functions/CreateFileFromTemplate';
import { fileExists } from '#functions/FileExists';
import { Spinner } from '@favware/colorette-spinner';
import { fromAsync, isErr } from '@sapphire/result';
import { Result } from '@sapphire/result';
import { blueBright, red } from 'colorette';
import findUp from 'find-up';
import { load } from 'js-yaml';
Expand Down Expand Up @@ -48,7 +48,7 @@ export default async (component: string, name: string) => {

const fail = (error: string, additionalExecution?: () => void) => {
spinner.error({ text: error });
if (additionalExecution) additionalExecution();
additionalExecution?.();
process.exit(1);
};

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

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

if (isErr(result)) {
return fail((result.error as Error).message, () => console.log(red((result.error as Error).message)));
}

spinner.success();
return result.match({
ok: () => {
spinner.success();

console.log(blueBright('Done!'));
process.exit(0);
console.log(blueBright('Done!'));
process.exit(0);
},
err: (error) => fail(error.message, () => console.log(red(error.message)))
});
};
76 changes: 27 additions & 49 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CreateFileFromTemplate } from '#functions/CreateFileFromTemplate';
import { fileExists } from '#functions/FileExists';
import { PromptNew, PromptNewObjectKeys } from '#prompts/PromptNew';
import { Spinner } from '@favware/colorette-spinner';
import { fromAsync, isErr, isOk } from '@sapphire/result';
import { Result } from '@sapphire/result';
import { blueBright, red } from 'colorette';
import { execa } from 'execa';
import { cp, readFile, rm, writeFile } from 'node:fs/promises';
Expand All @@ -18,24 +18,18 @@ async function editPackageJson(location: string, name: string) {

output.name = name;

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

return isOk(result);
return result.isOk();
}

async function installDeps(location: string, pm: string, verbose: boolean) {
const result = await fromAsync(() =>
execa(pm.toLowerCase(), ['install'], {
stdio: verbose ? 'inherit' : undefined,
cwd: `./${location}/`
})
);

if (isErr(result)) {
throw result.error;
}
const value = await execa(pm.toLowerCase(), ['install'], {
stdio: verbose ? 'inherit' : undefined,
cwd: `./${location}/`
});

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

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

async function configureYarnRc(location: string, name: string, value: string) {
await execa('yarn', ['config', 'set', name, value], { cwd: `./${location}/` });

return true;
}

async function installYarnV3(location: string, verbose: boolean) {
const result = await fromAsync(() =>
execa('yarn', ['set', 'version', 'berry'], {
stdio: verbose ? 'inherit' : undefined,
cwd: `./${location}/`
})
);

if (isErr(result)) {
throw result.error;
}
const value = await execa('yarn', ['set', 'version', 'berry'], {
stdio: verbose ? 'inherit' : undefined,
cwd: `./${location}/`
});

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

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

async function installYarnTypescriptPlugin(location: string) {
await execa('yarn', ['plugin', 'import', 'typescript'], { cwd: `./${location}/` });

return true;
}

async function initializeGitRepo(location: string) {
await execa('git', ['init'], { cwd: `./${location}/` });

return true;
}

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

const result = await fromAsync(async () => job());

if (isErr(result)) {
spinner.error({ text: red((result.error as Error).message) });
console.error(red((result.error as Error).message));
throw result.error;
}

spinner.success();
return true;
const result = await Result.fromAsync<any, Error>(() => job());
return result.match({
ok: () => {
spinner.success();
return true;
},
err: (error) => {
spinner.error({ text: red(error.message) });
console.error(red(error.message));
throw error;
}
});
}

async function cloneRepo(location: string, verbose: boolean) {
const result = await fromAsync(async () =>
execa('git', ['clone', repoUrl, `${location}/ghr`], {
stdio: verbose ? 'inherit' : undefined
})
);

if (isErr(result)) {
throw result.error;
}

if (result.value.exitCode !== 0) {
const value = await execa('git', ['clone', repoUrl, `${location}/ghr`], { stdio: verbose ? 'inherit' : undefined });
if (value.exitCode !== 0) {
throw new Error('An unknown error occurred while cloning the repository. Try running Sapphire CLI with "--verbose" flag.');
}

Expand Down
32 changes: 14 additions & 18 deletions src/functions/CommandExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
*/

import { fileExists } from '#functions/FileExists';
import { fromAsync, isErr } from '@sapphire/result';
import { execa } from 'execa';
import { Result } from '@sapphire/result';
import { execa, ExecaReturnValue } from 'execa';
import { constants } from 'node:fs';
import { access } from 'node:fs/promises';

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

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

return isErr(result);
return result.isErr();
}

function cleanWindowsCommand(input: string) {
Expand All @@ -52,13 +52,11 @@ async function commandExistsUnix(command: string): Promise<boolean> {
}
}

const result = await fromAsync(() => execa('which', [command]));

if (isErr(result)) {
return false;
}

return Boolean(result.value.stdout);
const result = await Result.fromAsync(() => execa('which', [command]));
return result.match({
err: () => false,
ok: (value: ExecaReturnValue<string>) => Boolean(value.stdout)
});
}

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

const result = await fromAsync(async () => execa('where', [cleanWindowsCommand(command)]));

if (isErr(result)) {
return fileExists(command);
}

return true;
const result = await Result.fromAsync(async () => execa('where', [cleanWindowsCommand(command)]));
return result.match({
err: () => fileExists(command),
ok: () => true
});
}

export async function CommandExists(command: string): Promise<boolean> {
Expand Down
6 changes: 3 additions & 3 deletions src/functions/FileExists.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fromAsync, isOk } from '@sapphire/result';
import { Result } from '@sapphire/result';
import { access } from 'node:fs/promises';

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

return isOk(result);
return result.isOk();
}
Loading