gocommand.js 1.7 KB
Newer Older
B
bryk 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Helper function that spawns a go binary process.
 */
import child from 'child_process';
import lodash from 'lodash';

21 22
import conf from './conf';

B
bryk 已提交
23
/**
24 25
 * Spawns Go process wrapped with the Godep command. Backend source files must me packaged with
 * 'package-backend-source' task before running this command.
B
bryk 已提交
26 27 28 29
 *
 * @param {!Array<string>} args
 * @param {function(?Error=)} doneFn
 */
30 31
export default function spawnGoProcess(args, doneFn) {
  // Add base directory to the gopath so that local imports work.
32 33 34 35 36
  if (!process.env.GOPATH) {
    doneFn(new Error('GOPATH is not set. Go process will not be spawned.'));
    return;
  }

37
  let sourceGopath = `${process.env.GOPATH}:${conf.paths.backendTmp}`;
38 39
  let env = lodash.merge(process.env, {GOPATH: sourceGopath});

B
bryk 已提交
40
  let goTask = child.spawn('godep', ['go'].concat(args), {
41
    env: env,
42 43
    stdio: 'inherit',
  });
B
bryk 已提交
44 45 46 47 48 49 50

  // Call Gulp callback on task exit. This has to be done to make Gulp dependency management
  // work.
  goTask.on('exit', function(code) {
    if (code === 0) {
      doneFn();
    } else {
51
      doneFn(new Error(`Go command error, code: ${code}`));
B
bryk 已提交
52 53 54
    }
  });
}