This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
948 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,3 +59,6 @@ typings/ | |
|
||
# next.js build output | ||
.next | ||
|
||
# Compiled code | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Changelog | ||
|
||
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/). | ||
|
||
## [0.0.0] - 2018-11-08 | ||
|
||
### Added | ||
|
||
- Add initial `updateLicenseHeader` function supporting JavaScript, TypeScript, PHP, PHTML and Twig. | ||
|
||
[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.0...HEAD | ||
[0.0.0]: https://github.com/splish-me/copyright-headers/compare/df12fe2868efc66641034590c3ffd37e0896afbb...HEAD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# copyright-headers | ||
A configurable Node.js utility to keep your copyright headers up to date | ||
|
||
A configurable Node.js utility to keep your copyright headers up to date. | ||
|
||
Documentation WIP, see [`scripts/license-headers.ts`](scripts/license-headers.ts) as an example for now. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"name": "@splish-me/copyright-headers", | ||
"version": "0.0.0", | ||
"homepage": "https://github.com/splish-me/copyright-headers", | ||
"bugs": { | ||
"url": "https://github.com/splish-me/copyright-headers/issues" | ||
}, | ||
"license": "MIT", | ||
"author": "Splish UG (haftungsbeschränkt)", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"main": "dist/index.js", | ||
"repository": "github:splish-me/copyright-headers", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "tsc", | ||
"format": "npm-run-all format:*", | ||
"lint": "npm-run-all lint:*", | ||
"license": "ts-node scripts/license-headers", | ||
"format:prettier": "yarn _prettier --write", | ||
"lint:prettier": "yarn _prettier --list-different", | ||
"_php-cs-fixer": "php php-cs-fixer.phar fix --config=php-cs-fixer.config.php", | ||
"_prettier": "prettier \"{src/**/*,*}.{js,jsx,ts,tsx,css,scss,sass,less,json,md,markdown,yaml,yml}\"" | ||
}, | ||
"dependencies": { | ||
"signale": "^1.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/glob": "^7.0.0", | ||
"@types/node": "^10.0.0", | ||
"@types/signale": "^1.2.0", | ||
"glob": "^7.0.0", | ||
"npm-run-all": "^4.0.0", | ||
"prettier": "^1.0.0", | ||
"rimraf": "^2.6.2", | ||
"ts-node": "^7.0.0", | ||
"typescript": "^3.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* This file is part of @splish-me/copyright-headers. | ||
* | ||
* @copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt) | ||
* @license https://opensource.org/licenses/MIT MIT License | ||
* @link https://github.com/splish-me/copyright-headers for the canonical source repository | ||
*/ | ||
import * as glob from 'glob' | ||
import * as path from 'path' | ||
import * as util from 'util' | ||
|
||
import { updateLicenseHeader } from '../src' | ||
|
||
const g = util.promisify(glob) | ||
|
||
const root = path.join(__dirname, '..') | ||
|
||
const lines = [ | ||
'This file is part of @splish-me/copyright-headers.', | ||
'', | ||
`@copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt)`, | ||
'@license https://opensource.org/licenses/MIT MIT License', | ||
'@link https://github.com/splish-me/copyright-headers for the canonical source repository' | ||
] | ||
|
||
g('@(scripts|src)/*.ts', { | ||
cwd: root | ||
}).then(files => { | ||
return files.map(file => { | ||
const filePath = path.join(root, file) | ||
|
||
return updateLicenseHeader({ | ||
filePath, | ||
lines, | ||
shouldUpdate: content => { | ||
return content.includes('Splish UG') | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* This file is part of @splish-me/copyright-headers. | ||
* | ||
* @copyright Copyright (c) 2018 Splish UG (haftungsbeschränkt) | ||
* @license https://opensource.org/licenses/MIT MIT License | ||
* @link https://github.com/splish-me/copyright-headers for the canonical source repository | ||
*/ | ||
import * as fs from 'fs' | ||
import * as signale from 'signale' | ||
import * as util from 'util' | ||
|
||
const readFile = util.promisify(fs.readFile) | ||
const writeFile = util.promisify(fs.writeFile) | ||
|
||
const options = { encoding: 'utf-8' } | ||
|
||
interface SourceLanguage { | ||
match: RegExp | ||
before?: string | ||
after?: string | ||
begin: string | ||
end: string | ||
buildLine: (line: string) => string | ||
} | ||
|
||
const cStyleComments: Pick<SourceLanguage, 'begin' | 'buildLine' | 'end'> = { | ||
begin: '/**', | ||
buildLine: line => { | ||
return ` *${line.length > 0 && !line.startsWith(' ') ? ' ' : ''}${line}` | ||
}, | ||
end: ' */' | ||
} | ||
|
||
const js: SourceLanguage = { | ||
match: /.jsx?$/, | ||
...cStyleComments | ||
} | ||
|
||
const ts: SourceLanguage = { | ||
...js, | ||
match: /.tsx?$/ | ||
} | ||
|
||
const php: SourceLanguage = { | ||
match: /(\.php|\.php\.dist)$/, | ||
before: '<?php\n', | ||
...cStyleComments | ||
} | ||
|
||
const phtml: SourceLanguage = { | ||
match: /\.phtml$/, | ||
before: '<?php\n', | ||
after: '\n?>', | ||
...cStyleComments | ||
} | ||
|
||
const twig: SourceLanguage = { | ||
match: /\.twig$/, | ||
begin: '{##', | ||
buildLine: line => { | ||
return ` #${line.length > 0 && !line.startsWith(' ') ? ' ' : ''}${line}` | ||
}, | ||
end: ' #}' | ||
} | ||
|
||
export async function updateLicenseHeader({ | ||
filePath, | ||
lines, | ||
shouldUpdate = () => false | ||
}: { | ||
filePath: string | ||
lines: string[] | ||
shouldUpdate?: (content: string) => boolean | ||
}) { | ||
const language = getSourceLanguage(filePath) | ||
|
||
if (!language) { | ||
console.log('[ERR] Unrecognized source language:', filePath) | ||
return Promise.resolve() | ||
} | ||
|
||
const header = getLicenseHeader(language, lines) | ||
const re = getLicenseHeaderRegExp(language) | ||
|
||
return readFile(filePath, options).then(content => { | ||
const match = content.match(re) | ||
|
||
if (!match) { | ||
signale.success('Added new license header to', filePath) | ||
// No license header present, add license header to the beginning | ||
return writeFile( | ||
filePath, | ||
`${header}\n${ | ||
language.before && content.startsWith(language.before) | ||
? content.substring(language.before.length) | ||
: content | ||
}`, | ||
options | ||
) | ||
} | ||
|
||
if (match[0] === header) { | ||
// Nothing to do here | ||
return Promise.resolve() | ||
} | ||
|
||
if (shouldUpdate(match[0])) { | ||
signale.success('Updated license header of', filePath) | ||
// License header present that should be overriden | ||
return writeFile(filePath, content.replace(re, header), options) | ||
} | ||
|
||
signale.info( | ||
'Did not update existing (and possibly external) license header of', | ||
filePath | ||
) | ||
|
||
return Promise.resolve() | ||
}) | ||
} | ||
|
||
function getSourceLanguage(filePath: string) { | ||
const supportedLanguages = [js, ts, php, phtml, twig] | ||
|
||
return supportedLanguages.find(lang => lang.match.test(filePath)) | ||
} | ||
|
||
function getLicenseHeader(language: SourceLanguage, lines: string[]) { | ||
return ( | ||
(language.before || '') + | ||
`${language.begin}\n` + | ||
lines.map(language.buildLine).join('\n') + | ||
`\n${language.end}` + | ||
(language.after || '') | ||
) | ||
} | ||
|
||
function getLicenseHeaderRegExp(language: SourceLanguage) { | ||
const forRe = (s: string) => s.replace(/(\?|\/|\*)/g, match => `\\${match}`) | ||
|
||
return new RegExp( | ||
`${forRe(language.before || '')}${forRe(language.begin)}\n(.+\n)*${forRe( | ||
language.end | ||
)}${forRe(language.after || '')}` | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": false, | ||
"alwaysStrict": true, | ||
"declaration": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"jsx": "react", | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"noImplicitThis": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"outDir": "dist", | ||
"strictFunctionTypes": true, | ||
"strictPropertyInitialization": true, | ||
"strictNullChecks": true, | ||
"target": "es6" | ||
}, | ||
"include": ["src"] | ||
} |
Oops, something went wrong.