|
1 |
| -'use strict'; |
| 1 | +import path from "path" |
| 2 | +import fs from 'fs'; |
| 3 | +import { fileURLToPath } from "url" |
2 | 4 |
|
3 |
| -if (typeof require !== 'function') { |
4 |
| - throw new Error('Sorry, purescript-spec-discovery only supports NodeJS environments!'); |
5 |
| -} |
| 5 | +const __filename = fileURLToPath(import.meta.url) |
| 6 | +const __dirname = path.dirname(__filename) |
6 | 7 |
|
7 |
| -var fs = require('fs'); |
8 |
| -var path = require('path'); |
| 8 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 9 | + throw new Error('Sorry, purescript-spec-discovery only supports NodeJS environments!'); |
| 10 | +} |
9 | 11 |
|
10 | 12 | function getMatchingModules(pattern) {
|
11 | 13 | var directories = fs.readdirSync(path.join(__dirname, '..'));
|
12 |
| - return directories.filter(function (directory) { |
| 14 | + const modulePromises = directories.filter(function (directory) { |
13 | 15 | return (new RegExp(pattern).test(directory));
|
14 | 16 | }).map(function (name) {
|
15 |
| - var module = require(path.join(__dirname, '..', name)); |
16 |
| - return (module && typeof module.spec !== 'undefined') ? module.spec : null; |
17 |
| - }).filter(function (x) { return x; }); |
| 17 | + var modulePromise = import(path.join(__dirname, '..', name, 'index.js')); |
| 18 | + return modulePromise.then( module => { |
| 19 | + return (module && typeof module.spec !== 'undefined') ? module.spec : null; |
| 20 | + }) |
| 21 | + }) |
| 22 | + const modules = Promise.all(modulePromises) |
| 23 | + return modules.then(ms => ms.filter(function (x) { return x; })); |
18 | 24 | }
|
19 | 25 |
|
20 |
| -exports.getSpecs = function (pattern) { |
| 26 | +export function getSpecs(pattern) { |
21 | 27 | return function () {
|
22 | 28 | return getMatchingModules(pattern);
|
23 | 29 | };
|
24 |
| -}; |
| 30 | +} |
0 commit comments