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

Commit 77eaa8c

Browse files
author
Cdok
committed
add support for shebangs, update readme
This commit adds support for #!/usr/ shebangs at the top of files, and will instead add the copyright header below the shebang if it exists Additionally, this commit updates the readme files to better reflect the current functionality of the --fix flag and lint mode
1 parent 88a4a69 commit 77eaa8c

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

copyright/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ npm run copyright:lint
3232

3333
This will add copyright headers to _all_ `.js` files in the `src` directory in the root of your project.
3434

35-
## Lint Mode
35+
## Fix Mode
3636

37-
Passing the `--fix` flag to this tool will disable lint mode.
37+
Passing the `--fix` flag to this tool will enable fix mode.
3838

39-
With lint mode enabled, the process will exit if any of the target directories contain files which do not have copyright headers.
39+
With fix mode enabled, the copyright headers will be added to each file that matches the glob and does not contain the headers.
4040

41-
In fix mode, the tool will add the copyright headers to any targetted files.
41+
Without this flag, the tool is run in lint mode, which will exit the process if any of the target files do not contain the header.
4242

4343
### Developing
4444

copyright/copyright.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ let langs = {}
2121
let lintMode = true
2222
let error = false
2323

24+
// we don't want to pass node and copyright.js directories to the glob
2425
const args = process.argv.filter((arg) => {
2526
return !/node|copyright/.test(arg)
2627
})
2728

29+
/**
30+
* getHeaderText - retrieve appropriate header file context from langs{} object
31+
* @param {String} ext file extension of desired header file
32+
* @return {String} content of appropriate header file
33+
*/
2834
const getHeaderText = (ext) => {
2935
if (!langs[ext]) {
3036
console.log(`${red}${blackBG}ERROR${defaultBG} - ${ext} is not supported (yet)`)
@@ -34,6 +40,11 @@ const getHeaderText = (ext) => {
3440
}
3541
}
3642

43+
/**
44+
* buildSupportedExtensions - initializes the langs{} object with the supported
45+
* extensions, as well as their respective (c) header content
46+
* @return {undefined} - populates the langs{} object
47+
*/
3748
const buildSupportedExtensions = () => {
3849
const headerDir = path.join(__dirname, './headers')
3950
fs
@@ -46,6 +57,7 @@ const buildSupportedExtensions = () => {
4657
})
4758
}
4859

60+
4961
if (args.length === 0 || args.indexOf('--help') >= 0) {
5062

5163
console.log(`
@@ -76,9 +88,10 @@ buildSupportedExtensions()
7688

7789
args
7890
.map((folder) => glob.sync(folder))
79-
.reduce((a, b) => a.concat(b))
91+
.reduce((a, b) => a.concat(b)) // flatten array of arrays
8092
.forEach((file) => {
81-
const content = fs.readFileSync(file)
93+
let content = fs.readFileSync(file)
94+
8295
const hasCopyrightHeader = content.includes('Copyright (c)')
8396
const ext = file.match(/\.[0-9a-z]+$/i)[0]
8497

@@ -87,7 +100,20 @@ args
87100
console.log(`${yellow}${file} ${red}missing copyright header`)
88101
error = true
89102
} else {
90-
const newData = getHeaderText(ext) + content
103+
const contentStr = content.toString().split('\n')
104+
let shebang = ''
105+
let newData = ''
106+
107+
// accomodate for shebang and insert before header
108+
if (contentStr[0].indexOf('#!') >= 0) {
109+
shebang = contentStr.shift()
110+
content = contentStr.join('\n')
111+
112+
newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + content
113+
} else {
114+
newData = getHeaderText(ext) + '\n' + content
115+
}
116+
91117
fs.writeFileSync(file, newData)
92118
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
93119
}

0 commit comments

Comments
 (0)