Skip to content

Commit 6f28f47

Browse files
fubhyJannis
authored andcommitted
Add support for workspaces and otherwise hoisted node modules.
1 parent 18a7bee commit 6f28f47

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/compiler.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,39 @@ class Compiler {
1818
this.options = options
1919
this.ipfs = options.ipfs
2020
this.sourceDir = path.dirname(options.subgraphManifest)
21+
this.libsDirs = []
2122

2223
for (
2324
let dir = path.resolve(this.sourceDir);
2425
// Terminate after the root dir or when we have found node_modules
25-
dir !== undefined && this.libsDir === undefined;
26+
dir !== undefined;
2627
// Continue with the parent directory, terminate after the root dir
2728
dir = path.dirname(dir) === dir ? undefined : path.dirname(dir)
2829
) {
2930
if (fs.existsSync(path.join(dir, 'node_modules'))) {
30-
this.libsDir = path.join(dir, 'node_modules')
31+
this.libsDirs.push(path.join(dir, 'node_modules'))
3132
}
3233
}
3334

34-
if (this.libsDir === undefined) {
35+
if (this.libsDirs.length === 0) {
3536
throw Error(
3637
`could not locate \`node_modules\` in parent directories of subgraph manifest`,
3738
)
3839
}
3940

41+
const globalsFile = path.join('@graphprotocol', 'graph-ts', 'global', 'global.ts')
42+
const globalsLib = this.libsDirs.find(item => {
43+
return fs.existsSync(path.join(item, globalsFile))
44+
})
45+
46+
if (!globalsLib) {
47+
throw Error(
48+
'Could not locate `@graphprotocol/graph-ts` package in parent directories of subgraph manifest.',
49+
)
50+
}
51+
52+
this.globalsFile = path.join(globalsLib, globalsFile)
53+
4054
process.on('uncaughtException', function(e) {
4155
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`)
4256
})
@@ -273,9 +287,8 @@ class Compiler {
273287
throw e
274288
}
275289

276-
let libs = this.libsDir
277-
let global = path.join(libs, '@graphprotocol', 'graph-ts', 'global', 'global.ts')
278-
global = path.relative(baseDir, global)
290+
let libs = this.libsDirs.join(',')
291+
let global = path.relative(baseDir, this.globalsFile)
279292

280293
asc.main(
281294
[
@@ -355,9 +368,8 @@ class Compiler {
355368
throw e
356369
}
357370

358-
let libs = this.libsDir
359-
let global = path.join(libs, '@graphprotocol', 'graph-ts', 'global', 'global.ts')
360-
global = path.relative(baseDir, global)
371+
let libs = this.libsDirs.join(',')
372+
let global = path.relative(baseDir, this.globalsFile)
361373

362374
asc.main(
363375
[

0 commit comments

Comments
 (0)