'use strict'; var _ = require('lodash'); var os = require('os'); var path = require('path'); var debug = require('debug')('cypress:cli'); var fs = require('../fs'); var getPlatformExecutable = function getPlatformExecutable() { var platform = os.platform(); switch (platform) { case 'darwin': return 'Cypress.app/Contents/MacOS/Cypress'; case 'linux': return 'Cypress/Cypress'; case 'win32': return 'Cypress/Cypress.exe'; // TODO handle this error using our standard default: throw new Error('Platform: "' + platform + '" is not supported.'); } }; var getInstallationDir = function getInstallationDir() { return path.join(__dirname, '..', '..', 'dist'); }; var getInfoFilePath = function getInfoFilePath() { var infoPath = path.join(getInstallationDir(), 'info.json'); debug('path to info.json file %s', infoPath); return infoPath; }; var getInstalledVersion = function getInstalledVersion() { return ensureFileInfoContents().tap(debug).get('version'); }; var getVerifiedVersion = function getVerifiedVersion() { return ensureFileInfoContents().get('verifiedVersion'); }; var ensureInstallationDir = function ensureInstallationDir() { return fs.ensureDirAsync(getInstallationDir()); }; var clearVersionState = function clearVersionState() { return ensureFileInfoContents().then(function (contents) { return writeInfoFileContents(_.omit(contents, 'version', 'verifiedVersion')); }); }; var writeInstalledVersion = function writeInstalledVersion(version) { return ensureFileInfoContents().then(function (contents) { return writeInfoFileContents(_.extend(contents, { version: version })); }); }; var getPathToExecutable = function getPathToExecutable() { return path.join(getInstallationDir(), getPlatformExecutable()); }; var getPathToUserExecutableDir = function getPathToUserExecutableDir() { return path.join(getInstallationDir(), getPlatformExecutable().split('/')[0]); }; var getInfoFileContents = function getInfoFileContents() { return fs.readJsonAsync(getInfoFilePath()); }; var ensureFileInfoContents = function ensureFileInfoContents() { return getInfoFileContents().catch(function () { debug('could not read info file'); return {}; }); }; var writeInfoFileContents = function writeInfoFileContents(contents) { return fs.outputJsonAsync(getInfoFilePath(), contents, { spaces: 2 }); }; module.exports = { clearVersionState: clearVersionState, writeInfoFileContents: writeInfoFileContents, ensureInstallationDir: ensureInstallationDir, ensureFileInfoContents: ensureFileInfoContents, getInfoFilePath: getInfoFilePath, getVerifiedVersion: getVerifiedVersion, getInstallationDir: getInstallationDir, getInstalledVersion: getInstalledVersion, getPathToUserExecutableDir: getPathToUserExecutableDir, getPathToExecutable: getPathToExecutable, writeInstalledVersion: writeInstalledVersion };