ts: Refactor workspace loading (#407)
This commit is contained in:
parent
f9c0822710
commit
619015d2ed
|
@ -15,7 +15,6 @@ let _populatedWorkspace = false;
|
||||||
*/
|
*/
|
||||||
const workspace = new Proxy({} as any, {
|
const workspace = new Proxy({} as any, {
|
||||||
get(workspaceCache: { [key: string]: Program }, programName: string) {
|
get(workspaceCache: { [key: string]: Program }, programName: string) {
|
||||||
const find = require("find");
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const process = require("process");
|
const process = require("process");
|
||||||
|
|
||||||
|
@ -43,23 +42,25 @@ const workspace = new Proxy({} as any, {
|
||||||
throw new Error("Could not find workspace root.");
|
throw new Error("Could not find workspace root.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const idlMap = new Map<string, Idl>();
|
const idlFolder = `${projectRoot}/target/idl`;
|
||||||
|
if (!fs.existsSync(idlFolder)) {
|
||||||
|
throw new Error(`${idlFolder} doesn't exist. Did you use "anchor build"?`);
|
||||||
|
}
|
||||||
|
|
||||||
find
|
const idlMap = new Map<string, Idl>();
|
||||||
.fileSync(/target\/idl\/.*\.json/, projectRoot)
|
fs.readdirSync(idlFolder).forEach(file => {
|
||||||
.reduce((programs: any, path: string) => {
|
const filePath = `${idlFolder}/${file}`;
|
||||||
const idlStr = fs.readFileSync(path);
|
const idlStr = fs.readFileSync(filePath);
|
||||||
const idl = JSON.parse(idlStr);
|
const idl = JSON.parse(idlStr);
|
||||||
idlMap.set(idl.name, idl);
|
idlMap.set(idl.name, idl);
|
||||||
const name = camelCase(idl.name, {pascalCase: true});
|
const name = camelCase(idl.name, {pascalCase: true});
|
||||||
if (idl.metadata && idl.metadata.address) {
|
if (idl.metadata && idl.metadata.address) {
|
||||||
programs[name] = new Program(
|
workspaceCache[name] = new Program(
|
||||||
idl,
|
idl,
|
||||||
new PublicKey(idl.metadata.address)
|
new PublicKey(idl.metadata.address)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return programs;
|
});
|
||||||
}, workspaceCache);
|
|
||||||
|
|
||||||
// Override the workspace programs if the user put them in the config.
|
// Override the workspace programs if the user put them in the config.
|
||||||
const anchorToml = toml.parse(
|
const anchorToml = toml.parse(
|
||||||
|
|
Loading…
Reference in New Issue