state.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use strict';
  2. var _ = require('lodash');
  3. var os = require('os');
  4. var path = require('path');
  5. var untildify = require('untildify');
  6. var debug = require('debug')('cypress:cli');
  7. var fs = require('../fs');
  8. var util = require('../util');
  9. var getPlatformExecutable = function getPlatformExecutable() {
  10. var platform = os.platform();
  11. switch (platform) {
  12. case 'darwin':
  13. return 'Contents/MacOS/Cypress';
  14. case 'linux':
  15. return 'Cypress';
  16. case 'win32':
  17. return 'Cypress.exe';
  18. // TODO handle this error using our standard
  19. default:
  20. throw new Error('Platform: "' + platform + '" is not supported.');
  21. }
  22. };
  23. var getPlatFormBinaryFolder = function getPlatFormBinaryFolder() {
  24. var platform = os.platform();
  25. switch (platform) {
  26. case 'darwin':
  27. return 'Cypress.app';
  28. case 'linux':
  29. return 'Cypress';
  30. case 'win32':
  31. return 'Cypress';
  32. // TODO handle this error using our standard
  33. default:
  34. throw new Error('Platform: "' + platform + '" is not supported.');
  35. }
  36. };
  37. var getBinaryPkgPath = function getBinaryPkgPath(binaryDir) {
  38. var platform = os.platform();
  39. switch (platform) {
  40. case 'darwin':
  41. return path.join(binaryDir, 'Contents', 'Resources', 'app', 'package.json');
  42. case 'linux':
  43. return path.join(binaryDir, 'resources', 'app', 'package.json');
  44. case 'win32':
  45. return path.join(binaryDir, 'resources', 'app', 'package.json');
  46. // TODO handle this error using our standard
  47. default:
  48. throw new Error('Platform: "' + platform + '" is not supported.');
  49. }
  50. };
  51. /**
  52. * Get path to binary directory
  53. */
  54. var getBinaryDir = function getBinaryDir() {
  55. var version = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : util.pkgVersion();
  56. return path.join(getVersionDir(version), getPlatFormBinaryFolder());
  57. };
  58. var getVersionDir = function getVersionDir() {
  59. var version = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : util.pkgVersion();
  60. return path.join(getCacheDir(), version);
  61. };
  62. /**
  63. * When executing "npm postinstall" hook, the working directory is set to
  64. * "<current folder>/node_modules/cypress", which can be surprising when using relative paths.
  65. */
  66. var isInstallingFromPostinstallHook = function isInstallingFromPostinstallHook() {
  67. // individual folders
  68. var cwdFolders = process.cwd().split(path.sep);
  69. var length = cwdFolders.length;
  70. return cwdFolders[length - 2] === 'node_modules' && cwdFolders[length - 1] === 'cypress';
  71. };
  72. var getCacheDir = function getCacheDir() {
  73. var cache_directory = util.getCacheDir();
  74. if (util.getEnv('CYPRESS_CACHE_FOLDER')) {
  75. var envVarCacheDir = untildify(util.getEnv('CYPRESS_CACHE_FOLDER'));
  76. debug('using environment variable CYPRESS_CACHE_FOLDER %s', envVarCacheDir);
  77. if (!path.isAbsolute(envVarCacheDir) && isInstallingFromPostinstallHook()) {
  78. var packageRootFolder = path.join('..', '..', envVarCacheDir);
  79. cache_directory = path.resolve(packageRootFolder);
  80. debug('installing from postinstall hook, original root folder is %s', packageRootFolder);
  81. debug('and resolved cache directory is %s', cache_directory);
  82. } else {
  83. cache_directory = path.resolve(envVarCacheDir);
  84. }
  85. }
  86. return cache_directory;
  87. };
  88. var parseRealPlatformBinaryFolderAsync = function parseRealPlatformBinaryFolderAsync(binaryPath) {
  89. return fs.realpathAsync(binaryPath).then(function (realPath) {
  90. debug('CYPRESS_RUN_BINARY has realpath:', realPath);
  91. if (!realPath.toString().endsWith(getPlatformExecutable())) {
  92. return false;
  93. }
  94. if (os.platform() === 'darwin') {
  95. return path.resolve(realPath, '..', '..', '..');
  96. }
  97. return path.resolve(realPath, '..');
  98. });
  99. };
  100. var getDistDir = function getDistDir() {
  101. return path.join(__dirname, '..', '..', 'dist');
  102. };
  103. var getBinaryStatePath = function getBinaryStatePath(binaryDir) {
  104. return path.join(binaryDir, 'binary_state.json');
  105. };
  106. var getBinaryStateContentsAsync = function getBinaryStateContentsAsync(binaryDir) {
  107. return fs.readJsonAsync(getBinaryStatePath(binaryDir)).catch({ code: 'ENOENT' }, SyntaxError, function () {
  108. debug('could not read binary_state.json file');
  109. return {};
  110. });
  111. };
  112. var getBinaryVerifiedAsync = function getBinaryVerifiedAsync(binaryDir) {
  113. return getBinaryStateContentsAsync(binaryDir).tap(debug).get('verified');
  114. };
  115. var clearBinaryStateAsync = function clearBinaryStateAsync(binaryDir) {
  116. return fs.removeAsync(getBinaryStatePath(binaryDir));
  117. };
  118. /**
  119. * @param {boolean} verified
  120. */
  121. var writeBinaryVerifiedAsync = function writeBinaryVerifiedAsync(verified, binaryDir) {
  122. return getBinaryStateContentsAsync(binaryDir).then(function (contents) {
  123. return fs.outputJsonAsync(getBinaryStatePath(binaryDir), _.extend(contents, { verified: verified }), { spaces: 2 });
  124. });
  125. };
  126. var getPathToExecutable = function getPathToExecutable(binaryDir) {
  127. return path.join(binaryDir, getPlatformExecutable());
  128. };
  129. var getBinaryPkgVersionAsync = function getBinaryPkgVersionAsync(binaryDir) {
  130. var pathToPackageJson = getBinaryPkgPath(binaryDir);
  131. debug('Reading binary package.json from:', pathToPackageJson);
  132. return fs.pathExistsAsync(pathToPackageJson).then(function (exists) {
  133. if (!exists) {
  134. return null;
  135. }
  136. return fs.readJsonAsync(pathToPackageJson).get('version');
  137. });
  138. };
  139. module.exports = {
  140. getPathToExecutable: getPathToExecutable,
  141. getPlatformExecutable: getPlatformExecutable,
  142. getBinaryPkgVersionAsync: getBinaryPkgVersionAsync,
  143. getBinaryVerifiedAsync: getBinaryVerifiedAsync,
  144. getBinaryPkgPath: getBinaryPkgPath,
  145. getBinaryDir: getBinaryDir,
  146. getCacheDir: getCacheDir,
  147. clearBinaryStateAsync: clearBinaryStateAsync,
  148. writeBinaryVerifiedAsync: writeBinaryVerifiedAsync,
  149. parseRealPlatformBinaryFolderAsync: parseRealPlatformBinaryFolderAsync,
  150. getDistDir: getDistDir,
  151. getVersionDir: getVersionDir
  152. };