Files
cypress/packages/server/lib/cache.js
T
decaffeinate 0f035b976c decaffeinate: Convert api.coffee and 35 other files to JS
decaffeinate: Convert reporter.coffee to JS
2020-06-04 14:03:01 -04:00

151 lines
3.5 KiB
JavaScript

/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const _ = require("lodash");
const path = require("path");
const Promise = require("bluebird");
const fs = require("./util/fs");
const appData = require("./util/app_data");
const FileUtil = require("./util/file");
const logger = require("./logger");
const fileUtil = new FileUtil({
path: appData.path("cache")
});
const convertProjectsToArray = function(obj) {
//# if our project structure is not
//# an array then its legacy and we
//# need to convert it
if (!_.isArray(obj.PROJECTS)) {
obj.PROJECTS = _.chain(obj.PROJECTS).values().map("PATH").compact().value();
return obj;
}
};
const renameSessionToken = function(obj) {
let st;
if (obj.USER && (st = obj.USER.session_token)) {
delete obj.USER.session_token;
obj.USER.sessionToken = st;
return obj;
}
};
module.exports = {
path: fileUtil.path,
defaults() {
return {
USER: {},
PROJECTS: []
};
},
_applyRewriteRules(obj = {}) {
return _.reduce([convertProjectsToArray, renameSessionToken], function(memo, fn) {
let ret;
if (ret = fn(memo)) {
return ret;
} else {
return memo;
}
}
, _.cloneDeep(obj));
},
read() {
return fileUtil.get().then(contents => {
return _.defaults(contents, this.defaults());
});
},
write(obj = {}) {
logger.info("writing to .cy cache", {cache: obj});
return fileUtil.set(obj).return(obj);
},
_getProjects(tx) {
return tx.get("PROJECTS", []);
},
_removeProjects(tx, projects, paths) {
//# normalize paths in array
projects = _.without(projects, ...[].concat(paths));
return tx.set({PROJECTS: projects});
},
getProjectRoots() {
return fileUtil.transaction(tx => {
return this._getProjects(tx).then(projects => {
const pathsToRemove = Promise.reduce(projects, (memo, path) => fs.statAsync(path)
.catch(() => memo.push(path)).return(memo)
, []);
return pathsToRemove.then(removedPaths => {
return this._removeProjects(tx, projects, removedPaths);
}).then(() => {
return this._getProjects(tx);
});
});
});
},
removeProject(path) {
return fileUtil.transaction(tx => {
return this._getProjects(tx).then(projects => {
return this._removeProjects(tx, projects, path);
});
});
},
insertProject(path) {
return fileUtil.transaction(tx => {
return this._getProjects(tx).then(projects => {
//# projects are sorted by most recently used, so add a project to
//# the start or move it to the start if it already exists
const existingIndex = _.findIndex(projects, project => project === path);
if (existingIndex > -1) {
projects.splice(existingIndex, 1);
}
projects.unshift(path);
return tx.set("PROJECTS", projects);
});
});
},
getUser() {
logger.info("getting user");
return fileUtil.get("USER", {});
},
setUser(user) {
logger.info("setting user", {user});
return fileUtil.set({USER: user});
},
removeUser() {
return fileUtil.set({USER: {}});
},
remove() {
return fileUtil.remove();
},
//# for testing purposes
__get: fileUtil.get.bind(fileUtil),
__removeSync() {
fileUtil._cache = {};
return fs.removeSync(this.path);
}
};