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

Commit 12b9b05

Browse files
author
Cdok
committed
Refactor of files / filter loop to set error flag
This commit refactors the filter loop to instead set the error flag inside of a forEach loop Additionally - it moves the higher order functions into a more streamlined pipeline style (thank you DBR)
1 parent d390c95 commit 12b9b05

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

copyright/copyright.js

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,29 @@ const copyright = {
2424
langs: {},
2525
run() {
2626
this.buildSupportedExtensions()
27-
let files = []
28-
29-
const processedGlobs = args.map((folder) => {
30-
files.push(glob.sync(folder))
31-
})
32-
33-
// flattens array of arrays returned by glob.sync
34-
files = files.reduce((a, b) => a.concat(b))
35-
36-
const filesContainingHeader = files.filter((file) => {
37-
const content = fs.readFileSync(file)
38-
const hasCopyrightHeader = content.includes('Copyright (c)')
39-
const ext = file.match(/\.[0-9a-z]+$/i)[0]
40-
41-
if (hasCopyrightHeader) {
42-
return true
43-
} else {
44-
if (this.lintMode) {
45-
console.log(`${yellow}${file} ${red}missing copyright header`)
46-
return false
47-
} else {
48-
const newData = this.getHeaderText(ext) + content
49-
fs.writeFileSync(file, newData)
50-
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
51-
return true
27+
let error = false
28+
29+
args
30+
.map((folder) => glob.sync(folder))
31+
.reduce((a, b) => a.concat(b))
32+
.forEach((file) => {
33+
const content = fs.readFileSync(file)
34+
const hasCopyrightHeader = content.includes('Copyright (c)')
35+
const ext = file.match(/\.[0-9a-z]+$/i)[0]
36+
37+
if (!hasCopyrightHeader) {
38+
if (this.lintMode) {
39+
console.log(`${yellow}${file} ${red}missing copyright header`)
40+
error = true
41+
} else {
42+
const newData = this.getHeaderText(ext) + content
43+
fs.writeFileSync(file, newData)
44+
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
45+
}
5246
}
53-
}
54-
})
47+
})
5548

56-
if (filesContainingHeader.length !== files.length) {
49+
if (error) {
5750
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
5851
process.exit(1)
5952
} else {
@@ -71,11 +64,12 @@ const copyright = {
7164
},
7265
buildSupportedExtensions() {
7366
const headerDir = path.join(__dirname, './headers')
74-
filenames = fs.readdirSync(headerDir)
75-
filenames.forEach((file) => {
76-
const extension = file.match(/\.[0-9a-z]+$/i)[0]
77-
this.langs[extension] = file
78-
})
67+
fs
68+
.readdirSync(headerDir)
69+
.forEach((file) => {
70+
const extension = file.match(/\.[0-9a-z]+$/i)[0]
71+
this.langs[extension] = file
72+
})
7973
}
8074
}
8175

0 commit comments

Comments
 (0)