【问题标题】:Is there anyway to split dangerfile to smaller parts?无论如何将危险文件拆分为较小的部分?
【发布时间】:2021-11-08 18:13:28
【问题描述】:

我正在尝试将危险文件拆分为小部分,因为我们添加了许多检查/自动化功能,因此它非常有用,但管理起来却变得更加困难。但是当我尝试成功时,它总是给我这个错误;

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

也许您知道频谱不再可用,我真的找不到任何方法来正常运行它。你知道它是如何工作的吗?这是我要运行的示例;

// ./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;

【问题讨论】:

    标签: danger


    【解决方案1】:

    我终于找到了解决办法。这个问题背后的原因是 Danger.js 不允许从每个文件中导入主文件(可能与 tree-shaking 有关)

    首先;我在危险文件上导入了danger.js,然后我使用了我想在其他文件上使用的函数作为函数属性。

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

    然后我将此函数发送到 OtherChecks 内的其他函数中:) 为了使其在 typescript 上工作,我还导入了具有 不同名称

    的 dangerjs
    // ./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();
        }
      }
    }
    

    在最后一步,我没有直接使用导出的 var,而是使用 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,
      };
    }
    
    

    希望对其他人有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2012-06-26
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      相关资源
      最近更新 更多