|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { print } from 'gluegun'; |
| 3 | +import open from 'open'; |
| 4 | +import { Args, Command, Flags, ux } from '@oclif/core'; |
| 5 | +import { createCompiler } from '../command-helpers/compiler'; |
| 6 | +import * as DataSourcesExtractor from '../command-helpers/data-sources'; |
| 7 | +import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs'; |
| 8 | +import Protocol from '../protocols'; |
| 9 | + |
| 10 | +export default class PublishCommand extends Command { |
| 11 | + static description = 'Publish to the Graph Network'; |
| 12 | + |
| 13 | + static args = { |
| 14 | + 'subgraph-manifest': Args.string({ |
| 15 | + default: 'subgraph.yaml', |
| 16 | + }), |
| 17 | + }; |
| 18 | + |
| 19 | + static flags = { |
| 20 | + help: Flags.help({ |
| 21 | + char: 'h', |
| 22 | + }), |
| 23 | + ipfs: Flags.string({ |
| 24 | + summary: 'Upload build results to an IPFS node.', |
| 25 | + char: 'i', |
| 26 | + default: DEFAULT_IPFS_URL, |
| 27 | + }), |
| 28 | + 'ipfs-hash': Flags.string({ |
| 29 | + summary: 'IPFS hash of the subgraph manifest to deploy.', |
| 30 | + required: false, |
| 31 | + }), |
| 32 | + 'webapp-url': Flags.string({ |
| 33 | + summary: 'URL of the web UI you want to use to deploy.', |
| 34 | + required: false, |
| 35 | + default: 'https://cli.thegraph.com/publish', |
| 36 | + }), |
| 37 | + }; |
| 38 | + |
| 39 | + /** |
| 40 | + * Prompt the user to open up the browser to continue publishing the subgraph |
| 41 | + */ |
| 42 | + async publishWithBrowser({ ipfsHash, webapp }: { ipfsHash: string; webapp: string }) { |
| 43 | + const answer = await ux.prompt( |
| 44 | + `Press ${chalk.green( |
| 45 | + 'y', |
| 46 | + )} (or any key) to open up the browser to continue publishing or ${chalk.yellow( |
| 47 | + 'q', |
| 48 | + )} to exit`, |
| 49 | + ); |
| 50 | + |
| 51 | + if (answer.toLowerCase() === 'q') { |
| 52 | + this.exit(0); |
| 53 | + } |
| 54 | + |
| 55 | + const URL = `${webapp}?id=${ipfsHash}`; |
| 56 | + |
| 57 | + print.success( |
| 58 | + `Finalize the publish of the subgraph from the Graph CLI publish page. Opening up the browser to continue publishing at ${URL}`, |
| 59 | + ); |
| 60 | + await open(URL); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + async run() { |
| 65 | + const { |
| 66 | + args: { 'subgraph-manifest': manifest }, |
| 67 | + flags: { 'ipfs-hash': ipfsHash, 'webapp-url': webUiUrl, ipfs }, |
| 68 | + } = await this.parse(PublishCommand); |
| 69 | + |
| 70 | + if (ipfsHash) { |
| 71 | + await this.publishWithBrowser({ ipfsHash, webapp: webUiUrl }); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + let protocol; |
| 76 | + try { |
| 77 | + const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest); |
| 78 | + protocol = Protocol.fromDataSources(dataSourcesAndTemplates); |
| 79 | + } catch (e) { |
| 80 | + this.error(e, { exit: 1 }); |
| 81 | + } |
| 82 | + |
| 83 | + const compiler = createCompiler(manifest, { |
| 84 | + ipfs, |
| 85 | + outputDir: 'build/', |
| 86 | + outputFormat: 'wasm', |
| 87 | + skipMigrations: false, |
| 88 | + protocol, |
| 89 | + }); |
| 90 | + |
| 91 | + // Exit with an error code if the compiler couldn't be created |
| 92 | + if (!compiler) { |
| 93 | + this.exit(1); |
| 94 | + return; |
| 95 | + } |
| 96 | + const result = await compiler.compile({ validate: true }); |
| 97 | + if (result === undefined || result === false) { |
| 98 | + // Compilation failed, not deploying. |
| 99 | + process.exitCode = 1; |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + await this.publishWithBrowser({ ipfsHash: result, webapp: webUiUrl }); |
| 104 | + return; |
| 105 | + } |
| 106 | +} |
0 commit comments