Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional shorter syntax #4

Open
timfish opened this issue Jul 24, 2017 · 13 comments
Open

Optional shorter syntax #4

timfish opened this issue Jul 24, 2017 · 13 comments

Comments

@timfish
Copy link

timfish commented Jul 24, 2017

makfy is just what I've been looking for! There are a couple of things its missing to make it perfect for me but I'm happy to contribute.

Many of my commands are simple one-liners with not much else. The rest simply call other commands in series and parallel. This makes the full syntax quite verbose in places.

Could users optionally just set the command to array of commands which get passed straight to await exec(...)? It appears this would still work even with stacked series/parallel arrays or commands.

module.exports = {
  commands: {
    clean: {
      run: async (exec) => {
        await exec('del-cli ./dist/**');
      }
    },
    build: {
      run: async (exec) => {
        await exec('tsc --pretty');
      }
    }
  }
};

becomes:

module.exports = {
  commands: {
    clean: ['del-cli ./dist/**'],
    build: ['tsc --pretty']
  }
};
@xaviergonz
Copy link
Owner

xaviergonz commented Jul 25, 2017

@timfish nice idea :)

In the meanwhile (while it gets implemented) I suggest you to use something like this

const run = (...commands) => {
  return {
    run: async(exec) => {
      await exec(...commands);
    }
  };
}


module.exports = {
  commands: {
    clean: run('del-cli ./dist/**', 'another command'),
    build: run('tsc --pretty')
  }
};

Do you think it is better to use a built-in 'run' approach or an array approach?

@timfish
Copy link
Author

timfish commented Jul 25, 2017

Great, one of the plus points of it being pure JavaScript!

One downside is that command names are limited to valid JavaScript. Automatic camel to kebab conversion buildTs -> build-ts would help.

Wildcard commands in parallel would be great too @build-*

@wmertens
Copy link

wmertens commented Jul 26, 2017 via email

@timfish
Copy link
Author

timfish commented Jul 26, 2017

I did try that but it errors.

// snip

module.exports = {
  commands: {
    clean: run('del-cli ./dist/**', 'another command'),
    'build-ts': run('tsc --pretty')
  }
};

and then:

> makfy --list 
using command file 'makfyfile.js'...
[ERROR] commands - 0: instance additionalProperty "build-ts" exists in instance when not allowed

@xaviergonz
Copy link
Owner

xaviergonz commented Jul 26, 2017

so basically you want buildTs to be also invokable by using build-ts?
is it ok even if the help command actually lists buildTs ?

or alternatively I could remove the restriction for commands to be valid JS identifiers

@xaviergonz
Copy link
Owner

xaviergonz commented Jul 26, 2017

As for wildcommands in parallel/series you can try (note - untested)

const expandCommands = (commands, startsWithName) => {
  return Object.keys(commands).filter(v => v.startsWith(startsWithName)).map(v => `@${v}`);
}

module.exports = {
  commands: {
    clean: {
      run: async (exec, args, utils) => {
        const allCommands = utils.makfyContext.commands; // or module.exports.commands
        // in series
        await exec(...expandCommands(allCommands, 'clean:');
        // in parallel
        await exec([ ...expandCommands(allCommands, 'clean:') ]);
      }
    }
  }
};

alternative implementation that would work with run:

const expandCommands = (startsWithName) => {
  return Object.keys(module.exports.commands).filter(v => v.startsWith(startsWithName)).map(v => `@${v}`);
}

module.exports = {
  commands: {
    clean: run(...expandCommands('clean:')), // parallel: run([ ...expandCommands('clean:') ]),
  }
};

@timfish
Copy link
Author

timfish commented Jul 26, 2017

@xaviergonz Looks good, I'll give it a try!

Currently in my npm scripts I have build:ts, build:html, build:scss plus more and then build which calls all those in parallel.

Underscores could work equally well here to denote the hierarchy and are valid js so no camel/kebab/etc case conversion would actually be unnecessary.

@xaviergonz
Copy link
Owner

I just released v1.2.0:

  • added support for commands/args/enum arg values with '-', '_' and ':' characters
  • 'fooBar' names are no longer automatically transformed to 'foo-bar' and vice-versa

Now you can freely use build:ts, build-ts or whatever as your command names :)

@xaviergonz
Copy link
Owner

Also I just updated the snipped above to show how it would work with the "run" function

@timfish
Copy link
Author

timfish commented Jul 26, 2017

Thanks!

Just realised for wildcard searching I have to be careful not to call the current function and create a huge recursive crash!

@xaviergonz
Copy link
Owner

either that or change the filter to

filter(v => v !== startsWithName && v.startsWith(startsWithName))

:)

@xaviergonz
Copy link
Owner

new in 1.4.0

Short syntax

If your command doesn't need any arguments or any description then you can use the run function to make it shorter to write:

js

const { run } = require('makfy');
...
  clean: run(
    // running sequentially
    "rimraf ./dist-a",
    "rimraf ./dist-b",
    // and these run after the other ones too, but in parallel!
    ["rimraf ./dist-c", "rimraf ./dist-d"]
  )

ts

import { run } from 'makfy';
...
  // note how command() is not needed when using run
  clean: run(
    // running sequentially
    "rimraf ./dist-a",
    "rimraf ./dist-b",
    // and these run after the other ones too, but in parallel!
    ["rimraf ./dist-c", "rimraf ./dist-d"]
  )

@xaviergonz
Copy link
Owner

xaviergonz commented Sep 9, 2018

not closing this since I still have to add the "*" commands in parallel and sequential

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants