diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-09-20 19:19:01 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-09-20 19:19:01 +0200 |
commit | ced0be930dd86bc204cab5b4e3a1cd83aed68ccf (patch) | |
tree | ede7dcb971554be669e9b4e9db42899c52fed20f /packages/gitbook-plugin/src | |
parent | 6d58a90613802b1e65cbf8c027093a1868119781 (diff) | |
download | gitbook-ced0be930dd86bc204cab5b4e3a1cd83aed68ccf.zip gitbook-ced0be930dd86bc204cab5b4e3a1cd83aed68ccf.tar.gz gitbook-ced0be930dd86bc204cab5b4e3a1cd83aed68ccf.tar.bz2 |
Working loading of plugin
Diffstat (limited to 'packages/gitbook-plugin/src')
-rw-r--r-- | packages/gitbook-plugin/src/cli.js | 23 | ||||
-rw-r--r-- | packages/gitbook-plugin/src/compile.js | 39 |
2 files changed, 57 insertions, 5 deletions
diff --git a/packages/gitbook-plugin/src/cli.js b/packages/gitbook-plugin/src/cli.js index e1f201b..20ceed5 100644 --- a/packages/gitbook-plugin/src/cli.js +++ b/packages/gitbook-plugin/src/cli.js @@ -1,13 +1,26 @@ +#! /usr/bin/env node + const program = require('commander'); +const path = require('path'); +const winston = require('winston'); + const pkg = require('../package.json'); +const compile = require('./compile'); -program.version(pkg.version) +const resolve = (input => path.resolve(process.cwd(), input)); -program - .command('build [plugin]') - .description('build a plugin') - .action(function(plugin, options) { +program.version(pkg.version); +winston.cli(); +program + .command('build [input] [output]') + .description('build a browser plugin') + .action(function(input, output, options) { + compile(resolve(input), resolve(output)) + .then( + () => winston.info('Plugin compiled successfully'), + (err) => winston.error('Error: ', err) + ); }); diff --git a/packages/gitbook-plugin/src/compile.js b/packages/gitbook-plugin/src/compile.js new file mode 100644 index 0000000..e7179e5 --- /dev/null +++ b/packages/gitbook-plugin/src/compile.js @@ -0,0 +1,39 @@ +const fs = require('fs'); +const Promise = require('q'); +const browserify = require('browserify'); +const babelify = require('babelify'); + +/** + * Compile a plugin to work with "gitbook-core" in the browser. + * @param {String} inputFile + * @param {String} outputFile + * @return {Promise} + */ +function compilePlugin(inputFile, outputFile) { + const d = Promise.defer(); + const b = browserify({ + standalone: 'GitBookPlugin' + }); + + b.add(inputFile); + b.external('react'); + b.external('react-dom'); + b.external('gitbook-core'); + b.transform(babelify, { + presets: [ + require('babel-preset-es2015'), + require('babel-preset-react') + ] + }); + + const output = fs.createWriteStream(outputFile); + + b.bundle() + .pipe(output) + .on('error', (err) => d.reject(err)) + .on('end', () => d.resolve()); + + return d.promise; +} + +module.exports = compilePlugin; |