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

Commit af2b08c

Browse files
committed
Merge branch 'release/0.0.0'
2 parents df12fe2 + 0f6e612 commit af2b08c

File tree

8 files changed

+948
-1
lines changed

8 files changed

+948
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
63+
# Compiled code
64+
dist/

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4+
5+
## [0.0.0] - 2018-11-08
6+
7+
### Added
8+
9+
- Add initial `updateLicenseHeader` function supporting JavaScript, TypeScript, PHP, PHTML and Twig.
10+
11+
[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.0...HEAD
12+
[0.0.0]: https://github.com/splish-me/copyright-headers/compare/df12fe2868efc66641034590c3ffd37e0896afbb...HEAD

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# copyright-headers
2-
A configurable Node.js utility to keep your copyright headers up to date
2+
3+
A configurable Node.js utility to keep your copyright headers up to date.
4+
5+
Documentation WIP, see [`scripts/license-headers.ts`](scripts/license-headers.ts) as an example for now.

package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@splish-me/copyright-headers",
3+
"version": "0.0.0",
4+
"homepage": "https://github.com/splish-me/copyright-headers",
5+
"bugs": {
6+
"url": "https://github.com/splish-me/copyright-headers/issues"
7+
},
8+
"license": "MIT",
9+
"author": "Splish UG (haftungsbeschränkt)",
10+
"files": [
11+
"dist",
12+
"src"
13+
],
14+
"main": "dist/index.js",
15+
"repository": "github:splish-me/copyright-headers",
16+
"scripts": {
17+
"prebuild": "rimraf dist",
18+
"build": "tsc",
19+
"format": "npm-run-all format:*",
20+
"lint": "npm-run-all lint:*",
21+
"license": "ts-node scripts/license-headers",
22+
"format:prettier": "yarn _prettier --write",
23+
"lint:prettier": "yarn _prettier --list-different",
24+
"_php-cs-fixer": "php php-cs-fixer.phar fix --config=php-cs-fixer.config.php",
25+
"_prettier": "prettier \"{src/**/*,*}.{js,jsx,ts,tsx,css,scss,sass,less,json,md,markdown,yaml,yml}\""
26+
},
27+
"dependencies": {
28+
"signale": "^1.3.0"
29+
},
30+
"devDependencies": {
31+
"@types/glob": "^7.0.0",
32+
"@types/node": "^10.0.0",
33+
"@types/signale": "^1.2.0",
34+
"glob": "^7.0.0",
35+
"npm-run-all": "^4.0.0",
36+
"prettier": "^1.0.0",
37+
"rimraf": "^2.6.2",
38+
"ts-node": "^7.0.0",
39+
"typescript": "^3.0.0"
40+
},
41+
"publishConfig": {
42+
"access": "public"
43+
}
44+
}

scripts/license-headers.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* This file is part of @splish-me/copyright-headers.
3+
*
4+
* @copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt)
5+
* @license https://opensource.org/licenses/MIT MIT License
6+
* @link https://github.com/splish-me/copyright-headers for the canonical source repository
7+
*/
8+
import * as glob from 'glob'
9+
import * as path from 'path'
10+
import * as util from 'util'
11+
12+
import { updateLicenseHeader } from '../src'
13+
14+
const g = util.promisify(glob)
15+
16+
const root = path.join(__dirname, '..')
17+
18+
const lines = [
19+
'This file is part of @splish-me/copyright-headers.',
20+
'',
21+
`@copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt)`,
22+
'@license https://opensource.org/licenses/MIT MIT License',
23+
'@link https://github.com/splish-me/copyright-headers for the canonical source repository'
24+
]
25+
26+
g('@(scripts|src)/*.ts', {
27+
cwd: root
28+
}).then(files => {
29+
return files.map(file => {
30+
const filePath = path.join(root, file)
31+
32+
return updateLicenseHeader({
33+
filePath,
34+
lines,
35+
shouldUpdate: content => {
36+
return content.includes('Splish UG')
37+
}
38+
})
39+
})
40+
})

src/index.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* This file is part of @splish-me/copyright-headers.
3+
*
4+
* @copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt)
5+
* @license https://opensource.org/licenses/MIT MIT License
6+
* @link https://github.com/splish-me/copyright-headers for the canonical source repository
7+
*/
8+
import * as fs from 'fs'
9+
import * as signale from 'signale'
10+
import * as util from 'util'
11+
12+
const readFile = util.promisify(fs.readFile)
13+
const writeFile = util.promisify(fs.writeFile)
14+
15+
const options = { encoding: 'utf-8' }
16+
17+
interface SourceLanguage {
18+
match: RegExp
19+
before?: string
20+
after?: string
21+
begin: string
22+
end: string
23+
buildLine: (line: string) => string
24+
}
25+
26+
const cStyleComments: Pick<SourceLanguage, 'begin' | 'buildLine' | 'end'> = {
27+
begin: '/**',
28+
buildLine: line => {
29+
return ` *${line.length > 0 && !line.startsWith(' ') ? ' ' : ''}${line}`
30+
},
31+
end: ' */'
32+
}
33+
34+
const js: SourceLanguage = {
35+
match: /.jsx?$/,
36+
...cStyleComments
37+
}
38+
39+
const ts: SourceLanguage = {
40+
...js,
41+
match: /.tsx?$/
42+
}
43+
44+
const php: SourceLanguage = {
45+
match: /(\.php|\.php\.dist)$/,
46+
before: '<?php\n',
47+
...cStyleComments
48+
}
49+
50+
const phtml: SourceLanguage = {
51+
match: /\.phtml$/,
52+
before: '<?php\n',
53+
after: '\n?>',
54+
...cStyleComments
55+
}
56+
57+
const twig: SourceLanguage = {
58+
match: /\.twig$/,
59+
begin: '{##',
60+
buildLine: line => {
61+
return ` #${line.length > 0 && !line.startsWith(' ') ? ' ' : ''}${line}`
62+
},
63+
end: ' #}'
64+
}
65+
66+
export async function updateLicenseHeader({
67+
filePath,
68+
lines,
69+
shouldUpdate = () => false
70+
}: {
71+
filePath: string
72+
lines: string[]
73+
shouldUpdate?: (content: string) => boolean
74+
}) {
75+
const language = getSourceLanguage(filePath)
76+
77+
if (!language) {
78+
console.log('[ERR] Unrecognized source language:', filePath)
79+
return Promise.resolve()
80+
}
81+
82+
const header = getLicenseHeader(language, lines)
83+
const re = getLicenseHeaderRegExp(language)
84+
85+
return readFile(filePath, options).then(content => {
86+
const match = content.match(re)
87+
88+
if (!match) {
89+
signale.success('Added new license header to', filePath)
90+
// No license header present, add license header to the beginning
91+
return writeFile(
92+
filePath,
93+
`${header}\n${
94+
language.before && content.startsWith(language.before)
95+
? content.substring(language.before.length)
96+
: content
97+
}`,
98+
options
99+
)
100+
}
101+
102+
if (match[0] === header) {
103+
// Nothing to do here
104+
return Promise.resolve()
105+
}
106+
107+
if (shouldUpdate(match[0])) {
108+
signale.success('Updated license header of', filePath)
109+
// License header present that should be overriden
110+
return writeFile(filePath, content.replace(re, header), options)
111+
}
112+
113+
signale.info(
114+
'Did not update existing (and possibly external) license header of',
115+
filePath
116+
)
117+
118+
return Promise.resolve()
119+
})
120+
}
121+
122+
function getSourceLanguage(filePath: string) {
123+
const supportedLanguages = [js, ts, php, phtml, twig]
124+
125+
return supportedLanguages.find(lang => lang.match.test(filePath))
126+
}
127+
128+
function getLicenseHeader(language: SourceLanguage, lines: string[]) {
129+
return (
130+
(language.before || '') +
131+
`${language.begin}\n` +
132+
lines.map(language.buildLine).join('\n') +
133+
`\n${language.end}` +
134+
(language.after || '')
135+
)
136+
}
137+
138+
function getLicenseHeaderRegExp(language: SourceLanguage) {
139+
const forRe = (s: string) => s.replace(/(\?|\/|\*)/g, match => `\\${match}`)
140+
141+
return new RegExp(
142+
`${forRe(language.before || '')}${forRe(language.begin)}\n(.+\n)*${forRe(
143+
language.end
144+
)}${forRe(language.after || '')}`
145+
)
146+
}

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": false,
4+
"alwaysStrict": true,
5+
"declaration": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"jsx": "react",
8+
"module": "commonjs",
9+
"moduleResolution": "node",
10+
"noImplicitAny": true,
11+
"noImplicitReturns": true,
12+
"noImplicitThis": true,
13+
"noUnusedLocals": true,
14+
"noUnusedParameters": true,
15+
"outDir": "dist",
16+
"strictFunctionTypes": true,
17+
"strictPropertyInitialization": true,
18+
"strictNullChecks": true,
19+
"target": "es6"
20+
},
21+
"include": ["src"]
22+
}

0 commit comments

Comments
 (0)