Skip to content

Commit

Permalink
feat: optionally auto-install fuelup during create fuels routine (#…
Browse files Browse the repository at this point in the history
…2149)

Co-authored-by: Chad Nehemiah <[email protected]>
Co-authored-by: Peter Smith <[email protected]>
  • Loading branch information
3 people committed May 6, 2024
1 parent 586e1e2 commit 7c3ef04
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-points-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-fuels": minor
---

feat: optionally auto-install `fuelup` during `create fuels` routine
46 changes: 46 additions & 0 deletions packages/create-fuels/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import prompts from 'prompts';

import packageJson from '../package.json';

import { checkIfFuelUpInstalled, installFuelUp } from './lib';

const log = (...data: unknown[]) => {
process.stdout.write(`${data.join(' ')}\n`);
};
Expand Down Expand Up @@ -113,6 +115,46 @@ function writeEnvFile(envFilePath: string, programsToInclude: ProgramsToInclude)
writeFileSync(envFilePath, newFileContents);
}

async function promptForFuelUpInstall() {
const shouldInstallFuelUp = await prompts(
{
type: 'confirm',
name: 'shouldInstallFuelUp',
message:
"It seems you don't have `fuelup` installed. `fuelup` is required to manage the Fuel toolchain and is a prerequisite for using this template app. Do you want to install it now?",
initial: true,
},
{ onCancel: () => process.exit(0) }
);
return shouldInstallFuelUp.shouldInstallFuelUp as boolean;
}

async function tryInstallFuelup(isVerbose: boolean = false) {
const fuelUpSpinner = ora({
text: 'Checking if fuelup is installed..',
color: 'green',
}).start();

if (checkIfFuelUpInstalled()) {
fuelUpSpinner.succeed('fuelup is already installed.');
return;
}

fuelUpSpinner.fail('fuelup not found.');

const shouldInstall = await promptForFuelUpInstall();

if (shouldInstall) {
installFuelUp(isVerbose);
} else {
log(
chalk.yellow(
'Warning: You will need to install fuelup manually. See https://docs.fuel.network/guides/installation/#running-fuelup-init'
)
);
}
}

export const setupProgram = () => {
const program = new Command(packageJson.name)
.version(packageJson.version)
Expand Down Expand Up @@ -144,6 +186,10 @@ export const runScaffoldCli = async ({
let projectPath = program.args[0] ?? (await promptForProjectPath());
const verboseEnabled = program.opts().verbose ?? false;

if (!process.env.VITEST) {
await tryInstallFuelup(verboseEnabled);
}

while (existsSync(projectPath)) {
log(
chalk.red(
Expand Down
32 changes: 32 additions & 0 deletions packages/create-fuels/src/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { execSync } from 'child_process';
import { log } from 'console';
import ora from 'ora';

export const checkIfFuelUpInstalled = () => {
try {
execSync('fuelup --version', { stdio: 'pipe' });
return true;
} catch (error) {
return false;
}
};

export const installFuelUp = (isVerbose: boolean = false) => {
const installFuelUpSpinner = ora({
text: 'Installing fuelup..',
color: 'green',
});
try {
execSync(`curl https://install.fuel.network | sh`, { stdio: 'inherit' });
installFuelUpSpinner.succeed('Successfully installed fuelup!');
} catch (error) {
if (isVerbose) {
log(error);
}
log(
installFuelUpSpinner.fail(
'An error occurred while installing `fuelup`. Please try again, or try installing it manually. See https://docs.fuel.network/guides/installation/#running-fuelup-init for more information.'
)
);
}
};

0 comments on commit 7c3ef04

Please sign in to comment.