UNPKG

@patchworkdev/pdk

Version:

Patchwork Development Kit

176 lines (175 loc) 7.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const extra_typings_1 = require("@commander-js/extra-typings"); const path_1 = __importDefault(require("path")); const dev_1 = require("./commands/dev"); const generate_1 = require("./commands/generate"); const network_1 = require("./commands/network"); const status_1 = require("./commands/status"); const cliProcessor_1 = require("./common/cliProcessor"); const config_1 = require("./common/helpers/config"); const error_1 = require("./common/helpers/error"); const logger_1 = require("./common/helpers/logger"); const generator_1 = require("./services/generator"); const lockFile_1 = __importDefault(require("./services/lockFile")); const wizardServer_1 = require("./wizardServer"); async function getConfigPath(configFile) { const configPath = configFile || (await (0, config_1.findConfig)()); if (!configPath) { throw new error_1.PDKError(error_1.ErrorCode.FILE_NOT_FOUND, `No patchwork project config file found: ${configFile !== undefined ? configFile : 'patchwork.config.ts'}`); } return configPath; } const program = new extra_typings_1.Command() .name('pdk') //.version('0.1.0') // options and hook have to be chained for types to properly resolve in preAction hook .option('-v, --verbose', 'Enable verbose logging') .hook('preAction', (thisCommand) => { const opts = thisCommand.opts(); (0, logger_1.setLogLevel)(opts.verbose ? 'debug' : 'info'); }); async function createLockFileMgr(optionalConfigPath) { const configPath = await getConfigPath(optionalConfigPath); const projectConfig = await (0, config_1.importPatchworkConfig)(configPath); const lockFileManager = new lockFile_1.default(configPath); const ctx = lockFileManager.getCtx(); ctx.configPath = configPath; ctx.config = projectConfig; ctx.rootDir = path_1.default.dirname(configPath); if (!ctx.artifacts) ctx.artifacts = {}; if (optionalConfigPath !== undefined) { lockFileManager.updateCtx(ctx); } else { lockFileManager.updateAndSaveCtx(ctx); } return lockFileManager; } (async () => { program .command('status') .description('Show the status of the current project') .action(async () => { const configPath = await getConfigPath(); await (0, status_1.status)(configPath); }); program .command('wizard') .description('Launch the Patchwork Wizard') .action(async () => { (0, wizardServer_1.launchWizardApp)(); }); const generate = program.command('generate').description('generate commands'); generate .command('contracts') .argument('[configFile]', 'Path to the patchwork project configuration file') .option('-o, --output <dir>', 'Output directory for the generated Solidity files') .option('-c, --contract <name>', 'Name of the specific contract to generate') .description('Generate patchwork contracts') .action(async (configFile, options) => { const ctx = (await createLockFileMgr(configFile)).getCtx(); await (0, generate_1.generateContracts)(ctx.config, options.output, options.contract); }); generate .command('deployScripts') .argument('[configFile]', 'Path to the patchwork project configuration file') .option('-c, --contractsDir <dir>', 'Directory containing the source Solidity files to deploy') .option('-o, --output <dir>', 'Output directory for the generated Solidity files') .description('Generate deploy scripts') .action(async (configFile, options) => { const ctx = (await createLockFileMgr(configFile)).getCtx(); await (0, generate_1.generateContractDeployScripts)(ctx.config, options.contractsDir, options.output); }); generate .command('all') .argument('[configFile]', 'Path to the patchwork project configuration file') .description('Generate all contracts and services') .action(async (configFile) => { const lockFileMgr = await createLockFileMgr(configFile); await (0, generate_1.generateAll)(lockFileMgr.getCtx().config); const generatorService = new generator_1.GeneratorService(lockFileMgr); await generatorService.runAllGenerators(); }); generate .command('services') .argument('[configFile]', 'Path to the patchwork project configuration file') .description('Generate all services') .action(async (configFile) => { const lockFileMgr = await createLockFileMgr(configFile); await new generator_1.GeneratorService(lockFileMgr).runAllGenerators(); }); generate .command('contractBuild') .argument('[configFile]', 'Path to the patchwork project configuration file') .description('Build contracts using Forge') .action(async (configFile) => { const ctx = (await createLockFileMgr(configFile)).getCtx(); await cliProcessor_1.cliProcessor.buildContracts(ctx.rootDir); }); // create a default ctx as plugins cannot take a special config file since they are discovered before CLI command configuration try { const lockFileMgr = await createLockFileMgr(); for (const plugin of lockFileMgr.getCtx().config.plugins) { if (plugin.generate) { generate .command(plugin.name.toLowerCase()) .description(`Run ${plugin.name} plugin generators`) .action(async () => { await new generator_1.GeneratorService(lockFileMgr).runGenerator(plugin.name.toLowerCase()); }); } } } catch (PDKError) { // No default config file found to configure plugin CLI commands so just omit. } // Local dev commands will only rely on a default patchwork.config.ts file as they are for a project generated using create-patchwork const dev = program.command('dev').description('local dev commands'); dev.command('up') .description('Run docker compose up for local dev') .action(async () => { console.info('Setting up docker compose for local dev'); const lockFileMgr = await createLockFileMgr(); await (0, dev_1.localDevUp)(lockFileMgr.getCtx().configPath, {}, new generator_1.GeneratorService(lockFileMgr)); }); dev.command('down') .description('Run docker compose down for local dev') .action(async () => { console.info('Tearing down docker compose for local dev'); const lockFileMgr = await createLockFileMgr(); await (0, dev_1.localDevDown)(lockFileMgr.getCtx().configPath); }); const network = program.command('network').description('network commands'); network .command('list') .description('list configured networks') .action(async () => { const lockFileMgr = await createLockFileMgr(); await (0, network_1.networkList)(lockFileMgr.getCtx().configPath); }); network .command('switch') .argument('<network>', 'Network to switch to') .description('switch selected network') .action(async (network) => { const lockFileMgr = await createLockFileMgr(); await (0, network_1.networkSwitch)(lockFileMgr.getCtx().configPath, network); }); try { await program.parseAsync(); } catch (error) { if (error instanceof error_1.PDKError) { console.error(`${error.code}: `, error.message); } else { console.error('Unknown Error:', error); } process.exit(1); } })();