Fully remove when.js dependency

This commit is contained in:
Nick O'Leary
2020-11-30 14:38:48 +00:00
parent beccdac717
commit 5992ed1fab
49 changed files with 299 additions and 357 deletions

View File

@@ -15,9 +15,7 @@
**/
var fs = require('fs-extra');
var when = require('when');
var fspath = require("path");
var nodeFn = require('when/node/function');
var util = require("./util");
@@ -90,14 +88,14 @@ function getLibraryEntry(type,path) {
var rootPath = fspath.join(libDir,type,path);
// don't create the folder if it does not exist - we are only reading....
return nodeFn.call(fs.lstat, rootPath).then(function(stats) {
return fs.lstat(rootPath).then(function(stats) {
if (stats.isFile()) {
return getFileBody(root,path);
}
if (path.substr(-1) == '/') {
path = path.substr(0,path.length-1);
}
return nodeFn.call(fs.readdir, rootPath).then(function(fns) {
return fs.readdir(rootPath).then(function(fns) {
var dirs = [];
var files = [];
fns.sort().filter(function(fn) {
@@ -143,21 +141,19 @@ function getLibraryEntry(type,path) {
}
module.exports = {
init: function(_settings) {
init: async function(_settings) {
settings = _settings;
libDir = fspath.join(settings.userDir,"lib");
libFlowsDir = fspath.join(libDir,"flows");
if (!settings.readOnly) {
return fs.ensureDir(libFlowsDir);
} else {
return when.resolve();
}
},
getLibraryEntry: getLibraryEntry,
saveLibraryEntry: function(type,path,meta,body) {
saveLibraryEntry: async function(type,path,meta,body) {
if (settings.readOnly) {
return when.resolve();
return;
}
if (type === "flows" && !path.endsWith(".json")) {
path += ".json";

View File

@@ -16,7 +16,6 @@
var fs = require('fs-extra');
var when = require('when');
var fspath = require("path");
var os = require('os');
@@ -108,12 +107,12 @@ Project.prototype.load = function () {
// package.json isn't valid JSON... is a merge underway?
project.package = {};
}
}));
}).catch(err => {})); //
if (missingFiles.indexOf('README.md') === -1) {
project.paths['README.md'] = fspath.join(project.paths.root,"README.md");
promises.push(fs.readFile(fspath.join(project.path,project.paths['README.md']),"utf8").then(function(content) {
project.description = content;
}));
}).catch(err => {}));
} else {
project.description = "";
}
@@ -133,9 +132,9 @@ Project.prototype.load = function () {
// project.paths.credentialsFile = fspath.join(project.path,"flow_cred.json");
// }
promises.push(project.loadRemotes());
promises.push(project.loadRemotes().catch(err => {}));
return when.settle(promises).then(function(results) {
return Promise.all(promises).then(function(results) {
return project;
})
});
@@ -239,7 +238,7 @@ Project.prototype.isMerging = function() {
return this.merging;
}
Project.prototype.update = function (user, data) {
Project.prototype.update = async function (user, data) {
var username;
if (!user) {
username = "_";
@@ -279,7 +278,7 @@ Project.prototype.update = function (user, data) {
this.credentialSecret !== existingSecret) { // key doesn't match provided existing key
var e = new Error("Cannot change credentialSecret without current key");
e.code = "missing_current_credential_key";
return when.reject(e);
throw e;
}
this.credentialSecret = secret;
@@ -292,7 +291,7 @@ Project.prototype.update = function (user, data) {
if (this.missingFiles.indexOf('package.json') !== -1) {
if (!data.files || !data.files.package) {
// Cannot update a project that doesn't have a known package.json
return Promise.reject("Cannot update project with missing package.json");
throw new Error("Cannot update project with missing package.json");
}
}
@@ -302,7 +301,7 @@ Project.prototype.update = function (user, data) {
// We have a package file. It could be one that doesn't exist yet,
// or it does exist and we need to load it.
if (!/package\.json$/.test(data.files.package)) {
return Promise.reject("Invalid package file: "+data.files.package)
return new Error("Invalid package file: "+data.files.package)
}
var root = data.files.package.substring(0,data.files.package.length-12);
this.paths.root = root;
@@ -391,20 +390,20 @@ Project.prototype.update = function (user, data) {
modifyRemotesPromise = modifyRemotesPromise.then(function() {
return project.loadRemotes();
});
promises.push(modifyRemotesPromise);
promises.push(modifyRemotesPromise.catch(err => {}));
}
}
}
if (saveSettings) {
promises.push(settings.set("projects",globalProjectSettings));
promises.push(settings.set("projects",globalProjectSettings).catch(err => {}));
}
var modifiedFiles = [];
if (saveREADME) {
promises.push(util.writeFile(fspath.join(this.path,this.paths['README.md']), this.description));
promises.push(util.writeFile(fspath.join(this.path,this.paths['README.md']), this.description).catch(err => {}));
modifiedFiles.push('README.md');
}
if (savePackage) {
@@ -416,10 +415,10 @@ Project.prototype.update = function (user, data) {
}
this.package = Object.assign(currentPackage,this.package);
return util.writeFile(fspath.join(project.path,this.paths['package.json']), JSON.stringify(this.package,"",4));
}));
}).catch(err => {}));
modifiedFiles.push('package.json');
}
return when.settle(promises).then(function(res) {
return Promise.all(promises).then(function(res) {
var gitSettings = getUserGitSettings(user) || {};
var workflowMode = (gitSettings.workflow||{}).mode || "manual";
if (workflowMode === 'auto') {
@@ -944,32 +943,17 @@ function createDefaultProject(user, project) {
}
function checkProjectFiles(project) {
var promises = [];
var paths = [];
var missing = [];
for (var file in defaultFileSet) {
if (defaultFileSet.hasOwnProperty(file)) {
paths.push(file);
promises.push(fs.stat(fspath.join(project.path,project.paths.root,file)));
(function(f) {
promises.push(fs.stat(fspath.join(project.path,project.paths.root,f)).catch(err => {
missing.push(f);
}));
})(file);
}
}
return when.settle(promises).then(function(results) {
var missing = [];
results.forEach(function(result,i) {
if (result.state === 'rejected') {
missing.push(paths[i]);
}
});
return missing;
}).then(function(missing) {
// if (createMissing) {
// var promises = [];
// missing.forEach(function(file) {
// promises.push(util.writeFile(fspath.join(projectPath,file),defaultFileSet[file](project)));
// });
// return promises;
// } else {
return missing;
// }
});
return Promise.all(promises).then(() => missing);
}
function createProject(user, metadata) {
var username;

View File

@@ -15,9 +15,7 @@
**/
var fs = require('fs-extra');
var when = require('when');
var fspath = require("path");
var nodeFn = require('when/node/function');
var crypto = require('crypto');
var storageSettings = require("../settings");
@@ -223,7 +221,6 @@ function loadProject(name) {
function getProject(user, name) {
checkActiveProject(name);
//return when.resolve(activeProject.info);
return Promise.resolve(activeProject.export());
}
@@ -495,7 +492,7 @@ var flowsFileBackup;
var credentialsFile;
var credentialsFileBackup;
function getFlows() {
async function getFlows() {
if (!initialFlowLoadComplete) {
initialFlowLoadComplete = true;
log.info(log._("storage.localfilesystem.user-dir",{path:settings.userDir}));
@@ -522,25 +519,25 @@ function getFlows() {
log.warn("Project repository is empty");
error = new Error("Project repository is empty");
error.code = "project_empty";
return when.reject(error);
throw error;
}
if (activeProject.missingFiles && activeProject.missingFiles.indexOf('package.json') !== -1) {
log.warn("Project missing package.json");
error = new Error("Project missing package.json");
error.code = "missing_package_file";
return when.reject(error);
throw error;
}
if (!activeProject.getFlowFile()) {
log.warn("Project has no flow file");
error = new Error("Project has no flow file");
error.code = "missing_flow_file";
return when.reject(error);
throw error;
}
if (activeProject.isMerging()) {
log.warn("Project has unmerged changes");
error = new Error("Project has unmerged changes. Cannot load flows");
error.code = "git_merge_conflict";
return when.reject(error);
throw error;
}
}
@@ -554,14 +551,14 @@ function getFlows() {
});
}
function saveFlows(flows, user) {
async function saveFlows(flows, user) {
if (settings.readOnly) {
return when.resolve();
return
}
if (activeProject && activeProject.isMerging()) {
var error = new Error("Project has unmerged changes. Cannot deploy new flows");
error.code = "git_merge_conflict";
return when.reject(error);
throw error;
}
flowsFileExists = true;
@@ -589,9 +586,9 @@ function getCredentials() {
return util.readFile(credentialsFile,credentialsFileBackup,{},'credentials');
}
function saveCredentials(credentials) {
async function saveCredentials(credentials) {
if (settings.readOnly) {
return when.resolve();
return;
}
var credentialData;

View File

@@ -15,7 +15,6 @@
**/
var fs = require('fs-extra');
var when = require('when');
var fspath = require("path");
var keygen = require("./keygen");

View File

@@ -14,7 +14,6 @@
* limitations under the License.
**/
var when = require('when');
var fs = require('fs-extra');
var fspath = require("path");
@@ -30,8 +29,8 @@ module.exports = {
settings = _settings;
sessionsFile = fspath.join(settings.userDir,".sessions.json");
},
getSessions: function() {
return when.promise(function(resolve,reject) {
getSessions: async function() {
return new Promise(function(resolve,reject) {
fs.readFile(sessionsFile,'utf8',function(err,data){
if (!err) {
try {
@@ -44,9 +43,9 @@ module.exports = {
})
});
},
saveSessions: function(sessions) {
saveSessions: async function(sessions) {
if (settings.readOnly) {
return when.resolve();
return;
}
return util.writeFile(sessionsFile,JSON.stringify(sessions));
}