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

Commit 8289b85

Browse files
author
Cdok
committed
refactor of promise / glob logic
this commit refactors the promise to instead use glob.sync, and then flatten the array of arrays that is return from glob. Additionally, this commit changes the --lint option to the default, and instead adds a --fix option to run the tool in fix mode
1 parent 54af418 commit 8289b85

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

copyright/copyright.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,45 @@ const args = process.argv.filter((arg) => {
2020
})
2121

2222
const copyright = {
23-
lintMode: false,
23+
lintMode: true,
2424
langs: {},
2525
run() {
2626
this.buildSupportedExtensions()
27+
let files = []
28+
2729
const processedGlobs = args.map((folder) => {
28-
return new Promise((resolve) => {
29-
glob(`${folder}`, (err, file) => {
30-
resolve(file[0])
31-
})
32-
})
30+
files.push(glob.sync(folder))
3331
})
3432

35-
Promise.all(processedGlobs).then((files) => {
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]
33+
// flattens array of arrays returned by glob.sync
34+
files = files.reduce((a, b) => a.concat(b))
4035

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
52-
}
53-
}
54-
})
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]
5540

56-
if (filesContainingHeader.length !== files.length) {
57-
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
58-
process.exit(1)
41+
if (hasCopyrightHeader) {
42+
return true
5943
} else {
60-
console.log(`${cyan}Copyright headers are present in target files`)
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
52+
}
6153
}
6254
})
55+
56+
if (filesContainingHeader.length !== files.length) {
57+
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
58+
process.exit(1)
59+
} else {
60+
console.log(`${cyan}Copyright headers are present in target files`)
61+
}
6362
},
6463
getHeaderText(ext) {
6564
if (!this.langs[ext]) {
@@ -71,13 +70,12 @@ const copyright = {
7170
}
7271
},
7372
buildSupportedExtensions() {
74-
const supportedHeaders = path.join(__dirname, './headers')
75-
fs.readdir(supportedHeaders, (err, filenames) => {
76-
filenames.forEach((file) => {
77-
const extension = file.match(/\.[0-9a-z]+$/i)[0]
78-
this.langs[extension] = file
79-
})
80-
});
73+
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+
})
8179
}
8280
}
8381

@@ -87,22 +85,22 @@ if (args.length === 0 || args.indexOf('--help') >= 0) {
8785
Usage: node copyright.js [options] glob [additional globs]
8886
8987
Example:
90-
${yellow}node copyright.js --lint${defaultFG} src/**/*.js
88+
${yellow}node copyright.js --fix${defaultFG} src/**/*.js
9189
9290
Options:
9391
94-
--lint enable lint mode
92+
--fix run in fix mode
9593
9694
Visit ${cyan}https://github.com/mobify/mobify-code-style${defaultFG} to learn more.
9795
`)
9896

9997
process.exit(0)
10098
}
10199

102-
// Sets lint flag if the user provides --lint command line arg
103-
if (args.indexOf('--lint') >= 0) {
104-
args.splice(args.indexOf('--lint'), 1)
105-
copyright.lintMode = true
100+
// Sets fix flag if the user provides --fix command line arg
101+
if (args.indexOf('--fix') >= 0) {
102+
args.splice(args.indexOf('--fix'), 1)
103+
copyright.lintMode = false
106104
}
107105

108106
copyright.run()

0 commit comments

Comments
 (0)