File size: 6,188 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use strict";

const _ = require('lodash');
const os = require('os');
const path = require('path');
const untildify = require('untildify');
const debug = require('debug')('cypress:cli');
const fs = require('../fs');
const util = require('../util');
const getPlatformExecutable = () => {
  const platform = os.platform();
  switch (platform) {
    case 'darwin':
      return 'Contents/MacOS/Cypress';
    case 'linux':
      return 'Cypress';
    case 'win32':
      return 'Cypress.exe';
    // TODO handle this error using our standard
    default:
      throw new Error(`Platform: "${platform}" is not supported.`);
  }
};
const getPlatFormBinaryFolder = () => {
  const platform = os.platform();
  switch (platform) {
    case 'darwin':
      return 'Cypress.app';
    case 'linux':
      return 'Cypress';
    case 'win32':
      return 'Cypress';
    // TODO handle this error using our standard
    default:
      throw new Error(`Platform: "${platform}" is not supported.`);
  }
};
const getBinaryPkgPath = binaryDir => {
  const platform = os.platform();
  switch (platform) {
    case 'darwin':
      return path.join(binaryDir, 'Contents', 'Resources', 'app', 'package.json');
    case 'linux':
      return path.join(binaryDir, 'resources', 'app', 'package.json');
    case 'win32':
      return path.join(binaryDir, 'resources', 'app', 'package.json');
    // TODO handle this error using our standard
    default:
      throw new Error(`Platform: "${platform}" is not supported.`);
  }
};

/**
 * Get path to binary directory
*/
const getBinaryDir = (version = util.pkgVersion()) => {
  return path.join(getVersionDir(version), getPlatFormBinaryFolder());
};
const getVersionDir = (version = util.pkgVersion(), buildInfo = util.pkgBuildInfo()) => {
  if (buildInfo && !buildInfo.stable) {
    version = ['beta', version, buildInfo.commitBranch, buildInfo.commitSha.slice(0, 8)].join('-');
  }
  return path.join(getCacheDir(), version);
};

/**
 * When executing "npm postinstall" hook, the working directory is set to
 * "<current folder>/node_modules/cypress", which can be surprising when using relative paths.
 */
const isInstallingFromPostinstallHook = () => {
  // individual folders
  const cwdFolders = process.cwd().split(path.sep);
  const length = cwdFolders.length;
  return cwdFolders[length - 2] === 'node_modules' && cwdFolders[length - 1] === 'cypress';
};
const getCacheDir = () => {
  let cache_directory = util.getCacheDir();
  if (util.getEnv('CYPRESS_CACHE_FOLDER')) {
    const envVarCacheDir = untildify(util.getEnv('CYPRESS_CACHE_FOLDER'));
    debug('using environment variable CYPRESS_CACHE_FOLDER %s', envVarCacheDir);
    if (!path.isAbsolute(envVarCacheDir) && isInstallingFromPostinstallHook()) {
      const packageRootFolder = path.join('..', '..', envVarCacheDir);
      cache_directory = path.resolve(packageRootFolder);
      debug('installing from postinstall hook, original root folder is %s', packageRootFolder);
      debug('and resolved cache directory is %s', cache_directory);
    } else {
      cache_directory = path.resolve(envVarCacheDir);
    }
  }
  return cache_directory;
};
const parseRealPlatformBinaryFolderAsync = binaryPath => {
  return fs.realpathAsync(binaryPath).then(realPath => {
    debug('CYPRESS_RUN_BINARY has realpath:', realPath);
    if (!realPath.toString().endsWith(getPlatformExecutable())) {
      return false;
    }
    if (os.platform() === 'darwin') {
      return path.resolve(realPath, '..', '..', '..');
    }
    return path.resolve(realPath, '..');
  });
};
const getDistDir = () => {
  return path.join(__dirname, '..', '..', 'dist');
};

/**
 * Returns full filename to the file that keeps the Test Runner verification state as JSON text.
 * Note: the binary state file will be stored one level up from the given binary folder.
 * @param {string} binaryDir - full path to the folder holding the binary.
 */
const getBinaryStatePath = binaryDir => {
  return path.join(binaryDir, '..', 'binary_state.json');
};
const getBinaryStateContentsAsync = binaryDir => {
  const fullPath = getBinaryStatePath(binaryDir);
  return fs.readJsonAsync(fullPath).catch({
    code: 'ENOENT'
  }, SyntaxError, () => {
    debug('could not read binary_state.json file at "%s"', fullPath);
    return {};
  });
};
const getBinaryVerifiedAsync = binaryDir => {
  return getBinaryStateContentsAsync(binaryDir).tap(debug).get('verified');
};
const clearBinaryStateAsync = binaryDir => {
  return fs.removeAsync(getBinaryStatePath(binaryDir));
};

/**
 * Writes the new binary status.
 * @param {boolean} verified The new test runner state after smoke test
 * @param {string} binaryDir Folder holding the binary
 * @returns {Promise<void>} returns a promise
 */
const writeBinaryVerifiedAsync = (verified, binaryDir) => {
  return getBinaryStateContentsAsync(binaryDir).then(contents => {
    return fs.outputJsonAsync(getBinaryStatePath(binaryDir), _.extend(contents, {
      verified
    }), {
      spaces: 2
    });
  });
};
const getPathToExecutable = binaryDir => {
  return path.join(binaryDir, getPlatformExecutable());
};

/**
 * Resolves with an object read from the binary app package.json file.
 * If the file does not exist resolves with null
 */
const getBinaryPkgAsync = binaryDir => {
  const pathToPackageJson = getBinaryPkgPath(binaryDir);
  debug('Reading binary package.json from:', pathToPackageJson);
  return fs.pathExistsAsync(pathToPackageJson).then(exists => {
    if (!exists) {
      return null;
    }
    return fs.readJsonAsync(pathToPackageJson);
  });
};
const getBinaryPkgVersion = o => _.get(o, 'version', null);
const getBinaryElectronVersion = o => _.get(o, 'electronVersion', null);
const getBinaryElectronNodeVersion = o => _.get(o, 'electronNodeVersion', null);
module.exports = {
  getPathToExecutable,
  getPlatformExecutable,
  // those names start to sound like Java
  getBinaryElectronNodeVersion,
  getBinaryElectronVersion,
  getBinaryPkgVersion,
  getBinaryVerifiedAsync,
  getBinaryPkgAsync,
  getBinaryPkgPath,
  getBinaryDir,
  getCacheDir,
  clearBinaryStateAsync,
  writeBinaryVerifiedAsync,
  parseRealPlatformBinaryFolderAsync,
  getDistDir,
  getVersionDir
};