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

Commit f7494ae

Browse files
author
Cdok
committed
Adds new "--update" option, update readme
This commit adds a new optional update flag, additionally, this commit adds the documentation / options on the udate flag into the readme. This flag (if passed), will check headers that already exist, and if they are not of the current year, will be updated appropriately
1 parent 77eaa8c commit f7494ae

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

copyright/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ This tool reads from the `headers/copyright-header.extension` files to select wh
77
Add the following to your `package.json` under `"scripts": { ... }`
88

99
```json
10-
"copyright:lint": "copyright 'glob'"
11-
"copyright:fix" "copyright 'glob' --fix"
10+
"copyright:lint": "copyright 'glob'",
11+
"copyright:fix" "copyright 'glob' --fix",
12+
"copyright:update" "copyright 'glob' --update"
1213
```
1314

1415
```bash
@@ -27,18 +28,26 @@ Add the following to your `package.json` file
2728
Then run
2829

2930
```bash
30-
npm run copyright:lint
31+
npm run copyright:fix
3132
```
3233

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

35-
## Fix Mode
36+
## Options
37+
38+
Without any flags, the tool is run in lint mode, which will exit the process if any of the target files do not contain the header.
39+
40+
### Fix Mode
3641

3742
Passing the `--fix` flag to this tool will enable fix mode.
3843

3944
With fix mode enabled, the copyright headers will be added to each file that matches the glob and does not contain the headers.
4045

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.
46+
### Update Mode
47+
48+
Passing the `--update` flag to this tool will enable update mode.
49+
50+
With fix mode enabled, the copyright headers will be updated to the current year in each file that matches the glob and contains headers.
4251

4352
### Developing
4453

copyright/copyright.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ const cyan = '\x1b[36m'
1616
const blackBG = '\x1b[40m'
1717
const defaultBG = '\x1b[49m'
1818
const defaultFG = '\x1b[39m'
19+
const currentYear = new Date().getFullYear()
1920

2021
let langs = {}
2122
let lintMode = true
23+
let updateMode = false
2224
let error = false
2325

2426
// we don't want to pass node and copyright.js directories to the glob
@@ -52,7 +54,7 @@ const buildSupportedExtensions = () => {
5254
.forEach((file) => {
5355
const extension = file.match(/\.[0-9a-z]+$/i)[0]
5456
const textPath = path.join(headerDir, file)
55-
const content = fs.readFileSync(textPath).toString().replace('year', new Date().getFullYear())
57+
const content = fs.readFileSync(textPath).toString().replace('year', currentYear)
5658
langs[extension] = content
5759
})
5860
}
@@ -71,6 +73,7 @@ if (args.length === 0 || args.indexOf('--help') >= 0) {
7173
Options:
7274
7375
--fix run in fix mode
76+
--update update the year in existing headers
7477
7578
Visit ${cyan}https://github.com/mobify/mobify-code-style${defaultFG} to learn more.
7679
`)
@@ -84,32 +87,41 @@ if (args.indexOf('--fix') >= 0) {
8487
lintMode = false
8588
}
8689

90+
if (args.indexOf('--update') >= 0) {
91+
args.splice(args.indexOf('--update'), 1)
92+
updateMode = true
93+
}
94+
8795
buildSupportedExtensions()
8896

8997
args
90-
.map((folder) => glob.sync(folder))
91-
.reduce((a, b) => a.concat(b)) // flatten array of arrays
98+
.map((folder) => glob.sync(folder)) // build array of files matching glob pattern
99+
.reduce((a, b) => a.concat(b)) // flatten array of arrays
92100
.forEach((file) => {
93-
let content = fs.readFileSync(file)
94-
101+
const content = fs.readFileSync(file)
95102
const hasCopyrightHeader = content.includes('Copyright (c)')
96103
const ext = file.match(/\.[0-9a-z]+$/i)[0]
97104

105+
let newData = ''
106+
107+
if (hasCopyrightHeader && updateMode) {
108+
newData = content.toString().replace(/(\(c\)\s)(\d{4})/, '$1' + currentYear)
109+
fs.writeFileSync(file, newData)
110+
console.log(`${green}Copyright header succesfully updated to ${currentYear} in ${magenta}${file}`)
111+
}
112+
98113
if (!hasCopyrightHeader) {
99114
if (lintMode) {
100115
console.log(`${yellow}${file} ${red}missing copyright header`)
101116
error = true
102117
} else {
103-
const contentStr = content.toString().split('\n')
104-
let shebang = ''
105-
let newData = ''
118+
let contentStr = content.toString().split('\n')
106119

107120
// accomodate for shebang and insert before header
108121
if (contentStr[0].indexOf('#!') >= 0) {
109-
shebang = contentStr.shift()
110-
content = contentStr.join('\n')
111-
112-
newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + content
122+
const shebang = contentStr.shift()
123+
contentStr = contentStr.join('\n')
124+
newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + contentStr
113125
} else {
114126
newData = getHeaderText(ext) + '\n' + content
115127
}

0 commit comments

Comments
 (0)