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

Commit 88a4a69

Browse files
author
Cdok
committed
small refactor to remove copyright object wrapper
This commit removes the need for the copyright object, and instead keeps all of the functions at the module-level, instead of bound to the copyright object
1 parent c9fa2ef commit 88a4a69

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

copyright/copyright.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,35 @@ const blackBG = '\x1b[40m'
1717
const defaultBG = '\x1b[49m'
1818
const defaultFG = '\x1b[39m'
1919

20+
let langs = {}
21+
let lintMode = true
22+
let error = false
23+
2024
const args = process.argv.filter((arg) => {
2125
return !/node|copyright/.test(arg)
2226
})
2327

24-
const copyright = {
25-
lintMode: true,
26-
langs: {},
27-
run() {
28-
this.buildSupportedExtensions()
29-
let error = false
30-
args
31-
.map((folder) => glob.sync(folder))
32-
.reduce((a, b) => a.concat(b))
33-
.forEach((file) => {
34-
const content = fs.readFileSync(file)
35-
const hasCopyrightHeader = content.includes('Copyright (c)')
36-
const ext = file.match(/\.[0-9a-z]+$/i)[0]
37-
38-
if (!hasCopyrightHeader) {
39-
if (this.lintMode) {
40-
console.log(`${yellow}${file} ${red}missing copyright header`)
41-
error = true
42-
} else {
43-
const newData = this.getHeaderText(ext) + content
44-
fs.writeFileSync(file, newData)
45-
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
46-
}
47-
}
48-
})
49-
50-
if (error) {
51-
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
52-
process.exit(1)
53-
} else {
54-
console.log(`${cyan}Copyright headers are present in target files`)
55-
}
56-
},
57-
getHeaderText(ext) {
58-
if (!this.langs[ext]) {
59-
console.log(`${red}${blackBG}ERROR${defaultBG} - ${ext} is not supported (yet)`)
60-
process.exit(1)
61-
} else {
62-
return this.langs[ext]
63-
}
64-
},
65-
buildSupportedExtensions() {
66-
const headerDir = path.join(__dirname, './headers')
67-
fs
68-
.readdirSync(headerDir)
69-
.forEach((file) => {
70-
const extension = file.match(/\.[0-9a-z]+$/i)[0]
71-
const textPath = path.join(headerDir, file)
72-
const content = fs.readFileSync(textPath).toString().replace('year', new Date().getFullYear())
73-
this.langs[extension] = content
74-
})
28+
const getHeaderText = (ext) => {
29+
if (!langs[ext]) {
30+
console.log(`${red}${blackBG}ERROR${defaultBG} - ${ext} is not supported (yet)`)
31+
process.exit(1)
32+
} else {
33+
return langs[ext]
7534
}
7635
}
7736

37+
const buildSupportedExtensions = () => {
38+
const headerDir = path.join(__dirname, './headers')
39+
fs
40+
.readdirSync(headerDir)
41+
.forEach((file) => {
42+
const extension = file.match(/\.[0-9a-z]+$/i)[0]
43+
const textPath = path.join(headerDir, file)
44+
const content = fs.readFileSync(textPath).toString().replace('year', new Date().getFullYear())
45+
langs[extension] = content
46+
})
47+
}
48+
7849
if (args.length === 0 || args.indexOf('--help') >= 0) {
7950

8051
console.log(`
@@ -98,7 +69,34 @@ if (args.length === 0 || args.indexOf('--help') >= 0) {
9869
// Sets fix flag if the user provides --fix command line arg
9970
if (args.indexOf('--fix') >= 0) {
10071
args.splice(args.indexOf('--fix'), 1)
101-
copyright.lintMode = false
72+
lintMode = false
10273
}
10374

104-
copyright.run()
75+
buildSupportedExtensions()
76+
77+
args
78+
.map((folder) => glob.sync(folder))
79+
.reduce((a, b) => a.concat(b))
80+
.forEach((file) => {
81+
const content = fs.readFileSync(file)
82+
const hasCopyrightHeader = content.includes('Copyright (c)')
83+
const ext = file.match(/\.[0-9a-z]+$/i)[0]
84+
85+
if (!hasCopyrightHeader) {
86+
if (lintMode) {
87+
console.log(`${yellow}${file} ${red}missing copyright header`)
88+
error = true
89+
} else {
90+
const newData = getHeaderText(ext) + content
91+
fs.writeFileSync(file, newData)
92+
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
93+
}
94+
}
95+
})
96+
97+
if (error) {
98+
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
99+
process.exit(1)
100+
} else {
101+
console.log(`${cyan}Copyright headers are present in target files`)
102+
}

0 commit comments

Comments
 (0)