Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
fix: improve finding source code path
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 7, 2018
1 parent 0124a60 commit aaf17b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -20,7 +20,9 @@
"@anycli/errors": "^0.2.1",
"@anycli/plugin-help": "^0.6.9",
"lodash.template": "^4.4.0",
"normalize-package-data": "^2.4.0"
"normalize-package-data": "^2.4.0",
"require-relative": "^0.8.7",
"require-resolve": "^0.0.2"
},
"devDependencies": {
"@anycli/test": "^0.10.13",
Expand Down
11 changes: 7 additions & 4 deletions src/commands/readme.ts
Expand Up @@ -9,6 +9,7 @@ import * as path from 'path'
import {castArray, compact, sortBy, template, uniqBy} from '../util'

const normalize = require('normalize-package-data')
const requireResolve = require('require-resolve')

export default class Readme extends Command {
static description = 'adds commands to readme'
Expand Down Expand Up @@ -155,17 +156,19 @@ USAGE
let repo = plugin.pjson.repository
let commandsDir = plugin.pjson.anycli.commands
if (!repo || !repo.url || !commandsDir) return
commandsDir = commandsDir.replace(/\.\//, '')
if (plugin.name === config.name) pluginName = process.cwd()
let commandPath = require.resolve(`${pluginName}/${commandsDir}/${c.id.replace(/:/g, '/')}`)
commandPath = commandPath.replace(path.dirname(require.resolve(`${pluginName}/package.json`)) + '/', '')
let commandPath = `${pluginName}/${commandsDir}/${c.id.replace(/:/g, '/')}`
let resolved = requireResolve(commandPath, plugin.root)
if (!resolved) {
process.emitWarning(`command not found commandPath: ${commandPath} root: ${plugin.root}`)
}
commandPath = resolved.src.replace(resolved.pkg.root + '/', '')
if (plugin.pjson.devDependencies.typescript) {
commandPath = commandPath.replace(/^lib\//, 'src/')
commandPath = commandPath.replace(/\.js$/, '.ts')
}
repo = repo.url.split('+')[1].replace(/\.git$/, '')
return `_See code: [${plugin.name}](${repo}/blob/v${plugin.version}/${commandPath})_`
// return `_From plugin: [${plugin.name}](${plugin.pjson.homepage})_`
}

commandUsage(command: Config.Command): string {
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Expand Up @@ -524,6 +524,10 @@ parse-passwd@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/parse-passwd/-/parse-passwd-1.0.0.tgz#6d5b934a456993b23d37f40a382d6f1666a8e5c6"

path-extra@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/path-extra/-/path-extra-1.0.3.tgz#7c112189a6e50d595790e7ad2037e44e410c1166"

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
Expand All @@ -546,6 +550,16 @@ pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"

require-relative@^0.8.7:
version "0.8.7"
resolved "https://registry.yarnpkg.com/require-relative/-/require-relative-0.8.7.tgz#7999539fc9e047a37928fa196f8e1563dabd36de"

require-resolve@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/require-resolve/-/require-resolve-0.0.2.tgz#bab410ab1aee2f3f55b79317451dd3428764e6f3"
dependencies:
x-path "^0.0.2"

resolve@^1.3.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.5.0.tgz#1f09acce796c9a762579f31b2c1cc4c3cddf9f36"
Expand Down Expand Up @@ -791,6 +805,12 @@ wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"

x-path@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/x-path/-/x-path-0.0.2.tgz#294d076bb97a7706cc070bbb2a6fd8c54df67b12"
dependencies:
path-extra "^1.0.2"

yn@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/yn/-/yn-2.0.0.tgz#e5adabc8acf408f6385fc76495684c88e6af689a"

0 comments on commit aaf17b5

Please sign in to comment.