sezna / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: script as function ?

Mehuge opened this issue · comments

This may be a daft idea, but anyway... I have been playing around with nps as a replacement for a build environment currently based on bash.

One thing I would like to be able to do is:

build: {
  script: buildScript,
}

where buildScript is a function that returns a string.

The reasoning behind this is that I would like to put some processing inside those functions, but I don't want that processing to run everytime nps is executed, which is what happens when doing this which is currently supported:

build: {
  script: generateBuildScript(),
}

The function would be passed input, the config key. An example buildScript function might be

function buildScript(input) {
  switch(input) {
  case "framework.update":
    const fs = require('fs');
    try {
      fs.accessSync('../framework', fs.constants.R_OK);
      return 'cd ../framework ; svn update .';
    } catch(e) {
    }
    return ': no action required';
  }
  return 'false';
}

The code to implement this is tiny, in get-script-to-run.js change getScript as follows

if (scriptName) {
    if (isPlainObject(scriptToRun)) {
      scriptToRun = resolveScriptObjectToString(scriptToRun)
    }
    if (isFunction(scriptToRun)) {
      scriptToRun = scriptToRun(input);
    }
}

I realise the project is in maintenance mode, and anyway there may be some reason supporting this doesn't make sense, but to me it feels like quite a powerful feature, but I understand if its beyond the intended scope for this project.

Hi @Mehuge,

I would worry that this makes nps more complicated and it's already more complicated than I wanted it to be. Sorry, but I don't think we'll support this feature. Thanks anyway!

Yeah no problem at all, I totally get where you are coming from, especially if you are putting this project to bed.

For anyone finding this and thinking, that would be neat. I forked this project and added the functionality https://www.npmjs.com/package/nps-plus

I ended up implementing it differently (better) and now the function can do all the work and just return success/failure status, or return null/undefined/empty string for a no-op or return a string and have those commands executed as if the string were specified directly in the config. I realise this is moving into grunt/gulp territory but I like the convenience of nps and it seems to be working well in my use case (replacing a bash script based build environment with package-scripts.js).

Couple of things it lacks atm are, better arguments support (nps "doit with something") and support for asynchronous functions, they have to be synchronous atm.

Thanks for implementing nps in the first place!

@Mehuge For those preferring monkey-patching over a separate package, I found a way to make nps accept functions for script entries: #196 (comment)

Most people would prefer your fork I think (it's less hacky), but the monkey-patch approach is available if desired.