Skip to content

Commit

Permalink
Feature/convert to esm (#165)
Browse files Browse the repository at this point in the history
This updates the module system to ESM, and updates all the dependencies.

Some additional changes:
- __dirname global does not exist anymore in ESM, patched with file url
api
- max node version that can be safely used for now is v19 (v20 has
breaking change with loader modules that are used by ts-node)
- @types/node kept at v18 (no spec for v19)
- ./bin/dev.js does not work because of ts-node error, will create a
watcher and an npm script to rebuild and use ./bin/run.js instead (if
solution is not found)

This will be kept in draft until #164 is merged, and this rebased to
master.
  • Loading branch information
codespool committed May 30, 2023
1 parent 166c078 commit e823a13
Show file tree
Hide file tree
Showing 40 changed files with 976 additions and 877 deletions.
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ dist
node_modules
oclif.manifest.json

packages/cli/test_project/**/*
packages/cli/temp_project/**/*
packages/cli/temp_proj/**/*

packages/cli/tmp/**/*
test_project/**/*
temp_project/**/*
temp_proj/**/*
tmp/**/*
17 changes: 0 additions & 17 deletions bin/dev

This file was deleted.

6 changes: 6 additions & 0 deletions bin/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ts-node

import "@polkadot/api-augment";

const oclif = await import("@oclif/core");
await oclif.execute({ type: "esm", development: true, dir: import.meta.url });
5 changes: 0 additions & 5 deletions bin/run

This file was deleted.

4 changes: 4 additions & 0 deletions bin/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node

const oclif = await import("@oclif/core");
await oclif.execute({ type: "esm", dir: import.meta.url });
65 changes: 37 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"repository": "https://github.com/AstarNetwork/swanky-cli",
"homepage": "https://github.com/AstarNetwork/swanky-cli",
"bugs": "https://github.com/AstarNetwork/swanky-cli/issues",
"type": "module",
"bin": {
"swanky": "./bin/run"
"swanky": "./bin/run.js"
},
"main": "dist/index.js",
"files": [
Expand All @@ -21,61 +22,63 @@
"@iarna/toml": "^2.2.5",
"@oclif/core": "2.8.5",
"@oclif/plugin-help": "5.2.9",
"@oclif/plugin-plugins": "3.0.1",
"@oclif/plugin-plugins": "3.1.1",
"@oclif/plugin-version": "1.3.4",
"bn.js": "5.2.1",
"chalk": "4",
"chalk": "5.2.0",
"change-case": "4.1.2",
"decompress": "4.2.1",
"enquirer": "^2.3.6",
"execa": "5.1.1",
"fs-extra": "10.1.0",
"globby": "11",
"execa": "7.1.1",
"fs-extra": "11.1.1",
"globby": "^13.1.4",
"handlebars": "4.7.7",
"inquirer": "8.2.5",
"inquirer": "9.2.6",
"inquirer-fuzzy-path": "^2.3.0",
"listr2": "5.0.7",
"lodash": "^4.17.21",
"listr2": "6.6.0",
"lodash-es": "^4.17.21",
"mocha": "10.2.0",
"mocha-suppress-logs": "0.3.1",
"mochawesome": "7.1.3",
"node-downloader-helper": "2.1.4",
"ora": "5.4.1",
"semver": "7.3.8",
"node-downloader-helper": "2.1.7",
"ora": "6.3.1",
"semver": "7.5.1",
"shelljs": "0.8.5",
"ts-mocha": "^10.0.0"
},
"devDependencies": {
"@oclif/test": "2.3.9",
"@polkadot/api": "9.14.2",
"@polkadot/api-augment": "9.14.2",
"@polkadot/api-contract": "9.14.2",
"@polkadot/keyring": "10.4.2",
"@polkadot/types": "9.14.2",
"@polkadot/util": "10.4.2",
"@polkadot/util-crypto": "10.4.2",
"@oclif/test": "2.3.21",
"@polkadot/api": "10.7.2",
"@polkadot/api-augment": "10.7.2",
"@polkadot/api-contract": "10.7.2",
"@polkadot/keyring": "12.2.1",
"@polkadot/types": "10.7.2",
"@polkadot/util": "12.2.1",
"@polkadot/util-crypto": "12.2.1",
"@types/chai": "4",
"@types/decompress": "4.2.4",
"@types/fs-extra": "9.0.13",
"@types/fs-extra": "11.0.1",
"@types/iarna__toml": "^2.0.2",
"@types/inquirer": "8.2.5",
"@types/inquirer": "9.0.3",
"@types/inquirer-fuzzy-path": "^2.3.6",
"@types/mocha": "9.0.0",
"@types/lodash-es": "^4.17.7",
"@types/mocha": "10.0.1",
"@types/node": "^18",
"@types/semver": "7.3.10",
"@types/shelljs": "0.8.11",
"@types/semver": "7.5.0",
"@types/shelljs": "0.8.12",
"@typescript-eslint/eslint-plugin": "5.59.7",
"@typescript-eslint/parser": "5.59.7",
"chai": "4",
"eslint": "8.41.0",
"eslint-config-prettier": "8.8.0",
"eslint-plugin-prettier": "4.2.1",
"nodemon": "^2.0.22",
"oclif": "3.9.0",
"prettier": "2.8.8",
"shx": "0.3.4",
"ts-node": "10.9.1",
"tslib": "2.5.2",
"typescript": "4.9.5"
"typescript": "5.0.4"
},
"peerDependencies": {
"@polkadot/util": "10.4.2",
Expand Down Expand Up @@ -105,7 +108,6 @@
}
},
"scripts": {
"build": "shx rm -rf dist && rm -f tsconfig.tsbuildinfo && tsc -b && yarn copy-templates",
"lint": "eslint . --ext .ts --quiet --config .eslintrc",
"postpack": "shx rm -f oclif.manifest.json",
"prepack": "yarn build && oclif manifest && oclif readme",
Expand All @@ -114,7 +116,14 @@
"tarball:macos": "oclif pack tarballs --targets=darwin-x64 --no-xz",
"tarball:linux": "oclif pack tarballs --targets=linux-x64 --no-xz",
"tarball:all": "oclif pack tarballs --targets=darwin-x64,linux-x64 --no-xz",
"copy-templates": "cp -R ./src/templates ./dist/lib"
"build:clean": "shx rm -rf dist && rm -f tsconfig.tsbuildinfo",
"build:ts": "tsc -b",
"build:templates": "cp -R ./src/templates ./dist",
"build": "yarn build:clean && yarn build:ts && yarn build:templates",
"dev:ts": "tsc --watch",
"dev:templates": "nodemon --watch ./src/templates --ext '*' --exec yarn build:templates",
"dev": "yarn dev:ts & yarn dev:templates",
"dev:run": "./bin/run.js"
},
"engines": {
"node": ">=18.0.0"
Expand Down
8 changes: 4 additions & 4 deletions src/commands/account/create.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command, Flags } from "@oclif/core";
import chalk = require("chalk");
import { writeJSON } from "fs-extra";
import { ensureSwankyProject, getSwankyConfig, ChainAccount, encrypt } from "../../lib";
import { AccountData } from "../../types";
import chalk from "chalk";
import { writeJSON } from "fs-extra/esm";
import { ensureSwankyProject, getSwankyConfig, ChainAccount, encrypt } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";
import inquirer from "inquirer";
export class CreateAccount extends Command {
static description = "Create a new dev account in config";
Expand Down
6 changes: 3 additions & 3 deletions src/commands/account/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from "@oclif/core";
import chalk = require("chalk");
import { ensureSwankyProject, getSwankyConfig } from "../../lib";
import { AccountData } from "../../types";
import chalk from "chalk";
import { ensureSwankyProject, getSwankyConfig } from "../../lib/index.js";
import { AccountData } from "../../types/index.js";

export class CreateAccount extends Command {
static description = "List dev accounts stored in config";
Expand Down
22 changes: 11 additions & 11 deletions src/commands/check/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Command } from "@oclif/core";
import { Listr } from "listr2";
import { commandStdoutOrNull, ensureSwankyProject } from "../../lib";
import { SwankyConfig } from "../../types";
import fs = require("fs-extra");
import path = require("node:path");
import { commandStdoutOrNull, ensureSwankyProject } from "../../lib/index.js";
import { SwankyConfig } from "../../types/index.js";
import { pathExistsSync, readJSON } from "fs-extra/esm";
import { readFileSync } from "fs";
import path from "node:path";
import TOML from "@iarna/toml";
import semver = require("semver");
import semver from "semver";

interface Ctx {
versions: {
Expand Down Expand Up @@ -64,18 +65,17 @@ export default class Check extends Command {
{
title: "Read ink dependencies",
task: async (ctx) => {
const swankyConfig = await fs.readJSON("swanky.config.json");
const swankyConfig = await readJSON("swanky.config.json");
ctx.swankyConfig = swankyConfig;
const contractInkVersions = {};

for (const contract in swankyConfig.contracts) {
const tomlPath = path.resolve(`contracts/${contract}/Cargo.toml`);
const doesCargoTomlExist = fs.pathExistsSync(tomlPath);
const doesCargoTomlExist = pathExistsSync(tomlPath);
if (!doesCargoTomlExist) {
contractInkVersions[contract] = null;
continue;
}

const cargoTomlString = fs.readFileSync(tomlPath, {
const cargoTomlString = readFileSync(tomlPath, {
encoding: "utf8",
});

Expand All @@ -96,7 +96,7 @@ export default class Check extends Command {
task: async (ctx) => {
const supportedInk = ctx.swankyConfig?.node.supportedInk;

const mismatched = {};
const mismatched: { [contractVersion: string]: string } = {};
Object.entries(ctx.versions.contracts).forEach(([contract, inkPackages]) => {
Object.entries(inkPackages).forEach(([inkPackage, version]) => {
if (semver.gt(version, supportedInk as string)) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/contract/compile.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Args, Command, Flags } from "@oclif/core";
import path = require("node:path");
import path from "node:path";
import {
storeArtifacts,
ensureSwankyProject,
getSwankyConfig,
Spinner,
generateTypes,
} from "../../lib";
} from "../../lib/index.js";
import { spawn } from "node:child_process";
import { pathExists } from "fs-extra";
import { pathExists } from "fs-extra/esm";

export class CompileContract extends Command {
static description = "Compile the smart contract(s) in your contracts directory";
Expand Down
14 changes: 7 additions & 7 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Args, Command, Flags } from "@oclif/core";
import path = require("node:path");
import { writeJSON } from "fs-extra";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import path from "node:path";
import { writeJSON } from "fs-extra/esm";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";
import {
ensureSwankyProject,
getSwankyConfig,
Expand All @@ -11,11 +11,11 @@ import {
Spinner,
decrypt,
AbiType,
} from "../../lib";
import { AccountData, Encrypted } from "../../types";
} from "../../lib/index.js";
import { AccountData, Encrypted } from "../../types/index.js";
import inquirer from "inquirer";
import chalk = require("chalk");
import { Contract } from "../../lib/contract";
import chalk from "chalk";
import { Contract } from "../../lib/contract.js";

export class DeployContract extends Command {
static description = "Deploy contract to a running node";
Expand Down
10 changes: 5 additions & 5 deletions src/commands/contract/explain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseCommand } from "../../lib/baseCommand";
import * as fs from "fs-extra";
import path = require("node:path");
import { BaseCommand } from "../../lib/baseCommand.js";
import { pathExistsSync } from "fs-extra/esm";
import path from "node:path";
import { readdirSync } from "node:fs";
import { printContractInfo } from "../../lib";
import { printContractInfo } from "../../lib/index.js";
import { Args } from "@oclif/core";

export class ExplainContract extends BaseCommand {
Expand Down Expand Up @@ -39,7 +39,7 @@ export class ExplainContract extends BaseCommand {
contractInfo.build?.artifactsPath,
`${args.contractName}.json`
);
if (!fs.existsSync(metadataPath)) {
if (!pathExistsSync(metadataPath)) {
this.error(`Metadata json file for ${args.contractName} contract not found`);
}

Expand Down
16 changes: 8 additions & 8 deletions src/commands/contract/new.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Args, Command, Flags } from "@oclif/core";
import path = require("node:path");
import { ensureDir, pathExists, writeJSON } from "fs-extra";
import path from "node:path";
import { ensureDir, pathExists, writeJSON } from "fs-extra/esm";
import {
getSwankyConfig,
ensureSwankyProject,
Spinner,
checkCliDependencies,
copyContractTemplateFiles,
processTemplates,
} from "../../lib";
import { getTemplates } from "../../lib";
import { email, name, pickTemplate } from "../../lib/prompts";
getTemplates,
} from "../../lib/index.js";
import { email, name, pickTemplate } from "../../lib/prompts.js";
import { paramCase, pascalCase, snakeCase } from "change-case";
import execa = require("execa");
import inquirer = require("inquirer");
import { execaCommandSync } from "execa";
import inquirer from "inquirer";

export class NewContract extends Command {
static description = "Generate a new smart contract template inside a project";
Expand Down Expand Up @@ -60,7 +60,7 @@ export class NewContract extends Command {
const questions = [
name(
"author",
() => execa.commandSync("git config --get user.name").stdout,
() => execaCommandSync("git config --get user.name").stdout,
"What is your name?"
),
email(),
Expand Down
4 changes: 2 additions & 2 deletions src/commands/contract/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContractPromise } from "@polkadot/api-contract";
import { ContractCall } from "../../lib/contractCall";
import { ContractPromise } from "@polkadot/api-contract/promise";
import { ContractCall } from "../../lib/contractCall.js";

export class Query extends ContractCall<typeof Query> {
static summary = "Call a query message on smart contract";
Expand Down
14 changes: 7 additions & 7 deletions src/commands/contract/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require("ts-mocha");
import "ts-mocha";
import { Command, Flags, Args } from "@oclif/core";
import path = require("node:path");
import { ensureSwankyProject, getSwankyConfig } from "../../lib";
import globby from "globby";
import path from "node:path";
import { ensureSwankyProject, getSwankyConfig } from "../../lib/index.js";
import { globby } from "globby";
import Mocha from "mocha";
import { emptyDir } from "fs-extra";
import * as shell from "shelljs";
import { Contract } from "../../lib/contract";
import { emptyDir } from "fs-extra/esm";
import shell from "shelljs";
import { Contract } from "../../lib/contract.js";

declare global {
var contractTypesPath: string; // eslint-disable-line no-var
Expand Down
6 changes: 3 additions & 3 deletions src/commands/contract/tx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Flags } from "@oclif/core";
import { ContractPromise } from "@polkadot/api-contract";
import { cryptoWaitReady } from "@polkadot/util-crypto";
import { ContractPromise } from "@polkadot/api-contract/promise";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";

import { ContractCall } from "../../lib/contractCall";
import { ContractCall } from "../../lib/contractCall.js";

export class Tx extends ContractCall<typeof Tx> {
static summary = "Call a Tx message on smart contract";
Expand Down
Loading

0 comments on commit e823a13

Please sign in to comment.