@@ -125,15 +125,48 @@ export function loadBasedir(basedir) {
125
125
return config ;
126
126
}
127
127
128
+ /**
129
+ * Resolve a pathname found in a config descriptor. First try to resolve it as
130
+ * a module path, and then if that doesn't work try to resolve it as an
131
+ * ordinary path relative to the directory in which the config file was found.
132
+ *
133
+ * @param dirname Path to directory containing the config file
134
+ * @param specPath Path found in a `sourceSpec` or `bundleSpec` property
135
+ *
136
+ * @return the absolute path corresponding to `specPath` if it can be
137
+ * determined.
138
+ */
139
+ function resolveSpecFromConfig ( dirname , specPath ) {
140
+ try {
141
+ return require . resolve ( specPath ) ;
142
+ } catch ( e ) {
143
+ if ( e . code === 'MODULE_NOT_FOUND' ) {
144
+ return path . resolve ( dirname , specPath ) ;
145
+ } else {
146
+ throw e ;
147
+ }
148
+ }
149
+ }
150
+
151
+ /**
152
+ * For each entry in a config descriptor (i.e, `vats`, `bundles`, etc), convert
153
+ * it to normal form: resolve each pathname to a context-insensitive absolute
154
+ * path and make sure it has a `parameters` property if it's supposed to.
155
+ *
156
+ * @param desc The config descriptor to be normalized.
157
+ * @param dirname The pathname of the directory in which the config file was found
158
+ * @param expectParameters `true` if the entries should have parameters (for
159
+ * example, `true` for `vats` but `false` for bundles).
160
+ */
128
161
function normalizeConfigDescriptor ( desc , dirname , expectParameters ) {
129
162
if ( desc ) {
130
163
for ( const name of Object . keys ( desc ) ) {
131
164
const entry = desc [ name ] ;
132
165
if ( entry . sourceSpec ) {
133
- entry . sourceSpec = path . resolve ( dirname , entry . sourceSpec ) ;
166
+ entry . sourceSpec = resolveSpecFromConfig ( dirname , entry . sourceSpec ) ;
134
167
}
135
168
if ( entry . bundleSpec ) {
136
- entry . bundleSpec = path . resolve ( dirname , entry . bundleSpec ) ;
169
+ entry . bundleSpec = resolveSpecFromConfig ( dirname , entry . bundleSpec ) ;
137
170
}
138
171
if ( expectParameters && ! entry . parameters ) {
139
172
entry . parameters = { } ;
@@ -142,6 +175,17 @@ function normalizeConfigDescriptor(desc, dirname, expectParameters) {
142
175
}
143
176
}
144
177
178
+ /**
179
+ * Read and parse a swingset config file and return it in normalized form.
180
+ *
181
+ * @param configPath Path to the config file to be processed
182
+ *
183
+ * @return the contained config object, in normalized form, or null if the
184
+ * requested config file did not exist.
185
+ *
186
+ * @throws if the file existed but was inaccessible, malformed, or otherwise
187
+ * invalid.
188
+ */
145
189
export function loadSwingsetConfigFile ( configPath ) {
146
190
try {
147
191
const config = JSON . parse ( fs . readFileSync ( configPath ) ) ;
@@ -398,21 +442,9 @@ export async function buildVatController(
398
442
if ( ! desc . bundleName ) {
399
443
names . push ( name ) ;
400
444
if ( desc . sourceSpec ) {
401
- const sourceSpec = desc . sourceSpec ;
402
- if ( ! ( sourceSpec [ 0 ] === '.' || path . isAbsolute ( sourceSpec ) ) ) {
403
- throw Error (
404
- 'sourceSpec must be relative (./foo) or absolute (/foo) not bare (foo)' ,
405
- ) ;
406
- }
407
- presumptiveBundles . push ( bundleSource ( sourceSpec ) ) ;
445
+ presumptiveBundles . push ( bundleSource ( desc . sourceSpec ) ) ;
408
446
} else if ( desc . bundleSpec ) {
409
- const bundleSpec = desc . bundleSpec ;
410
- if ( ! ( bundleSpec [ 0 ] === '.' || path . isAbsolute ( bundleSpec ) ) ) {
411
- throw Error (
412
- 'bundleSpec must be relative (./foo) or absolute (/foo) not bare (foo)' ,
413
- ) ;
414
- }
415
- presumptiveBundles . push ( fs . readFileSync ( bundleSpec ) ) ;
447
+ presumptiveBundles . push ( fs . readFileSync ( desc . bundleSpec ) ) ;
416
448
} else if ( desc . bundle ) {
417
449
presumptiveBundles . push ( desc . bundle ) ;
418
450
} else {
0 commit comments