Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
refactor(utils): delete temp file after user is done
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Garant committed Nov 4, 2019
1 parent 1cc1280 commit 77204c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ fs.writeFileSync = () => {}

fs.readFileSync = () => fileContents

fs.unlinkSync = () => {}

module.exports = fs
17 changes: 11 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as nock from 'nock'
import * as zlib from 'zlib'
import * as open from 'opn'
import { spawnSync, execSyncInteractiveStream } from './exec'
import { readFileSync, writeFileSync } from 'fs'
import { readFileSync, writeFileSync, unlinkSync } from 'fs'
import * as logger from './logger'

const testing = process.env.NODE_ENV === 'testing'
Expand All @@ -27,7 +27,7 @@ export function openUrl(url) {
* @example
* openFileInEditor('temp-gh-issue-title.txt', '# Add a pr title msg on the next line')
*/
export function openFileInEditor(fileName: string, msg: string) {
export function openFileInEditor(fileName: string, msg: string): string {
try {
const filePath = `${__dirname}/${fileName}`

Expand All @@ -39,19 +39,24 @@ export function openFileInEditor(fileName: string, msg: string) {

const newFileContents = readFileSync(filePath).toString()

return cleanFileContents(newFileContents)
const commentMark = fileName.endsWith('.md') ? '<!--' : '#'

unlinkSync(filePath)

return cleanFileContents(newFileContents, commentMark)
} catch (err) {
logger.error('Could not use your editor to get a custom message\n', err)
logger.error('Could not use your editor to store a custom message\n', err)
}
}

/**
* Removes # comments and trims new lines
* @param {string} commentMark - refers to the comment mark which is different for each file
*/
export function cleanFileContents(fileContents: string): string {
export function cleanFileContents(fileContents: string, commentMark = '#'): string {
return fileContents
.split('\n')
.filter(line => !line.startsWith('#'))
.filter(line => !line.startsWith(commentMark))
.join('\n')
.trim()
}
Expand Down

0 comments on commit 77204c5

Please sign in to comment.