UNPKG

create-near-app

Version:

Quickly scaffold your dApp on NEAR Blockchain

191 lines 7.79 kB
"use strict"; 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|vite-react|none]') .option('--contract [ts|rs|none]') .option('--template [auction-adv|auction]') .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, template, install } = options; return { contract, frontend, template, projectName, install, error: undefined }; } exports.getUserArgs = getUserArgs; const appChoices = [ { title: 'Web Application', description: 'A Web App that talks with Near contracts', value: 'gateway' }, { title: 'Smart Contract', description: 'A smart contract to be deployed in the Near Blockchain', value: 'contract', }, ]; const templateChoices = [ { title: 'Auction', description: 'A simple smart contract where users bid using NEAR, ideal for beginners', value: 'auction' }, { title: 'Auction (with FT)', description: 'A more advanced contract where users bid using FT and the winner gets an NFT, for experienced users', value: 'auction-adv' }, ]; const contractChoices = [ { title: 'Rust (recommended)', description: 'A smart contract written in Rust', value: 'rs' }, { title: 'Typescript (experimental)', description: 'A smart contract written in typescript', value: 'ts' }, ]; const frontendChoices = [ { title: 'Vite (React)', description: 'A web-app built using Vite with React', value: 'vite-react' }, { 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' }, ]; const appPrompt = { type: 'select', name: 'app', message: 'What do you want to build?', choices: appChoices, }; const templatePrompt = { type: 'select', name: 'template', message: 'Select a contract template', choices: templateChoices, }; const frontendPrompt = { type: 'select', name: 'frontend', message: 'Select a framework for your frontend', choices: frontendChoices, }; const contractPrompt = [ { type: 'select', name: 'contract', message: 'Select a language for your contract', choices: contractChoices, } ]; const namePrompts = { type: 'text', name: 'projectName', message: 'Name your project (we will create a directory with that name)', initial: 'near-template', }; 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 template and language const { template } = await promptUser(templatePrompt); let { contract } = await promptUser(contractPrompt); const { projectName } = await promptUser(namePrompts); const install = contract === 'ts' ? (await promptUser(npmPrompt)).install : false; return { frontend: 'none', contract, template, 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.template && !types_1.TEMPLATES.includes(args.template)) { show.argsError(`Invalid template type: ${args.template}`); 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; } if (args.contract !== 'none' && !args.template) { show.argsError('Please specify a template for your contract'); return false; } return true; }; //# sourceMappingURL=user-input.js.map