expandArgs(args) in Nue.js source code.

Ramu NarasingaRamu Narasinga
2 min read

In this article, we will review a function named expandArgs(args) in Nue.js source code.

To give you some context, this function, expandArgs, is called in another function named getArgs. All these are part of cli.js file in Nue.js source code. Nue.js a web framework.

expandArgs

// [-npe] --> [-n, -p, -e]
export function expandArgs(args) {
  const arr = []
  args.forEach(arg => {
    if (arg[0] == '-' && arg[1] != '-' && arg[2]) {
      arg.slice(1).split('').forEach(el => arr.push('-' + el))
    } else {
      arr.push(arg)
    }
  })
  return arr
}

You will find this above code in packages/nuekit/src/cli.js. The comment just above this function explains what this function does.

Given an argument -npe , it is converted to [-n, -p, -e] and the check is straight forward too.

if (arg[0] == '-' && arg[1] != '-' && arg[2]) {

and this function is used in getArgs function as shown below:

export function getArgs(argv) {
  const commands = ['serve', 'build', 'init', 'create', 'docs']
  const args = { paths: [], root: null }
  let opt

  expandArgs(argv).forEach((arg) => {
    // skip
    if (arg == '--') {

      // test suite
    } else if (arg.endsWith('.test.js')) {
      args.test = true

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Github —  https://github.com/ramu-narasinga

My website —  https://ramunarasinga.com

My Youtube channel —  https://www.youtube.com/@ramu-narasinga

Learning platform —  https://thinkthroo.com

Codebase Architecture —  https://app.thinkthroo.com/architecture

Best practices —  https://app.thinkthroo.com/best-practices

Production-grade projects —  https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/nuejs/nue/blob/master/packages/nuekit/src/cli.js#L7

  2. https://github.com/nuejs/nue/blob/master/packages/nuekit/src/cli.js#L25

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.