ensureArray function in Tsup source code.

Ramu NarasingaRamu Narasinga
2 min read

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(',')
}

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

Example 2:

you will find this below code, at line 116

if (flags.external) {
  const external = ensureArray(flags.external)
  options.external = external
}

Example 3:

you will find this below code, at line 137

if (flags.inject) {
  const inject = ensureArray(flags.inject)
  options.inject = inject
}

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],
    }
  }, {})
}

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

0
Subscribe to my newsletter

Read articles from Ramu Narasinga directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ramu Narasinga
Ramu Narasinga

I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.