You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
20 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var balUtilFlow, balUtilModules, balUtilPaths, balUtilTypes, isWindows, _ref, _ref1, _ref2, _ref3,
  4. __slice = [].slice;
  5. balUtilModules = null;
  6. balUtilFlow = require(__dirname + '/flow');
  7. balUtilPaths = require(__dirname + '/paths');
  8. balUtilTypes = require(__dirname + '/types');
  9. isWindows = (typeof process !== "undefined" && process !== null) && process.platform.indexOf('win') === 0;
  10. if ((_ref = global.numberOfOpenProcesses) == null) {
  11. global.numberOfOpenProcesses = 0;
  12. }
  13. if ((_ref1 = global.maxNumberOfOpenProcesses) == null) {
  14. global.maxNumberOfOpenProcesses = (_ref2 = process.env.NODE_MAX_OPEN_PROCESSES) != null ? _ref2 : 30;
  15. }
  16. if ((_ref3 = global.waitingToOpenProcessDelay) == null) {
  17. global.waitingToOpenProcessDelay = 100;
  18. }
  19. balUtilModules = {
  20. requireFresh: function(path) {
  21. var result;
  22. path = require('path').resolve(path);
  23. delete require.cache[path];
  24. result = require(path);
  25. delete require.cache[path];
  26. return result;
  27. },
  28. isWindows: function() {
  29. return isWindows;
  30. },
  31. openProcess: function(next) {
  32. if (global.numberOfOpenProcesses < 0) {
  33. throw new Error("balUtilModules.openProcess: the numberOfOpenProcesses is [" + global.numberOfOpenProcesses + "] which should be impossible...");
  34. }
  35. if (global.numberOfOpenProcesses >= global.maxNumberOfOpenProcesses) {
  36. setTimeout(function() {
  37. return balUtilModules.openProcess(next);
  38. }, global.waitingToOpenProcessDelay);
  39. } else {
  40. ++global.numberOfOpenProcesses;
  41. next();
  42. }
  43. return this;
  44. },
  45. closeProcess: function(next) {
  46. --global.numberOfOpenProcesses;
  47. if (typeof next === "function") {
  48. next();
  49. }
  50. return this;
  51. },
  52. spawn: function(command, opts, next) {
  53. balUtilModules.openProcess(function() {
  54. var err, pid, spawn, stderr, stdout, _ref4;
  55. spawn = require('child_process').spawn;
  56. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  57. pid = null;
  58. err = null;
  59. stdout = '';
  60. stderr = '';
  61. if (balUtilTypes.isString(command)) {
  62. command = command.split(' ');
  63. }
  64. if (balUtilTypes.isArray(command)) {
  65. pid = spawn(command[0], command.slice(1), opts);
  66. } else {
  67. pid = spawn(command.command, command.args || [], command.options || opts);
  68. }
  69. pid.stdout.on('data', function(data) {
  70. var dataStr;
  71. dataStr = data.toString();
  72. if (opts.output) {
  73. process.stdout.write(dataStr);
  74. }
  75. return stdout += dataStr;
  76. });
  77. pid.stderr.on('data', function(data) {
  78. var dataStr;
  79. dataStr = data.toString();
  80. if (opts.output) {
  81. process.stderr.write(dataStr);
  82. }
  83. return stderr += dataStr;
  84. });
  85. pid.on('exit', function(code, signal) {
  86. err = null;
  87. if (code !== 0) {
  88. err = new Error(stderr || 'exited with a non-zero status code');
  89. }
  90. balUtilModules.closeProcess();
  91. return next(err, stdout, stderr, code, signal);
  92. });
  93. if (opts.stdin) {
  94. pid.stdin.write(opts.stdin);
  95. return pid.stdin.end();
  96. }
  97. });
  98. return this;
  99. },
  100. spawnMultiple: function(commands, opts, next) {
  101. var command, results, tasks, _i, _len, _ref4;
  102. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  103. results = [];
  104. tasks = new balUtilFlow.Group(function(err) {
  105. return next(err, results);
  106. });
  107. if (!balUtilTypes.isArray(commands)) {
  108. commands = [commands];
  109. }
  110. for (_i = 0, _len = commands.length; _i < _len; _i++) {
  111. command = commands[_i];
  112. tasks.push({
  113. command: command
  114. }, function(complete) {
  115. return balUtilModules.spawn(this.command, opts, function() {
  116. var args, err;
  117. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  118. err = args[0] || null;
  119. results.push(args);
  120. return complete(err);
  121. });
  122. });
  123. }
  124. tasks.sync();
  125. return this;
  126. },
  127. exec: function(command, opts, next) {
  128. balUtilModules.openProcess(function() {
  129. var exec, _ref4;
  130. exec = require('child_process').exec;
  131. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  132. return exec(command, opts, function(err, stdout, stderr) {
  133. balUtilModules.closeProcess();
  134. return next(err, stdout, stderr);
  135. });
  136. });
  137. return this;
  138. },
  139. execMultiple: function(commands, opts, next) {
  140. var command, results, tasks, _i, _len, _ref4;
  141. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  142. results = [];
  143. tasks = new balUtilFlow.Group(function(err) {
  144. return next(err, results);
  145. });
  146. if (!balUtilTypes.isArray(commands)) {
  147. commands = [commands];
  148. }
  149. for (_i = 0, _len = commands.length; _i < _len; _i++) {
  150. command = commands[_i];
  151. tasks.push({
  152. command: command
  153. }, function(complete) {
  154. return balUtilModules.exec(this.command, opts, function() {
  155. var args, err;
  156. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  157. err = args[0] || null;
  158. results.push(args);
  159. return complete(err);
  160. });
  161. });
  162. }
  163. tasks.sync();
  164. return this;
  165. },
  166. determineExecPath: function(possiblePaths, next) {
  167. var foundPath, possiblePath, tasks, _i, _len;
  168. foundPath = null;
  169. tasks = new balUtilFlow.Group(function(err) {
  170. return next(err, foundPath);
  171. });
  172. for (_i = 0, _len = possiblePaths.length; _i < _len; _i++) {
  173. possiblePath = possiblePaths[_i];
  174. if (!possiblePath) {
  175. continue;
  176. }
  177. tasks.push({
  178. possiblePath: possiblePath
  179. }, function(complete) {
  180. possiblePath = this.possiblePath;
  181. return balUtilModules.spawn([possiblePath, '--version'], function(err, stdout, stderr, code, signal) {
  182. if (err) {
  183. return complete();
  184. } else {
  185. foundPath = possiblePath;
  186. return tasks.exit();
  187. }
  188. });
  189. });
  190. }
  191. tasks.sync();
  192. return this;
  193. },
  194. getExecPath: function(executableName, next) {
  195. var key, path, pathUtil, paths, _i, _len;
  196. pathUtil = require('path');
  197. if (balUtilModules.isWindows()) {
  198. paths = process.env.PATH.split(/;/g);
  199. } else {
  200. paths = process.env.PATH.split(/:/g);
  201. }
  202. paths.unshift(process.cwd());
  203. for (key = _i = 0, _len = paths.length; _i < _len; key = ++_i) {
  204. path = paths[key];
  205. paths[key] = pathUtil.join(path, executableName);
  206. }
  207. balUtilModules.determineExecPath(paths, next);
  208. return this;
  209. },
  210. getHomePath: function(next) {
  211. var homePath, pathUtil;
  212. if (balUtilModules.cachedHomePath != null) {
  213. next(null, balUtilModules.cachedHomePath);
  214. return this;
  215. }
  216. pathUtil = require('path');
  217. homePath = process.env.USERPROFILE || process.env.HOME;
  218. homePath || (homePath = null);
  219. balUtilModules.cachedHomePath = homePath;
  220. next(null, homePath);
  221. return this;
  222. },
  223. getTmpPath: function(next) {
  224. var pathUtil, tmpDirName, tmpPath;
  225. if (balUtilModules.cachedTmpPath != null) {
  226. next(null, balUtilModules.cachedTmpPath);
  227. return this;
  228. }
  229. pathUtil = require('path');
  230. tmpDirName = isWindows ? 'temp' : 'tmp';
  231. tmpPath = process.env.TMPDIR || process.env.TMP || process.env.TEMP;
  232. if (!tmpPath) {
  233. balUtilModules.getHomePath(function(err, homePath) {
  234. if (err) {
  235. return next(err);
  236. }
  237. tmpPath = pathUtil.resolve(homePath, tmpDirName);
  238. if (!tmpPath) {
  239. return tmpPath = isWindows ? pathUtil.resolve(process.env.windir || 'C:\\Windows', tmpDirName) : '/tmp';
  240. }
  241. });
  242. }
  243. tmpPath || (tmpPath = null);
  244. balUtilModules.cachedTmpPath = tmpPath;
  245. next(null, tmpPath);
  246. return this;
  247. },
  248. getGitPath: function(next) {
  249. var pathUtil, possiblePaths;
  250. if (balUtilModules.cachedGitPath != null) {
  251. next(null, balUtilModules.cachedGitPath);
  252. return this;
  253. }
  254. pathUtil = require('path');
  255. possiblePaths = isWindows ? [process.env.GIT_PATH, process.env.GITPATH, 'git', pathUtil.resolve('/Program Files (x64)/Git/bin/git.exe'), pathUtil.resolve('/Program Files (x86)/Git/bin/git.exe'), pathUtil.resolve('/Program Files/Git/bin/git.exe')] : [process.env.GIT_PATH, process.env.GITPATH, 'git', '/usr/local/bin/git', '/usr/bin/git'];
  256. balUtilModules.determineExecPath(possiblePaths, function(err, gitPath) {
  257. balUtilModules.cachedGitPath = gitPath;
  258. if (err) {
  259. return next(err);
  260. }
  261. if (!gitPath) {
  262. return next(new Error('Could not locate git binary'));
  263. }
  264. return next(null, gitPath);
  265. });
  266. return this;
  267. },
  268. getNodePath: function(next) {
  269. var pathUtil, possiblePaths;
  270. if (balUtilModules.cachedNodePath != null) {
  271. next(null, balUtilModules.cachedNodePath);
  272. return this;
  273. }
  274. pathUtil = require('path');
  275. possiblePaths = isWindows ? [process.env.NODE_PATH, process.env.NODEPATH, (/node(.exe)?$/.test(process.execPath) ? process.execPath : ''), 'node', pathUtil.resolve('/Program Files (x64)/nodejs/node.exe'), pathUtil.resolve('/Program Files (x86)/nodejs/node.exe'), pathUtil.resolve('/Program Files/nodejs/node.exe')] : [process.env.NODE_PATH, process.env.NODEPATH, (/node$/.test(process.execPath) ? process.execPath : ''), 'node', '/usr/local/bin/node', '/usr/bin/node', '~/bin/node'];
  276. balUtilModules.determineExecPath(possiblePaths, function(err, nodePath) {
  277. balUtilModules.cachedNodePath = nodePath;
  278. if (err) {
  279. return next(err);
  280. }
  281. if (!nodePath) {
  282. return next(new Error('Could not locate node binary'));
  283. }
  284. return next(null, nodePath);
  285. });
  286. return this;
  287. },
  288. getNpmPath: function(next) {
  289. var pathUtil, possiblePaths;
  290. if (balUtilModules.cachedNpmPath != null) {
  291. next(null, balUtilModules.cachedNpmPath);
  292. return this;
  293. }
  294. pathUtil = require('path');
  295. possiblePaths = isWindows ? [process.env.NPM_PATH, process.env.NPMPATH, (/node(.exe)?$/.test(process.execPath) ? process.execPath.replace(/node(.exe)?$/, 'npm.cmd') : ''), 'npm', pathUtil.resolve('/Program Files (x64)/nodejs/npm.cmd'), pathUtil.resolve('/Program Files (x86)/nodejs/npm.cmd'), pathUtil.resolve('/Program Files/nodejs/npm.cmd')] : [process.env.NPM_PATH, process.env.NPMPATH, (/node$/.test(process.execPath) ? process.execPath.replace(/node$/, 'npm') : ''), 'npm', '/usr/local/bin/npm', '/usr/bin/npm', '~/node_modules/.bin/npm'];
  296. balUtilModules.determineExecPath(possiblePaths, function(err, npmPath) {
  297. balUtilModules.cachedNpmPath = npmPath;
  298. if (err) {
  299. return next(err);
  300. }
  301. if (!npmPath) {
  302. return next(new Error('Could not locate npm binary'));
  303. }
  304. return next(null, npmPath);
  305. });
  306. return this;
  307. },
  308. gitCommand: function(command, opts, next) {
  309. var performSpawn, _ref4;
  310. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  311. if (balUtilTypes.isString(command)) {
  312. command = command.split(' ');
  313. } else if (!balUtilTypes.isArray(command)) {
  314. return next(new Error('unknown command type'));
  315. }
  316. performSpawn = function() {
  317. command.unshift(opts.gitPath);
  318. return balUtilModules.spawn(command, opts, next);
  319. };
  320. if (opts.gitPath) {
  321. performSpawn();
  322. } else {
  323. balUtilModules.getGitPath(function(err, gitPath) {
  324. if (err) {
  325. return next(err);
  326. }
  327. opts.gitPath = gitPath;
  328. return performSpawn();
  329. });
  330. }
  331. return this;
  332. },
  333. gitCommands: function(commands, opts, next) {
  334. var command, results, tasks, _i, _len, _ref4;
  335. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  336. results = [];
  337. tasks = new balUtilFlow.Group(function(err) {
  338. return next(err, results);
  339. });
  340. if (!balUtilTypes.isArray(commands)) {
  341. commands = [commands];
  342. }
  343. for (_i = 0, _len = commands.length; _i < _len; _i++) {
  344. command = commands[_i];
  345. tasks.push({
  346. command: command
  347. }, function(complete) {
  348. return balUtilModules.gitCommand(this.command, opts, function() {
  349. var args, err;
  350. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  351. err = args[0] || null;
  352. results.push(args);
  353. return complete(err);
  354. });
  355. });
  356. }
  357. tasks.sync();
  358. return this;
  359. },
  360. nodeCommand: function(command, opts, next) {
  361. var performSpawn, _ref4;
  362. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  363. if (balUtilTypes.isString(command)) {
  364. command = command.split(' ');
  365. } else if (!balUtilTypes.isArray(command)) {
  366. return next(new Error('unknown command type'));
  367. }
  368. performSpawn = function() {
  369. command.unshift(opts.nodePath);
  370. return balUtilModules.spawn(command, opts, next);
  371. };
  372. if (opts.nodePath) {
  373. performSpawn();
  374. } else {
  375. balUtilModules.getNodePath(function(err, nodePath) {
  376. if (err) {
  377. return next(err);
  378. }
  379. opts.nodePath = nodePath;
  380. return performSpawn();
  381. });
  382. }
  383. return this;
  384. },
  385. nodeCommands: function(commands, opts, next) {
  386. var command, results, tasks, _i, _len, _ref4;
  387. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  388. results = [];
  389. tasks = new balUtilFlow.Group(function(err) {
  390. return next(err, results);
  391. });
  392. if (!balUtilTypes.isArray(commands)) {
  393. commands = [commands];
  394. }
  395. for (_i = 0, _len = commands.length; _i < _len; _i++) {
  396. command = commands[_i];
  397. tasks.push({
  398. command: command
  399. }, function(complete) {
  400. return balUtilModules.nodeCommand(this.command, opts, function() {
  401. var args, err;
  402. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  403. err = args[0] || null;
  404. results.push(args);
  405. return complete(err);
  406. });
  407. });
  408. }
  409. tasks.sync();
  410. return this;
  411. },
  412. npmCommand: function(command, opts, next) {
  413. var performSpawn, _ref4;
  414. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  415. if (balUtilTypes.isString(command)) {
  416. command = command.split(' ');
  417. } else if (!balUtilTypes.isArray(command)) {
  418. return next(new Error('unknown command type'));
  419. }
  420. performSpawn = function() {
  421. command.unshift(opts.npmPath);
  422. return balUtilModules.spawn(command, opts, next);
  423. };
  424. if (opts.npmPath) {
  425. performSpawn();
  426. } else {
  427. balUtilModules.getNpmPath(function(err, npmPath) {
  428. if (err) {
  429. return next(err);
  430. }
  431. opts.npmPath = npmPath;
  432. return performSpawn();
  433. });
  434. }
  435. return this;
  436. },
  437. npmCommands: function(commands, opts, next) {
  438. var command, results, tasks, _i, _len, _ref4;
  439. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  440. results = [];
  441. tasks = new balUtilFlow.Group(function(err) {
  442. return next(err, results);
  443. });
  444. if (!balUtilTypes.isArray(commands)) {
  445. commands = [commands];
  446. }
  447. for (_i = 0, _len = commands.length; _i < _len; _i++) {
  448. command = commands[_i];
  449. tasks.push({
  450. command: command
  451. }, function(complete) {
  452. return balUtilModules.npmCommand(this.command, opts, function() {
  453. var args, err;
  454. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  455. err = args[0] || null;
  456. results.push(args);
  457. return complete(err);
  458. });
  459. });
  460. }
  461. tasks.sync();
  462. return this;
  463. },
  464. initGitRepo: function(opts, next) {
  465. var branch, commands, gitPath, logger, output, path, remote, url, _ref4;
  466. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  467. path = opts.path, remote = opts.remote, url = opts.url, branch = opts.branch, logger = opts.logger, output = opts.output, gitPath = opts.gitPath;
  468. remote || (remote = 'origin');
  469. branch || (branch = 'master');
  470. commands = [['init'], ['remote', 'add', remote, url], ['fetch', remote], ['pull', remote, branch], ['submodule', 'init'], ['submodule', 'update', '--recursive']];
  471. if (logger) {
  472. logger.log('debug', "Initializing git repo with url [" + url + "] on directory [" + path + "]");
  473. }
  474. balUtilModules.gitCommands(commands, {
  475. gitPath: gitPath,
  476. cwd: path,
  477. output: output
  478. }, function() {
  479. var args;
  480. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  481. if (args[0] != null) {
  482. return next.apply(null, args);
  483. }
  484. if (logger) {
  485. logger.log('debug', "Initialized git repo with url [" + url + "] on directory [" + path + "]");
  486. }
  487. return next.apply(null, args);
  488. });
  489. return this;
  490. },
  491. initOrPullGitRepo: function(opts, next) {
  492. var branch, path, remote, _ref4,
  493. _this = this;
  494. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  495. path = opts.path, remote = opts.remote, branch = opts.branch;
  496. remote || (remote = 'origin');
  497. branch || (branch = 'master');
  498. balUtilPaths.ensurePath(path, function(err, exists) {
  499. if (err) {
  500. return complete(err);
  501. }
  502. if (exists) {
  503. opts.cwd = path;
  504. return balUtilModules.gitCommand(['pull', remote, branch], opts, next);
  505. } else {
  506. return balUtilModules.initGitRepo(opts, next);
  507. }
  508. });
  509. return this;
  510. },
  511. initNodeModules: function(opts, next) {
  512. var force, logger, nodeModulesPath, packageJsonPath, partTwo, path, pathUtil, _ref4;
  513. pathUtil = require('path');
  514. _ref4 = balUtilFlow.extractOptsAndCallback(opts, next), opts = _ref4[0], next = _ref4[1];
  515. path = opts.path, logger = opts.logger, force = opts.force;
  516. opts.cwd = path;
  517. packageJsonPath = pathUtil.join(path, 'package.json');
  518. nodeModulesPath = pathUtil.join(path, 'node_modules');
  519. partTwo = function() {
  520. return balUtilPaths.exists(packageJsonPath, function(exists) {
  521. var command;
  522. if (!exists) {
  523. return next();
  524. }
  525. command = ['install'];
  526. if (force) {
  527. command.push('--force');
  528. }
  529. if (logger) {
  530. logger.log('debug', "Initializing node modules\non: " + dirPath + "\nwith:", command);
  531. }
  532. return balUtilModules.npmCommand(command, opts, function() {
  533. var args;
  534. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  535. if (args[0] != null) {
  536. return next.apply(null, args);
  537. }
  538. if (logger) {
  539. logger.log('debug', "Initialized node modules\non: " + dirPath + "\nwith:", command);
  540. }
  541. return next.apply(null, args);
  542. });
  543. });
  544. };
  545. if (force === false) {
  546. balUtilPaths.exists(nodeModulesPath, function(exists) {
  547. if (exists) {
  548. return next();
  549. }
  550. return partTwo();
  551. });
  552. } else {
  553. partTwo();
  554. }
  555. return this;
  556. }
  557. };
  558. module.exports = balUtilModules;
  559. }).call(this);