@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
467 lines (466 loc) • 24.1 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 } from "fs";
import { BLOCKCHAIN_TOOLING_CHOICES, PACAKGE_MANAGER_CHOICES, TEMPLATES, isDegitTemplate, isGitTemplate, } 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";
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 === "next-sdk-quickstart") {
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 promptForTooling = 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));
return [2 /*return*/, tooling];
}
});
}); };
var promptForPackageManager = function () { return __awaiter(void 0, void 0, void 0, function () {
var packageManagerChoice, packageManager;
return __generator(this, function (_a) {
switch (_a.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 = (_a.sent()).packageManager;
console.log("Selected package manager: ".concat(packageManager));
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) { return (input ? true : "Project name cannot be empty"); },
},
])];
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*/, promptForTooling()];
case 3:
tooling = _c.sent();
return [4 /*yield*/, promptForPackageManager()];
case 4:
packageManager = _c.sent();
dynamicEnvId = undefined;
if (!(templateId === "metamask-dynamic")) return [3 /*break*/, 8];
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?",
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:
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();
_a.label = 1;
case 1:
_a.trys.push([1, 15, , 16]);
if (!isDegitTemplate(template)) return [3 /*break*/, 3];
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 2:
_a.sent();
return [3 /*break*/, 7];
case 3:
if (!isGitTemplate(template)) return [3 /*break*/, 6];
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 4:
_a.sent();
return [4 /*yield*/, fs.rm(path.join(destinationPath, ".git"), {
recursive: true,
force: true,
})];
case 5:
_a.sent();
return [3 /*break*/, 7];
case 6:
spinner.fail("Template preparation failed.");
throw new Error("Template has neither repo_url nor degitSource defined.");
case 7:
packageJsonPath = path.join(destinationPath, "package.json");
_a.label = 8;
case 8:
_a.trys.push([8, 11, , 12]);
spinner.text = "Updating package name to ".concat(path.basename(projectName), "...");
return [4 /*yield*/, fs.readFile(packageJsonPath, "utf-8")];
case 9:
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 10:
_a.sent();
return [3 /*break*/, 12];
case 11:
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*/, 12];
case 12:
if (!dynamicEnvId) return [3 /*break*/, 14];
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 13:
_a.sent();
spinner.text = ".env file created successfully.";
_a.label = 14;
case 14:
spinner.succeed("Template \"".concat(template.name, "\" prepared successfully in ").concat(destinationPath, "."));
return [3 /*break*/, 16];
case 15:
error_1 = _a.sent();
spinner.fail("Error preparing template \"".concat(template.name, "\"."));
console.error("Error details:", error_1);
throw error_1;
case 16: 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", "blockchain"), {
recursive: true,
})];
case 6:
_a.sent();
return [4 /*yield*/, fs.mkdir(path.join(projectName, "packages", "site"), {
recursive: true,
})];
case 7:
_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) { return __awaiter(void 0, void 0, void 0, function () {
var projectName, templateId, blockchainPath;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
projectName = options.projectName, templateId = options.templateId;
console.log("Setting up project with Foundry...");
return [4 /*yield*/, initializeMonorepo(options)];
case 1:
_a.sent();
console.log("Initializing Foundry project...");
blockchainPath = path.join(projectName, "packages", "blockchain");
return [4 /*yield*/, execAsync("cd ".concat(blockchainPath, " && forge init . --no-commit"))];
case 2:
_a.sent();
return [4 /*yield*/, cloneTemplate({
templateId: templateId,
projectName: projectName,
dynamicEnvId: options.dynamicEnvId,
blockchain_tooling: "foundry",
}, path.join(projectName, "packages", "site"))];
case 3:
_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 options, installCommand, mainSpinner, projectPath, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, promptForOptions(args)];
case 1:
options = _a.sent();
installCommand = "".concat(options.packageManager, " install");
mainSpinner = ora("Setting up your Web3 project...").start();
_a.label = 2;
case 2:
_a.trys.push([2, 10, , 11]);
if (!(options.blockchain_tooling === "hardhat")) return [3 /*break*/, 4];
mainSpinner.text = "Creating Hardhat project structure...";
return [4 /*yield*/, createHardhatProject(options)];
case 3:
_a.sent();
return [3 /*break*/, 8];
case 4:
if (!(options.blockchain_tooling === "foundry")) return [3 /*break*/, 6];
mainSpinner.text = "Creating Foundry project structure...";
return [4 /*yield*/, createFoundryProject(options)];
case 5:
_a.sent();
return [3 /*break*/, 8];
case 6:
mainSpinner.text = "Cloning base template...";
return [4 /*yield*/, cloneTemplate({
templateId: options.templateId,
projectName: options.projectName,
dynamicEnvId: options.dynamicEnvId,
blockchain_tooling: "none",
}, options.projectName)];
case 7:
_a.sent();
_a.label = 8;
case 8:
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 9:
_a.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 ".concat(options.packageManager, " run dev"));
console.log(" Starts the development server.");
}
console.log("\nHappy Hacking!");
return [3 /*break*/, 11];
case 10:
error_2 = _a.sent();
mainSpinner.fail("An error occurred during project creation.");
console.error("Error details:", error_2);
return [3 /*break*/, 11];
case 11: return [2 /*return*/];
}
});
}); };