<commit_before>fs = require "fs"
Path = require "path"
ResourceWriter = require "./ResourceWriter"
SafeReader = require "./SafeReader"
logger = require "logger-sharelatex"

# for \tikzexternalize 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'.

module.exports = TikzManager =

	checkMainFile: (compileDir, mainFile, resources, callback = (error, usesTikzExternalize) ->) ->
		# if there's already an output.tex file, we don't want to touch it
		for resource in resources
			if resource.path is "output.tex"
				logger.log compileDir: compileDir, mainFile: mainFile, "output.tex already in resources"
				return callback(null, false)
		# if there's no output.tex, see if we are using tikz/pgf in the main file
		ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
			return callback(error) if error?
			SafeReader.readFile path, 65536, "utf8", (error, content) ->
				return callback(error) if error?
				usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0
				logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, "checked for tikzexternalize"
				callback null, usesTikzExternalize

	injectOutputFile: (compileDir, mainFile, callback = (error) ->) ->
		ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
			return callback(error) if error?
			fs.readFile path, "utf8", (error, content) ->
				return callback(error) if error?
				logger.log compileDir: compileDir, mainFile: mainFile, "copied file to ouput.tex for tikz"
				# use wx flag to ensure that output file does not already exist
				fs.writeFile Path.join(compileDir, "output.tex"), content, {flag:'wx'}, callback<commit_msg>fix typo in log message<commit_after>fs = require "fs"
Path = require "path"
ResourceWriter = require "./ResourceWriter"
SafeReader = require "./SafeReader"
logger = require "logger-sharelatex"

# for \tikzexternalize 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'.

module.exports = TikzManager =

	checkMainFile: (compileDir, mainFile, resources, callback = (error, usesTikzExternalize) ->) ->
		# if there's already an output.tex file, we don't want to touch it
		for resource in resources
			if resource.path is "output.tex"
				logger.log compileDir: compileDir, mainFile: mainFile, "output.tex already in resources"
				return callback(null, false)
		# if there's no output.tex, see if we are using tikz/pgf in the main file
		ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
			return callback(error) if error?
			SafeReader.readFile path, 65536, "utf8", (error, content) ->
				return callback(error) if error?
				usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0
				logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, "checked for tikzexternalize"
				callback null, usesTikzExternalize

	injectOutputFile: (compileDir, mainFile, callback = (error) ->) ->
		ResourceWriter.checkPath compileDir, mainFile, (error, path) ->
			return callback(error) if error?
			fs.readFile path, "utf8", (error, content) ->
				return callback(error) if error?
				logger.log compileDir: compileDir, mainFile: mainFile, "copied file to output.tex for tikz"
				# use wx flag to ensure that output file does not already exist
				fs.writeFile Path.join(compileDir, "output.tex"), content, {flag:'wx'}, callback
{'fraction_non_alphanumeric': 0.10312764158918004, 'fraction_numerical': 0.004508312200619893, 'mean_word_length': 4.469953775038521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7649115', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1049, 'n_words': 377}