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

Commit 0eaba06

Browse files
committed
Merge pull request #20 from purescript-contrib/topic/local-purescript-bin
Topic/local purescript bin
2 parents 876e280 + dd64b32 commit 0eaba06

File tree

3 files changed

+93
-66
lines changed

3 files changed

+93
-66
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ Install with [npm](https://npmjs.org/package/gulp-purescript)
1010
npm install gulp-purescript --save-dev
1111
```
1212

13+
## Binaries
14+
15+
This plugin requires that the PureScript binaries first be installed. The binaries may be installed using the [purescript](https://www.npmjs.com/package/purescript) NPM package or as described on the PureScript [installation](https://github.com/purescript/purescript/wiki/Language-Guide:-Getting-Started#installation) section of the GitHub wiki.
16+
1317
## Example
1418

1519
```js
16-
var gulp = require('gulp')
17-
, purescript = require('gulp-purescript')
18-
;
20+
var gulp = require('gulp');
21+
22+
var purescript = require('gulp-purescript');
23+
1924
gulp.task('purescript', function(){
20-
return (
21-
gulp.src('src/**/*.purs.hs').
22-
pipe(purescript.psc({noPrelude: true})).
23-
pipe(gulp.dest('dist/'))
24-
);
25+
return gulp.src('src/**/*.purs').
26+
pipe(purescript.psc({noPrelude: true})).
27+
pipe(gulp.dest('build'));
2528
});
2629
```
2730

2831
## API
2932

30-
Refer to the PureScript [usage](http://docs.purescript.org/en/latest/start.html#compiler-usage) section for additional details on the behaviour of each option below.
33+
Refer to the PureScript [compiler usage](https://github.com/purescript/purescript/wiki/Language-Guide:-Getting-Started#compiler-usage) section of the Github wiki for additional details on the behaviour of each option below.
3134

3235
### purescript.psc(options)
3336

index.js

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ var logalot = require('logalot');
2020

2121
var multipipe = require('multipipe');
2222

23+
var resolveBin = require('resolve-bin');
24+
2325
var options = require('./options');
2426

25-
var pluginName = 'gulp-purescript';
27+
var packageJson = require('./package.json');
28+
29+
var pluginName = packageJson.name;
30+
31+
var purescriptPackage = 'purescript';
2632

2733
var psciFilename = '.psci';
2834

@@ -38,16 +44,24 @@ var PluginError = gutil.PluginError;
3844

3945
var File = gutil.File;
4046

41-
function runCommand(cmd, args, k) {
42-
var err = [ 'Failed to find ' + gutil.colors.magenta(cmd), 'in your path.'
43-
, 'Please ensure that ' + gutil.colors.magenta(cmd)
44-
, 'is available on your system.' ].join(' ');
47+
function resolve(cmd, callback) {
48+
var err = 'Failed to find ' + gutil.colors.cyan(cmd) + '. Please ensure it is available on your system.';
4549

46-
var that = this;
50+
resolveBin(purescriptPackage, {executable: cmd}, function(e, bin){
51+
if (!e) callback(null, bin);
52+
else {
53+
which(cmd, function(e){
54+
if (e) callback(err);
55+
else callback(null, cmd);
56+
});
57+
}
58+
});
59+
}
4760

48-
which(cmd, function(e){
49-
if (e) that.emit('error', new PluginError(pluginName, err));
50-
else k(child_process.spawn(cmd, args));
61+
function execute(cmd, args, callback) {
62+
resolve(cmd, function(e, bin){
63+
if (e) callback(new PluginError(pluginName, e));
64+
else callback(null, child_process.spawn(bin, args));
5165
});
5266
}
5367

@@ -102,21 +116,24 @@ function psc(opts) {
102116

103117
var buffere = new Buffer(0);
104118

105-
runCommand.apply(this, [options.psc.cmd, args, function(cmd){
106-
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
107-
108-
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
109-
110-
cmd.on('close', function(code){
111-
if (code !== 0) callback(new PluginError(pluginName, buffere.toString()));
112-
else {
113-
callback(null, new File({
114-
path: output,
115-
contents: buffero
116-
}));
117-
}
118-
});
119-
}]);
119+
execute(options.psc.cmd, args, function(e, cmd){
120+
if (e) callback(e);
121+
else {
122+
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
123+
124+
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
125+
126+
cmd.on('close', function(code){
127+
if (code !== 0) callback(new PluginError(pluginName, buffere.toString()));
128+
else {
129+
callback(null, new File({
130+
path: output,
131+
contents: buffero
132+
}));
133+
}
134+
});
135+
}
136+
});
120137
}
121138

122139
return multipipe(collectPaths(), through2.obj(transform));
@@ -130,24 +147,27 @@ function pscMake(opts) {
130147

131148
var buffere = new Buffer(0);
132149

133-
runCommand.apply(this, [options.pscMake.cmd, args, function(cmd){
134-
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
135-
136-
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
137-
138-
cmd.on('close', function(code){
139-
var message =
140-
function() { return [ gutil.colors.cyan(options.pscMake.cmd)
141-
, buffero.toString()
142-
, buffere.toString() ].join('\n') };
143-
144-
if (code !== 0) callback(new PluginError(pluginName, message()));
145-
else {
146-
if (isVerbose) logalot.info(message());
147-
callback();
148-
}
149-
});
150-
}]);
150+
execute(options.pscMake.cmd, args, function(e, cmd){
151+
if (e) callback(e);
152+
else {
153+
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
154+
155+
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
156+
157+
cmd.on('close', function(code){
158+
var message =
159+
function() { return [ gutil.colors.cyan(options.pscMake.cmd)
160+
, buffero.toString()
161+
, buffere.toString() ].join('\n') };
162+
163+
if (code !== 0) callback(new PluginError(pluginName, message()));
164+
else {
165+
if (isVerbose) logalot.info(message());
166+
callback();
167+
}
168+
});
169+
}
170+
});
151171
};
152172

153173
return multipipe(collectPaths(), through2.obj(transform));
@@ -161,21 +181,24 @@ function pscDocs(opts) {
161181

162182
var buffere = new Buffer(0);
163183

164-
runCommand.apply(this, [options.pscDocs.cmd, args, function(cmd){
165-
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
166-
167-
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
168-
169-
cmd.on('close', function(code){
170-
if (code !== 0) callback(new PluginError(pluginName, buffere.toString()));
171-
else {
172-
callback(null, new File({
173-
path: '.',
174-
contents: buffero
175-
}));
176-
}
177-
});
178-
}]);
184+
execute(options.pscDocs.cmd, args, function(e, cmd){
185+
if (e) callback(e);
186+
else {
187+
cmd.stdout.on('data', function(stdout){buffero = Buffer.concat([buffero, new Buffer(stdout)]);});
188+
189+
cmd.stderr.on('data', function(stderr){buffere = Buffer.concat([buffere, new Buffer(stderr)]);});
190+
191+
cmd.on('close', function(code){
192+
if (code !== 0) callback(new PluginError(pluginName, buffere.toString()));
193+
else {
194+
callback(null, new File({
195+
path: '.',
196+
contents: buffero
197+
}));
198+
}
199+
});
200+
}
201+
});
179202
}
180203

181204
return multipipe(collectPaths(), through2.obj(transform));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"logalot": "^2.1.0",
2929
"minimist": "^1.1.1",
3030
"multipipe": "^0.1.2",
31+
"resolve-bin": "^0.3.0",
3132
"through2": "^0.6.3",
3233
"which": "^1.0.9"
3334
},

0 commit comments

Comments
 (0)