Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit ef5393f

Browse files
author
Cdok
committed
removes leading \n characters from written files
This commit adds a recursive function to strip newline chars from files which start with leading \n characters, or contain \n chars after their shebang
1 parent f00e8b0 commit ef5393f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

copyright/copyright.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ const buildSupportedExtensions = () => {
6262
})
6363
}
6464

65+
/**
66+
* Removes extra \n characters from the top of any files
67+
* to ensure more consistent spacing between copyright headers
68+
* @param {String} content original file to edit
69+
* @return {String} new file with no leading \n
70+
*/
71+
const removeLeadingNewlines = (content) => {
72+
if (content[0] === '') {
73+
content.shift()
74+
removeLeadingNewlines(content)
75+
}
76+
return content
77+
}
6578

6679
if (args.length === 0 || args.indexOf('--help') >= 0) {
6780

@@ -125,10 +138,13 @@ args
125138
// accomodate for shebang and insert before header
126139
if (contentStr[0].indexOf('#!') >= 0) {
127140
const shebang = contentStr.shift()
128-
contentStr = contentStr.join('\n')
141+
debugger
142+
contentStr = removeLeadingNewlines(contentStr).join('\n')
129143
newData = shebang + '\n' + getHeaderText(ext) + '\n' + contentStr // eslint-disable-line prefer-template
130144
} else {
131-
newData = getHeaderText(ext) + `\n${content}` // eslint-disable-line prefer-template
145+
debugger
146+
contentStr = removeLeadingNewlines(contentStr).join('\n')
147+
newData = getHeaderText(ext) + `\n${contentStr}` // eslint-disable-line prefer-template
132148
}
133149

134150
fs.writeFileSync(file, newData)

0 commit comments

Comments
 (0)