Skip to content

Commit

Permalink
Refactor path adaptation (#317)
Browse files Browse the repository at this point in the history
* [skip ci]
  • Loading branch information
Nathan-SL authored Feb 22, 2023
1 parent 67f5ff4 commit f8e373c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
18 changes: 3 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
let newPath: string;
if (userConfig.paths && userConfig.paths.starknetSources) {
const userPath = userConfig.paths.starknetSources;
if (path.isAbsolute(userPath)) {
newPath = userPath;
} else {
newPath = path.normalize(path.join(config.paths.root, userPath));
}
newPath = adaptPath(config.paths.root, userPath);
config.paths.starknetSources = userConfig.paths.starknetSources;
} else {
const defaultPath = path.join(config.paths.root, DEFAULT_STARKNET_SOURCES_PATH);
Expand All @@ -107,11 +103,7 @@ extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) =>
let newPath: string;
if (userConfig.paths && userConfig.paths.starknetArtifacts) {
const userPath = userConfig.paths.starknetArtifacts;
if (path.isAbsolute(userPath)) {
newPath = userPath;
} else {
newPath = path.normalize(path.join(config.paths.root, userPath));
}
newPath = adaptPath(config.paths.root, userPath);
config.paths.starknetArtifacts = userConfig.paths.starknetArtifacts;
} else {
const defaultPath = path.join(config.paths.root, DEFAULT_STARKNET_ARTIFACTS_PATH);
Expand Down Expand Up @@ -207,11 +199,7 @@ extendEnvironment((hre) => {
const accountPaths = extractAccountPaths(hre);
const cairoPaths = [];
for (const cairoPath of hre.config.paths.cairoPaths || []) {
if (!path.isAbsolute(cairoPath)) {
cairoPaths.push(adaptPath(hre.config.paths.root, cairoPath));
} else {
cairoPaths.push(cairoPath);
}
cairoPaths.push(adaptPath(hre.config.paths.root, cairoPath));
}

hre.starknetWrapper = new DockerWrapper(
Expand Down
17 changes: 7 additions & 10 deletions src/task-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
getNetwork,
getAccountPath,
isStarknetDevnet,
warn
warn,
adaptPath
} from "./utils";
import {
HardhatNetworkConfig,
Expand Down Expand Up @@ -94,16 +95,14 @@ export async function starknetCompileAction(args: TaskArguments, hre: HardhatRun
}
for (let i = 0; i < cairoPaths.length; i++) {
if (!path.isAbsolute(cairoPaths[i])) {
cairoPaths[i] = path.normalize(path.join(root, cairoPaths[i]));
cairoPaths[i] = adaptPath(root, cairoPaths[i]);
}
}

const cairoPath = cairoPaths.join(":");
let statusCode = 0;
for (let sourcesPath of sourcesPaths) {
if (!path.isAbsolute(sourcesPath)) {
sourcesPath = path.normalize(path.join(root, sourcesPath));
}
sourcesPath = adaptPath(root, sourcesPath);
checkSourceExists(sourcesPath);
const files = await traverseFiles(sourcesPath, "*.cairo");
const recompiler = new Recompiler(hre);
Expand Down Expand Up @@ -199,7 +198,7 @@ export async function starknetVoyagerAction(args: TaskArguments, hre: HardhatRun

function getMainVerificationPath(contractPath: string, root: string) {
if (!path.isAbsolute(contractPath)) {
contractPath = path.normalize(path.join(root, contractPath));
contractPath = adaptPath(root, contractPath);
if (!fs.existsSync(contractPath)) {
throw new StarknetPluginError(`File ${contractPath} does not exist`);
}
Expand Down Expand Up @@ -268,7 +267,7 @@ function handleMultiPartContractVerification(
) {
paths.forEach(function (item: string, index: number) {
if (!path.isAbsolute(item)) {
paths[index] = path.normalize(path.join(root, item));
paths[index] = adaptPath(root, item);
if (!fs.existsSync(paths[index])) {
throw new StarknetPluginError(`File ${paths[index]} does not exist`);
}
Expand Down Expand Up @@ -426,9 +425,7 @@ export async function starknetMigrateAction(args: TaskArguments, hre: HardhatRun
const files: string[] = args.paths || [defaultSourcesPath];
const cairoFiles: string[] = [];
for (let file of files) {
if (!path.isAbsolute(file)) {
file = path.normalize(path.join(root, file));
}
file = adaptPath(root, file);
cairoFiles.push(file);
}

Expand Down
15 changes: 14 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,21 @@ export function getArtifactPath(sourcePath: string, paths: ProjectPathsConfig):
return path.join(paths.starknetArtifacts, suffix);
}

/**
* Adapts path relative to the root of the project and
* tilde will be resolved to homedir
* @param root string representing the root path set on hre or config
* @param newPath string representing the path provided by the user
* @returns adapted path
*/
export function adaptPath(root: string, newPath: string): string {
return path.normalize(path.join(root, newPath));
let adaptedPath = newPath;
if (newPath[0] === "~") {
adaptedPath = path.normalize(path.join(process.env.HOME, newPath.slice(1)));
} else if (!path.isAbsolute(newPath)) {
adaptedPath = path.normalize(path.join(root, newPath));
}
return adaptedPath;
}

export function checkArtifactExists(artifactsPath: string): void {
Expand Down
2 changes: 1 addition & 1 deletion test/configuration-tests/with-cairo-path/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
network: process.env.NETWORK
},
paths: {
cairoPaths: ["./new-sources"]
cairoPaths: ["./new-sources", "~/another-source"]
},
networks: {
devnet: {
Expand Down
2 changes: 2 additions & 0 deletions www/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ module.exports = {
### Paths
Prefer providing absolute paths when using the plugin. If a relative path is provided, it will be resolved relative to the root of the project.
```typescript
module.exports = {
paths: {
Expand Down

0 comments on commit f8e373c

Please sign in to comment.