UNPKG

create-readme

Version:

Automatically creates README.md based on package.json and other existing files.

108 lines (95 loc) 4 kB
// Generated by CoffeeScript 2.3.2 (function() { var Promise, UsageParser, fs, languages, logger, path; logger = require('../logger'); Promise = require('bluebird'); fs = Promise.promisifyAll(require('fs')); path = require('path'); languages = require('../../data/languages.json'); // Parses example files module.exports = UsageParser = (function() { class UsageParser { // Creates a new UsageParser // addUsage can optionally be given as an array of strings that will // be concatenated by the module. This is to enhance readability // of package.json // @param (options) [Object] An optional set of options // @option options encoding [String] Encoding for reading the file ['utf-8'] // @option options replaceReferences [Boolean] Replace '../' in examples with pkg name [true] // @option options addUsage [String] Additional text for the usage section [''] // @option options exampleDir [String] Examples dir to be used unless in package.json ['examples'] constructor(options) { var base, base1, base2, base3; this.options = options; if (this.options == null) { this.options = {}; } if ((base = this.options).encoding == null) { base.encoding = 'utf-8'; } if ((base1 = this.options).replaceReferences == null) { base1.replaceReferences = true; } if ((base2 = this.options).addUsage == null) { base2.addUsage = ''; } if ((base3 = this.options).exampleDir == null) { base3.exampleDir = "examples"; } } // Includes the files from the examples directory as usage examples. If // `this.options.replaceModuleReferences` is true, Strings of the form // `'../'` and `"../"` will be replaced by the package name, so that // `require("../")` will show as `require('my-package-name')`. // @param pkg [Object] package.json data // @returns [Promise<Object>] Usage information [{name: string, file: string}] run(pkg) { var exampleDir, ref, ref1; logger.info("Creating usage examples"); exampleDir = (ref = (ref1 = pkg.directories) != null ? ref1.example : void 0) != null ? ref : this.options.exampleDir; exampleDir = path.join(process.cwd(), exampleDir); logger.debug(" Looking for examples in dir " + exampleDir); return fs.readdirAsync(exampleDir).catch({ code: 'ENOENT' }, function() { return []; }).filter(function(file) { return Object.keys(languages).indexOf(path.extname(file)) > -1; }).map((file) => { var content; logger.debug(" Parsing file" + file); content = fs.readFileAsync(path.join(exampleDir, file), { encoding: this.options.encoding }).then((content) => { var regex; if (!this.options.replaceReferences) { return content; } logger.debug(" Replacing occurances of '../' and \"../\" with " + pkg.name); // Replace occurences of '../' and "../" with 'my-package-name' regex = /(?:'\.\.\/')|(?:"\.\.\/")/g; return content.replace(regex, "'" + pkg.name + "'"); }); return Promise.resolve({ lang: languages[path.extname(file)], content: content }).props(); }).then((examples) => { var ref2; if ((examples.length === (ref2 = this.options.addUsage.length) && ref2 === 0)) { logger.debug(" No examples and no addUsage"); return null; } else { return { examples: examples, description: this.options.addUsage }; } }); } }; // @property [String] The name of this component UsageParser.name = "UsageParser"; return UsageParser; }).call(this); }).call(this);