From d15057cfc6907e690d56c8a4cec5ea25471c4d1d Mon Sep 17 00:00:00 2001 From: Sean Trane Date: Tue, 6 Nov 2018 21:30:34 -0500 Subject: [PATCH] fix: replace Fetch with Finda --- package-lock.json | 11 ++ package.json | 1 + src/app/app.spec.ts | 1 - src/shared/yo-repo-fetch.spec.ts | 242 ----------------------- src/shared/yo-repo-fetch.ts | 330 ------------------------------- src/shared/yo-repo-prompts.ts | 16 +- 6 files changed, 20 insertions(+), 581 deletions(-) delete mode 100644 src/shared/yo-repo-fetch.spec.ts delete mode 100644 src/shared/yo-repo-fetch.ts diff --git a/package-lock.json b/package-lock.json index d501b0e..415bd4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2946,6 +2946,17 @@ "semver-regex": "^1.0.0" } }, + "finda": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/finda/-/finda-1.0.3.tgz", + "integrity": "sha512-ksKMYkOadMDkur8M4ETlt2uJrcaHXw++hbTmC+kOdf0iLIa1tkTQtlHTbFwUgtdeWnT3Ke4lgSdG7rwPyZJlcw==", + "requires": { + "empty-dir": "^1.0.0", + "parse-git-config": "^2.0.3", + "shelljs": "^0.8.2", + "spdx-correct": "^3.0.2" + } + }, "findup-sync": { "version": "0.4.2", "resolved": "https://registry.npmjs.org/findup-sync/-/findup-sync-0.4.2.tgz", diff --git a/package.json b/package.json index d6ee7f0..80e4bea 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "date-fns": "^1.29.0", "empty-dir": "^1.0.0", "figlet": "^1.2.0", + "finda": "^1.0.3", "generator-license": "^5.4.0", "marked": "^0.4.0", "parse-git-config": "^2.0.3", diff --git a/src/app/app.spec.ts b/src/app/app.spec.ts index 0112f3e..3607f42 100644 --- a/src/app/app.spec.ts +++ b/src/app/app.spec.ts @@ -8,7 +8,6 @@ import * as helpers from 'yeoman-test'; import { RepoGenerator } from './'; // Ensure other unit tests are run first: -require('../shared/yo-repo-fetch.spec'); require('../shared/yo-repo-prompts.spec'); require('../ci/ci.spec'); require('../contributing/contributing.spec'); diff --git a/src/shared/yo-repo-fetch.spec.ts b/src/shared/yo-repo-fetch.spec.ts deleted file mode 100644 index 501d20d..0000000 --- a/src/shared/yo-repo-fetch.spec.ts +++ /dev/null @@ -1,242 +0,0 @@ -import { assert, expect, should } from 'chai'; -import * as mocha from 'mocha'; -import * as path from 'path'; - -import { Fetch } from './yo-repo-fetch'; - -describe('Yo Repo Fetch', function() { - - let fetch: Fetch; - - beforeEach(function() { - fetch = new Fetch(); - fetch.resetPackageJsonPath(path.join(__dirname, '../../package.json')); - }); - - describe('Fetch class', function() { - - it('should have expected methods and properties', function() { - expect(fetch).to.have.property('packageJsonPath'); - expect(fetch).to.respondTo('resetPackageJsonPath'); - }); - - it('should have default value for packageJsonPath property', function() { - expect(fetch.packageJsonPath).to.include('package.json'); - }); - - it('should reset packageJsonPath property', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageJsonPath).to.include('missing.json'); - }); - - it('should reset packageJsonPath property to default', function() { - expect(fetch.packageJsonPath).to.include('package.json'); - }); - - }); - - describe('_getFromPackage function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('_getFromPackage'); - }); - - it('should fetch name property from package.json', function() { - expect(fetch._getFromPackage('name')).to.equal('generator-repo'); - }); - - it('should return a default value when property is not found', function() { - expect(fetch._getFromPackage('xyz23', 'default')).to.equal('default'); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch._getFromPackage('name', 'default')).to.equal('default'); - }); - - }); - - describe('authorName function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('authorName'); - }); - - it('should fetch author.name property from package.json', function() { - expect(fetch.authorName()).to.be.a('string'); - }); - - it('should fetch author name property from ~/.gitconfig', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.authorName()).to.be.a('string'); - }); - - }); - - describe('authorEmail function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('authorEmail'); - }); - - it('should fetch author.email property from package.json', function() { - expect(fetch.authorEmail()).to.include('.com'); - }); - - it('should fetch author email property from ~/.gitconfig', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.authorEmail()).to.include('.com'); - }); - - }); - - describe('authorUrl function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('authorUrl'); - }); - - it('should fetch author.url property from package.json', function() { - expect(fetch.authorUrl()).to.be.a('string'); - }); - - }); - - describe('gitEmail function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('gitEmail'); - }); - - it('should fetch the git user email address', function() { - expect(fetch.gitEmail()).to.be.a('string'); - }); - - }); - - describe('githubUsername function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('githubUsername'); - }); - - it('should fetch the GitHub username', function() { - expect(fetch.githubUsername()).to.be.a('string'); - }); - - }); - - describe('gitName function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('gitName'); - }); - - it('should fetch the git user name', function() { - expect(fetch.gitName()).to.be.a('string'); - }); - - }); - - describe('packageDescription function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageDescription'); - }); - - it('should fetch description property from package.json', function() { - expect(fetch.packageDescription()).to.include('generator'); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageDescription('default')).to.equal('default'); - expect(fetch.packageDescription('')).to.equal(''); - }); - - }); - - describe('packageDestination function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageDestination'); - }); - - it('should fetch destination directory for the repo/package', function() { - expect(fetch.packageDestination('./')).to.include('/'); - }); - - }); - - describe('packageLicense function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageLicense'); - }); - - it('should fetch license property from package.json', function() { - expect(fetch.packageLicense()).to.equal('M'); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageLicense('MIT')).to.equal('M'); - expect(fetch.packageLicense('')).to.equal(''); - }); - - }); - - describe('packageName function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageName'); - }); - - it('should fetch name property from package.json', function() { - expect(fetch.packageName()).to.include('generator'); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageName('default')).to.equal('default'); - }); - - }); - - describe('packageRepository function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageRepository'); - }); - - it('should fetch repository property from package.json', function() { - expect(fetch.packageRepository()).to.include('yo-repo'); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageRepository('default')).to.include('default'); - expect(fetch.packageRepository('')).to.include('-package'); - }); - - }); - - describe('packageVersion function', function() { - - it('should exist', function() { - expect(fetch).to.respondTo('packageVersion'); - }); - - it('should fetch version property from package.json', function() { - expect(fetch.packageVersion()).to.match(/\d+\.\d+\.\d+\-?[\w\d\_\-\.]*/); - }); - - it('should return a default value when package.json does not exist', function() { - fetch.resetPackageJsonPath('missing.json'); - expect(fetch.packageVersion('0.0.0-development')).to.equal('0.0.0-development'); - expect(fetch.packageVersion('')).to.equal(''); - }); - - }); - -}); diff --git a/src/shared/yo-repo-fetch.ts b/src/shared/yo-repo-fetch.ts deleted file mode 100644 index ca36093..0000000 --- a/src/shared/yo-repo-fetch.ts +++ /dev/null @@ -1,330 +0,0 @@ -import { sync as emptyDirSync } from 'empty-dir'; -import { existsSync } from 'fs-extra'; -// import githubUsername = require('github-username'); -import { get, isString, startCase, trimEnd } from 'lodash'; -import { hostname, userInfo } from 'os'; -// import { sync as gitConfigSync } from 'parse-git-config'; -import { resolve as pathResolve } from 'path'; -import { exec as shExec, which as shWhich } from 'shelljs'; -import spdxCorrect = require('spdx-correct'); - -/** - * Fetch, a utility for finding common variables and values - * - * @export - * @class Fetch - */ -export class Fetch { - - cache: Map; - packageJsonPath: string; - - constructor() { - this.resetPackageJsonPath(); - this.cache = new Map(); - } - - resetPackageJsonPath(value = 'package.json') { - this.packageJsonPath = value; - } - - /** - * Fetch author name - * - * Try to find author name via; - * - require(package.json).author.name - * - * @param {string} [defaultValue] - * @returns {string} - * @memberof Fetch - */ - authorName(defaultValue?: string): string { - let name = this._getFromPackage('author.name', this._getFromPackage('author', defaultValue)); - if (isString(name)) name = trimEnd((name.match(/^[^\<\(]+/g) || []).join('')); - if (!get(name, 'length')) { - // let config = gitConfigSync(); - // if (typeof config === 'undefined') config = gitConfigSync({ path: '~/.gitconfig' }); - // name = (typeof config.user !== 'undefined') ? config.user.name : null; - name = this.gitName(); - } - if (!get(name, 'length')) name = startCase(this.username()); - return name; - } - - /** - * Fetch author email - * - * Try to find author email via; - * - require(package.json).author.email - * - * @param {string} [defaultEmail] - * @returns {string} - * @memberof Fetch - */ - authorEmail(defaultEmail?: string): string { - const author = this._getFromPackage('author.name', this._getFromPackage('author', null)); - let email = this._getFromPackage('author.email'); - if (isString(author) && typeof email === 'undefined') { - email = (author.match(/<[^@]+.+(?=>)/g) || []).join('').substr(1); - } - if (!get(email, 'length')) { - // let config = gitConfigSync(); - // if (typeof config === 'undefined') config = gitConfigSync({ path: '~/.gitconfig' }); - // email = (typeof config.user !== 'undefined') ? config.user.email : null; - email = this.gitEmail(); - } - if (!get(email, 'length')) { - email = defaultEmail || `${userInfo().username}@${hostname()}`; - } - return email; - } - - /** - * Fetch author URL - * - * Try to find author URL via; - * - require(package.json).author.url - * - * @param {*} [username=this.userName()] - * @param {string} [defaultValue] - * @returns {string} - * @memberof Fetch - */ - authorUrl(username = this.username(), defaultValue?: string): string { - const author = this._getFromPackage('author.name', this._getFromPackage('author', null)); - let url = this._getFromPackage('author.url', defaultValue); - if (isString(author) && typeof url === 'undefined') { - url = (author.match(/\([^\<\>]+(?=\))/g) || []).join('').substr(1); - } - if (!get(url, 'length')) url = `https://github.com/${username}`; - return url; - } - - /** - * Fetch git email address - * - * Try to find git email address via; - * - git config --get user.email - * - * @returns {string} - * @memberof Fetch - */ - gitEmail(): string { - const cacheKey = 'gitEmail_' + process.cwd(); - let email = this.cache.get(cacheKey); - if (email) { - return email; - } - if (shWhich('git')) { - email = shExec('git config --get user.email', { silent: true }).stdout.toString().trim(); - this.cache.set(cacheKey, email); - } - return email; - } - - /** - * Fetch GitHub Username - * - * Try to find GitHub Username via; - * - sub-string of this.gitEmail() - * - * @returns {string} - * @memberof Fetch - */ - githubUsername(): string { - const gitEmail = this.gitEmail(); - if (gitEmail.indexOf('@') !== -1 && gitEmail.indexOf('github') !== -1) { - return gitEmail.substr(0, gitEmail.indexOf('@')); - } - return undefined; - } - - /** - * Fetch git name - * - * Try to find git name via; - * - git config --get user.name - * - * @returns {string} - * @memberof Fetch - */ - gitName(): string { - const cacheKey = 'gitName_' + process.cwd(); - let name = this.cache.get(cacheKey); - if (name) { - return name; - } - if (shWhich('git')) { - name = shExec('git config --get user.name', { silent: true }).stdout.toString().trim(); - this.cache.set(cacheKey, name); - } - return name; - } - - /** - * Fetch package description - * - * Try to find package description via; - * - require(package.json).description - * - * @param {string} [defaultValue=`A description for ${this.packageName()}`] - * @returns {string} - * @memberof Fetch - */ - packageDescription(defaultValue = `A description for ${this.packageName()}`): string { - let description = this._getFromPackage('description', defaultValue); - if (!get(description, 'length')) description = defaultValue; - return description; - } - - /** - * Fetch package license - * - * Try to find package license via; - * - require(package.json).license - * - * @param {string} [defaultValue='MIT'] - * @returns {string} - * @memberof Fetch - */ - packageLicense(defaultValue = 'MIT'): string { - let license = this._getFromPackage('license', defaultValue); - license = (!get(license, 'length')) ? defaultValue : get(spdxCorrect(license), '0', ''); - if (!get(license, 'length')) license = defaultValue; - return license; - } - - /** - * Fetch package name - * - * Try to find package name via; - * - require(package.json).name - * - * @param {string} [defaultValue=`${this.userName()}-package`] - * @returns {string} - * @memberof Fetch - */ - packageName(defaultValue = `${this.username()}-package`): string { - const name = this._getFromPackage('name', defaultValue); - if (!get(name, 'length')) { - if (emptyDirSync(process.cwd()) || existsSync(pathResolve('.git'))) { - return (process.cwd().match(/[^\/]+$/g) || [defaultValue]).join(''); - } - return defaultValue; - } - return name; - } - - /** - * Fetch package destination - * - * Try to find package destination (directory or path) via; - * - resolve path via parameter - * - current directory - * - * @param {string} directoryOrPath - * @returns {string} - * @memberof Fetch - */ - packageDestination(directoryOrPath: string): string { - if (emptyDirSync(process.cwd()) || existsSync(pathResolve('.git'))) { - return process.cwd(); - } - return pathResolve(directoryOrPath); - } - - /** - * Fetch package repository URL - * - * Try to find package repository URL via; - * - require(package.json).repository.url - * - require(package.json).repository - * - * @param {*} [username=this.userName()] - * @param {string} packageName - * @returns {string} - * @memberof Fetch - */ - packageRepository(username = this.username(), packageName?: string): string { - if (!packageName) packageName = this.packageName(`${username}s-package`); - const defaultValue = `https://github.com/${username}/${packageName}`; - let repository = this._getFromPackage('repository', defaultValue); - if (!get(repository, 'length')) repository = defaultValue; - return repository; - } - - /** - * Fetch package version - * - * Try to find package version via; - * - require(package.json).version - * - * @param {string} [defaultVersion='0.0.0'] - * @returns {string} - * @memberof Fetch - */ - packageVersion(defaultVersion = '0.0.0'): string { - let version = this._getFromPackage('version', defaultVersion); - if (!get(version, 'length')) version = defaultVersion; - return version; - } - - /** - * Fetch username - * - * Try to find username via; - * - require(package.json).repository.url - * - require(package.json).repository - * - require(package.json).homepage.url - * - require(package.json).homepage - * - require(package.json).author.email - * - require(package.json).author - * - * @param {*} [defaultValue=this.githubUsername()] - * @returns {string} - * @memberof Fetch - */ - username(defaultValue = this.githubUsername()): string { - let username = ''; - if (existsSync(pathResolve(this.packageJsonPath))) { - const pkg = require(pathResolve(this.packageJsonPath)); - username = ( - ( - get(pkg, 'repository.url', pkg.repository) || - get(pkg, 'homepage.url', pkg.homepage) || - '' - ).match(/github\.com\/[^\/]+/g) || [] - ) - .join('') - .substr(11); - } - if (username.length >= 1) { - return username; - } - if (typeof defaultValue !== 'undefined') { - return defaultValue; - } - return userInfo().username; - } - - /** - * Get value from a property in package.json file - * - * @param {string} path - * @param {*} [defaultValue] - * @returns {*} - * @memberof Fetch - */ - _getFromPackage(path: string, defaultValue?: any): any { - const packagePath = pathResolve(this.packageJsonPath); - if (existsSync(packagePath)) { - return get(require(packagePath), path, defaultValue); - } - return defaultValue; - } - -} - -export const fetch = new Fetch(); - -export default fetch; diff --git a/src/shared/yo-repo-prompts.ts b/src/shared/yo-repo-prompts.ts index c0a7b7b..4545618 100644 --- a/src/shared/yo-repo-prompts.ts +++ b/src/shared/yo-repo-prompts.ts @@ -1,8 +1,8 @@ import chalk from 'chalk'; +import finda from 'finda'; import { resolve as pathResolve } from 'path'; import handleError from './handle-error'; -import fetch from './yo-repo-fetch'; import YoRepoInterface from './yo-repo.interface'; export interface YoRepoPromptsInterface { @@ -246,7 +246,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { 'Example: ' + `@${this.username}\n` + ' Reference: require(package.json).author.name', ), - default: fetch.authorName(`@${this.username}`), + default: finda.authorName(`@${this.username}`), }); } @@ -325,7 +325,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { 'How would you describe your repo/package?', 'Package Description:', ), - default: fetch.packageDescription(), + default: finda.packageDescription(), }); } @@ -345,7 +345,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { 'Destination directory:', 'Default is the current directory.', ), - default: fetch.packageDestination(this.packageName), + default: finda.packageDestination(this.packageName), }); return pathResolve(destination); } @@ -444,7 +444,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { ' Reference: require(package.json).name\n' + ' For more info; https://docs.npmjs.com/files/package.json#name', ), - default: fetch.packageName(), + default: finda.packageName(), }); } @@ -482,7 +482,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { 'Repository Name:', 'Example: https://github.com/<%= profile_name %>/<%= repository_name %>', ), - default: fetch.packageName(), + default: finda.packageName(), }); } @@ -521,7 +521,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { githubUsername = res; }) .catch((err) => { - if (typeof githubUsername === 'undefined') githubUsername = fetch.username(); + if (typeof githubUsername === 'undefined') githubUsername = finda.username(); }); return this.yo.optionOrPrompt({ type: 'input', @@ -552,7 +552,7 @@ export class YoRepoPrompts implements YoRepoPromptsInterface { ' Reference: require(package.json).version\n' + ' For more info; https://semver.org', ), - default: fetch.packageVersion('0.0.0'), + default: finda.packageVersion('0.0.0'), }); }