Skip to content

Instantly share code, notes, and snippets.

@Salakar Salakar/index.js Secret
Last active Apr 3, 2019

Embed
What would you like to do?
A sample Rift plugin implementation.
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;
@Salakar

This comment has been minimized.

Copy link
Owner Author

Salakar commented Dec 28, 2018

Anatomy of a Plugin

Plugins can extend the CLI in five key ways.

  • Actions - global.Actions- shared actions with configurable input definitions that can be used by any plugin, e.g. Actions.openUrl is an action.
  • Commands
    • Define CLI commands that can be run by users, including in REPL mode.
  • Generators - global.Generators
    • Define templates to generate anything from a new Project to a React Native Module.
  • Helpers - global.Helpers
    • Helper utilities that can be consumed by actions, commands, tasks and generators.
  • Tasks - global.Tasks- Executable in any process, configurable to be fire and forget detachable or run backgrounded in CLI REPL mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.