Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit c8fb297

Browse files
committed
Refactoring promises to streams
Migrating back to a stream implementation for the tasks to allow conventient piping with other streams such as `gulp.dest`.
1 parent d5a0ab6 commit c8fb297

File tree

7 files changed

+112
-124
lines changed

7 files changed

+112
-124
lines changed

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ var gulp = require('gulp');
2121

2222
var purescript = require('gulp-purescript');
2323

24-
gulp.task('purescript', function(){
25-
return gulp.src('src/**/*.purs').
26-
pipe(purescript.psc({noPrelude: true})).
27-
pipe(gulp.dest('build'));
24+
gulp.task('psc', function(){
25+
return purescript.psc({
26+
src: 'src/*.purs'
27+
});
2828
});
2929
```
3030

@@ -38,11 +38,11 @@ Invokes the `psc` command. The following options are supported.
3838

3939
###### `src` (String or String Array)
4040

41-
The location of the source files to compile. Glob syntax is supported.
41+
Files to compile. Glob syntax is supported.
4242

4343
###### `ffi` (String or String Array)
4444

45-
Sets one or more `--ffi=<string>` that specifies files for code that is included with a `foreign import` in the PureScript source.
45+
Files for code that is included with a `foreign import` in the PureScript source. Glob syntax is supported.
4646

4747
###### `noTco` (Boolean)
4848

@@ -78,7 +78,7 @@ Invokes the `psc-bundle` command. The following options are supported.
7878

7979
###### `src` (String or String Array)
8080

81-
The location of the `psc`-produced javascript source files to bundle. Glob syntax is supported.
81+
The `psc`-produced JavaScript source files to bundle. Glob syntax is supported.
8282

8383
###### `output` (String)
8484

@@ -98,7 +98,11 @@ Sets `--browser-namespace=<string>` that specifies the namespace that PureScript
9898

9999
### `purescript.pscDocs(options)`
100100

101-
Invokes the `pscDocs` command. The following options are supported.
101+
Invokes the `psc-docs` command. The following options are supported.
102+
103+
###### `src` (String or String Array)
104+
105+
Files to be used for generating the documentation. Glob syntax is supported.
102106

103107
###### `format` (markdown | etags | ctags)
104108

@@ -110,11 +114,19 @@ Sets `--docgen=...` that can be used to filter the modules documentation is gene
110114

111115
- If a string value is provided, the documentation for that single module will be generated.
112116
- If a list of strings is provided, the documentation for all listed modules will be generated.
113-
- If an object with module name/filename pairs (for example, `{ Module: "docs/Module.md" }`) is provided, files will be written for each of the modules. In this mode, the task requires no `dest` as no value is returned.
117+
- If an object with module name/filename pairs (for example, `{ Module: 'docs/Module.md' }`) is provided, files will be written for each of the modules. In this mode, the task requires no `dest` as no value is returned.
118+
119+
### `purescript.psci(options)`
120+
121+
Generates a `.psci` file.
114122

115-
### `purescript.dotPsci()`
123+
###### `src` (String or String Array)
124+
125+
Files added to the `.psci` file with the `:m` command. Glob syntax is supported.
126+
127+
###### `ffi` (String or String Array)
116128

117-
Generates a `.psci` file in the current directory. Each source file is added with the `:m` command.
129+
Files added to the `.psci` file with the `:f` command. Glob syntax is supported.
118130

119131
## Command line arguments
120132

entry.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,20 @@
22

33
var gulpPurescript = require('GulpPurescript.Plugin');
44

5-
var Promise = require('promise');
6-
7-
function promisify(aff) {
8-
return new Promise(function(resolve, reject){
9-
var errback = function(error){
10-
return function(){
11-
reject(error);
12-
};
13-
};
14-
var callback = function(result){
15-
return function(){
16-
resolve(result);
17-
};
18-
};
19-
aff(errback)(callback)();
20-
});
21-
}
22-
235
function psc(options) {
24-
return promisify(gulpPurescript.psc(options));
6+
return gulpPurescript.psc(options)();
257
}
268

279
function pscBundle(options) {
28-
return promisify(gulpPurescript.pscBundle(options));
10+
return gulpPurescript.pscBundle(options)();
2911
}
3012

3113
function pscDocs(options) {
32-
return promisify(gulpPurescript.pscDocs(options));
14+
return gulpPurescript.pscDocs(options)();
3315
}
3416

3517
function psci(options) {
36-
return promisify(gulpPurescript.psci(options));
18+
return gulpPurescript.psci(options)();
3719
}
3820

3921
module.exports.psc = psc;

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"gulp-util": "^3.0.4",
3636
"logalot": "^2.1.0",
3737
"minimist": "^1.1.1",
38-
"promise": "^7.0.3",
3938
"resolve-bin": "^0.3.0",
4039
"which": "^1.0.9"
4140
},

src/FS.purs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Plugin.purs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module GulpPurescript.Plugin
88
, psci
99
) where
1010

11-
import Control.Monad.Aff (Aff(), runAff)
11+
import Control.Monad.Aff (Aff())
1212
import Control.Monad.Eff (Eff())
1313
import Control.Monad.Eff.Class (liftEff)
1414
import Control.Monad.Eff.Exception (Error())
@@ -26,7 +26,6 @@ import Data.Tuple.Nested (tuple2)
2626

2727
import GulpPurescript.Buffer (Buffer(), mkBufferFromString)
2828
import GulpPurescript.ChildProcess (ChildProcess(), spawn)
29-
import GulpPurescript.FS (FS(), writeFile)
3029
import GulpPurescript.Glob (Glob(), globAll)
3130
import GulpPurescript.GulpUtil (File(), mkFile, mkPluginError)
3231
import GulpPurescript.Logalot (Logalot(), info)
@@ -36,7 +35,7 @@ import GulpPurescript.Options (Psci(..), pscOptions, pscBundleOptions, pscDocsOp
3635
import GulpPurescript.Package (Pkg(), Package(..), package)
3736
import GulpPurescript.Path (relative)
3837
import GulpPurescript.ResolveBin (ResolveBin(), resolveBin)
39-
import GulpPurescript.Stream (Stream(), ReadableStream(), mkReadableStreamFromBuffer)
38+
import GulpPurescript.Stream (Stream(), ReadableStream(), mkReadableStreamFromAff)
4039
import GulpPurescript.Which (Which(), which)
4140

4241
newtype Argv = Argv { verbose :: Boolean }
@@ -46,7 +45,6 @@ instance isForeignArgv :: IsForeign Argv where
4645

4746
type Effects eff =
4847
( cp :: ChildProcess
49-
, fs :: FS
5048
, glob :: Glob
5149
, logalot :: Logalot
5250
, os :: OS
@@ -110,42 +108,41 @@ execute cmd args = do
110108
result <- spawn cmd' args'
111109
return result
112110

113-
psc :: forall eff. Foreign -> Errorback eff -> Callback eff Unit -> Eff (Effects eff) Unit
114-
psc opts eb cb = runAff eb cb $ do
111+
psc :: forall eff. Foreign -> Eff (Effects eff) (ReadableStream Unit)
112+
psc opts = mkReadableStreamFromAff $ do
115113
output <- either (throwPluginError <<< show)
116114
(execute pscCommand)
117115
(pscOptions opts)
118116
if isVerbose
119117
then liftEff $ info $ pscCommand ++ "\n" ++ output
120118
else pure unit
121119

122-
pscBundle :: forall eff. Foreign -> Errorback eff -> Callback eff (ReadableStream Buffer) -> Eff (Effects eff) Unit
123-
pscBundle opts eb cb = runAff eb cb (either (throwPluginError <<< show) run (pscBundleOptions opts))
120+
pscBundle :: forall eff. Foreign -> Eff (Effects eff) (ReadableStream File)
121+
pscBundle opts = mkReadableStreamFromAff (either (throwPluginError <<< show) run (pscBundleOptions opts))
124122
where
125-
run :: [String] -> Aff (Effects eff) (ReadableStream Buffer)
126-
run args = do
127-
bundle <- execute pscBundleCommand args
128-
liftEff (mkReadableStreamFromBuffer (mkBufferFromString bundle))
123+
run :: [String] -> Aff (Effects eff) File
124+
run args = mkFile "." <$> mkBufferFromString
125+
<$> execute pscBundleCommand args
129126

130-
pscDocs :: forall eff. Foreign -> Errorback eff -> Callback eff File -> Eff (Effects eff) Unit
131-
pscDocs opts eb cb = runAff eb cb (either (throwPluginError <<< show) run (pscDocsOptions opts))
127+
pscDocs :: forall eff. Foreign -> Eff (Effects eff) (ReadableStream File)
128+
pscDocs opts = mkReadableStreamFromAff (either (throwPluginError <<< show) run (pscDocsOptions opts))
132129
where
133130
run :: [String] -> Aff (Effects eff) File
134131
run args = mkFile "." <$> mkBufferFromString
135132
<$> execute pscDocsCommand args
136133

137-
psci :: forall eff. Foreign -> Errorback eff -> Callback eff Unit -> Eff (Effects eff) Unit
138-
psci opts eb cb = runAff eb cb (either (throwPluginError <<< show) write (read opts))
134+
psci :: forall eff. Foreign -> Eff (Effects eff) (ReadableStream File)
135+
psci opts = mkReadableStreamFromAff (either (throwPluginError <<< show) run (read opts))
139136
where
140-
write :: Psci -> Aff (Effects eff) Unit
141-
write (Psci a) = do
137+
run :: Psci -> Aff (Effects eff) File
138+
run (Psci a) = do
142139
srcs <- globAll (either pure id a.src)
143140
ffis <- globAll (either pure id (fromMaybe (Right []) (runNullOrUndefined a.ffi)))
144141

145-
let srcLines = joinWith "\n" (loadModule <$> concat srcs)
146-
ffiLines = joinWith "\n" (loadForeign <$> concat ffis)
142+
let lines = (loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)
143+
buffer = mkBufferFromString (joinWith "\n" lines)
147144

148-
writeFile psciFilename (srcLines ++ ffiLines)
145+
return (mkFile psciFilename buffer)
149146

150147
loadModule :: String -> String
151148
loadModule a = psciLoadModuleCommand ++ " " ++ relative cwd a

src/Stream.purs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,56 @@
11
module GulpPurescript.Stream
22
( Stream()
33
, ReadableStream()
4-
, mkReadableStreamFromBuffer
4+
, mkReadableStreamFromAff
55
) where
66

7+
import Control.Monad.Aff (Aff(), runAff)
78
import Control.Monad.Eff (Eff())
9+
import Control.Monad.Eff.Exception (Error())
810

9-
import GulpPurescript.Buffer (Buffer())
11+
import Data.Function
1012

1113
foreign import data Stream :: !
1214

13-
data ReadableStream o
15+
data ReadableStream out
1416

15-
foreign import mkReadableStreamFromBuffer """
16-
function mkReadableStreamFromBuffer(buffer) {
17+
type RunAff eff a = (Error -> Eff eff Unit) -> (a -> Eff eff Unit) -> Aff eff a -> Eff eff Unit
18+
19+
mkReadableStreamFromAff :: forall eff1 eff2 out. Aff eff1 out -> Eff (stream :: Stream | eff2) (ReadableStream out)
20+
mkReadableStreamFromAff = runFn2 mkReadableStreamFromAffFn runAff
21+
22+
foreign import mkReadableStreamFromAffFn """
23+
function mkReadableStreamFromAffFn(runAff, aff) {
1724
return function(){
18-
var Readable = require('stream').Readable;
25+
var stream = require('stream');
1926
20-
var stream = Readable();
27+
var objectMode = true;
2128
22-
stream._read = function(){
23-
stream.push(buffer);
24-
return stream.push(null);
29+
var readable = new stream.Readable({objectMode: objectMode});
30+
31+
readable._read = function(){
2532
};
2633
27-
return stream;
34+
function onError(e) {
35+
return function(){
36+
readable.emit('error', e);
37+
};
38+
}
39+
40+
function onSuccess(a) {
41+
return function(){
42+
readable.push(a);
43+
readable.push(null);
44+
};
45+
}
46+
47+
var eff = runAff(onError)(onSuccess)(aff);
48+
49+
eff();
50+
51+
return readable;
2852
};
2953
}
30-
""" :: forall eff. Buffer -> Eff (stream :: Stream | eff) (ReadableStream Buffer)
54+
""" :: forall eff1 eff2 out. Fn2 (RunAff eff1 out)
55+
(Aff eff1 out)
56+
(Eff (stream :: Stream | eff2) (ReadableStream out))

0 commit comments

Comments
 (0)