ts: Refactor workspace loading (#407)

This commit is contained in:
Rad 2021-06-18 17:20:40 +12:00 committed by GitHub
parent f9c0822710
commit 619015d2ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 17 deletions

View File

@ -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(