DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Edited on • Originally published at thinkthroo.com

ensureArray function in Tsup source code.

In this article, we will review ensureArray function in Tsup source code.

We will look at:

  1. ensureArray function definition.

  2. Where is ensureArray function invoked?

ensureArray function definition

You will find the below code in cli-main.ts in Tsup source code at line 7

function ensureArray(input: string): string[] {
  return Array.isArray(input) ? input : input.split(',')
}
Enter fullscreen mode Exit fullscreen mode

This function returns input as an array of strings. For that, it first checks if the input is an array using Array.isArray method. If that is true, then the input is returned since it is an array otherwise the input is split based on comma.

Where is ensureArray function invoked?

Overall, ensureArray is used in four places in cli-main.ts.

Example 1: 

At line 112, you will find this below code

const format = ensureArray(flags.format) as Format[]
options.format = format
Enter fullscreen mode Exit fullscreen mode

Example 2:

you will find this below code, at line 116

if (flags.external) {
  const external = ensureArray(flags.external)
  options.external = external
}
Enter fullscreen mode Exit fullscreen mode

Example 3:

you will find this below code, at line 137

if (flags.inject) {
  const inject = ensureArray(flags.inject)
  options.inject = inject
}
Enter fullscreen mode Exit fullscreen mode

Example 4:

you will find this below code at line 144

if (flags.loader) {
  const loader = ensureArray(flags.loader)
  options.loader = loader.reduce((result, item) => {
    const parts = item.split('=')
    return {
      ...result,
      [parts[0]]: parts[1],
    }
  }, {})
}
Enter fullscreen mode Exit fullscreen mode

About me

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Build Shadcn CLI from scratch.

References:

  1. https://github.com/egoist/tsup/blob/main/src/cli-main.ts#L7

  2. https://github.com/egoist/tsup/blob/main/src/cli-main.ts#L112

Top comments (0)