简体   繁体   中英

Using an variable object inside a Gruntfile

I'm trying to avoid duplicate code by using a variable object inside a Gruntfile with a set of specified parameters. I apologize if this is declared incorrectly, as I'm not entirely sure how to create an object variable in gruntjs. The goal is to use sonarProperties inside the sonarRunner config. In the if block, add some additional lines, and the else block, just use sonarProperties. Unfortunately my syntax is incorrect. Is this even possible? I'm basing it off of a gulpfile and would like to do something similar.

Sample gulpfile:

const packageName = require('./package.json').name;
gulp.task('sonar', callback => {
  let sonarProperties = {
    // #################################################
    // # General Configuration
    // #################################################
    'sonar.projectKey': `microservice:${packageName}`,

    'sonar.sourceEncoding': 'UTF-8',
    'sonar.login': process.env.SONAR_TOKEN,

    // #################################################
    // # Javascript Configuration
    // #################################################
    'sonar.language': 'javascript',
    'sonar.sources': 'src',
    'sonar.tests': 'test',
    'sonar.javascript.lcov.reportPaths': 'coverage/lcov.info',
    'sonar.coverage.exclusions': 'src/**/*.spec.js',
  };

  if (process.env.SONAR_ANALYSIS_TYPE === 'pr') {
    sonarProperties = {
      ...sonarProperties, // #################################################
      // # Github Configuration
      // #################################################
      'sonar.pullrequest.provider': 'github',
      'sonar.pullrequest.branch': process.env.branch,
      'sonar.pullrequest.key': process.env.pr_numbers,
      'sonar.pullrequest.base': process.env.base_branch,
      'sonar.pullrequest.github.repository': process.env.repo,
      'sonar.scm.revision': process.env.sha,
    };
  }

Here's the pertinent points of my gruntfile:

sonarProperties: [{
    projectKey: 'microservice:<%= pkg.name %>',
    projectName: 'Microservice - <%= pkg.name %>',

    sourceEncoding: 'UTF-8',
    login: 'admin',
    password: 'admin',

    host: {
      url: 'http://localhost:9000/'
    },

    language: 'js',
    sources: 'js',
    tests: 'test',
    testExecutionReportPaths: 'test_coverage_reporter/report.xml',
    javascript: {
      lcov: {
        reportPaths: 'test_coverage/lcov.info'
      }
    },
  }],

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    sonarRunner: {
      analysis: {
        options: {
          debug: true,
          separator: '\n',
          sonar: (function() {
            if (process.env.SONAR_ANALYSIS_TYPE === 'pr') {
              return {
                ...sonarProperties
                moreParams: someData,
              };
            } else {
              return {
                // use just sonarProperties
              };
            }
          }())

        }
      }
    }

  });

I was able to create the function with the following:

grunt.registerTask('sonar', function () {
  let sonarProperties = {
      // #################################################
      // # General Configuration
      // #################################################
      .. 
}

And declaring it as a callback from the beginning as a grunt task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM