| 197 | } |
| 198 | |
| 199 | async init() { |
| 200 | debug('init'); |
| 201 | |
| 202 | // Validate root |
| 203 | // <https://nodejs.org/api/esm.html#esm_mandatory_file_extensions> |
| 204 | if ( |
| 205 | isSANB(this.config.root) /* istanbul ignore next */ && |
| 206 | !isInvalidPath(this.config.root) |
| 207 | ) { |
| 208 | const stats = await fs.promises.stat(this.config.root); |
| 209 | if (!stats.isDirectory()) { |
| 210 | throw new Error(`Root directory of ${this.config.root} does not exist`); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Validate timeout |
| 215 | this.config.timeout = this.parseValue(this.config.timeout); |
| 216 | debug('timeout', this.config.timeout); |
| 217 | |
| 218 | // Validate interval |
| 219 | this.config.interval = this.parseValue(this.config.interval); |
| 220 | debug('interval', this.config.interval); |
| 221 | |
| 222 | // |
| 223 | // if `this.config.jobs` is an empty array |
| 224 | // then we should try to load `jobs/index.js` |
| 225 | // |
| 226 | debug('root', this.config.root); |
| 227 | debug('doRootCheck', this.config.doRootCheck); |
| 228 | debug('jobs', this.config.jobs); |
| 229 | if ( |
| 230 | this.config.root && |
| 231 | this.config.doRootCheck && |
| 232 | (!Array.isArray(this.config.jobs) || this.config.jobs.length === 0) |
| 233 | ) { |
| 234 | try { |
| 235 | const importPath = join(this.config.root, this.config.defaultRootIndex); |
| 236 | debug('importPath', importPath); |
| 237 | const importUrl = pathToFileURL(importPath).toString(); |
| 238 | debug('importUrl', importUrl); |
| 239 | // hint: import statement expect a esm-url-string, not a file-path-string (https://github.com/breejs/bree/issues/202) |
| 240 | // otherwise the following error is expected: |
| 241 | // Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. |
| 242 | // On Windows, absolute paths must be valid file:// URLs. |
| 243 | const obj = await import(importUrl); |
| 244 | if (typeof obj.default !== 'object') { |
| 245 | throw new ImportError( |
| 246 | `Root index file missing default export at: ${importPath}` |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | this.config.jobs = obj.default; |
| 251 | } catch (err) { |
| 252 | debug(err); |
| 253 | |
| 254 | // |
| 255 | // NOTE: this is only applicable for Node <= 12.20.0 |
| 256 | // |