@consensys/create-web3-app
Version:
CLI tool for generating Web3 starter projects, streamlining the setup of monorepo structures with a frontend (Next.js or React) and blockchain tooling (HardHat or Foundry). It leverages the commander library for command-line interactions and guides users
615 lines (614 loc) • 31.9 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import { exec } from "child_process";
import { promises as fs, constants as fsConstants } from "fs";
import { BLOCKCHAIN_TOOLING_CHOICES, PACAKGE_MANAGER_CHOICES, TEMPLATES, isDegitTemplate, isGitTemplate, CLI_VERSION, isGitAvailable, } from "../constants/index.js";
import path from "path";
import util from "util";
import inquirer from "inquirer";
import degit from "degit";
import ora from "ora";
import chalk from "chalk";
import { identifyRun, track, flush } from "../analytics/index.js";
export var execAsync = util.promisify(exec);
var promptForFramework = function () { return __awaiter(void 0, void 0, void 0, function () {
var templateChoices, frameworkName, selectedTemplate;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
templateChoices = TEMPLATES.map(function (template) {
if (template.id === "metamask-nextjs-wagmi") {
return {
name: "".concat(template.name, " ").concat(chalk.hex("#FFA500")("(Recommended)")),
value: template.name,
};
}
return template.name;
});
return [4 /*yield*/, inquirer.prompt([
{
type: "list",
name: "frameworkName",
message: "Please select the template you want to use:",
choices: templateChoices,
},
])];
case 1:
frameworkName = (_a.sent()).frameworkName;
console.log("Selected template: ".concat(frameworkName));
selectedTemplate = TEMPLATES.find(function (template) { return template.name === frameworkName; });
if (!selectedTemplate) {
throw new Error("Internal error: Could not find template data for selected name \"".concat(frameworkName, "\""));
}
return [2 /*return*/, selectedTemplate.id];
}
});
}); };
var promptForBlockchainTooling = function () { return __awaiter(void 0, void 0, void 0, function () {
var toolingChoice, tooling;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
toolingChoice = BLOCKCHAIN_TOOLING_CHOICES.map(function (choice) { return choice.name; });
return [4 /*yield*/, inquirer.prompt([
{
type: "list",
name: "tooling",
message: "Would you like to include blockchain tooling?",
choices: toolingChoice,
},
])];
case 1:
tooling = (_a.sent()).tooling;
console.log("Selected tooling: ".concat(tooling));
if (tooling === "Foundry") {
console.log(chalk.yellow("\nNote: Foundry's 'forge' CLI must be installed and in your PATH to use this option."));
}
return [2 /*return*/, tooling];
}
});
}); };
var promptForPackageManager = function () { return __awaiter(void 0, void 0, void 0, function () {
var packageManagerChoice, packageManager, pnpmAvailable, _a, installPnpmNow, installError_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
packageManagerChoice = PACAKGE_MANAGER_CHOICES.map(function (choice) { return choice.name; });
return [4 /*yield*/, inquirer.prompt([
{
type: "list",
name: "packageManager",
message: "Please select the package manager you want to use:",
choices: packageManagerChoice,
},
])];
case 1:
packageManager = (_b.sent()).packageManager;
console.log("Selected package manager: ".concat(packageManager));
if (!(packageManager === "pnpm")) return [3 /*break*/, 12];
pnpmAvailable = true;
_b.label = 2;
case 2:
_b.trys.push([2, 4, , 5]);
return [4 /*yield*/, execAsync("pnpm -v")];
case 3:
_b.sent();
return [3 /*break*/, 5];
case 4:
_a = _b.sent();
pnpmAvailable = false;
return [3 /*break*/, 5];
case 5:
if (!!pnpmAvailable) return [3 /*break*/, 12];
console.log(chalk.yellow("pnpm is not installed or not found in your PATH."));
return [4 /*yield*/, inquirer.prompt([
{
type: "confirm",
name: "installPnpmNow",
message: "Would you like to install pnpm globally using npm now?",
default: true,
},
])];
case 6:
installPnpmNow = (_b.sent()).installPnpmNow;
if (!installPnpmNow) return [3 /*break*/, 11];
_b.label = 7;
case 7:
_b.trys.push([7, 9, , 10]);
console.log(chalk.blue("Installing pnpm globally via npm..."));
return [4 /*yield*/, execAsync("npm install -g pnpm")];
case 8:
_b.sent();
console.log(chalk.green("pnpm installed successfully."));
return [3 /*break*/, 10];
case 9:
installError_1 = _b.sent();
throw new Error("Failed to install pnpm automatically. Please install it manually and re-run the command.");
case 10: return [3 /*break*/, 12];
case 11: throw new Error("pnpm installation declined. Please install pnpm manually or choose a different package manager.");
case 12: return [2 /*return*/, packageManager];
}
});
}); };
var promptForProjectDetails = function (args) { return __awaiter(void 0, void 0, void 0, function () {
var projectName;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!args) return [3 /*break*/, 2];
return [4 /*yield*/, inquirer.prompt([
{
type: "input",
name: "projectName",
message: "Please specify a name for your project: ",
validate: function (input) {
if (!input) {
return "Project name cannot be empty";
}
var kebabCaseRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
if (!kebabCaseRegex.test(input)) {
return "Project name must be in kebab-case (e.g., my-awesome-project)";
}
return true;
},
},
])];
case 1:
projectName = (_a.sent()).projectName;
console.log("Creating project with name:", projectName);
return [2 /*return*/, projectName];
case 2: return [2 /*return*/, args];
}
});
}); };
export var promptForOptions = function (args) { return __awaiter(void 0, void 0, void 0, function () {
var projectName, templateId, tooling, packageManager, dynamicEnvId, addDynamicIdNow, providedDynamicId, options;
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, promptForProjectDetails(args)];
case 1:
projectName = _c.sent();
return [4 /*yield*/, promptForFramework()];
case 2:
templateId = _c.sent();
return [4 /*yield*/, promptForBlockchainTooling()];
case 3:
tooling = _c.sent();
return [4 /*yield*/, promptForPackageManager()];
case 4:
packageManager = _c.sent();
dynamicEnvId = undefined;
if (!(templateId === "metamask-dynamic")) return [3 /*break*/, 9];
return [4 /*yield*/, inquirer.prompt([
{
type: "confirm",
name: "addDynamicIdNow",
message: "The selected template uses Dynamic.xyz. You'll need a Dynamic Environment ID added to a .env file. Would you like to add it now? You can get one from https://app.dynamic.xyz/dashboard/developer/api",
default: true,
},
])];
case 5:
addDynamicIdNow = (_c.sent()).addDynamicIdNow;
if (!addDynamicIdNow) return [3 /*break*/, 7];
return [4 /*yield*/, inquirer.prompt([
{
type: "password",
name: "providedDynamicId",
message: "Please paste your Dynamic Environment ID:",
mask: "*",
validate: function (input) {
return input ? true : "Dynamic Environment ID cannot be empty";
},
},
])];
case 6:
providedDynamicId = (_c.sent()).providedDynamicId;
dynamicEnvId = providedDynamicId;
console.log("Dynamic Environment ID received.");
return [3 /*break*/, 8];
case 7:
console.log(chalk.yellow("Okay, please remember to add NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID=<your_id> to the .env file in your site's directory later."));
_c.label = 8;
case 8: return [3 /*break*/, 10];
case 9:
if (templateId === "metamask-web3auth") {
console.log(chalk.yellow("\nNote: The selected template requires a Web3Auth client ID. You can obtain one from https://dashboard.web3auth.io/ and later add NEXT_PUBLIC_WEB3AUTH_CLIENT_ID=<your_id> to a .env file in your site's directory."));
}
_c.label = 10;
case 10:
options = {
projectName: projectName,
templateId: templateId,
blockchain_tooling: (_a = BLOCKCHAIN_TOOLING_CHOICES.find(function (choice) { return choice.name === tooling; })) === null || _a === void 0 ? void 0 : _a.value,
packageManager: (_b = PACAKGE_MANAGER_CHOICES.find(function (choice) { return choice.name === packageManager; })) === null || _b === void 0 ? void 0 : _b.value,
dynamicEnvId: dynamicEnvId,
};
if (!TEMPLATES.some(function (t) { return t.id === options.templateId; })) {
throw new Error("Invalid template ID resolved: ".concat(options.templateId));
}
return [2 /*return*/, options];
}
});
}); };
export var cloneTemplate = function (options, destinationPath) { return __awaiter(void 0, void 0, void 0, function () {
var templateId, projectName, dynamicEnvId, blockchain_tooling, template, spinner, emitter, packageJsonPath, packageJsonContent, packageJson, newPackageJsonContent, pkgError_1, envContent, envPath, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
templateId = options.templateId, projectName = options.projectName, dynamicEnvId = options.dynamicEnvId, blockchain_tooling = options.blockchain_tooling;
template = TEMPLATES.find(function (t) { return t.id === templateId; });
if (!template) {
throw new Error("Template with id \"".concat(templateId, "\" not found."));
}
spinner = ora("Preparing template \"".concat(template.name, "\" into ").concat(destinationPath, "...")).start();
return [4 /*yield*/, isGitAvailable()];
case 1:
if (!(_a.sent())) {
spinner.fail("Git is not installed or not found in your PATH.");
track("git_not_installed", {
destination_path: destinationPath,
template_id: templateId,
});
throw new Error("Git is required to clone templates. Please install Git (https://git-scm.com/downloads) and try again.");
}
_a.label = 2;
case 2:
_a.trys.push([2, 16, , 17]);
if (!isDegitTemplate(template)) return [3 /*break*/, 4];
spinner.text = "Cloning template \"".concat(template.name, "\" from ").concat(template.degitSource, " using degit...");
emitter = degit(template.degitSource, {
cache: false,
force: true,
verbose: false,
});
return [4 /*yield*/, emitter.clone(destinationPath)];
case 3:
_a.sent();
return [3 /*break*/, 8];
case 4:
if (!isGitTemplate(template)) return [3 /*break*/, 7];
spinner.text = "Cloning template \"".concat(template.name, "\" from ").concat(template.repo_url, " using git...");
return [4 /*yield*/, execAsync("git clone ".concat(template.repo_url, " ").concat(destinationPath))];
case 5:
_a.sent();
return [4 /*yield*/, fs.rm(path.join(destinationPath, ".git"), {
recursive: true,
force: true,
})];
case 6:
_a.sent();
return [3 /*break*/, 8];
case 7:
spinner.fail("Template preparation failed.");
throw new Error("Template has neither repo_url nor degitSource defined.");
case 8:
packageJsonPath = path.join(destinationPath, "package.json");
_a.label = 9;
case 9:
_a.trys.push([9, 12, , 13]);
spinner.text = "Updating package name to ".concat(path.basename(projectName), "...");
return [4 /*yield*/, fs.readFile(packageJsonPath, "utf-8")];
case 10:
packageJsonContent = _a.sent();
packageJson = JSON.parse(packageJsonContent);
packageJson.name = path.basename(projectName);
if (blockchain_tooling !== "none") {
packageJson.name = "site";
}
newPackageJsonContent = JSON.stringify(packageJson, null, 2);
return [4 /*yield*/, fs.writeFile(packageJsonPath, newPackageJsonContent, "utf-8")];
case 11:
_a.sent();
return [3 /*break*/, 13];
case 12:
pkgError_1 = _a.sent();
console.warn("Warning: Could not update package.json name in ".concat(destinationPath, ". Manual update might be needed. Error: ").concat(pkgError_1 instanceof Error ? pkgError_1.message : pkgError_1));
return [3 /*break*/, 13];
case 13:
if (!dynamicEnvId) return [3 /*break*/, 15];
spinner.text = "Creating .env file with Dynamic Environment ID...";
envContent = "NEXT_PUBLIC_DYNAMIC_ENVIRONMENT_ID=".concat(dynamicEnvId, "\n");
envPath = path.join(destinationPath, ".env");
return [4 /*yield*/, fs.writeFile(envPath, envContent, "utf-8")];
case 14:
_a.sent();
spinner.text = ".env file created successfully.";
_a.label = 15;
case 15:
spinner.succeed("Template \"".concat(template.name, "\" prepared successfully in ").concat(destinationPath, "."));
return [3 /*break*/, 17];
case 16:
error_1 = _a.sent();
spinner.fail("Error preparing template \"".concat(template.name, "\"."));
console.error("Error details:", error_1);
throw error_1;
case 17: return [2 /*return*/];
}
});
}); };
export var initializeMonorepo = function (options) { return __awaiter(void 0, void 0, void 0, function () {
var projectName, packageManager, rootPackageJson;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
projectName = options.projectName, packageManager = options.packageManager;
console.log("Initializing monorepo structure...");
return [4 /*yield*/, fs.mkdir(path.join(projectName, "packages"), { recursive: true })];
case 1:
_a.sent();
if (!(packageManager === "pnpm")) return [3 /*break*/, 3];
return [4 /*yield*/, fs.writeFile(path.join(projectName, "pnpm-workspace.yaml"), "packages:\n - 'packages/*'")];
case 2:
_a.sent();
_a.label = 3;
case 3: return [4 /*yield*/, fs.writeFile(path.join(projectName, ".gitignore"), "node_modules\n.DS_Store\npackages/*/node_modules\npackages/*/.DS_Store\npackages/*/dist\npackages/*/.env\npackages/*/.turbo\npackages/*/coverage")];
case 4:
_a.sent();
rootPackageJson = {
name: projectName,
private: true,
workspaces: ["packages/*"],
scripts: {},
};
return [4 /*yield*/, fs.writeFile(path.join(projectName, "package.json"), JSON.stringify(rootPackageJson, null, 2))];
case 5:
_a.sent();
return [4 /*yield*/, fs.mkdir(path.join(projectName, "packages", "site"), {
recursive: true,
})];
case 6:
_a.sent();
console.log("Monorepo structure initialized.");
return [2 /*return*/];
}
});
}); };
export var createHardhatProject = function (options) { return __awaiter(void 0, void 0, void 0, function () {
var projectName, templateId;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
projectName = options.projectName, templateId = options.templateId;
console.log("Setting up project with HardHat...");
return [4 /*yield*/, initializeMonorepo(options)];
case 1:
_a.sent();
console.log("Cloning Hardhat template...");
return [4 /*yield*/, execAsync("git clone https://github.com/Consensys/hardhat-template.git ".concat(path.join(projectName, "packages", "blockchain")))];
case 2:
_a.sent();
return [4 /*yield*/, fs.rm(path.join(projectName, "packages", "blockchain", ".git"), {
recursive: true,
force: true,
})];
case 3:
_a.sent();
return [4 /*yield*/, cloneTemplate({
templateId: templateId,
projectName: projectName,
dynamicEnvId: options.dynamicEnvId,
blockchain_tooling: "hardhat",
}, path.join(projectName, "packages", "site"))];
case 4:
_a.sent();
console.log("Hardhat project setup complete.");
return [2 /*return*/];
}
});
}); };
export var createFoundryProject = function (options, spinner) { return __awaiter(void 0, void 0, void 0, function () {
var projectName, templateId, forgeAvailable, error_2, switchToHardhat, blockchainPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
projectName = options.projectName, templateId = options.templateId;
forgeAvailable = true;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, execAsync("forge --version")];
case 2:
_a.sent();
console.log(chalk.green("Foundry (forge) installation verified. Proceeding with setup..."));
return [3 /*break*/, 4];
case 3:
error_2 = _a.sent();
forgeAvailable = false;
return [3 /*break*/, 4];
case 4:
if (!!forgeAvailable) return [3 /*break*/, 8];
spinner === null || spinner === void 0 ? void 0 : spinner.stop();
console.log(chalk.yellow("Looks like Foundry is not installed or not found in your PATH."));
return [4 /*yield*/, inquirer.prompt([
{
type: "confirm",
name: "switchToHardhat",
message: "Would you like to switch to Hardhat instead?",
default: true,
},
])];
case 5:
switchToHardhat = (_a.sent()).switchToHardhat;
track("foundry_not_installed", {
attempted_blockchain_tooling: "foundry",
switched_to_hardhat: switchToHardhat,
});
if (!switchToHardhat) return [3 /*break*/, 7];
console.log(chalk.blue("Switching to Hardhat setup..."));
Object.assign(options, { blockchain_tooling: "hardhat" });
if (spinner) {
spinner.text = "Creating Hardhat project structure...";
spinner.start();
}
return [4 /*yield*/, createHardhatProject(options)];
case 6:
_a.sent();
return [2 /*return*/];
case 7:
spinner === null || spinner === void 0 ? void 0 : spinner.start();
throw new Error("Forge (Foundry) is not installed or not found in your PATH. Please install it to continue.\nInstallation guide: https://book.getfoundry.sh/getting-started/installation");
case 8:
console.log("Setting up project with Foundry...");
return [4 /*yield*/, initializeMonorepo(options)];
case 9:
_a.sent();
console.log("Initializing Foundry project with 'forge init'...");
blockchainPath = path.join(projectName, "packages", "blockchain");
return [4 /*yield*/, fs.mkdir(blockchainPath, { recursive: true })];
case 10:
_a.sent();
return [4 /*yield*/, execAsync("cd ".concat(blockchainPath, " && foundryup && forge init . --no-git"))];
case 11:
_a.sent();
return [4 /*yield*/, cloneTemplate({
templateId: templateId,
projectName: projectName,
dynamicEnvId: options.dynamicEnvId,
blockchain_tooling: "foundry",
}, path.join(projectName, "packages", "site"))];
case 12:
_a.sent();
console.log("Foundry project setup complete.");
return [2 /*return*/];
}
});
}); };
export var createProject = function (args) { return __awaiter(void 0, void 0, void 0, function () {
var _a, options, installCommand, mainSpinner, t0, projectPath, error_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, fs.access(process.cwd(), fsConstants.W_OK)];
case 1:
_b.sent();
return [3 /*break*/, 3];
case 2:
_a = _b.sent();
console.error(chalk.red("The directory you're in is read-only for your user. Please cd to a writable folder (e.g. your home directory) or run the terminal as Administrator."));
track("cwd_not_writable", {
cwd: process.cwd(),
});
return [2 /*return*/];
case 3:
identifyRun();
return [4 /*yield*/, promptForOptions(args)];
case 4:
options = _b.sent();
installCommand = "".concat(options.packageManager, " install");
mainSpinner = ora("Setting up your Web3 project...").start();
t0 = Date.now();
_b.label = 5;
case 5:
_b.trys.push([5, 13, 14, 15]);
track("cli_started", {
cli_version: CLI_VERSION,
});
if (!(options.blockchain_tooling === "hardhat")) return [3 /*break*/, 7];
mainSpinner.text = "Creating Hardhat project structure...";
return [4 /*yield*/, createHardhatProject(options)];
case 6:
_b.sent();
return [3 /*break*/, 11];
case 7:
if (!(options.blockchain_tooling === "foundry")) return [3 /*break*/, 9];
mainSpinner.text = "Creating Foundry project structure...";
return [4 /*yield*/, createFoundryProject(options, mainSpinner)];
case 8:
_b.sent();
return [3 /*break*/, 11];
case 9:
mainSpinner.text = "Cloning base template...";
return [4 /*yield*/, cloneTemplate({
templateId: options.templateId,
projectName: options.projectName,
dynamicEnvId: options.dynamicEnvId,
blockchain_tooling: "none",
}, options.projectName)];
case 10:
_b.sent();
_b.label = 11;
case 11:
mainSpinner.text = "Installing dependencies using ".concat(options.packageManager, "... (This may take a few minutes)");
projectPath = options.projectName;
return [4 /*yield*/, execAsync("cd ".concat(projectPath, " && ").concat(installCommand))];
case 12:
_b.sent();
mainSpinner.succeed("Project setup complete!");
console.log("\nSuccess! Created ".concat(options.projectName, "."));
console.log("Inside that directory, you can run several commands:");
if (options.blockchain_tooling !== "none") {
console.log("\n In the root directory (".concat(options.projectName, "):"));
console.log(" ".concat(options.packageManager, " run dev"));
console.log(" Runs the frontend development server.");
console.log("\n In packages/blockchain:");
console.log(" ".concat(options.packageManager, " run compile"));
console.log(" Compiles the smart contracts.");
console.log(" ".concat(options.packageManager, " run test"));
console.log(" Runs the contract tests.");
}
else {
console.log("\n cd packages/site && ".concat(options.packageManager, " run dev"));
console.log(" Starts the development server.");
}
track("project_created", {
template_id: options.templateId,
blockchain_tooling: options.blockchain_tooling,
package_manager: options.packageManager,
dynamic_env: Boolean(options.dynamicEnvId),
exec_time_ms: Date.now() - t0,
});
console.log("\nHappy Hacking!");
return [3 /*break*/, 15];
case 13:
error_3 = _b.sent();
mainSpinner.fail("An error occurred during project creation.");
track("project_creation_failed", {
template_id: options === null || options === void 0 ? void 0 : options.templateId,
blockchain_tooling: options === null || options === void 0 ? void 0 : options.blockchain_tooling,
error_message: error_3.message,
});
console.error("Error details:", error_3);
return [3 /*break*/, 15];
case 14:
flush();
return [7 /*endfinally*/];
case 15: return [2 /*return*/];
}
});
}); };