Skip to content

Commit

Permalink
refactor(agoric): Convert RESM to NESM (merge #3635)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Aug 13, 2021
2 parents b373c5a + 6627996 commit 99d8f6e
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 41 deletions.
2 changes: 0 additions & 2 deletions packages/agoric-cli/bin/agoric

This file was deleted.

1 change: 1 addition & 0 deletions packages/agoric-cli/bin/agoric
12 changes: 7 additions & 5 deletions packages/agoric-cli/integration-tests/test-workflow.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global __dirname process setTimeout clearTimeout setInterval clearInterval */
/* global process setTimeout clearTimeout setInterval clearInterval */
// eslint-disable-next-line import/no-extraneous-dependencies
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava';
import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava.js';

import fs from 'fs';
import path from 'path';
Expand All @@ -11,10 +11,12 @@ import { request } from 'http';

import { spawn } from 'child_process';

import { makePspawn } from '../lib/helpers';
import { makePspawn } from '../lib/helpers.js';

const TIMEOUT_SECONDS = 20 * 60;

const dirname = new URL('./', import.meta.url).pathname;

// To keep in sync with https://agoric.com/documentation/getting-started/

// Note that we currently only test:
Expand Down Expand Up @@ -45,10 +47,10 @@ test('workflow', async t => {
}

// Run all main programs with the '--sdk' flag if we are in agoric-sdk.
const extraArgs = fs.existsSync(`${__dirname}/../../cosmic-swingset`)
const extraArgs = fs.existsSync(`${dirname}/../../cosmic-swingset`)
? ['--sdk']
: [];
const agoricCli = path.join(__dirname, '..', 'bin', 'agoric');
const agoricCli = path.join(dirname, '..', 'bin', 'agoric');
function myMain(args) {
// console.error('running agoric-cli', ...extraArgs, ...args);
return pspawnStdout(agoricCli, [...extraArgs, ...args], {
Expand Down
2 changes: 1 addition & 1 deletion packages/agoric-cli/lib/cosmos.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import { makePspawn } from './helpers';
import { makePspawn } from './helpers.js';

export default async function cosmosMain(progname, rawArgs, powers, opts) {
const IMAGE = `agoric/agoric-sdk`;
Expand Down
13 changes: 10 additions & 3 deletions packages/agoric-cli/lib/deploy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* global require process setTimeout setInterval clearInterval */
/* global process setTimeout setInterval clearInterval */
/* eslint-disable no-await-in-loop */

import { E, makeCapTP } from '@agoric/captp';
import { makePromiseKit } from '@agoric/promise-kit';
import bundleSource from '@agoric/bundle-source';
import path from 'path';
import inquirer from 'inquirer';
import createRequire from 'esm';

import { getAccessToken } from '@agoric/access-token';

const require = createRequire({});

// note: CapTP has its own HandledPromise instantiation, and the contract
// must use the same one that CapTP uses. We achieve this by not bundling
// captp, and doing a (non-isolated) dynamic import of the deploy script
Expand Down Expand Up @@ -220,8 +224,11 @@ export { bootPlugin } from ${JSON.stringify(absPath)};
}

// use a dynamic import to load the deploy script, it is unconfined
// eslint-disable-next-line import/no-dynamic-require,global-require
const mainNS = require(pathResolve(moduleFile));
// eslint-disable-next-line import/no-dynamic-require
const mainNS = require(moduleFile);
// TODO Node.js ESM support if package.json of template says "type":
// "module":
// const mainNS = await import(pathResolve(moduleFile));
const main = mainNS.default;
if (typeof main !== 'function') {
console.error(
Expand Down
9 changes: 6 additions & 3 deletions packages/agoric-cli/lib/entrypoint.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env -S node

/* global process */
// TODO Remove babel-standalone preinitialization
// TODO Remove babel-standalone and esm preinitialization
// https://github.com/endojs/endo/issues/768
import 'esm';
import '@agoric/babel-standalone';
import '@agoric/install-ses';

Expand All @@ -11,10 +14,10 @@ import rawFs from 'fs';
import os from 'os';

// Configure logs.
import './anylogger-agoric';
import './anylogger-agoric.js';
import anylogger from 'anylogger';

import main from './main';
import main from './main.js';

const fs = rawFs.promises;
const log = anylogger('agoric');
Expand Down
2 changes: 1 addition & 1 deletion packages/agoric-cli/lib/init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import { makePspawn } from './helpers';
import { makePspawn } from './helpers.js';

// Use either an absolute template URL, or find it relative to DAPP_URL_BASE.
const gitURL = (relativeOrAbsoluteURL, base) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/agoric-cli/lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* global __dirname process Buffer */
/* global process Buffer */
import path from 'path';
import chalk from 'chalk';
import { makePspawn } from './helpers';
import { makePspawn } from './helpers.js';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);

export default async function installMain(progname, rawArgs, powers, opts) {
const { anylogger, fs, spawn } = powers;
Expand Down Expand Up @@ -63,7 +66,7 @@ export default async function installMain(progname, rawArgs, powers, opts) {
log('removing', linkFolder);
await rimraf(linkFolder);

const sdkRoot = path.resolve(__dirname, `../../..`);
const sdkRoot = path.resolve(dirname, `../../..`);
const sdkDirs = await workspaceDirectories(sdkRoot);
await Promise.all(
sdkDirs.map(async location => {
Expand Down
23 changes: 13 additions & 10 deletions packages/agoric-cli/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
/* global __dirname process */
/* global process */
import { Command } from 'commander';

import path from 'path';
import { assert, details as X } from '@agoric/assert';
import cosmosMain from './cosmos';
import deployMain from './deploy';
import initMain from './init';
import installMain from './install';
import setDefaultsMain from './set-defaults';
import startMain from './start';
import walletMain from './open';
import cosmosMain from './cosmos.js';
import deployMain from './deploy.js';
import initMain from './init.js';
import installMain from './install.js';
import setDefaultsMain from './set-defaults.js';
import startMain from './start.js';
import walletMain from './open.js';

const DEFAULT_DAPP_TEMPLATE = 'dapp-fungible-faucet';
const DEFAULT_DAPP_URL_BASE = 'git://github.com/Agoric/';

const STAMP = '_agstate';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);

const main = async (progname, rawArgs, powers) => {
const { anylogger, fs } = powers;
const log = anylogger('agoric');
Expand All @@ -41,7 +44,7 @@ const main = async (progname, rawArgs, powers) => {

program.storeOptionsAsProperties(false);

const pj = await fs.readFile(`${__dirname}/../package.json`);
const pj = await fs.readFile(`${dirname}/../package.json`);
const pkg = JSON.parse(pj);
program.name(pkg.name).version(pkg.version);

Expand Down
2 changes: 1 addition & 1 deletion packages/agoric-cli/lib/set-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
finishCosmosApp,
finishTendermintConfig,
finishCosmosGenesis,
} from './chain-config';
} from './chain-config.js';

export default async function setDefaultsMain(progname, rawArgs, powers, opts) {
const { anylogger, fs } = powers;
Expand Down
11 changes: 7 additions & 4 deletions packages/agoric-cli/lib/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global __dirname process setTimeout */
/* global process setTimeout */
import path from 'path';
import chalk from 'chalk';
import { createHash } from 'crypto';
Expand All @@ -9,9 +9,12 @@ import {
finishTendermintConfig,
finishCosmosGenesis,
finishCosmosApp,
} from './chain-config';
} from './chain-config.js';

import { makePspawn } from './helpers';
import { makePspawn } from './helpers.js';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);

const PROVISION_COINS = `100000000${STAKING_DENOM},50000000000${CENTRAL_DENOM},100provisionpass,100sendpacketpass`;
const DELEGATE0_COINS = `50000000${STAKING_DENOM}`;
Expand Down Expand Up @@ -113,7 +116,7 @@ export default async function startMain(progname, rawArgs, powers, opts) {

let agSolo;
if (opts.sdk) {
agSolo = path.resolve(__dirname, '../../solo/src/entrypoint.js');
agSolo = path.resolve(dirname, '../../solo/src/entrypoint.js');
} else {
agSolo = `ag-solo`;
}
Expand Down
9 changes: 3 additions & 6 deletions packages/agoric-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "agoric",
"version": "0.13.11",
"description": "Manage the Agoric Javascript smart contract platform",
"parsers": {
"js": "mjs"
},
"type": "module",
"main": "lib/main.js",
"bin": "bin/agoric",
"files": [
Expand All @@ -14,6 +12,7 @@
"scripts": {
"build": "exit 0",
"test": "ava",
"test:c8": "c8 $C8_OPTIONS ava --config=ava-nesm.config.js",
"test:xs": "exit 0",
"integration-test": "ava --config .ava-integration-test.config.js",
"lint-check": "yarn lint",
Expand All @@ -23,6 +22,7 @@
"devDependencies": {
"@agoric/swingset-vat": "^0.19.0",
"ava": "^3.12.1",
"c8": "^7.7.2",
"tmp": "^0.1.0"
},
"dependencies": {
Expand Down Expand Up @@ -64,9 +64,6 @@
"files": [
"test/**/test-*.js"
],
"require": [
"esm"
],
"timeout": "2m"
},
"prettier": {
Expand Down
5 changes: 3 additions & 2 deletions packages/agoric-cli/test/test-main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* global globalThis */
// TODO Remove babel-standalone preinitialization
// TODO Remove babel-standalone and esm preinitialization
// https://github.com/endojs/endo/issues/768
import 'esm';
import '@agoric/babel-standalone';
import '@agoric/install-ses';
import test from 'ava';
import fs from 'fs';
import anylogger from 'anylogger';

import main from '../lib/main';
import main from '../lib/main.js';

test('sanity', async t => {
const stubAnylogger = () => {
Expand Down

0 comments on commit 99d8f6e

Please sign in to comment.