1

I'm trying to split dangerfile into small parts because we added many checks/automation so it's super helpful but it gets harder to manage it. but when I try to make it, it always gives me this error;

Unable to evaluate the Dangerfile
 
Hey there, it looks like you're trying to import the danger module. Turns out
that the code you write in a Dangerfile.js is actually a bit of a sneaky hack. 

When running Danger, the import or require for Danger is removed before the code
is evaluated. Instead all of the imports are added to the global runtime, so if
you are importing Danger to use one of it's functions - you should instead just
use the global object for the root DSL elements.

There is a spectrum thread for discussion here:
  - https://spectrum.chat/?t=0a005b56-31ec-4919-9a28-ced623949d4d

Maybe as you know that spectrum is not available anymore, I really couldn't find any way to run it properly. Do you have any idea how it would work? This is my example to run;

// ./DANGERFILE.TS
//
//
import { OtherChecks } from './dangerjs/checks/other';
OtherChecks();
// ./dangerjs/checks/other.ts
//
//
import mentor from 'danger-plugin-mentor';
import { isGoingToProd, isSkipDanger } from '../variables';

export function OtherChecks() {
  if (!isSkipDanger) {
    if (!isGoingToProd) {
      mentor();
    }
  }
}
// ./dangerjs/variable.ts
//
//
import { danger } from 'danger';
import {
  committedFiles,
  inCommit,
  prTitle,
  linkToSourceRepo,
} from 'danger-plugin-toolbox';

//
// CONSTS
//
export const packageChanged: boolean = inCommit('package.json');
export const lockfileChanged: boolean = inCommit('yarn.lock');
export const npmLockfile: boolean = inCommit('package-lock.json');
export const allFilesLen: number = committedFiles.length;
export const isWIP: boolean = prTitle.includes('[WIP]');
export const isSkipped: boolean = prTitle.includes('[SKIP]');
export const isSkipDanger: boolean = prTitle.includes('[SKIP-DANGER]');
export const { assignees } = danger.github.pr;

// gits
export const targetBranch = danger.github.pr.base.ref;
export const headBranch = danger.github.pr.head.ref;
export const isMergeRefMaster = targetBranch === 'master';
export const isMergeRefProd = targetBranch === 'production';
export const isMergeHeadDev = headBranch === 'dev';
export const isMergeHeadMaster = headBranch === 'master';
export const isGoingToProd = isMergeRefProd && isMergeHeadMaster;
the_bluescreen
  • 3,126
  • 2
  • 18
  • 31

1 Answers1

1

I found the solution finally. The reason behind this issue is that Danger.js disallows to import the main file from every file (probably related to tree-shaking)

First; I imported danger.js on the dangerfile and then I used the function that I want to use on other files as a function prop.

// ./DANGERFILE.TS
//
//
import { danger } from 'danger';
import { OtherChecks } from './dangerjs/checks/other';
OtherChecks(danger.github);

then I sent this function into other functions inside of OtherChecks :) To make it work on typescript, I also imported dangerjs with different name

// ./dangerjs/checks/other.ts
//
//
import * as __dm from 'danger';
import mentor from 'danger-plugin-mentor';
import getVariables from '../variables';

export function OtherChecks(github: typeof __dm.danger.github) {
  const { isSkipDanger, isGoingToProd } = getVariables(github);
  if (!isSkipDanger) {
    if (!isGoingToProd) {
      mentor();
    }
  }
}

On the last step, instead of using exported vars directly, I created a function with props.

import * as __dm from 'danger';
import {
  committedFiles,
  inCommit,
  inCommitGrep,
  prTitle,
  linkToSourceRepo,
} from 'danger-plugin-toolbox';

export default function variables(github: typeof __dm.danger.github) {
  const packageChanged: boolean = inCommit('package.json');
  const lockfileChanged: boolean = inCommit('yarn.lock');
  const npmLockfile: boolean = inCommit('package-lock.json');
  const readme: boolean = inCommit('readme.md');
  const allFilesLen: number = committedFiles.length;
  const isWIP: boolean = prTitle.includes('[WIP]');
  const isSkipped: boolean = prTitle.includes('[SKIP]');
  const isSkipDanger: boolean = prTitle.includes('[SKIP-DANGER]');

  // gits
  const targetBranch = github.pr.base.ref;
  const headBranch = github.pr.head.ref;
  const isMergeRefMaster = targetBranch === 'master';
  const isMergeRefProd = targetBranch === 'production';
  const isMergeHeadDev = headBranch === 'dev';
  const isMergeHeadMaster = headBranch === 'master';
  const isGoingToProd = isMergeRefProd && isMergeHeadMaster;

  return {
    modifiedMD,
    packageChanged,
    lockfileChanged,
    npmLockfile,
    allFilesLen,
    isWIP,
    isSkipped,
    isSkipDanger,
    targetBranch,
    headBranch,
    isMergeRefMaster,
    isMergeRefProd,
    isMergeHeadDev,
    isMergeHeadMaster,
    isGoingToProd,
    readme,
  };
}

I hope it would be helpful for others :)

the_bluescreen
  • 3,126
  • 2
  • 18
  • 31