Skip to content

Commit

Permalink
feat(init): use --dapp-template (default @agoric/dapp-simple-exchange)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 4, 2020
1 parent d80d925 commit 3bdf8ff
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 24 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
"packages/wallet-frontend",
"packages/zoe",
"packages/cosmic-swingset",
"packages/dapp-simple-exchange",
"packages/dapp-simple-exchange/.agservers",
"packages/dapp-simple-exchange/api",
"packages/dapp-simple-exchange/contract",
"packages/dapp-simple-exchange/ui",
"packages/generator-agoric-dapp",
"packages/agoric-cli",
"packages/agoric-cli/template/.agservers",
"packages/agoric-cli/template/api",
"packages/agoric-cli/template/contract",
"packages/agoric-cli/template/ui",
"packages/deployment"
],
"devDependencies": {
Expand Down
56 changes: 43 additions & 13 deletions packages/agoric-cli/lib/init.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import parseArgs from 'minimist';
import chalk from 'chalk';

const DEFAULT_DAPP_TEMPLATE = '@agoric/dapp-simple-exchange';

export default async function initMain(progname, rawArgs, priv) {
const { console, error, fs } = priv;
const { _: args, force } = parseArgs(rawArgs, { boolean: ['force'] });
const { _: args, force, 'dapp-template': dappTemplate } = parseArgs(rawArgs, {
boolean: ['force'],
default: { 'dapp-template': DEFAULT_DAPP_TEMPLATE },
});

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

const slashPJson = '/package.json';
const pjson = require.resolve(`${dappTemplate}${slashPJson}`);
const dappRoot = pjson.substr(0, pjson.length - slashPJson.length);

const {
mkdir,
stat,
Expand All @@ -34,15 +43,15 @@ export default async function initMain(progname, rawArgs, priv) {
name === 'node_modules' ||
(suffix === '/.agservers' && name !== 'package.json' && name[0] !== '.');

const writeTemplate = async (templateDir, stem) => {
const writeTemplate = async (templateDir, destDir = DIR, 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);
return writeFile(`${destDir}${stem}`, content);
};

const recursiveTemplate = async (templateDir, suffix = '') => {
const recursiveTemplate = async (templateDir, destDir = DIR, suffix = '') => {
const cur = `${templateDir}${suffix}`;
const list = await readdir(cur);
await Promise.all(
Expand All @@ -54,32 +63,53 @@ export default async function initMain(progname, rawArgs, priv) {
const st = await lstat(`${templateDir}${stem}`);
let target;
try {
target = await stat(`${DIR}${stem}`);
target = await stat(`${destDir}${stem}`);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}
}
if (st.isDirectory()) {
if (!target) {
console.log(`mkdir ${DIR}${stem}`);
await mkdir(`${DIR}${stem}`);
console.log(`mkdir ${destDir}${stem}`);
await mkdir(`${destDir}${stem}`);
}
await recursiveTemplate(templateDir, `${stem}`);
await recursiveTemplate(templateDir, destDir, `${stem}`);
} else if (st.isSymbolicLink()) {
console.log(`symlink ${DIR}${stem}`);
console.log(`symlink ${destDir}${stem}`);
await symlink(
await readlink(`${templateDir}${stem}`),
`${DIR}${stem}`,
`${destDir}${stem}`,
);
} else {
console.log(`write ${DIR}${stem}`);
await writeTemplate(templateDir, stem);
console.log(`write ${destDir}${stem}`);
await writeTemplate(templateDir, destDir, stem);
}
}),
);
};
await recursiveTemplate(`${__dirname}/../template`);
await recursiveTemplate(dappRoot);
await mkdir(`${DIR}/.agwallet`);
await recursiveTemplate(`${__dirname}/../agwallet`, `${DIR}/.agwallet`);

const ps = ['', 'api/', 'contract/', 'ui/'].map(dir => {
const path = `${DIR}/${dir}package.json`;
return readFile(path, 'utf-8')
.then(contents => JSON.parse(contents))
.then(pkg => {
if (!pkg.name || !pkg.name.startsWith(dappTemplate)) {
throw Error(
`${path}: "name" must start with ${JSON.stringify(dappTemplate)}`,
);
}
pkg.name = `${DIR}${pkg.name.substr(dappTemplate.length)}`;
const json = JSON.stringify(pkg, undefined, 2);
return writeFile(path, json);
})
.catch(e => console.error(chalk.bold.blue(`Cannot rewrite ${path}`), e));
});

await Promise.all(ps);

console.log(chalk.bold.yellow(`Done initializing`));
return 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-simple-exchange/.agservers/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@agoric/dapp-agoric-servers",
"name": "@agoric/dapp-simple-exchange-agservers",
"version": "0.0.1",
"description": "Agoric server instances for @DIR@",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-simple-exchange/api/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@agoric/dapp-api",
"name": "@agoric/dapp-simple-exchange-api",
"private": true,
"version": "0.1.0",
"description": "@DIR@ Agoric Dapp web server handler",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-simple-exchange/contract/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@agoric/dapp-contract",
"name": "@agoric/dapp-simple-exchange-contract",
"version": "0.1.0",
"private": true,
"description": "Contract for the @DIR@ Agoric Dapp",
Expand Down
5 changes: 3 additions & 2 deletions packages/dapp-simple-exchange/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@agoric/dapp",
"version": "independent",
"name": "@agoric/dapp-simple-exchange",
"version": "0.0.1",
"private": true,
"useWorkspaces": true,
"main": "index.js",
"workspaces": [
"api",
"contract",
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-simple-exchange/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@agoric/dapp-ui",
"name": "@agoric/dapp-simple-exchange-ui",
"version": "0.0.1",
"description": "Frontend UI",
"private": true,
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6301,6 +6301,11 @@ eslint-plugin-react-hooks@^2.2.0:
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-2.3.0.tgz#53e073961f1f5ccf8dd19558036c1fac8c29d99a"
integrity sha512-gLKCa52G4ee7uXzdLiorca7JIQZPPXRAQDXV83J4bUEeUuc5pIEyZYAZ45Xnxe5IuupxEqHS+hUhSLIimK1EMw==

eslint-plugin-react-hooks@^2.3.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-2.5.0.tgz#c50ab7ca5945ce6d1cf8248d9e185c80b54171b6"
integrity sha512-bzvdX47Jx847bgAYf0FPX3u1oxU+mKU8tqrpj4UX9A96SbAmj/HVEefEy6rJUog5u8QIlOPTKZcBpGn5kkKfAQ==

eslint-plugin-react-hooks@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-2.4.0.tgz#db6ee1cc953e3a217035da3d4e9d4356d3c672a4"
Expand Down Expand Up @@ -6336,7 +6341,7 @@ eslint-plugin-react@7.18.0, eslint-plugin-react@^7.15.1:
prop-types "^15.7.2"
resolve "^1.14.2"

eslint-plugin-react@^7.18.3:
eslint-plugin-react@^7.18.0, eslint-plugin-react@^7.18.3:
version "7.18.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.18.3.tgz#8be671b7f6be095098e79d27ac32f9580f599bc8"
integrity sha512-Bt56LNHAQCoou88s8ViKRjMB2+36XRejCQ1VoLj716KI1MoE99HpTVvIThJ0rvFmG4E4Gsq+UgToEjn+j044Bg==
Expand Down

0 comments on commit 3bdf8ff

Please sign in to comment.