2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let TikzManager
|
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
const ResourceWriter = require('./ResourceWriter')
|
|
|
|
const SafeReader = require('./SafeReader')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2017-03-16 12:55:53 -04:00
|
|
|
|
2020-02-19 06:14:14 -05:00
|
|
|
// for \tikzexternalize or pstool to work the main file needs to match the
|
|
|
|
// jobname. Since we set the -jobname to output, we have to create a
|
|
|
|
// copy of the main file as 'output.tex'.
|
2017-03-16 12:55:53 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = TikzManager = {
|
|
|
|
checkMainFile(compileDir, mainFile, resources, callback) {
|
|
|
|
// if there's already an output.tex file, we don't want to touch it
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
for (const resource of Array.from(resources)) {
|
|
|
|
if (resource.path === 'output.tex') {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
|
|
|
{ compileDir, mainFile },
|
|
|
|
'output.tex already in resources'
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
return callback(null, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if there's no output.tex, see if we are using tikz/pgf or pstool in the main file
|
2021-07-13 07:04:48 -04:00
|
|
|
return ResourceWriter.checkPath(
|
|
|
|
compileDir,
|
|
|
|
mainFile,
|
|
|
|
function (error, path) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return SafeReader.readFile(
|
|
|
|
path,
|
|
|
|
65536,
|
|
|
|
'utf8',
|
|
|
|
function (error, content) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const usesTikzExternalize =
|
|
|
|
(content != null
|
|
|
|
? content.indexOf('\\tikzexternalize')
|
|
|
|
: undefined) >= 0
|
|
|
|
const usesPsTool =
|
|
|
|
(content != null ? content.indexOf('{pstool}') : undefined) >= 0
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2021-07-13 07:04:48 -04:00
|
|
|
{ compileDir, mainFile, usesTikzExternalize, usesPsTool },
|
|
|
|
'checked for packages needing main file as output.tex'
|
|
|
|
)
|
|
|
|
const needsMainFile = usesTikzExternalize || usesPsTool
|
|
|
|
return callback(null, needsMainFile)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
2021-07-13 07:04:48 -04:00
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2017-09-29 12:00:53 -04:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
injectOutputFile(compileDir, mainFile, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return ResourceWriter.checkPath(
|
|
|
|
compileDir,
|
|
|
|
mainFile,
|
|
|
|
function (error, path) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return fs.readFile(path, 'utf8', function (error, content) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug(
|
2021-07-13 07:04:48 -04:00
|
|
|
{ compileDir, mainFile },
|
|
|
|
'copied file to output.tex as project uses packages which require it'
|
|
|
|
)
|
|
|
|
// use wx flag to ensure that output file does not already exist
|
|
|
|
return fs.writeFile(
|
|
|
|
Path.join(compileDir, 'output.tex'),
|
|
|
|
content,
|
|
|
|
{ flag: 'wx' },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|