overleaf/services/clsi/test/acceptance/coffee/helpers/Client.js

148 lines
4.2 KiB
JavaScript
Raw Normal View History

/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let Client;
const request = require("request");
const fs = require("fs");
const Settings = require("settings-sharelatex");
2014-02-12 12:27:43 -05:00
const host = "localhost";
2014-02-12 12:27:43 -05:00
module.exports = (Client = {
host: Settings.apis.clsi.url,
2014-02-12 12:27:43 -05:00
randomId() {
return Math.random().toString(16).slice(2);
},
2014-02-12 12:27:43 -05:00
compile(project_id, data, callback) {
if (callback == null) { callback = function(error, res, body) {}; }
return request.post({
url: `${this.host}/project/${project_id}/compile`,
json: {
2014-02-12 12:27:43 -05:00
compile: data
}
}, callback);
},
2014-02-12 12:27:43 -05:00
clearCache(project_id, callback) {
if (callback == null) { callback = function(error, res, body) {}; }
return request.del(`${this.host}/project/${project_id}`, callback);
},
2014-02-12 12:27:43 -05:00
getOutputFile(response, type) {
for (let file of Array.from(response.compile.outputFiles)) {
if ((file.type === type) && file.url.match(`output.${type}`)) {
return file;
}
}
return null;
},
2014-02-12 12:27:43 -05:00
runServer(port, directory) {
const express = require("express");
const app = express();
app.use(express.static(directory));
console.log("starting test server on", port, host);
return app.listen(port, host).on("error", function(error) {
console.error("error starting server:", error.message);
return process.exit(1);
});
},
2017-10-20 10:16:35 -04:00
2014-02-12 12:27:43 -05:00
syncFromCode(project_id, file, line, column, callback) {
if (callback == null) { callback = function(error, pdfPositions) {}; }
return request.get({
url: `${this.host}/project/${project_id}/sync/code`,
2014-04-08 10:18:56 -04:00
qs: {
file,
line,
column
2014-04-08 10:18:56 -04:00
}
}, function(error, response, body) {
if (error != null) { return callback(error); }
return callback(null, JSON.parse(body));
});
},
2014-04-08 10:18:56 -04:00
syncFromPdf(project_id, page, h, v, callback) {
if (callback == null) { callback = function(error, pdfPositions) {}; }
return request.get({
url: `${this.host}/project/${project_id}/sync/pdf`,
2014-04-08 10:18:56 -04:00
qs: {
page,
h, v
2014-04-08 10:18:56 -04:00
}
}, function(error, response, body) {
if (error != null) { return callback(error); }
return callback(null, JSON.parse(body));
});
},
2014-04-08 10:18:56 -04:00
compileDirectory(project_id, baseDirectory, directory, serverPort, callback) {
if (callback == null) { callback = function(error, res, body) {}; }
const resources = [];
let entities = fs.readdirSync(`${baseDirectory}/${directory}`);
let rootResourcePath = "main.tex";
while (entities.length > 0) {
var entity = entities.pop();
const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`);
if (stat.isDirectory()) {
entities = entities.concat(fs.readdirSync(`${baseDirectory}/${directory}/${entity}`).map(function(subEntity) {
if (subEntity === "main.tex") {
rootResourcePath = `${entity}/${subEntity}`;
}
return `${entity}/${subEntity}`;
})
);
} else if (stat.isFile() && (entity !== "output.pdf")) {
const extension = entity.split(".").pop();
if (["tex", "bib", "cls", "sty", "pdf_tex", "Rtex", "ist", "md", "Rmd"].indexOf(extension) > -1) {
resources.push({
path: entity,
content: fs.readFileSync(`${baseDirectory}/${directory}/${entity}`).toString()
});
} else if (["eps", "ttf", "png", "jpg", "pdf", "jpeg"].indexOf(extension) > -1) {
resources.push({
path: entity,
url: `http://${host}:${serverPort}/${directory}/${entity}`,
2014-02-12 12:27:43 -05:00
modified: stat.mtime
});
}
}
}
2014-02-12 12:27:43 -05:00
return fs.readFile(`${baseDirectory}/${directory}/options.json`, (error, body) => {
const req = {
resources,
rootResourcePath
};
2014-02-12 12:27:43 -05:00
if ((error == null)) {
body = JSON.parse(body);
req.options = body;
}
2014-02-12 12:27:43 -05:00
return this.compile(project_id, req, callback);
});
},
2014-02-12 12:27:43 -05:00
wordcount(project_id, file, callback) {
if (callback == null) { callback = function(error, pdfPositions) {}; }
return request.get({
url: `${this.host}/project/${project_id}/wordcount`,
2015-06-08 17:35:24 -04:00
qs: {
file
2015-06-08 17:35:24 -04:00
}
}, function(error, response, body) {
if (error != null) { return callback(error); }
return callback(null, JSON.parse(body));
});
}
});