create-sui-dapp
Version:
Command-line interface for Sui dApp Starter
260 lines (259 loc) • 12.3 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 = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["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 chalk from 'chalk';
import { execSync } from 'child_process';
import commandExists from 'command-exists';
import fs from 'fs';
import inquirer from 'inquirer';
import ora from 'ora';
import path from 'path';
import { fileURLToPath } from 'url';
import { APP_NAME, DEFAULT_TEMPLATE, SOURCE_REPO, TEMPLATES, } from './constants.js';
export var scaffoldProject = function (projectName, template) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
checkFolderExists(projectName);
return [4 /*yield*/, runCommand("git clone --depth 1 ".concat(SOURCE_REPO, " \"").concat(projectName, "\""), 'Cloning the source repo', 'Cannot clone the source repo')];
case 1:
_a.sent();
return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && rm -rf ./.git"), 'Removing old git history', 'Cannot remove old git history')];
case 2:
_a.sent();
return [4 /*yield*/, initTemplate(projectName, template)];
case 3:
_a.sent();
return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && git init && git add . && git commit -m \"Initial commit\""), 'Initializing a new git repo', 'Cannot initialize a new git repo')];
case 4: return [4 /*yield*/, _a.sent()];
case 5:
_a.sent();
return [4 /*yield*/, isPnpmInstalled()];
case 6:
if (!_a.sent()) return [3 /*break*/, 8];
return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && pnpm install"), 'Installing dependencies', 'Cannot install dependencies')];
case 7:
_a.sent();
return [3 /*break*/, 9];
case 8:
displayErrorMessage('PNPM is not found. Please install it first https://pnpm.io/installation and then run `pnpm install` from the project root.');
_a.label = 9;
case 9: return [4 /*yield*/, isSuibaseInstalled()];
case 10:
if (!(_a.sent())) {
displayErrorMessage('The starter needs Suibase, which can be found here https://suibase.io');
}
displayInfoMessage('\nHappy coding!');
console.log("\n ~ and if you like ".concat(chalk.green(APP_NAME), ", give it a star ").concat(SOURCE_REPO, "\n"));
return [2 /*return*/];
}
});
}); };
export var checkGit = function () { return __awaiter(void 0, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, commandExists('git')];
case 1:
_b.sent();
return [3 /*break*/, 3];
case 2:
_a = _b.sent();
displayErrorMessage('Git is not found. Please install https://git-scm.com/downloads');
process.exit(1);
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
}); };
export function promptForSettings(args) {
return __awaiter(this, void 0, void 0, function () {
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',
default: 'my-sui-dapp',
message: 'Please specify a name for your project: ',
validate: function (input) {
if (input == null || input.trim().length === 0) {
return 'Project name cannot be empty';
}
return true;
},
},
{
type: 'list',
name: 'template',
choices: TEMPLATES,
default: DEFAULT_TEMPLATE,
message: 'Please choose a frontend framework you want to use:',
},
])];
case 1: return [2 /*return*/, _a.sent()];
case 2: return [2 /*return*/, null];
}
});
});
}
var isPnpmInstalled = function () { return __awaiter(void 0, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, commandExists('pnpm')];
case 1:
_b.sent();
return [2 /*return*/, true];
case 2:
_a = _b.sent();
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
}); };
var isSuibaseInstalled = function () { return __awaiter(void 0, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, commandExists('msui')];
case 1:
_b.sent();
return [2 /*return*/, true];
case 2:
_a = _b.sent();
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
}); };
var checkFolderExists = function (projectPath) {
var fullPath = path.resolve(process.cwd(), projectPath);
var doesFolderExist = false;
try {
doesFolderExist = fs.existsSync(fullPath);
}
catch (_a) {
doesFolderExist = true;
}
if (doesFolderExist) {
displayErrorMessage('The folder already exists. Please remove or choose another project name.');
process.exit(1);
}
};
export var displayErrorMessage = function (message) {
console.error(chalk.red(message));
};
export var displayInfoMessage = function (message) {
console.log(chalk.green(message));
};
export var getPackageVersion = function () {
try {
var packageFile = fs.readFileSync(path.join(getCliDirectory(), '../package.json'), 'utf8');
var packageMeta = JSON.parse(packageFile);
return packageMeta.version;
}
catch (e) {
displayErrorMessage("Cannot read package meta-data.");
console.error(e);
process.exit(1);
}
};
var getCliDirectory = function () {
var currentFileUrl = import.meta.url;
return path.dirname(decodeURI(fileURLToPath(currentFileUrl)));
};
var runCommand = function (command, startMessage, errorMessage, verbose) {
if (verbose === void 0) { verbose = false; }
var ignore = !verbose ? 'ignore' : undefined;
var spinner = ora({
text: startMessage,
stream: process.stdout,
spinner: 'star',
}).start();
try {
execSync(command, {
stdio: ignore,
});
spinner.succeed();
}
catch (e) {
spinner.fail(chalk.red(errorMessage));
verbose && console.error(e);
process.exit(1);
}
};
export var capitalize = function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
export var initTemplate = function (projectName, template) { return __awaiter(void 0, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = template;
switch (_a) {
case 'greeting-react': return [3 /*break*/, 1];
case 'greeting-next': return [3 /*break*/, 3];
case 'counter-react': return [3 /*break*/, 5];
}
return [3 /*break*/, 7];
case 1: return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && rm -rf ./packages/backend-counter && rm -rf ./packages/frontend-greeting-next && rm -rf ./packages/frontend-counter-react"), 'Initializing the Greeting (React) template', 'Cannot initialize the Greeting (React) template')];
case 2:
_b.sent();
return [2 /*return*/];
case 3: return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && rm -rf ./packages/backend-counter && rm -rf ./packages/frontend && rm -rf ./packages/frontend-counter-react && mv -f ./packages/frontend-greeting-next ./packages/frontend"), 'Initializing the Greeting (Next.js) template', 'Cannot initialize the Greeting (Next.js) template')];
case 4:
_b.sent();
return [2 /*return*/];
case 5: return [4 /*yield*/, runCommand("cd \"".concat(projectName, "\" && rm -rf ./packages/backend && rm -rf ./packages/frontend && rm -rf ./packages/frontend-greeting-next && mv -f ./packages/frontend-counter-react ./packages/frontend && mv -f ./packages/backend-counter ./packages/backend"), 'Initializing the Counter (React) template', 'Cannot initialize the Counter (React) template')];
case 6:
_b.sent();
return [2 /*return*/];
case 7:
displayErrorMessage("".concat(template, " template is not found"));
return [2 /*return*/];
}
});
}); };