utils.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var path = require('path');
  2. var glob = require('glob');
  3. var fs = require('fs-extra');
  4. var childProcess = require('child_process');
  5. var sortPackageJson = require('sort-package-json');
  6. /**
  7. * Get all of the lerna package paths.
  8. */
  9. function getLernaPaths() {
  10. var basePath = path.resolve('.');
  11. var lernaConfig = require(path.join(basePath, 'lerna.json'));
  12. var paths = [];
  13. for (var config of lernaConfig.packages) {
  14. paths = paths.concat(glob.sync(path.join(basePath, config)));
  15. }
  16. return paths;
  17. }
  18. /**
  19. * Get all of the core package paths.
  20. */
  21. function getCorePaths() {
  22. var spec = path.resolve(path.join('.', 'packages', '*'));
  23. return glob.sync(spec);
  24. }
  25. /**
  26. * Write a package.json if necessary.
  27. */
  28. function ensurePackageData(data, pkgJsonPath) {
  29. var text = JSON.stringify(sortPackageJson(data), null, 2) + '\n';
  30. var orig = fs.readFileSync(pkgJsonPath).toString();
  31. if (text !== orig) {
  32. fs.writeFileSync(pkgJsonPath, text);
  33. return true;
  34. }
  35. return false;
  36. }
  37. /**
  38. * Run a command with terminal output.
  39. */
  40. function run(cmd, options) {
  41. options = options || {};
  42. options['stdio'] = [1,2,3];
  43. console.log('>', cmd);
  44. childProcess.execSync(cmd, options);
  45. }
  46. module.exports = {
  47. getLernaPaths: getLernaPaths,
  48. getCorePaths: getCorePaths,
  49. ensurePackageData: ensurePackageData,
  50. run: run
  51. };