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

Commit 6021693

Browse files
author
Jeremy Wiebe
committed
Merge branch 'develop' into npm-auto-publish
2 parents 3221841 + 79fc65a commit 6021693

File tree

8 files changed

+230
-1
lines changed

8 files changed

+230
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.8.0 (May 3, 2017)
2+
- Adds new copyright-header management tool
3+
14
## v2.7.4 (February 2, 2017)
25
- Add new classname prefixes to CSS documentation: `pw-` and `qa-`
36
- Add links to alternative CSSComb text editor plugins

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,11 @@ instructions on using `pep8`, `pylint` and `pyflakes` to check Python code.
112112

113113
See the [`docs`](docs) directory for setup and configuration of Markdown
114114
linting.
115+
116+
## Copyright Headers
117+
118+
All Mobify source files should contain copyright headers at the beginning of the
119+
file.
120+
121+
See the [`copyright`](copyright) directory for setup and configuration of the
122+
copyright manager tool

copyright/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Mobify Copyright Headers
2+
3+
This tool reads from the `headers/copyright-header.extension` files to select what header to insert, where `extension` is the target file extension
4+
5+
For any new `copyright-header.extension` files, write `Copyright (c) year` instead of writing the numerical year, as the tool will find and replace `year` with the current year.
6+
7+
## Quick Start
8+
9+
Add the following to your `package.json` under `"scripts": { ... }`
10+
11+
```json
12+
"copyright:lint": "copyright 'glob'",
13+
"copyright:fix" "copyright 'glob' --fix",
14+
"copyright:update" "copyright 'glob' --update"
15+
```
16+
17+
**NOTE**: The glob must be wrapped in single quotes to ensure that the globbing is handled by the copyright tool.
18+
19+
Without wrapping the glob pattern in single quotes, your shell will expand the glob, and may not return the same directories across different environments.
20+
21+
22+
```bash
23+
npm install mobify-code-style --save
24+
npm run copyright:lint
25+
```
26+
27+
## Example
28+
29+
Add the following to your `package.json` file
30+
31+
```json
32+
"copyright:fix": "copyright './src/**/*.js' --fix",
33+
```
34+
35+
Then run
36+
37+
```bash
38+
npm run copyright:fix
39+
```
40+
41+
This will add copyright headers to _all_ `.js` files in the `src` directory in the root of your project.
42+
43+
## Options
44+
45+
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.
46+
47+
### Fix Mode
48+
49+
Passing the `--fix` flag to this tool will enable fix mode.
50+
51+
With fix mode enabled, the copyright headers will be added to each file that matches the glob and does not contain the headers.
52+
53+
### Update Mode
54+
55+
Passing the `--update` flag to this tool will enable update mode.
56+
57+
With update mode enabled, the copyright headers will be updated to the current year in each file that matches the glob and already contains headers.
58+
59+
### Developing
60+
61+
`node --inspect --debug-brk copyright.js '../../some-project/source-folder/**/*.js'`

copyright/copyright.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/usr/bin/env node
2+
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
4+
/* Copyright (c) 2017 Mobify Research & Development Inc. All rights reserved. */
5+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
6+
7+
const glob = require('glob')
8+
const fs = require('fs')
9+
const path = require('path')
10+
11+
const red = '\x1b[31m'
12+
const green = '\x1b[32m'
13+
const yellow = '\x1b[33m'
14+
const magenta = '\x1b[35m'
15+
const cyan = '\x1b[36m'
16+
const blackBG = '\x1b[40m'
17+
const defaultBG = '\x1b[49m'
18+
const defaultFG = '\x1b[39m'
19+
20+
const currentYear = new Date().getFullYear()
21+
const langs = {}
22+
23+
let lintMode = true
24+
let updateMode = false
25+
let error = false
26+
27+
// we don't want to pass node and copyright.js directories to the glob
28+
const args = process.argv.filter((arg) => {
29+
return !/node|copyright/.test(arg)
30+
})
31+
32+
/**
33+
* getHeaderText - retrieve appropriate header file context from langs{} object
34+
* @param {String} ext file extension of desired header file
35+
* @return {String} content of appropriate header file
36+
*/
37+
const getHeaderText = (ext) => {
38+
if (!langs[ext]) {
39+
console.log(`${red}${blackBG}ERROR${defaultBG} - ${ext} is not supported (yet)`)
40+
process.exit(1)
41+
}
42+
return langs[ext]
43+
}
44+
45+
/**
46+
* buildSupportedExtensions - initializes the langs{} object with the supported
47+
* extensions, as well as their respective (c) header content
48+
* @return {undefined} - populates the langs{} object
49+
*/
50+
const buildSupportedExtensions = () => {
51+
const headerDir = path.join(__dirname, './headers')
52+
fs
53+
.readdirSync(headerDir)
54+
.forEach((file) => {
55+
const extension = file.match(/\.[0-9a-z]+$/i)[0]
56+
const textPath = path.join(headerDir, file)
57+
const content = fs
58+
.readFileSync(textPath)
59+
.toString()
60+
.replace('year', currentYear)
61+
62+
langs[extension] = content
63+
})
64+
}
65+
66+
67+
if (args.length === 0 || args.indexOf('--help') >= 0) {
68+
69+
console.log(`
70+
Usage: node copyright.js [options] 'glob' ['additional globs']
71+
72+
If your glob is not targetting all nested directories, ensure that the glob string is wrapped in single quotes
73+
74+
Example:
75+
${yellow}node copyright.js --fix${defaultFG} 'src/**/*.js'
76+
77+
Options:
78+
79+
--fix run in fix mode
80+
--update update the year in existing headers
81+
82+
Visit ${cyan}https://github.com/mobify/mobify-code-style${defaultFG} to learn more.
83+
`)
84+
85+
process.exit(0)
86+
}
87+
88+
// Sets fix flag if the user provides --fix command line arg
89+
if (args.indexOf('--fix') >= 0) {
90+
args.splice(args.indexOf('--fix'), 1)
91+
lintMode = false
92+
}
93+
94+
if (args.indexOf('--update') >= 0) {
95+
args.splice(args.indexOf('--update'), 1)
96+
updateMode = true
97+
}
98+
99+
buildSupportedExtensions()
100+
101+
args
102+
.map((folder) => glob.sync(folder)) // build array of files matching glob pattern
103+
.reduce((a, b) => a.concat(b)) // flatten array of arrays
104+
.forEach((file) => {
105+
const content = fs.readFileSync(file)
106+
const hasCopyrightHeader = content.includes('Copyright (c)')
107+
const ext = file.match(/\.[0-9a-z]+$/i)[0]
108+
109+
let newData = ''
110+
111+
if (hasCopyrightHeader && updateMode) {
112+
newData = content.toString().replace(/(\(c\)\s)(\d{4})/, `$1 ${currentYear}`)
113+
fs.writeFileSync(file, newData)
114+
console.log(`${green}Copyright header succesfully updated to ${currentYear} in ${magenta}${file}`)
115+
}
116+
117+
if (!hasCopyrightHeader) {
118+
if (lintMode) {
119+
console.log(`${yellow}${file} ${red}missing copyright header`)
120+
error = true
121+
} else {
122+
let contentStr = content.toString().split('\n')
123+
124+
// accomodate for shebang and insert before header
125+
if (contentStr[0].indexOf('#!') >= 0) {
126+
const shebang = contentStr.shift()
127+
contentStr = contentStr.join('\n')
128+
newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + contentStr // eslint-disable-line prefer-template
129+
} else {
130+
newData = getHeaderText(ext) + `\n${content}` // eslint-disable-line prefer-template
131+
}
132+
133+
fs.writeFileSync(file, newData)
134+
console.log(`${green}Copyright header succesfully written into ${magenta}${file}`)
135+
}
136+
}
137+
})
138+
139+
if (error) {
140+
console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`)
141+
process.exit(1)
142+
} else {
143+
console.log(`${cyan}Copyright headers are present in target files`)
144+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
2+
/* Copyright (c) year Mobify Research & Development Inc. All rights reserved. */
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
2+
/* Copyright (c) year Mobify Research & Development Inc. All rights reserved. */
3+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
3+
Copyright (c) year Mobify Research & Development Inc. All rights reserved.
4+
5+
"""

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
"url": "https://github.com/mobify/mobify-code-style/issues"
1313
},
1414
"bin": {
15-
"lint-md": "docs/bin/lint-md"
15+
"lint-md": "docs/bin/lint-md",
16+
"copyright": "copyright/copyright.js"
1617
},
1718
"homepage": "https://github.com/mobify/mobify-code-style",
1819
"scripts": {
1920
"test": "scripts/test.sh"
2021
},
2122
"dependencies": {
23+
"glob": "^7.1.1",
2224
"remark-cli": "2.1.0",
2325
"remark-lint": "5.2.0",
2426
"remark-preset-lint-recommended": "1.0.0"

0 commit comments

Comments
 (0)