github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/circle/internal/orbs/node/node.go (about)

     1  // Copyright 2022 Harness, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package node
    16  
    17  import (
    18  	"fmt"
    19  
    20  	circle "github.com/drone/go-convert/convert/circle/yaml"
    21  	harness "github.com/drone/spec/dist/go"
    22  )
    23  
    24  // Convert converts an Orb to a Harness step.
    25  func Convert(command string, step *circle.Custom) *harness.Step {
    26  	switch command {
    27  	case "install", "install-packages", "install-yarn":
    28  		return convertInstallCmd(step)
    29  	case "run":
    30  		return convertRunJob(step)
    31  	case "test":
    32  		return convertTestJob(step)
    33  	default:
    34  		return nil
    35  	}
    36  }
    37  
    38  // helper function converts a node/test job
    39  func convertTestJob(step *circle.Custom) *harness.Step {
    40  	var steps []*harness.Step
    41  	steps = append(steps, convertInstallCmd(step))
    42  	steps = append(steps, convertTestCmd(step))
    43  	return &harness.Step{
    44  		Name: "test",
    45  		Type: "group",
    46  		Spec: &harness.StepGroup{
    47  			Steps: steps,
    48  		},
    49  	}
    50  }
    51  
    52  // helper function converts a node/test
    53  // command to a run step.
    54  //
    55  // https://github.com/CircleCI-Public/node-orb/blob/master/src/jobs/test.yml
    56  func convertTestCmd(step *circle.Custom) *harness.Step {
    57  	cmd := "test"
    58  	run := "npm %s"
    59  
    60  	switch step.Params["test-results-for"] {
    61  	case "mocha":
    62  		run = `npm run %s -- --reporter mocha-multi --reporter-options spec=-,mocha-junit-reporter=-`
    63  	case "jest":
    64  		run = `npm run %s -- --reporters=default --reporters=jest-junit`
    65  	}
    66  
    67  	if p, ok := step.Params["run-command"].(string); ok {
    68  		cmd = p
    69  	}
    70  
    71  	return &harness.Step{
    72  		Name: "run_tests",
    73  		Type: "script",
    74  		Spec: &harness.StepExec{
    75  			Run: fmt.Sprintf(run, cmd),
    76  		},
    77  	}
    78  }
    79  
    80  // helper function converts a node/test job
    81  func convertRunJob(step *circle.Custom) *harness.Step {
    82  	var steps []*harness.Step
    83  	steps = append(steps, convertInstallCmd(step))
    84  	steps = append(steps, convertRunCmd(step))
    85  	return &harness.Step{
    86  		Name: "test",
    87  		Type: "group",
    88  		Spec: &harness.StepGroup{
    89  			Steps: steps,
    90  		},
    91  	}
    92  }
    93  
    94  // helper function converts a node/run
    95  // orb to a run step.
    96  func convertRunCmd(step *circle.Custom) *harness.Step {
    97  	manager := "npm"
    98  	command := "ci"
    99  	if s, _ := step.Params["yarn-run"].(string); s != "" {
   100  		manager = "yarn"
   101  		command = s
   102  	}
   103  	if s, _ := step.Params["npm-run"].(string); s != "" {
   104  		manager = "npm"
   105  		command = s
   106  	}
   107  	return &harness.Step{
   108  		Name: fmt.Sprintf("%s_run", manager),
   109  		Type: "script",
   110  		Spec: &harness.StepExec{
   111  			Run: fmt.Sprintf("%s run %s", manager, command),
   112  		},
   113  	}
   114  }
   115  
   116  // helper function converts a node/install-packages
   117  // command to a run step.
   118  func convertInstallCmd(step *circle.Custom) *harness.Step {
   119  	if step.Params["install-yarn"] == true ||
   120  		step.Params["pkg-manager"] == "yarn" {
   121  		return &harness.Step{
   122  			Name: "install_packages",
   123  			Type: "script",
   124  			Spec: &harness.StepExec{
   125  				Run: "yarn install",
   126  			},
   127  		}
   128  	}
   129  	return &harness.Step{
   130  		Name: "install_packages",
   131  		Type: "script",
   132  		Spec: &harness.StepExec{
   133  			Run: "npm install",
   134  		},
   135  	}
   136  }