-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core-cli): read token and network from config.json (#4208)
- Loading branch information
1 parent
9eb1ce9
commit 49e89a8
Showing
11 changed files
with
154 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Console } from "@arkecosystem/core-test-framework"; | ||
import { DiscoverConfig } from "@packages/core-cli/src/commands"; | ||
import { ensureDirSync, writeJSON } from "fs-extra"; | ||
import { join } from "path"; | ||
import { dirSync, setGracefulCleanup } from "tmp"; | ||
|
||
let cli; | ||
let cmd; | ||
let configPath; | ||
const config = { token: "token", network: "testnet" }; | ||
|
||
jest.mock("env-paths", () => () => ({ | ||
config: configPath, | ||
})); | ||
|
||
beforeAll(() => setGracefulCleanup()); | ||
|
||
beforeEach(() => { | ||
cli = new Console(); | ||
|
||
cmd = cli.app.resolve(DiscoverConfig); | ||
|
||
configPath = join(dirSync().name, "token-core"); | ||
}); | ||
|
||
describe("DiscoverConfig", () => { | ||
it("should return undefined if configuration can't be found", async () => { | ||
await expect(cmd.discover()).resolves.toEqual(undefined); | ||
}); | ||
|
||
it("should return configuration if found on default config location", async () => { | ||
ensureDirSync(join(configPath, "testnet")); | ||
|
||
await writeJSON(join(configPath, "testnet", "config.json"), config); | ||
|
||
await expect(cmd.discover("token", "testnet")).resolves.toEqual(config); | ||
}); | ||
|
||
it("should return configuration if found on CORE_PATH_CONFIG location", async () => { | ||
process.env.CORE_PATH_CONFIG = join(configPath, "testnet"); | ||
|
||
ensureDirSync(join(configPath, "testnet")); | ||
|
||
await writeJSON(join(configPath, "testnet", "config.json"), config); | ||
|
||
await expect(cmd.discover()).resolves.toEqual(config); | ||
|
||
delete process.env.CORE_PATH_CONFIG; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import envPaths from "env-paths"; | ||
import { readJSON } from "fs-extra"; | ||
import { join } from "path"; | ||
|
||
import { injectable } from "../ioc"; | ||
|
||
interface Config { | ||
token: string; | ||
network: string; | ||
} | ||
|
||
/** | ||
* @export | ||
* @class DiscoverNetwork | ||
*/ | ||
@injectable() | ||
export class DiscoverConfig { | ||
/** | ||
* @returns {Promise<string>} | ||
* @memberof DiscoverNetwork | ||
* @param token | ||
* @param network | ||
*/ | ||
public async discover(token: string = "", network: string = ""): Promise<Config | undefined> { | ||
try { | ||
return await readJSON(join(process.env.CORE_PATH_CONFIG!, "config.json")); | ||
} catch {} | ||
|
||
try { | ||
return await readJSON(join(envPaths(token, { suffix: "core" }).config, network, "config.json")); | ||
} catch {} | ||
|
||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./command-help"; | ||
export * from "./command"; | ||
export * from "./discover-commands"; | ||
export * from "./discover-config"; | ||
export * from "./discover-network"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"token": "ark", | ||
"network": "devnet" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"token": "ark", | ||
"network": "mainnet" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"token": "ark", | ||
"network": "testnet" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters