Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created September 26, 2017 10:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save back2dos/65bebc2d43bb8f004a8f2aed05166b51 to your computer and use it in GitHub Desktop.
NPM based installation for haxeshim.
import Sys.*;
using sys.FileSystem;
using sys.io.File;
class NpmInstall {
static function die(reason:String, ?code = 500) {
println(reason);
exit(code);
}
static function main()
for (a in args())
install(a);
static function install(lib) {
switch command('npm', ['install', lib, '--prefix', '.']) {
case 0:
var dir = 'node_modules/$lib';
var npmVersion = haxe.Json.parse('$dir/package.json'.getContent()).version;
var haxelibJson =
try '$dir/haxelib.json'.getContent()
catch (e:Dynamic) null;
var cfg = switch haxelibJson {
case null:
println('No haxelib.json found in $dir, assuming top level class path');
{
cp: dir,
version: npmVersion,
}
case v:
var data:{ version: String, ?classPath: String } = haxe.Json.parse(v);
{
cp: switch data.classPath {
case null: dir;
case v: v;
},
version: data.version
}
}
if (!'haxe_libraries'.exists())
'haxe_libraries'.createDirectory();
'haxe_libraries/$lib.hxml'.saveContent([
'# @install: npm install $lib@$npmVersion --prefix .',
'-cp ${cfg.cp}',
'-D $lib=${cfg.version}',
try '$dir/extraParams.hxml'.getContent() catch (e:Dynamic) '',
].join('\n'));
case v: die('npm install $lib failed', v);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment