shelljs/scripts/generate-docs.js
Nate Fischer 2d0428b1c9 Switch commands.json -> commands.js (#668)
WebPack has issues with importing JSON directly and using JavaScript
methods on it. For this reason, using the `.forEach()` method on the
imported JSON array caused a method-not-found error.

Instead, we can make this a real JavaScript array by keeping it in a
JavaScript file and exporting it as a field.

It should be noted that exporting it as `exports = [...]` does not work,
because Node won't actually interpret it as a JavaScript array (with
the required methods). So instead we can export it as
`exports.list = [...]` and access the `list` field to get the real
JavaScript array of command names.

Fixes #667
2017-02-28 01:34:51 -08:00

36 lines
1.0 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* globals cat, cd, echo, grep, sed, ShellString */
require('../global');
echo('Appending docs to README.md');
cd(__dirname + '/..');
// Extract docs from shell.js
var docs = grep('^//@', 'shell.js');
// Insert the docs for all the registered commands
docs = docs.replace(/\/\/@commands\n/g, function () {
return require('../commands').map(function (commandName) {
var file = './src/' + commandName + '.js';
return grep('^//@', file) + '\n';
}).join('');
});
// Now extract docs from the remaining src/*.js files
docs = docs.replace(/\/\/@include (.+)/g, function (match, path) {
var file = path.match('.js$') ? path : path + '.js';
return grep('^//@', file);
});
// Remove '//@'
docs = docs.replace(/\/\/@ ?/g, '');
// Wipe out the old docs
ShellString(cat('README.md').replace(/## Command reference(.|\n)*\n## Team/, '## Command reference\n## Team')).to('README.md');
// Append new docs to README
sed('-i', /## Command reference/, '## Command reference\n\n' + docs, 'README.md');
echo('All done.');