JSPM

  • Created
  • Published
  • Downloads 234
  • Score
    100M100P100Q84044F
  • License MIT

Install PureScript to a given directory

Package Exports

  • install-purescript

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (install-purescript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

install-purescript

NPM version Build Status Build status Coverage Status

Install PureScript to a given directory

cconst {execFile} = require('child_process');
const installPurescript = require('download-or-build-purescript');

installPurescript('./dest', {version: '0.11.6'}).subscribe({
  next(event) {
    if (event.id === 'search-cache' && event.found) {
      console.log('✓ Found a cache.');
      return;
    }

    if (event.id === 'restore-cache:complete') {
      console.log('✓ Cached binary restored.');
      return;
    }

    if (event.id === 'check-binary:complete') {
      console.log('✓ Binary works correctly.');
      return;
    }
  }
  complete() {
    execFile('./dest/purs', ['--version'], (err, stdout) => {
      stdout.toString(); //=> '0.11.6\n'
    });
  }
});

Installation

Use npm.

npm install install-purescript

API

const installPurescript = require('download-or-build-purescript');

installPurescript(dir [, options])

path: string (a directory path where the PureScript binary will be installed)
options: Object
Return: Observable (zenparsing's implementation)

When the Observable is subscribed,

  1. it searches the standard cache directory for an already cached PureScript binary, and restores the cache if available
  2. if a cached binary is not available, it downloads a prebuilt binary from the PureScript release page
  3. if a prebuilt binary is not available, it downloads the PureScript source code and builds a binary form it
  4. Cache the downloaded or built binary to the standard cache directory

while successively sending events to its Observer.

Events

Every event object has id property with one of these values:

 |
search-cache -- x -+- head ------------ x -+- check-stack ----- x -+
 |                 |   |                   |   |                   |
 o                 |   o                   |   o                   |
 |                 |   |                   |   |                   |
restore-cache - x -+  download-binary - x -+  download-source - x -+
 |                 |   |                   |   |                   |
 o                 |   o                   |   o                   |
 |                 |   |                   |   |                   |
check-binary -- x -+  check-binary ---- x -+  setup ----------- x -+
 |                     |                       |                   |
 o                     |                       o                   |
 |                     |                       |                   |
 |                     |                      build ----------- x -+
 |                     |                       |                   |
 |                     o                       o                   |
 |                     |                       |                   |
*******************   *****************       *****************   ^^^^^^^
 Restored a            Downloaded a            Built a binary      Error
 binary from cache     prebuilt binary         from the source    ^^^^^^^
*******************   *****************       *****************
                       |                       |
                      write-cache             write-cache
search-cache

Fires when it checks if a tgz archive of the required PureScript binary exists in the cache directory.

{
  id: 'search-cache',
  path: <string>, // path to the .tgz file
  found: <boolean>, // whether a cache is found or not
  warning: <string | null>
}

If the cache directory has too many contents, warning property will become a message to recommend removing unused caches to free disk usage:

The cache directory ~/.cache/purescript has 6 contents, most of whom would be currently unused:

* v0.11.1-darwin-x64.tgz
* v0.11.2-darwin-x64.tgz
* v0.11.3-darwin-x64.tgz
* v0.11.4-darwin-x64.tgz

and 2 more

You can free disk usage by deleting obsolete caches or simply removing the cache directory itself.
restore-cache

Fires many times while extracting the a binary from the cached tgz archive.

{
  id: 'restore-cache',
  bytes: <number>,
  header: <Object>
}

Fires when it starts to check if a prebuilt binary is provided for the current platform.

{
  id: 'head'
}
head:fail

Fires when it cannot start downloading the binary, for example no prebuilt ones are provided for the current platform.

{
  id: 'head:fail',
  error: <Error>
}
head:complete

Fires when it confirms that a prebuilt binary is provided for the current platform.

{
  id: 'head:complete'
}
download-binary

Fires many times while downloading and extracting the prebuilt binary.

entry and response properties are derived from dl-tar.

{
  id: 'download-binary',
  entry: {
    bytes: <number>,
    header: <Object>
  },
  response: {
    bytes: <number>,
    headers: <Object>,
    url: <string>
  }
}
download-binary:fail

Fires when it fails to download the binary somehow.

{
  id: 'download-binary:fail',
  error: <Error>
}
download-binary:complete

Fires when the prebuilt binary is successfully downloaded.

{
  id: 'download-binary:complete'
}
check-binary

Fires when it starts to verify the downloaded prebuilt binary works correctly, by running purs --version.

{
  id: 'check-binary'
}
check-binary:fail

Fires when the downloaded binary doesn't work correctly.

{
  id: 'check-binary:fail',
  error: <Error>
}
check-binary:complete

Fires when it verifies the downloaded binary works correctly.

{
  id: 'check-binary:complete'
}
check-stack

Fires after one of these events: head:fail download-binary:fail check-binary:fail.

path property is the absolute path of the stack command, and version property is its version.

{
  id: 'check-stack',
  path: <string>,
  version: <string>
}
check-stack:complete

Fires after making sure the stack command is installed in your $PATH.

{
  id: 'check-binary:complete'
}
download-source

Fires many times while downloading and extracting the PureScript source code.

entry and response properties are derived from dl-tar.

{
  id: 'download-source',
  entry: {
    bytes: <number>,
    header: <Object>
  },
  response: {
    bytes: <number>,
    headers: <Object>,
    url: <string>
  }
}
download-source:complete

Fires when the source code is successfully downloaded.

{
  id: 'download-source'
}
setup setup:complete build build:complete

Inherited from build-purescript.

Errors

Every error passed to the Observer has id property that indicates which step the error occurred at.

// When the `stack` command is not installed
downloadOrBuildPureScript('.').subscribe({
  error(err) {
    err.message; //=> '`stack` command is not found in your PATH ...'
    err.id; //=> 'check-stack'
  }
});

// When your machine lose the internet connection while downloading the source
downloadOrBuildPureScript('.').subscribe({
  error(err) {
    err.message; //=> 'socket hang up'
    err.id; //=> 'download-source'
  }
});

onComplete value

Type: string (an absolute path of the created binary)

Unlike the current draft spec of Observable, zen-observable allows an Observable to send value to the complete fallback and this library follows its behavior.

downloadOrBuildPurescript('/my/dir').subscribe({
  complete(path) {
    path; //=> '/my/dir/purs'
  }
});

Options

Options are directly passed to download-purescript and build-purescript.

Note that when the platform option is specified to the different platform:

  • check-binary and check-binary:complete steps will be skipped.
  • *:fail steps will be skipped and it just pass the error to its Observer.

Additionally, you can use the following:

rename

Type: Function
Default: v => v

Receives the original binary name (purs on POSIX, purs.exe on Windows) and modifies the binary name to its return value.

const {extname} = require('path');

downloadOrBuildPurescript('./dest', {
  rename(originalName) {
    const ext = extname(originalName); //=> '' on POSIX, '.exe' on Windows

    return `foo${ext}`;
  }
}).subscribe({
  complete() {
    // Creates a binary to './dest/foo' on POSIX, './dest/foo.exe' on Windows
  }
});

License

Copyright (c) 2017 Shinnosuke Watanabe

Licensed under the MIT License.