create-near-app
Version:
Quickly scaffold your dApp on NEAR Blockchain
172 lines • 6.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.projectPath = exports.promptAndGetConfig = exports.getUserAnswers = exports.getUserArgs = void 0;
const types_1 = require("./types");
const chalk_1 = __importDefault(require("chalk"));
const prompts_1 = __importDefault(require("prompts"));
const commander_1 = require("commander");
const show = __importStar(require("./messages"));
const tracking_1 = require("./tracking");
const fs_1 = __importDefault(require("fs"));
async function getUserArgs() {
commander_1.program
.argument('[projectName]')
.option('--frontend [next-page|next-app|none]')
.option('--contract [ts|rs|none]')
.option('--install')
.addHelpText('after', 'You can create a frontend or a contract with tests');
commander_1.program.parse();
const options = commander_1.program.opts();
const [projectName] = commander_1.program.args;
const { contract, frontend, install } = options;
return { contract, frontend, projectName, install, error: undefined };
}
exports.getUserArgs = getUserArgs;
const appChoices = [
{ title: 'A Web App', description: 'A Web App that talks with Near contracts', value: 'gateway' },
{
title: 'A Smart Contract', description: 'A smart contract to be deployed in the Near Blockchain', value: 'contract',
},
];
const contractChoices = [
{ title: 'JS/TS Contract', description: 'A Near contract written in javascript/typescript', value: 'ts' },
{ title: 'Rust Contract', description: 'A Near contract written in Rust', value: 'rs' },
{ title: 'Python Contract', description: 'A Near contract written in Python', value: 'py' },
];
const frontendChoices = [
{ title: 'NextJs (Classic)', description: 'A web-app built using Next.js Page Router', value: 'next-page' },
{ title: 'NextJS (App Router)', description: 'A web-app built using Next.js new App Router', value: 'next-app' },
{ title: 'Vite (React)', description: 'A web-app built using Vite with React', value: 'vite-react' },
];
const appPrompt = {
type: 'select',
name: 'app',
message: 'What do you want to build?',
choices: appChoices,
};
const frontendPrompt = {
type: 'select',
name: 'frontend',
message: 'Select a framework for your frontend',
choices: frontendChoices,
};
const contractPrompt = [
{
type: 'select',
name: 'contract',
message: 'Select a smart contract template for your project',
choices: contractChoices,
}
];
const namePrompts = {
type: 'text',
name: 'projectName',
message: 'Name your project (we will create a directory with that name)',
initial: 'hello-near',
};
const npmPrompt = {
type: 'confirm',
name: 'install',
message: (0, chalk_1.default) `Run {bold {blue 'npm install'}} now?`,
initial: true,
};
const promptUser = async (prompts) => {
// Prompt, and exit if user cancels
return (0, prompts_1.default)(prompts, { onCancel: () => process.exit(0) });
};
async function getUserAnswers() {
// Either the user wants a gateway or a contract
const { app } = await promptUser(appPrompt);
if (app === 'gateway') {
// If gateway, ask for the framework to use
const { frontend, projectName, install } = await promptUser([frontendPrompt, namePrompts, npmPrompt]);
return { frontend, contract: 'none', projectName, install, error: undefined };
}
else {
// If platform is Window, return the error
if (process.platform === 'win32') {
return { frontend: 'none', contract: 'none', projectName: '', install: false, error: show.windowsWarning };
}
// If contract, ask for the language for the contract
let { contract } = await promptUser(contractPrompt);
const { projectName } = await promptUser(namePrompts);
const install = contract === 'ts' ? (await promptUser(npmPrompt)).install : false;
return { frontend: 'none', contract, projectName, install, error: undefined };
}
}
exports.getUserAnswers = getUserAnswers;
async function promptAndGetConfig() {
// process cli args
let args = await getUserArgs();
// If no args, prompt user
if (!args.projectName) {
show.welcome();
args = await getUserAnswers();
}
if (args.error) {
(0, tracking_1.trackUsage)('none', 'none');
return args.error();
}
// Homogenizing terminal args with prompt args
args.contract = args.contract || 'none';
args.frontend = args.frontend || 'none';
if (!validateUserArgs(args))
return;
// track user input
const { frontend, contract } = args;
(0, tracking_1.trackUsage)(frontend, contract);
let path = (0, exports.projectPath)(args.projectName);
if (fs_1.default.existsSync(path)) {
return show.directoryExists(path);
}
return { config: args, projectPath: path };
}
exports.promptAndGetConfig = promptAndGetConfig;
const projectPath = (projectName) => `${process.cwd()}/${projectName}`;
exports.projectPath = projectPath;
const validateUserArgs = (args) => {
if (!types_1.FRONTENDS.includes(args.frontend)) {
show.argsError(`Invalid frontend type: ${args.frontend}`);
return false;
}
if (!types_1.CONTRACTS.includes(args.contract)) {
show.argsError(`Invalid contract type: ${args.contract}`);
return false;
}
if (!args.projectName) {
show.argsError('Please provide a project name');
return false;
}
if ((args.contract === 'none') === (args.frontend === 'none')) {
show.argsError('Please create a contract OR a frontend');
return false;
}
return true;
};
//# sourceMappingURL=user-input.js.map