@nomicfoundation/hardhat-foundry
Advanced tools
Comparing version 1.0.2-dev.0 to 1.0.2
@@ -6,13 +6,6 @@ import { NomicLabsHardhatPluginError } from "hardhat/internal/core/errors"; | ||
} | ||
export declare class FoundryRunner { | ||
private _forgeCommand; | ||
private static _instance; | ||
private _cachedRemappings; | ||
private constructor(); | ||
static create(): FoundryRunner; | ||
getForgeConfig(): any; | ||
getRemappings(): Promise<Remappings>; | ||
installDependency(dependency: string): Promise<void>; | ||
} | ||
export declare function getForgeConfig(): any; | ||
export declare function getRemappings(): Promise<Remappings>; | ||
export declare function installDependency(dependency: string): Promise<void>; | ||
export {}; | ||
//# sourceMappingURL=foundry.d.ts.map |
@@ -6,9 +6,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FoundryRunner = exports.HardhatFoundryError = void 0; | ||
exports.installDependency = exports.getRemappings = exports.getForgeConfig = exports.HardhatFoundryError = void 0; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
const child_process_1 = require("child_process"); | ||
const errors_1 = require("hardhat/internal/core/errors"); | ||
const os_1 = __importDefault(require("os")); | ||
const util_1 = require("util"); | ||
const exec = (0, util_1.promisify)(child_process_1.exec); | ||
let cachedRemappings; | ||
class HardhatFoundryError extends errors_1.NomicLabsHardhatPluginError { | ||
@@ -28,87 +28,41 @@ constructor(message, parent) { | ||
} | ||
function runningOnWindows() { | ||
return os_1.default.platform() === "win32"; | ||
function getForgeConfig() { | ||
return JSON.parse(runCmdSync("forge config --json")); | ||
} | ||
class FoundryRunner { | ||
constructor(_forgeCommand) { | ||
this._forgeCommand = _forgeCommand; | ||
} | ||
static create() { | ||
if (FoundryRunner._instance !== undefined) { | ||
return FoundryRunner._instance; | ||
} | ||
const potentialForgeCommands = ["forge"]; | ||
if (runningOnWindows()) { | ||
potentialForgeCommands.push("%USERPROFILE%\\.cargo\\bin\\forge"); | ||
} | ||
else { | ||
potentialForgeCommands.push("~/.foundry/bin/forge"); | ||
} | ||
console.log("[Issue 449] Potential forge commands:", potentialForgeCommands); | ||
for (const potentialForgeCommand of potentialForgeCommands) { | ||
try { | ||
(0, child_process_1.execSync)(`${potentialForgeCommand} --version`, { | ||
stdio: "pipe", | ||
}).toString(); | ||
FoundryRunner._instance = new FoundryRunner(potentialForgeCommand); | ||
console.log(`[Issue 449] '${potentialForgeCommand}' worked`); | ||
return FoundryRunner._instance; | ||
} | ||
catch (error) { | ||
console.log(`[Issue 449] '${potentialForgeCommand}' didn't work:`, error.message, `(code: ${error.status}})`); | ||
if (error.status === 127 || // unix | ||
error.toString().includes("is not recognized") === true || // windows (code: 1) | ||
error.toString().includes("cannot find the path") === true // windows (code: 1) | ||
) { | ||
// command not found, then try the next potential command | ||
exports.getForgeConfig = getForgeConfig; | ||
async function getRemappings() { | ||
// Get remappings only once | ||
if (cachedRemappings === undefined) { | ||
cachedRemappings = runCmd("forge remappings").then((remappingsTxt) => { | ||
const remappings = {}; | ||
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/); | ||
for (const remappingLine of remappingLines) { | ||
const fromTo = remappingLine.split("="); | ||
if (fromTo.length !== 2) { | ||
continue; | ||
} | ||
else { | ||
// command found but execution failed | ||
throw error; | ||
const [from, to] = fromTo; | ||
// source names with "node_modules" in it have special treatment in hardhat core, so we skip them | ||
if (to.includes("node_modules")) { | ||
continue; | ||
} | ||
remappings[from] = to; | ||
} | ||
} | ||
console.log("[Issue 449] No command worked"); | ||
throw new HardhatFoundryError(`Couldn't find forge binary. Performed lookup: ${JSON.stringify(potentialForgeCommands)}`); | ||
return remappings; | ||
}); | ||
} | ||
getForgeConfig() { | ||
return JSON.parse(runCmdSync(`${this._forgeCommand} config --json`)); | ||
return cachedRemappings; | ||
} | ||
exports.getRemappings = getRemappings; | ||
async function installDependency(dependency) { | ||
const cmd = `forge install --no-commit ${dependency}`; | ||
console.log(`Running '${chalk_1.default.blue(cmd)}'`); | ||
try { | ||
await exec(cmd); | ||
} | ||
async getRemappings() { | ||
// Return remappings if they were already loaded | ||
if (this._cachedRemappings !== undefined) { | ||
return this._cachedRemappings; | ||
} | ||
// Get remappings from foundry | ||
const remappingsTxt = await runCmd(`${this._forgeCommand} remappings`); | ||
const remappings = {}; | ||
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/); | ||
for (const remappingLine of remappingLines) { | ||
const fromTo = remappingLine.split("="); | ||
if (fromTo.length !== 2) { | ||
continue; | ||
} | ||
const [from, to] = fromTo; | ||
// source names with "node_modules" in it have special treatment in hardhat core, so we skip them | ||
if (to.includes("node_modules")) { | ||
continue; | ||
} | ||
remappings[from] = to; | ||
} | ||
this._cachedRemappings = remappings; | ||
return remappings; | ||
catch (error) { | ||
throw new ForgeInstallError(dependency, error); | ||
} | ||
async installDependency(dependency) { | ||
const cmd = `${this._forgeCommand} install --no-commit ${dependency}`; | ||
console.log(`Running '${chalk_1.default.blue(cmd)}'`); | ||
try { | ||
await exec(cmd); | ||
} | ||
catch (error) { | ||
throw new ForgeInstallError(dependency, error); | ||
} | ||
} | ||
} | ||
exports.FoundryRunner = FoundryRunner; | ||
exports.installDependency = installDependency; | ||
function runCmdSync(cmd) { | ||
@@ -115,0 +69,0 @@ try { |
@@ -23,4 +23,3 @@ "use strict"; | ||
// Load foundry config | ||
const foundryRunner = foundry_1.FoundryRunner.create(); | ||
const foundryConfig = foundryRunner.getForgeConfig(); | ||
const foundryConfig = (0, foundry_1.getForgeConfig)(); | ||
// Ensure required keys exist | ||
@@ -52,4 +51,3 @@ if (foundryConfig?.src === undefined || | ||
} | ||
const foundryRunner = foundry_1.FoundryRunner.create(); | ||
const remappings = await foundryRunner.getRemappings(); | ||
const remappings = await (0, foundry_1.getRemappings)(); | ||
for (const [from, to] of Object.entries(remappings)) { | ||
@@ -63,3 +61,3 @@ if (importName.startsWith(from) && !importName.startsWith(".")) { | ||
// Task that includes the remappings in solc input | ||
(0, config_1.internalTask)(task_names_1.TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async ({ dependencyGraph, file, }, _hre, runSuper) => { | ||
(0, config_1.internalTask)(task_names_1.TASK_COMPILE_SOLIDITY_GET_COMPILATION_JOB_FOR_FILE).setAction(async ({ dependencyGraph, file, }, hre, runSuper) => { | ||
const job = (await runSuper({ dependencyGraph, file })); | ||
@@ -69,4 +67,3 @@ if (!pluginActivated || isCompilationJobCreationError(job)) { | ||
} | ||
const foundryRunner = foundry_1.FoundryRunner.create(); | ||
const remappings = await foundryRunner.getRemappings(); | ||
const remappings = await (0, foundry_1.getRemappings)(); | ||
job.getSolcConfig().settings.remappings = Object.entries(remappings).map(([from, to]) => `${from}=${to}`); | ||
@@ -90,4 +87,3 @@ return job; | ||
].join("\n")); | ||
const foundryRunner = foundry_1.FoundryRunner.create(); | ||
await foundryRunner.installDependency("foundry-rs/forge-std"); | ||
await (0, foundry_1.installDependency)("foundry-rs/forge-std"); | ||
}); | ||
@@ -94,0 +90,0 @@ function isCompilationJobCreationError(x) { |
{ | ||
"name": "@nomicfoundation/hardhat-foundry", | ||
"version": "1.0.2-dev.0", | ||
"version": "1.0.2", | ||
"description": "Hardhat plugin that adds Hardhat support to Foundry projects", | ||
@@ -5,0 +5,0 @@ "repository": "github:nomicfoundation/hardhat", |
import chalk from "chalk"; | ||
import { exec as execCallback, execSync } from "child_process"; | ||
import { NomicLabsHardhatPluginError } from "hardhat/internal/core/errors"; | ||
import os from "os"; | ||
import { promisify } from "util"; | ||
@@ -11,2 +10,4 @@ | ||
let cachedRemappings: Promise<Remappings> | undefined; | ||
export class HardhatFoundryError extends NomicLabsHardhatPluginError { | ||
@@ -30,112 +31,44 @@ constructor(message: string, parent?: Error) { | ||
function runningOnWindows() { | ||
return os.platform() === "win32"; | ||
export function getForgeConfig() { | ||
return JSON.parse(runCmdSync("forge config --json")); | ||
} | ||
export class FoundryRunner { | ||
private static _instance: FoundryRunner | undefined; | ||
private _cachedRemappings: Remappings | undefined; | ||
export async function getRemappings() { | ||
// Get remappings only once | ||
if (cachedRemappings === undefined) { | ||
cachedRemappings = runCmd("forge remappings").then((remappingsTxt) => { | ||
const remappings: Remappings = {}; | ||
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/); | ||
for (const remappingLine of remappingLines) { | ||
const fromTo = remappingLine.split("="); | ||
if (fromTo.length !== 2) { | ||
continue; | ||
} | ||
private constructor(private _forgeCommand: string) {} | ||
const [from, to] = fromTo; | ||
public static create() { | ||
if (FoundryRunner._instance !== undefined) { | ||
return FoundryRunner._instance; | ||
} | ||
const potentialForgeCommands = ["forge"]; | ||
if (runningOnWindows()) { | ||
potentialForgeCommands.push("%USERPROFILE%\\.cargo\\bin\\forge"); | ||
} else { | ||
potentialForgeCommands.push("~/.foundry/bin/forge"); | ||
} | ||
console.log( | ||
"[Issue 449] Potential forge commands:", | ||
potentialForgeCommands | ||
); | ||
for (const potentialForgeCommand of potentialForgeCommands) { | ||
try { | ||
execSync(`${potentialForgeCommand} --version`, { | ||
stdio: "pipe", | ||
}).toString(); | ||
FoundryRunner._instance = new FoundryRunner(potentialForgeCommand); | ||
console.log(`[Issue 449] '${potentialForgeCommand}' worked`); | ||
return FoundryRunner._instance; | ||
} catch (error: any) { | ||
console.log( | ||
`[Issue 449] '${potentialForgeCommand}' didn't work:`, | ||
error.message, | ||
`(code: ${error.status}})` | ||
); | ||
if ( | ||
error.status === 127 || // unix | ||
error.toString().includes("is not recognized") === true || // windows (code: 1) | ||
error.toString().includes("cannot find the path") === true // windows (code: 1) | ||
) { | ||
// command not found, then try the next potential command | ||
// source names with "node_modules" in it have special treatment in hardhat core, so we skip them | ||
if (to.includes("node_modules")) { | ||
continue; | ||
} else { | ||
// command found but execution failed | ||
throw error; | ||
} | ||
remappings[from] = to; | ||
} | ||
} | ||
console.log("[Issue 449] No command worked"); | ||
throw new HardhatFoundryError( | ||
`Couldn't find forge binary. Performed lookup: ${JSON.stringify( | ||
potentialForgeCommands | ||
)}` | ||
); | ||
return remappings; | ||
}); | ||
} | ||
public getForgeConfig() { | ||
return JSON.parse(runCmdSync(`${this._forgeCommand} config --json`)); | ||
} | ||
return cachedRemappings; | ||
} | ||
public async getRemappings() { | ||
// Return remappings if they were already loaded | ||
if (this._cachedRemappings !== undefined) { | ||
return this._cachedRemappings; | ||
} | ||
export async function installDependency(dependency: string) { | ||
const cmd = `forge install --no-commit ${dependency}`; | ||
console.log(`Running '${chalk.blue(cmd)}'`); | ||
// Get remappings from foundry | ||
const remappingsTxt = await runCmd(`${this._forgeCommand} remappings`); | ||
const remappings: Remappings = {}; | ||
const remappingLines = remappingsTxt.split(/\r\n|\r|\n/); | ||
for (const remappingLine of remappingLines) { | ||
const fromTo = remappingLine.split("="); | ||
if (fromTo.length !== 2) { | ||
continue; | ||
} | ||
const [from, to] = fromTo; | ||
// source names with "node_modules" in it have special treatment in hardhat core, so we skip them | ||
if (to.includes("node_modules")) { | ||
continue; | ||
} | ||
remappings[from] = to; | ||
} | ||
this._cachedRemappings = remappings; | ||
return remappings; | ||
try { | ||
await exec(cmd); | ||
} catch (error: any) { | ||
throw new ForgeInstallError(dependency, error); | ||
} | ||
public async installDependency(dependency: string) { | ||
const cmd = `${this._forgeCommand} install --no-commit ${dependency}`; | ||
console.log(`Running '${chalk.blue(cmd)}'`); | ||
try { | ||
await exec(cmd); | ||
} catch (error: any) { | ||
throw new ForgeInstallError(dependency, error); | ||
} | ||
} | ||
} | ||
@@ -142,0 +75,0 @@ |
@@ -18,3 +18,8 @@ import { extendConfig, internalTask, task } from "hardhat/config"; | ||
import chalk from "chalk"; | ||
import { FoundryRunner, HardhatFoundryError } from "./foundry"; | ||
import { | ||
getForgeConfig, | ||
getRemappings, | ||
HardhatFoundryError, | ||
installDependency, | ||
} from "./foundry"; | ||
@@ -39,4 +44,3 @@ const TASK_INIT_FOUNDRY = "init-foundry"; | ||
// Load foundry config | ||
const foundryRunner = FoundryRunner.create(); | ||
const foundryConfig = foundryRunner.getForgeConfig(); | ||
const foundryConfig = getForgeConfig(); | ||
@@ -92,4 +96,3 @@ // Ensure required keys exist | ||
const foundryRunner = FoundryRunner.create(); | ||
const remappings = await foundryRunner.getRemappings(); | ||
const remappings = await getRemappings(); | ||
@@ -117,3 +120,3 @@ for (const [from, to] of Object.entries(remappings)) { | ||
}, | ||
_hre, | ||
hre, | ||
runSuper | ||
@@ -129,4 +132,3 @@ ): Promise<CompilationJob | CompilationJobCreationError> => { | ||
const foundryRunner = FoundryRunner.create(); | ||
const remappings = await foundryRunner.getRemappings(); | ||
const remappings = await getRemappings(); | ||
job.getSolcConfig().settings.remappings = Object.entries(remappings).map( | ||
@@ -174,4 +176,3 @@ ([from, to]) => `${from}=${to}` | ||
const foundryRunner = FoundryRunner.create(); | ||
await foundryRunner.installDependency("foundry-rs/forge-std"); | ||
await installDependency("foundry-rs/forge-std"); | ||
} | ||
@@ -178,0 +179,0 @@ ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
26455
438