-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature run scripts #148
Feature run scripts #148
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { BaseCommand } from "../../lib/baseCommand"; | ||
import { Args } from "@oclif/core"; | ||
import { ensureSwankyProject } from "@astar-network/swanky-core"; | ||
import { existsSync } from "fs-extra"; | ||
import { fork } from "child_process"; | ||
import path = require("node:path"); | ||
|
||
export class RunCommand extends BaseCommand<typeof RunCommand> { | ||
static description = "Run a user-defined scripts"; | ||
|
||
static args = { | ||
scriptName: Args.string({ | ||
name: "scriptName", | ||
required: true, | ||
description: "Name of the script to run", | ||
}), | ||
}; | ||
|
||
async run(): Promise<void> { | ||
await ensureSwankyProject(); | ||
|
||
const { args } = await this.parse(RunCommand); | ||
|
||
let scriptName = args.scriptName; | ||
if (!scriptName.endsWith(".ts")) { | ||
scriptName += ".ts"; | ||
} | ||
|
||
const scriptPath = path.resolve("scripts", scriptName); | ||
if (!existsSync(scriptPath)) { | ||
throw new Error(`Script ${args.scriptName} does not exist`) | ||
} | ||
|
||
await new Promise((resolve, reject) => { | ||
const childProcess = fork(scriptPath, [], { | ||
stdio: "inherit", | ||
execArgv: ["--require", "ts-node/register/transpile-only"], | ||
env: { ...process.env }, | ||
}); | ||
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered dynamically loading and executing the script instead of running it in a separate process? |
||
|
||
childProcess.once("close", (status) => { | ||
this.log(`Script ${scriptPath} exited with status code ${status ?? "null"}`); | ||
|
||
resolve(status as number); | ||
}); | ||
childProcess.once("error", reject); | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ApiPromise, WsProvider } from "@polkadot/api"; | ||
|
||
// `swanky script run 00_deploy` will run this script. | ||
|
||
// User-defined script to run. | ||
// This is just an deploy contract example, you can change it freely. | ||
|
||
import { | ||
getSwankyConfig, | ||
AccountData, | ||
ChainAccount, | ||
Encrypted, | ||
decrypt, | ||
resolveNetworkUrl | ||
} from "@astar-network/swanky-core"; | ||
import FlipperFactory from "../typedContracts/flipper/constructors/flipper"; | ||
import Flipper from "../typedContracts/flipper/contracts/flipper"; | ||
import inquirer from "inquirer"; | ||
import chalk from "chalk"; | ||
|
||
// Change account alias to use | ||
const accountName = "alice"; | ||
|
||
// Change network name to deploy to | ||
const networkName = "local"; | ||
|
||
async function main() { | ||
const config = await getSwankyConfig(); | ||
|
||
// Keyring settings | ||
const accountData = config.accounts.find( | ||
(account: AccountData) => account.alias === accountName | ||
); | ||
if (!accountData) { | ||
throw new Error("Provided account alias not found in swanky.config.json"); | ||
} | ||
|
||
const mnemonic = accountData.isDev | ||
? (accountData.mnemonic as string) | ||
: decrypt( | ||
accountData.mnemonic as Encrypted, | ||
( | ||
await inquirer.prompt([ | ||
{ | ||
type: "password", | ||
message: `Enter password for ${chalk.yellowBright(accountData.alias)}: `, | ||
name: "password", | ||
}, | ||
]) | ||
).password | ||
); | ||
|
||
const deployer = new ChainAccount(mnemonic).pair; | ||
|
||
// Network settings | ||
const networkUrl = resolveNetworkUrl(config, networkName ?? ""); | ||
const wsProvider = new WsProvider(networkUrl); | ||
const api = await ApiPromise.create({ provider: wsProvider }); | ||
|
||
// Deploy flipper contract whose initial state is set to `true`. | ||
const flipperFactory = new FlipperFactory(api, deployer); | ||
const initialState = true; | ||
|
||
const contract = new Flipper( | ||
(await flipperFactory.new(initialState)).address, | ||
deployer, | ||
api | ||
); | ||
|
||
console.log(`Flipper with initial state \`true\` deployed to ${contract.address}`); | ||
|
||
await api.disconnect(); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exitCode = 1; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having both
script
andrun
looks redundant.Why not use just
run
, so it's more in line with convention in f.ex. npm (swanky run deploy
)?