import React from 'react'; import path from 'path'; import fs from 'fs'; import shell from 'shelljs'; import { fastRender, pathExists, isTruffleArtifact, TruffleArtifact, requireAuthData } from "../services"; import { App, TruffleFlow, ErrorBox } from "../ui"; import { ArgShape, UniversalArgs } from "../cli"; import { Argv } from 'yargs'; export const command = 'truffle'; export const desc = "Run in a Truffle project directory to make a dapp for one of your deployed contracts."; export function builder(yargs: Argv) { yargs.middleware(requireAuthData); } export function handler(args:ArgShape) { const thisDir = process.cwd(); const authFile = args.authFile if (typeof authFile === 'undefined') return fastRender( ) if (!pathExists(['truffle.js', 'truffle-config.js'], thisDir)) return fastRender( ) if (!pathExists('./build/contracts', thisDir)) return fastRender( ) // Fetch all the files in there const artifactDir = path.resolve(thisDir, './build/contracts'); const artifactFilenames = shell.ls(artifactDir).filter(name => name.endsWith('.json')); // console.log('Found artifact filenames: ',artifactFilenames); const unfilteredArtifacts:TruffleArtifact[] = artifactFilenames.map(filename => { const artifact = JSON.parse(fs.readFileSync(path.resolve(artifactDir, filename)).toString()) if (artifact.contractName) artifact.contract_name = artifact.contractName; return artifact; }) const artifacts = unfilteredArtifacts.filter(artifact => isTruffleArtifact(artifact) && artifact.contract_name !== 'Migrations') if (artifacts.length === 0) { return fastRender( ) } // Filter for the ones which have values inside their "network" key const deployedArtifacts = artifacts.filter(artifact => { if (!artifact.networks) return false; return Object.keys(artifact.networks).length > 0; }) if (deployedArtifacts.length === 0) { return fastRender( ) } // Got deployable artifacts, good to go! fastRender( }/> ) }