All files / src/context/directory/handlers actions.ts

92.15% Statements 47/51
68.75% Branches 11/16
90% Functions 9/10
95.83% Lines 46/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114  1x 1x 1x   1x 1x       1x         53x   53x   3x 3x 3x           3x   3x 3x 3x 3x     3x     3x       1x     1x       1x   1x       1x 1x 1x   1x 1x 1x   1x       1x                           2x 2x     2x 2x 1x     1x   1x       2x 2x 2x   1x 1x 1x 1x       1x         1x  
/* eslint-disable consistent-return */
import fs from 'fs-extra';
import path from 'path';
import { constants } from '../../../tools';
 
import { getFiles, existsMustBeDir, loadJSON, sanitize } from '../../../utils';
import log from '../../../logger';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
import { Action, isMarketplaceAction } from '../../../tools/auth0/handlers/actions';
 
type ParsedActions = ParsedAsset<'actions', Asset[]>;
 
function parse(context: DirectoryContext): ParsedActions {
  const actionsFolder = path.join(context.filePath, constants.ACTIONS_DIRECTORY);
 
  if (!existsMustBeDir(actionsFolder)) return { actions: null }; // Skip
 
  const files = getFiles(actionsFolder, ['.json']);
  const actions = files.map((file) => {
    const action = {
      ...loadJSON(file, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      }),
    };
    const actionFolder = path.join(constants.ACTIONS_DIRECTORY, `${action.name}`);
 
    Eif (action.code) {
      const toUnixPath = (somePath) =>
        somePath.replace(/[\\/]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '');
      action.code = context.loadFile(toUnixPath(action.code), actionFolder);
    }
 
    return action;
  });
 
  return { actions };
}
 
function mapSecrets(secrets) {
  Iif (secrets && secrets.length > 0) {
    return secrets.map((secret) => ({ name: secret.name, value: secret.value }));
  }
  return [];
}
 
function mapActionCode(filePath, action) {
  const { code } = action;
 
  Iif (!code) {
    return '';
  }
 
  const actionName = sanitize(action.name);
  const actionFolder = path.join(filePath, constants.ACTIONS_DIRECTORY, `${actionName}`);
  fs.ensureDirSync(actionFolder);
 
  const codeFile = path.join(actionFolder, 'code.js');
  log.info(`Writing ${codeFile}`);
  fs.writeFileSync(codeFile, code);
 
  return `${codeFile}`;
}
 
function mapToAction(filePath, action): Partial<Action> {
  return {
    name: action.name,
    code: mapActionCode(filePath, action),
    runtime: action.runtime,
    status: action.status,
    dependencies: action.dependencies,
    secrets: mapSecrets(action.secrets),
    supported_triggers: action.supported_triggers,
    deployed: action.deployed || action.all_changes_deployed,
    installed_integration_id: action.installed_integration_id,
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { actions } = context.assets;
  Iif (!actions) return;
 
  // Marketplace actions are not currently supported for management (See ESD-23225)
  const filteredActions = actions.filter((action) => {
    if (isMarketplaceAction(action)) {
      log.warn(
        `Skipping export of marketplace action "${action.name}". Management of marketplace actions are not currently supported.`
      );
      return false;
    }
    return true;
  });
 
  // Create Actions folder
  const actionsFolder = path.join(context.filePath, constants.ACTIONS_DIRECTORY);
  fs.ensureDirSync(actionsFolder);
  filteredActions.forEach((action) => {
    // Dump template metadata
    const name = sanitize(action.name);
    const actionFile = path.join(actionsFolder, `${name}.json`);
    log.info(`Writing ${actionFile}`);
    fs.writeFileSync(actionFile, JSON.stringify(mapToAction(context.filePath, action), null, 2));
  });
}
 
const actionsHandler: DirectoryHandler<ParsedActions> = {
  parse,
  dump,
};
 
export default actionsHandler;