diff options
Diffstat (limited to 'packages/gitbook-plugin/src')
-rw-r--r-- | packages/gitbook-plugin/src/cli.js | 84 | ||||
-rw-r--r-- | packages/gitbook-plugin/src/compile.js | 41 | ||||
-rw-r--r-- | packages/gitbook-plugin/src/create.js | 61 | ||||
-rw-r--r-- | packages/gitbook-plugin/src/index.js | 0 |
4 files changed, 186 insertions, 0 deletions
diff --git a/packages/gitbook-plugin/src/cli.js b/packages/gitbook-plugin/src/cli.js new file mode 100644 index 0000000..06e421d --- /dev/null +++ b/packages/gitbook-plugin/src/cli.js @@ -0,0 +1,84 @@ +#! /usr/bin/env node + +const program = require('commander'); +const path = require('path'); +const winston = require('winston'); +const inquirer = require('inquirer'); + +const pkg = require('../package.json'); +const compile = require('./compile'); +const create = require('./create'); + +const resolve = (input => path.resolve(process.cwd(), input)); + +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) + ); + }); + +program + .command('create [output]') + .description('create a new plugin') + .action(function(output, options) { + inquirer.prompt([ + { + name: 'title', + message: 'Title (as displayed on GitBook.com):' + }, + { + name: 'name', + message: 'Name (unique identifier for the plugin):' + }, + { + name: 'desc', + message: 'Description:' + }, + { + name: 'github', + message: 'GitHub repository URL:' + }, + { + name: 'categories', + message: 'Categories (as displayed on GitBook.com):', + type: 'checkbox', + choices: [ + 'analytics', + 'search', + 'content', + 'structure', + 'social', + 'visual' + ] + } + ]) + .then(answers => { + output = resolve(output || answers.name); + return create(output, answers); + }) + .then( + () => winston.info(`Plugin created successfully in "${output}"`), + (err) => winston.error('Error: ', err) + ); + }); + +program + .command('test [plugin]') + .description('test specs for a plugin') + .action(function(plugin, options) { + + }); + + +program.parse(process.argv); + +// Display help if no arguments +if (!program.args.length) program.help(); diff --git a/packages/gitbook-plugin/src/compile.js b/packages/gitbook-plugin/src/compile.js new file mode 100644 index 0000000..61c8777 --- /dev/null +++ b/packages/gitbook-plugin/src/compile.js @@ -0,0 +1,41 @@ +const fs = require('fs-extra'); +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') + ] + }); + + fs.ensureFileSync(outputFile); + + 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; diff --git a/packages/gitbook-plugin/src/create.js b/packages/gitbook-plugin/src/create.js new file mode 100644 index 0000000..31edb85 --- /dev/null +++ b/packages/gitbook-plugin/src/create.js @@ -0,0 +1,61 @@ +const fs = require('fs-extra'); +const path = require('path'); +const GITBOOK_VERSION = require('../package.json').version; + +const TEMPLATE_DIR = path.resolve(__dirname, '../template'); + +/** + * Create a new plugin + * @param {String} outputDir + * @param {String} spec.title + * @param {String} spec.name + * @param {String} spec.desc + * @param {Array} spec.keywords + */ +function create(outputDir, spec) { + const pkg = { + 'title': `${spec.title}`, + 'name': `gitbook-plugin-${spec.name}`, + 'description': `${spec.desc}`, + 'version': '0.0.0', + 'main': 'index.js', + 'browser': './_assets/plugin.js', + 'ebook': './_assets/plugin.js', + 'dependencies': { + 'gitbook-core': '^' + GITBOOK_VERSION + }, + 'devDependencies': { + 'gitbook-plugin': '^' + GITBOOK_VERSION, + 'eslint': '3.7.1', + 'eslint-config-gitbook': '1.4.0' + }, + 'engines': { + 'gitbook': '>=4.0.0-alpha.0' + }, + 'scripts': { + 'lint': 'eslint ./', + 'build-website': 'gitbook-plugin build ./src/index.js ./_assets/plugin.js', + 'prepublish': 'npm run build-website', + 'test': 'gitbook-plugin test && npm run lint' + }, + 'homepage': `${spec.github}`, + 'keywords': spec.categories.map(category => `gitbook:${category}`), + 'repository': { + 'type': 'git', + 'url': `${spec.github}.git` + }, + 'bugs': { + 'url': `${spec.github}/issues` + } + }; + + fs.copySync(TEMPLATE_DIR, outputDir, { + clobber: true + }); + + fs.outputJsonSync(path.resolve(outputDir, 'package.json'), pkg, { + spaces: 2 + }); +} + +module.exports = create; diff --git a/packages/gitbook-plugin/src/index.js b/packages/gitbook-plugin/src/index.js new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/packages/gitbook-plugin/src/index.js |