This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make plugin more easily extendable
- Loading branch information
Showing
5 changed files
with
99 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
// tslint:disable no-console | ||
let debug: any | ||
try { debug = require('debug') } catch {} | ||
|
||
export default (...scope: string[]) => (...args: any[]) => { | ||
if (debug) debug(['@anycli/config', ...scope].join(':'))(...args) | ||
function displayWarnings() { | ||
if (process.listenerCount('warning') > 1) return | ||
process.on('warning', (warning: any) => { | ||
console.error(warning.stack) | ||
if (warning.detail) console.error(warning.detail) | ||
}) | ||
} | ||
|
||
export default (...scope: string[]) => { | ||
if (!debug) return (..._: any[]) => {} | ||
const d = debug(['@anycli/config', ...scope].join(':')) | ||
if (d.enabled) displayWarnings() | ||
return (...args: any[]) => d(...args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,6 @@ | ||
import * as Globby from 'globby' | ||
import * as path from 'path' | ||
|
||
import {Command} from './command' | ||
import Debug from './debug' | ||
|
||
export interface Manifest { | ||
version: string | ||
commands: {[id: string]: Command} | ||
} | ||
|
||
const debug = Debug() | ||
|
||
export namespace Manifest { | ||
export type FindCommandCB = (id: string) => Command.Class | ||
|
||
export function build(version: string, dir: string, findCommand: FindCommandCB): Manifest { | ||
let globby: typeof Globby | ||
try { | ||
globby = require('globby') | ||
} catch { | ||
debug('not loading plugins, globby not found') | ||
return {} as any | ||
} | ||
debug(`loading IDs from ${dir}`) | ||
const ids = globby.sync(['**/*.+(js|ts)', '!**/*.+(d.ts|test.ts|test.js)'], {cwd: dir}) | ||
.map(file => { | ||
const p = path.parse(file) | ||
const topics = p.dir.split('/') | ||
let command = p.name !== 'index' && p.name | ||
return [...topics, command].filter(f => f).join(':') | ||
}) | ||
debug('found ids', ids) | ||
let commands = ids.map(id => { | ||
try { | ||
return [id, Command.toCached(findCommand(id))] | ||
} catch (err) { | ||
process.emitWarning(err) | ||
} | ||
}) | ||
|
||
return { | ||
version, | ||
commands: commands | ||
.filter((f): f is [string, Command] => !!f) | ||
.reduce((commands, [id, c]) => { | ||
commands[id] = c | ||
return commands | ||
}, {} as {[k: string]: Command}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters