function convert()

in npm/fbx2gltf/index.js [27:95]


function convert(srcFile, destFile, opts = []) {
  return new Promise((resolve, reject) => {
    try {
      let binExt = os.type() === 'Windows_NT' ? '.exe' : '';
      let tool = path.join(__dirname, 'bin', os.type(), 'FBX2glTF' + binExt);
      if (!fs.existsSync(tool)) {
        throw new Error(`Unsupported OS: ${os.type()}`);
      }

      let destExt = path.extname(destFile).toLowerCase();

      if (!destExt) {
        destExt = '.gltf'

        const srcFilename = path.basename(srcFile, path.extname(srcFile))
        destFile = path.join(destFile, srcFilename + destExt)
      }

      if (destExt !== '.glb' && destExt !== '.gltf') {
        throw new Error(`Unsupported file extension: ${destFile}`);
      }

      const binary = opts.includes('--binary') || opts.includes('-b');

      if (binary && destExt !== '.glb') {
        destExt = '.glb';
      } else if (!binary && destExt === 'glb') {
        opts.push('--binary');
      }

      let srcPath = fs.realpathSync(srcFile);
      let destDir = fs.realpathSync(path.dirname(destFile));
      let destFilename = path.basename(destFile, path.extname(destFile)) + destExt;
      let destPath = path.join(destDir, destFilename);

      let args = opts.slice(0);
      args.push('--input', srcPath, '--output', destPath);
      let child = childProcess.spawn(tool, args);

      let output = '';
      child.stdout.on('data', (data) => output += data);
      child.stderr.on('data', (data) => output += data);
      child.on('error', reject);
      child.on('close', code => {
        // the FBX SDK may create an .fbm dir during conversion; delete!
        let fbmCruft = srcPath.replace(/.fbx$/i, '.fbm');
        // don't stick a fork in things if this fails, just log a warning
        const onError = error =>
          error && console.warn(`Failed to delete ${fbmCruft}: ${error}`);
        try {
          fs.existsSync(fbmCruft) && rimraf(fbmCruft, {}, onError);
        } catch (error) {
          onError(error);
        }

        // non-zero exit code is failure
        if (code != 0) {
          reject(new Error(`Converter output:\n` +
                           (output.length ? output : "<none>")));
        } else {
          resolve(destPath);
        }
      });

    } catch (error) {
      reject(error);
    }
  });
}