Skip to content

Commit

Permalink
chore(agoric-cli): refactor the CLI start function
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 6, 2020
1 parent 5ef46cd commit f4cce4f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 31 deletions.
112 changes: 81 additions & 31 deletions packages/agoric-cli/lib/start.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import parseArgs from 'minimist';
import parseArgs from 'minimist';
import path from 'path';
import chalk from 'chalk';

const FAKE_CHAIN_DELAY = 5;

export default async function startMain(progname, rawArgs, priv, opts) {
const { console, error, fs, spawn, process } = priv;
const {
reset,
_: args,
} = parseArgs(rawArgs, {
const { reset, _: args } = parseArgs(rawArgs, {
boolean: ['reset'],
});

const pspawn = (...args) =>
new Promise((resolve, reject) => {
const cp = spawn(...args);
const pspawn = (cmd, cargs, ...rest) => {
console.log(chalk.blueBright(cmd, ...cargs));
return new Promise((resolve, reject) => {
const cp = spawn(cmd, cargs, ...rest);
cp.on('exit', resolve);
cp.on('error', () => resolve(-1));
});
};

const exists = async file => {
try {
Expand All @@ -26,41 +28,89 @@ export default async function startMain(progname, rawArgs, priv, opts) {
}
};

const linkHtml = async name => {
console.log(chalk.green('linking html directories'));
const dappHtml = `.agservers/${name}/dapp-html`;
const htmlWallet = `.agservers/${name}/html/wallet`;
// await Promise.all([fs.unlink(dappHtml).catch(() => {}), fs.unlink(htmlWallet).catch(() => {})]);
await Promise.all([
fs.symlink('../../ui/build', dappHtml).catch(() => {}),
fs.symlink('../../../.agwallet', htmlWallet).catch(() => {}),
]);
};

let agSolo;
let agServer;
if (opts.sdk) {
agSolo = `${__dirname}/../../cosmic-swingset/bin/ag-solo`;
agSolo = path.resolve(__dirname, '../../cosmic-swingset/bin/ag-solo');
} else {
if (!await exists('.agservers/node_modules')) {
if (!(await exists('.agservers/node_modules'))) {
return error(`you must first run '${progname} install'`);
}
agSolo = `${process.cwd()}/node_modules/@agoric/cosmic-swingset/bin/ag-solo`;
}

if (reset) {
console.log(chalk.green('removing .agservers/solo'));
await pspawn('rm', ['-rf', '.agservers/solo'], { stdio: 'inherit' });
}
async function startDev(profileName) {
const fakeGCI = 'myFakeGCI';
if (!(await exists(agServer))) {
console.log(chalk.yellow(`initializing ${profileName}`));
await pspawn(agSolo, ['init', profileName, '--egresses=fake'], {
stdio: 'inherit',
cwd: '.agservers',
});
}

// Run scenario3.
if (!await exists('.agservers/solo')) {
console.log(chalk.yellow('initializing solo'))
await pspawn(agSolo, ['init', 'solo', '--egresses=none'], {
console.log(
chalk.yellow(`setting fake chain with ${FAKE_CHAIN_DELAY} second delay`),
);
await pspawn(
agSolo,
[
'set-fake-chain',
'--role=two_chain',
`--delay=${FAKE_CHAIN_DELAY}`,
fakeGCI,
],
{
stdio: 'inherit',
cwd: agServer,
},
);
await linkHtml(profileName);

await pspawn(agSolo, ['start', '--role=two_client'], {
stdio: 'inherit',
cwd: '.agservers',
cwd: agServer,
});
}

console.log(chalk.green('linking html directories'));
const dappHtml = '.agservers/solo/dapp-html';
const htmlWallet = '.agservers/solo/html/wallet';
// await Promise.all([fs.unlink(dappHtml).catch(() => {}), fs.unlink(htmlWallet).catch(() => {})]);
await Promise.all([
fs.symlink('../../ui/build', dappHtml).catch(() => {}),
fs.symlink('../../../.agwallet', htmlWallet).catch(() => {}),
]);
async function startTestnet(profileName) {

await pspawn(agSolo, ['start', '--role=three_client'], {
stdio: 'inherit',
cwd: '.agservers/solo',
});
}

const profiles = {
dev: startDev,
testnet: startTestnet,
};

const profileName = args[0] || 'dev';
const startFn = profiles[profileName];
if (!startFn) {
return error(
`unrecognized profile name ${profileName}; use one of: ${Object.keys(
profiles,
)
.sort()
.join(', ')}`,
);
}

agServer = `.agservers/${profileName}`;
if (reset) {
console.log(chalk.green(`removing ${agServer}`));
// FIXME: Use portable rimraf.
await pspawn('rm', ['-rf', agServer], { stdio: 'inherit' });
}

await startFn(profileName, agServer);
}
2 changes: 2 additions & 0 deletions packages/agoric-cli/template/.agservers/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ integration-test/transform-tests/output
/docs/build/

/solo
/dev
/testnet
/chain

0 comments on commit f4cce4f

Please sign in to comment.