-
-
Save Salakar/7452a211abfc8d4a96269e57bb059465 to your computer and use it in GitHub Desktop.
A sample Rift plugin implementation.
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
const rr = require.resolve; | |
const { Plugin, Hook } = Helpers; | |
// provide path to package json | |
const plugin = Plugin.register(rr('./../package')); | |
const { | |
registerHook, | |
registerTask, | |
registerHelper, | |
describeConfig, | |
registerCommand, | |
registerGenerator, | |
} = plugin; | |
/* -------------- * | |
* CONFIG | |
* -------------- */ | |
describeConfig( | |
{ | |
// DEFINITION | |
caching: 'boolean?', | |
backgroundPrefetch: 'boolean?', | |
allowAnonymousFirebaseUsageStatistics: 'boolean?', | |
}, | |
{ | |
// DEFAULTS | |
caching: true, | |
backgroundPrefetch: true, | |
allowAnonymousFirebaseUsageStatistics: true, | |
} | |
); | |
/* -------------- * | |
* TASKS | |
* -------------- */ | |
registerTask( | |
{ | |
id: 'template:task', | |
name: 'My Task', | |
description: 'My Task Template', | |
// only allow one instance of this task to run per project | |
// default true | |
singleton: false, | |
// how long this task has to finish execution, 0 for indefinite | |
// default 60000 - or 0 for no timeout | |
timeout: 60000, | |
// whether the task should be independent of the main process | |
// if it's `detached` and the parent process is quit the task will still continue to run | |
// default false | |
detached: true, | |
// default true | |
ui: false, | |
}, | |
rr('./tasks/template') | |
); | |
// Tasks.run('template:task', {}); | |
/* -------------- * | |
* APIS | |
* -------------- */ | |
// -> Helpers.Template | |
registerHelper( | |
{ | |
// must be globally unique | |
name: 'Template', | |
// optional | |
description: 'Provides foo and other bars.', | |
/* custom opts */ | |
baz: 'daz', | |
}, | |
rr('./helpers/template') | |
); | |
// console.log(Helpers.Template.foo()); | |
/* -------------- * | |
* GENERATORS | |
* -------------- */ | |
// -> React Native Firebase Starter | |
registerGenerator({ | |
// must be globally unique | |
id: 'generate:template', | |
name: 'Template Generator', | |
// optional | |
description: 'Generates a plugin template.', | |
/* custom opts */ | |
baz: 'daz', | |
}, rr('./generators/template')); | |
/* -------------- * | |
* COMMANDS | |
* -------------- */ | |
registerCommand('test command', rr('./commands/template')); | |
// TODO | |
// registerCommand({ | |
// definition: 'test command', | |
// | |
// /* custom opts */ | |
// baz: 'daz', | |
// }, rr('./commands/template')); | |
/* -------------- * | |
* HOOKS | |
* -------------- */ | |
registerHook(Hook.PLUGIN_INITIALIZED, async event => { | |
console.log(event); | |
// const { initiator, plugin } = event; | |
// - This plugins provided Helpers are available | |
// - Other plugins provided Helpers are available | |
// console.log(initiator); // the task/helper/command/generator that triggered the plugin to initialize | |
}); | |
registerHook(Hook.PLUGIN_FIRST_RUN, async event => { | |
console.log(event); | |
// const { plugin, installationTimestamp } = event; | |
// const { packageJSON } = plugin; | |
}); | |
// Export the plugin | |
module.exports = plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anatomy of a Plugin
Plugins can extend the CLI in five key ways.
global.Actions
- shared actions with configurable input definitions that can be used by any plugin, e.g.Actions.openUrl
is an action.global.Generators
global.Helpers
global.Tasks
- Executable in any process, configurable to be fire and forget detachable or run backgrounded in CLI REPL mode.