UNPKG

create-readme

Version:

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

84 lines (70 loc) 2.87 kB
// Generated by CoffeeScript 2.3.2 (function() { var PackageJSONReader, Promise, fs, githubUrl2Obj, logger, path; Promise = require('bluebird'); logger = require('./logger'); fs = require('fs'); path = require('path'); githubUrl2Obj = require('github-url-to-object'); // Reads data from package.json module.exports = PackageJSONReader = (function() { var GITHUB_URL_REGEXP; class PackageJSONReader { // Create a new PackageJSONReader. It checks that package.json contains the fields // [['name', 'version', 'description']] // @param (options) [Object] Optional options. // @option options fieldsNeeded [Array<String>] Fields required from packages.json // @option options packagePath [<String>] Path to packages.json, relative to cwd ('./package.json') constructor(options) { var base, base1, base2; this.options = options; if (this.options == null) { this.options = {}; } if ((base = this.options).packagePath == null) { base.packagePath = './package.json'; } if ((base1 = this.options).encoding == null) { base1.encoding = 'utf-8'; } if ((base2 = this.options).fieldsNeeded == null) { base2.fieldsNeeded = ['name', 'description']; } } // Reads package.json // @returns [Object] The parsed data from package.json read() { var fieldName, i, len, pkg, pkgPath, ref, ref1, ref2, ref3; pkgPath = path.join(process.cwd(), this.options.packagePath); logger.debug("Reading package.json from " + pkgPath); pkg = JSON.parse(fs.readFileSync(pkgPath, { encoding: this.options.encoding })); ref = this.options.fieldsNeeded; // Check if all required fields are filled in for (i = 0, len = ref.length; i < len; i++) { fieldName = ref[i]; if (pkg[fieldName] == null) { throw new Error('package.json ' + fieldName + ' field has to be defined'); } } // Parse git repo url if (((ref1 = pkg.repository) != null ? ref1.type : void 0) === 'git') { try { pkg.git = githubUrl2Obj(pkg.repository.url); if (!(((ref2 = pkg.git.user) != null ? ref2.length : void 0) > 0 && ((ref3 = pkg.git.repo) != null ? ref3.length : void 0) > 0)) { pkg.git = void 0; logger.warn('No git repository defined'); } } catch (error) { throw new Error('package.json repository.url cannot be parsed'); } } return pkg; } }; // REGEXP that gets username and repo name from a github url GITHUB_URL_REGEXP = /.*github\.com\/([\w-_\.]+)\/([\w-_\.]+)(?:\.git)/; return PackageJSONReader; }).call(this); }).call(this);