amzn/style-dictionary

View on GitHub
lib/register/action.js

Summary

Maintainability
A
0 mins
Test Coverage
/*
 * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */
var chalk = require('chalk');

/**
 * Adds a custom action to the style property builder. Custom
 * actions can do whatever you need, such as: copying files,
 * base64'ing files, running other build scripts, etc.
 * After you register a custom action, you then use that
 * action in a platform your config.json
 *
 * You can perform operations on files generated by the style dictionary
 * as actions run after these files are generated.
 * Actions are run sequentially, if you write synchronous code then
 * it will block other actions, or if you use asynchronous code like Promises
 * it will not block.
 *
 * @static
 * @memberof module:style-dictionary
 * @param {Object} action
 * @param {String} action.name - The name of the action
 * @param {Function} action.do - The action in the form of a function.
 * @param {Function} [action.undo] - A function that undoes the action.
 * @returns {module:style-dictionary}
 * @example
 * ```js
 * StyleDictionary.registerAction({
 *   name: 'copy_assets',
 *   do: function(dictionary, config) {
 *     console.log('Copying assets directory');
 *     fs.copySync('assets', config.buildPath + 'assets');
 *   },
 *   undo: function(dictionary, config) {
 *     console.log('Cleaning assets directory');
 *     fs.removeSync(config.buildPath + 'assets');
 *   }
 * });
 * ```
 */
function registerAction(options) {
  if (typeof options.name !== 'string')
    throw new Error('name must be a string');
  if (typeof options.do !== 'function')
    throw new Error('do must be a function');

  this.action[options.name] = {
    do: options.do,
    undo: options.undo
  };

  return this;
}

module.exports = registerAction;