diff --git a/.gitignore b/.gitignore index 4d159c2..5ba39cf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ /output/ /.psci* /src/.webpack.js +/.spago/ +/.psc-ide-port diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ccfdd1e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +Features: +- Update to v0.15.0 and support es modules + +Breaking changes: +- `discover` now needs `MonadAff` constraint instead of `MonadEffect` due to dynamic imports returning promises diff --git a/package.json b/package.json new file mode 100644 index 0000000..73de491 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "purescript-spec-discovery", + "version": "1.0.0", + "description": "purescript-spec-discovery is an extension to [purescript-spec](https://github.com/purescript-spec/purescript-spec) that finds specs automatically, given a regular expression pattern.", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/working-group-purescript-es/purescript-spec-discovery.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/working-group-purescript-es/purescript-spec-discovery/issues" + }, + "homepage": "https://github.com/working-group-purescript-es/purescript-spec-discovery#readme", + "type": "module" +} diff --git a/packages.dhall b/packages.dhall new file mode 100644 index 0000000..e51258b --- /dev/null +++ b/packages.dhall @@ -0,0 +1,24 @@ +let upstream = + https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall + sha256:5f32c078b014909642302b328bd9bebcdcedc301956a709b302f19521680a0aa + +in upstream + with metadata.version = "v0.15.0-alpha-02" + with spec = + { repo = "https://github.com/purescript-spec/purescript-spec.git" + , version = "master" + , dependencies = + [ "aff" + , "ansi" + , "avar" + , "console" + , "exceptions" + , "foldable-traversable" + , "fork" + , "now" + , "pipes" + , "prelude" + , "strings" + , "transformers" + ] + } diff --git a/spago.dhall b/spago.dhall new file mode 100644 index 0000000..1c6fe89 --- /dev/null +++ b/spago.dhall @@ -0,0 +1,27 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. + +Need help? See the following resources: +- Spago documentation: https://github.com/purescript/spago +- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html + +When creating a new Spago project, you can use +`spago init --no-comments` or `spago init -C` +to generate this file without the comments in this block. +-} +{ name = "spec-discovery" +, dependencies = + [ "aff" + , "aff-promise" + , "arrays" + , "console" + , "effect" + , "foldable-traversable" + , "node-fs" + , "prelude" + , "spec" + ] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +} diff --git a/src/Test/Spec/Discovery.js b/src/Test/Spec/Discovery.js index 58aa212..cbd6642 100644 --- a/src/Test/Spec/Discovery.js +++ b/src/Test/Spec/Discovery.js @@ -1,24 +1,30 @@ -'use strict'; +import path from "path" +import fs from 'fs'; +import { fileURLToPath } from "url" -if (typeof require !== 'function') { - throw new Error('Sorry, purescript-spec-discovery only supports NodeJS environments!'); -} +const __filename = fileURLToPath(import.meta.url) +const __dirname = path.dirname(__filename) -var fs = require('fs'); -var path = require('path'); +if (import.meta.url === `file://${process.argv[1]}`) { + throw new Error('Sorry, purescript-spec-discovery only supports NodeJS environments!'); +} function getMatchingModules(pattern) { var directories = fs.readdirSync(path.join(__dirname, '..')); - return directories.filter(function (directory) { + const modulePromises = directories.filter(function (directory) { return (new RegExp(pattern).test(directory)); }).map(function (name) { - var module = require(path.join(__dirname, '..', name)); - return (module && typeof module.spec !== 'undefined') ? module.spec : null; - }).filter(function (x) { return x; }); + var modulePromise = import(path.join(__dirname, '..', name, 'index.js')); + return modulePromise.then( module => { + return (module && typeof module.spec !== 'undefined') ? module.spec : null; + }) + }) + const modules = Promise.all(modulePromises) + return modules.then(ms => ms.filter(function (x) { return x; })); } -exports.getSpecs = function (pattern) { +export function getSpecs(pattern) { return function () { return getMatchingModules(pattern); }; -}; +} diff --git a/src/Test/Spec/Discovery.purs b/src/Test/Spec/Discovery.purs index db4182d..75ffba2 100644 --- a/src/Test/Spec/Discovery.purs +++ b/src/Test/Spec/Discovery.purs @@ -2,14 +2,20 @@ module Test.Spec.Discovery (discover) where import Prelude +import Control.Promise (Promise) +import Control.Promise as Promise import Data.Traversable (sequence_) import Effect (Effect) -import Effect.Class (class MonadEffect, liftEffect) +import Effect.Aff.Class (class MonadAff, liftAff) import Test.Spec (Spec) -foreign import getSpecs :: String - -> Effect (Array (Spec Unit)) +foreign import getSpecs + :: String + -> Effect (Promise (Array (Spec Unit))) -discover :: forall m. MonadEffect m => String - -> m (Spec Unit) -discover pattern = getSpecs pattern >>= (pure <<< sequence_) # liftEffect +discover + :: forall m + . MonadAff m + => String + -> m (Spec Unit) +discover pattern = getSpecs pattern # Promise.toAffE >>= (pure <<< sequence_) # liftAff diff --git a/test/Main.purs b/test/Main.purs index c8c57fb..4f6ab6b 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -9,4 +9,4 @@ import Test.Spec.Reporter.Console (consoleReporter) import Test.Spec.Runner (runSpec) main :: Effect Unit -main = discover "Test\\.Spec\\.Discovery.*Spec" >>= runSpec [consoleReporter] >>> launchAff_ +main = launchAff_ $ discover "Test\\.Spec\\.Discovery.*Spec" >>= runSpec [consoleReporter]