shelljs/scripts/generate-docs.js
Nate Fischer 519308489c refactor: move exec-child logic into main function
No change to logic. This refactors the exec-child.js script to move all
of its main logic into a main() function. This function is only invoked
if the script is executed, not when it is imported.

Importing the script is now a NOOP instead of throwing an exception.
It's still not advisable to import the script, however this can be done
if absolutely necessary to trick JavaScript bundlers which try to prune
non-imported code files.

Partially related to issue #1160 and #1172.
2025-02-20 00:32:19 -08:00

41 lines
1.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* globals cat, cd, echo, grep, sed, ShellString */
require('../global');
var path = require('path');
echo('Appending docs to README.md');
cd(path.join(__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';
var commandDoc = grep('^//@', file).toString();
if (commandDoc !== '') {
commandDoc += '\n';
}
return commandDoc;
}).join('');
});
// Now extract docs from the remaining src/*.js files
docs = docs.replace(/\/\/@include (.+)/g, function (match, filename) {
return grep('^//@', filename);
});
// 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.');