Skip to content

Commit

Permalink
Rename variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 29, 2021
1 parent 475cd85 commit 3380622
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/report/finalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const hasContent = function ({ content }) {
// Also add footer and padding.
const joinContents = function (contents) {
const [{ output, format, colors, footerString }] = contents
const contentsString = contents.map(getContentProperty).join('\n')
const contentsStringA = `${contentsString}${footerString}`
const contentsStringB = handleContent(contentsStringA, format, colors)
return { contentsString: contentsStringB, output }
const content = contents.map(getContentProperty).join('\n')
const contentA = `${content}${footerString}`
const contentB = handleContent(contentA, format, colors)
return { content: contentB, output }
}

const getContentProperty = function ({ content }) {
Expand Down
22 changes: 9 additions & 13 deletions src/report/insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,26 @@ const getFileContent = async function (output) {
}
}

export const insertContents = async function (
output,
contentsString,
fileContent,
) {
export const insertContents = async function (output, content, fileContent) {
const newline = detectNewline.graceful(fileContent)
const lines = fileContent.split(newline)

const contentsStringA = replaceNewline(contentsString, newline)
const linesA = insertToLines(lines, contentsStringA, output)
const contentA = replaceNewline(content, newline)
const linesA = insertToLines(lines, contentA, output)

const contentsStringB = linesA.join(newline)
await writeFileContent(output, contentsStringB)
const contentB = linesA.join(newline)
await writeFileContent(output, contentB)
}

const replaceNewline = function (contentsString, newline) {
return stripFinalNewline(contentsString.split(UNIX_NEWLINE).join(newline))
const replaceNewline = function (content, newline) {
return stripFinalNewline(content.split(UNIX_NEWLINE).join(newline))
}

const UNIX_NEWLINE = '\n'

// We require both delimiters so that user is aware that both should be moved
// when moving lines around
const insertToLines = function (lines, contentsString, output) {
const insertToLines = function (lines, content, output) {
const startLine = getLineIndex(lines, START_LINE_TOKEN)
const endLine = getLineIndex(lines, END_LINE_TOKEN)

Expand All @@ -79,7 +75,7 @@ const insertToLines = function (lines, contentsString, output) {

const start = lines.slice(0, startLine + 1)
const end = lines.slice(endLine)
return [...start, contentsString, ...end]
return [...start, content, ...end]
}

const getLineIndex = function (lines, token) {
Expand Down
2 changes: 1 addition & 1 deletion src/report/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const reportStart = async function (result, previous, config) {
export const reportPreview = async function (result, historyInfo, config) {
const contents = await computeContents(result, historyInfo, config)
const contentsA = finalizeContents(contents)
return contentsA.length === 0 ? '' : contentsA[0].contentsString
return contentsA.length === 0 ? '' : contentsA[0].content
}

// Compute the report contents.
Expand Down
12 changes: 6 additions & 6 deletions src/report/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const outputContents = async function (contents) {
await Promise.all(contents.map(outputContent))
}

const outputContent = async function ({ contentsString, output }) {
const outputContent = async function ({ content, output }) {
if (output === 'stdout') {
await printToStdout(contentsString)
await printToStdout(content)
return
}

Expand All @@ -32,19 +32,19 @@ const outputContent = async function ({ contentsString, output }) {
const fileContent = await detectInsert(output)

if (fileContent !== undefined) {
await insertContents(output, contentsString, fileContent)
await insertContents(output, content, fileContent)
return
}

await overwriteContents(output, contentsString)
await overwriteContents(output, content)
}

const overwriteContents = async function (output, contentsString) {
const overwriteContents = async function (output, content) {
const outputDir = dirname(output)
await fs.mkdir(outputDir, { recursive: true })

try {
await writeFileAtomic(output, contentsString)
await writeFileAtomic(output, content)
} catch (error) {
throw new UserError(
`Could not write to "output" "${output}"\n${error.message}`,
Expand Down

0 comments on commit 3380622

Please sign in to comment.