`npm run script` equivalent for Haxe project

I want to be able to run some command right before I start working with the code. It would run compiler, file watcher and live-reload (JS web project). The command is lengthy and due to Windows/Linux differences I need to store it in a text file in both forms (Windows uses different slashes for directories).

I know there’s Haxelib, but I couldn’t find any similar “scripts”: {} thing in the haxelib json file documentation.

Do you guys know what would help me? I’m considering using npm just for scripts but I’l like to avoid that if there’s Haxe alternative for it.

You would be able to add -cmd to your HXML file, but that occurs after your build, not before.

If you would like to have a watcher and live-reload for a JS project, using NPM might be the best approach, because there are many tools available for that task.

If you haven’t seen it already, you might check out the OpenFL samples for NPM:

https://github.com/openfl/openfl-samples/tree/master/npm

You can remove the haxe “devDependency” to use a global Haxe install, or keep it in to work with a local copy.

Otherwise, I think it turns into one of a couple options:

1.) Add another dependency (such as NPM) to help with managing scripts
2.) Write bash files for Linux and macOS, and batch files for Windows
3.) Write and compile a Neko script for use on both

If you use Neko, it would look like this:

MyScript.hx

class MyScript {
    public static function main () {
        var args = Sys.args ();
        Sys.command ("echo Hello World");
    }
}

build.hxml

-main MyScript
-neko run.n

Compile

haxe build.hxml

Run

neko run.n
2 Likes

You can use haxe --run Init arg1 arg2 ....

Define Init.hx like so:

class Init {
  static function main() {
    trace(Sys.args());
  }
}

For brevity you can also define an init.hxml:

# add more stuff here if you need, like -lib args or whatever
--run Init

And then you can run haxe init.hxml arg1 arg2 ....

3 Likes

Nice to see that there are couple of options :slight_smile:

For now I’ve chosen just running Haxe files without compilation/outputting executable files. I will have couple of files in “tasks” directory, just to keep it tidy. In my case, I already have fast-live-reload (installed globally with npm), so now I’m just running:

haxe --run tasks.TestPage --debug

(which I can configure to be run on hitting key combination in my IDE)

And here’s the content of /tasks/TestPage.hx

package tasks;

class TestPage {
  static function main() {
    var cmdArgs = Sys.args();
    var debug:Bool = (cmdArgs[0] == '--debug');

    var args:Array<String> = [
      '--interval', '50',
      '-s', 'bin/web/',
      'src/', '-e', 'flow build web${debug ? ' --debug' : ''}',
      'assets/', '-e', 'flow build web${debug ? ' --debug' : ''}'
    ];

    Sys.command('fast-live-reload', args);
  }
}

It is just awesome that Sys.command function takes care of converting slashes in my command arguments :smiley:
Thanks a lot for the input!

(I shall still test if it works fine on my linux PC tomorrow, though)

I don’t think it does. Windows can actually deal with forward slashes in paths just fine.

A bit OT: wow fast-live-reload look like a good replacement of / improvement over livereloadx.