#!/usr/bin/env node
// This file is part of gltfpack and is distributed under the terms of MIT License.
var gltfpack = require('./library.js');

var fs = require('fs');
var cp = require('child_process');

var args = process.argv.slice(2);

var paths = {
	"basisu": process.env["BASISU_PATH"],
	"toktx": process.env["TOKTX_PATH"],
};

var interface = {
	read: function (path) {
		return fs.readFileSync(path);
	},
	write: function (path, data) {
		fs.writeFileSync(path, data);
	},
	execute: function (command) {
		// perform substitution of command executable with environment-specific paths
		var pk = Object.keys(paths);
		for (var pi = 0; pi < pk.length; ++pi) {
			if (command.startsWith(pk[pi] + " ")) {
				command = paths[pk[pi]] + command.substr(pk[pi].length);
				break;
			}
		}

		var ret = cp.spawnSync(command, [], {shell:true});
		return ret.status == null ? 256 : ret.status;
	},
	unlink: function (path) {
		fs.unlinkSync(path);
	},
};

gltfpack.pack(args, interface)
	.then(function (log) {
		process.stdout.write(log);
		process.exit(0);
	})
	.catch(function (err) {
		process.stderr.write(err.message);
		process.exit(1);
	});

#!/usr/bin/env node
// This file is part of gltfpack and is distributed under the terms of MIT License.
var gltfpack = require('./library.js');

var fs = require('fs');
var cp = require('child_process');

var args = process.argv.slice(2);

var paths = {
	"basisu": process.env["BASISU_PATH"],
	"toktx": process.env["TOKTX_PATH"],
};

var interface = {
	read: function (path) {
		return fs.readFileSync(path);
	},
	write: function (path, data) {
		fs.writeFileSync(path, data);
	},
	execute: function (command) {
		var arg = command.split(' ');
		var exe = arg.shift();

		// perform substitution of command executable with environment-specific paths
		exe = paths[exe] || exe;

		var ret = cp.spawnSync(exe, arg);
		return ret.status == null ? 256 : ret.status;
	},
	unlink: function (path) {
		fs.unlinkSync(path);
	},
};

gltfpack.pack(args, interface)
	.then(function (log) {
		process.stdout.write(log);
		process.exit(0);
	})
	.catch(function (err) {
		process.stderr.write(err.message);
		process.exit(1);
	});

Fix texture compression spawn in node.js
gltfpack: Fix texture compression spawn in node.js

Our previous code blindly replaced the executable in the input command
with the env. variable value, which actually didn't handle cases when
the environment variable wasn't set and additionally didn't handle paths
with spaces well.

We now use shell:false execution option and give it a pre-split command
line - this doesn't work for arguments with spaces but we never handled
them correctly (and also they shouldn't occur as the paths that are
passed to the compressor are that of temp files...).
