Skip to content

Commit

Permalink
feat(init): first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 1, 2019
1 parent 671ea78 commit b8069f9
Show file tree
Hide file tree
Showing 13 changed files with 1,497 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ integration-test/transform-tests/output

# generated Sphinx/ReadTheDocs files
/docs/build/
/foo
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# New Repo
# Agoric Javascript Smart Contract Platform

This repository should be a compilation of everything that a new
Agoric repo should have, including ESLint settings, prettier settings,
package.json dependencies and scripts, licenses, sample tests, CircleCI config, and
VSCode testing config.
## Getting Started

See [SETUP-DELETEME](SETUP-DELETEME.md) for starting steps.
```sh
agoric init dapp
# agoric init --ui=react dapp
cd dapp
agoric start
agoric deploy
# Navigate to http://localhost:8000/
```
75 changes: 75 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import parseArgs from 'minimist';
import fs from 'fs';
import path from 'path';

import templateMain from './template';

export default async function initMain(progname, rawArgs, priv) {
const { console, error } = priv;
const {
_: args,
} = parseArgs(rawArgs);

if (args.length !== 1) {
error(`you must specify exactly one DIR`);
}
const [DIR] = args;

const { mkdir, stat, lstat, symlink, readdir, readFile, writeFile } = fs.promises;

console.log(`initializing ${DIR}`);
await mkdir(DIR);

const templateDir = `${__dirname}/../template`;
const writeTemplate = async stem => {
const template = await readFile(`${templateDir}${stem}`, 'utf-8');
const content = template.replace(/['"]@DIR@['"]/g, JSON.stringify(DIR)).replace(/@DIR@/g, DIR);
return writeFile(`${DIR}${stem}`, content);
}

const recursiveTemplate = async (templateDir, suffix = '') => {
const cur = `${templateDir}${suffix}`;
const list = await readdir(cur);
await Promise.all(list.map(async name => {
const stem = `${suffix}/${name}`;
const st = await lstat(`${templateDir}${stem}`);
let target;
try {
target = await stat(`${DIR}${stem}`);
} catch (e) {}
if (target) {
return;
}
if (st.isDirectory()) {
console.log(`mkdir ${DIR}${stem}`);
await mkdir(`${DIR}${stem}`);
await recursiveTemplate(templateDir, `${stem}`)
} else {
console.log(`write ${DIR}${stem}`);
await writeTemplate(stem);
}
}));
};
await recursiveTemplate(templateDir);

const agservers = path.join(DIR, '.agservers');
await mkdir(agservers);

const solo = path.join(agservers, 'solo');
const chain = path.join(agservers, 'chain');
await Promise.all([mkdir(solo), mkdir(chain)]);
const chainSolo = path.join(chain, 'solo');
const chainCosmos = path.join(chain, 'cosmos');
await Promise.all([mkdir(chainSolo), mkdir(chainCosmos)]);

// Create links to all the solo nodes' html directories.
await Promise.all([
mkdir(`${chainSolo}/html`),
mkdir(`${solo}/html`),
])

await Promise.all([
symlink('../../../ui/build', `${chainSolo}/dapp-html`),
symlink('../../ui/build', `${solo}/dapp-html`),
]);
}
16 changes: 15 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import parseArgs from 'minimist';

const VERSION = 'Agoric 0.1.0';

const main = async (progname, rawArgs, { console, error }) => {
const main = async (progname, rawArgs, privs) => {
const { console, error } = privs;
const { _: args, ...opts } = parseArgs(rawArgs, {
boolean: ['version', 'help'],
stopEarly: true,
});

const subMain = (fn, args) => {
const subProgname = `${progname}: ${args[0]}`;
const subError = (...rest) => error(`${args[0]}:`, ...rest);
return fn(subProgname, args.slice(1), { ...privs, error: subError });
};

const usage = status => {
if (status) {
console.error(chalk.bold(`Type '${progname} --help' for more information.`));
Expand Down Expand Up @@ -40,6 +47,13 @@ help display this help and exit
return usage(1);
case 'help':
return usage(0);
case 'start':
return subMain((await import('./start')).default, args);
// TODO
case 'template':
return subMain((await import('./template')).default, args);
case 'init':
return subMain((await import('./init')).default, args);
default:
error(`unrecognized COMMAND`, cmd);
return usage(1);
Expand Down
Empty file added lib/start.js
Empty file.
Empty file added lib/template.js
Empty file.
Loading

0 comments on commit b8069f9

Please sign in to comment.