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

Commit

Permalink
fix: readme
Browse files Browse the repository at this point in the history
Fixes #24
Fixes #41
  • Loading branch information
jdx committed Jun 15, 2018
1 parent d5e231e commit 4ca47f2
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions src/commands/readme.ts
Expand Up @@ -156,32 +156,57 @@ USAGE
if (!pluginName) return
let plugin = config.plugins.find(p => p.name === c.pluginName)
if (!plugin) return
normalize(plugin.pjson)
let repo = plugin.pjson.repository
let commandsDir = plugin.pjson.oclif.commands
if (!repo || !repo.url || !commandsDir) return
let commandPath = `${commandsDir.replace('./', '')}/${c.id.replace(/:/g, '/')}.js`
if (process.platform !== 'win32') {
// TODO: make this also work on windows
let base = plugin.name === config.name ? config.root : `${config.root}/node_modules/${plugin.name}`
commandPath = require.resolve(base + '/' + commandPath.replace(/\.js$/, ''))
commandPath = commandPath.replace(base + '/', '')
}
if (plugin.pjson.devDependencies.typescript) {
commandPath = commandPath.replace(/^lib\//, 'src/')
commandPath = commandPath.replace(/\.js$/, '.ts')
}
repo = repo.url.split('+')[1].replace(/\.git$/, '')
const repo = this.repo(plugin)
if (!repo) return
let label = plugin.name
let version = plugin.version
let commandPath = this.commandPath(config, plugin, c)
if (!commandPath) return
if (config.name === plugin.name) {
label = commandPath
version = process.env.OCLIF_NEXT_VERSION || version
}
return `_See code: [${label}](${repo}/blob/v${version}/${commandPath})_`
}

commandUsage(command: Config.Command): string {
private repo(plugin: Config.IPlugin): string | undefined {
const pjson = {...plugin.pjson}
normalize(pjson)
let repo = pjson.repository && pjson.repository.url
if (!repo) return
let url = new URL(repo)
if (!['github.com', 'gitlab.com'].includes(url.hostname)) return
return `https://${url.hostname}${url.pathname.replace(/\.git$/, '')}`
}

/**
* fetches the path to a command
*/
private commandPath(config: Config.IConfig, plugin: Config.IPlugin, c: Config.Command): string | undefined {
let commandsDir = plugin.pjson.oclif.commands
if (!commandsDir || process.platform === 'win32') return
let commandPath = `${commandsDir.replace('./', '')}/${c.id.replace(/:/g, '/')}.js`
let root = config.root
let base: string
while (root !== '/') {
base = plugin.name === config.name ? root : `${root}/node_modules/${plugin.name}`
try {
commandPath = require.resolve(base + '/node_modules/' + commandPath.replace(/\.js$/, ''))
break
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err
root = path.dirname(root)
}
}
commandPath = commandPath.replace(base! + '/', '')
if (plugin.pjson.devDependencies.typescript) {
commandPath = commandPath.replace(/^lib\//, 'src/')
commandPath = commandPath.replace(/\.js$/, '.ts')
}
return commandPath
}

private commandUsage(command: Config.Command): string {
const arg = (arg: Config.Command.Arg) => {
let name = arg.name.toUpperCase()
if (arg.required) return `${name}`
Expand Down

0 comments on commit 4ca47f2

Please sign in to comment.