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

Commit

Permalink
Merge branch 'release/0.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
inyono committed Nov 8, 2018
2 parents df12fe2 + 0f6e612 commit af2b08c
Show file tree
Hide file tree
Showing 8 changed files with 948 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# Compiled code
dist/
12 changes: 12 additions & 0 deletions CHANGELOG.md
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
5 changes: 4 additions & 1 deletion README.md
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.
44 changes: 44 additions & 0 deletions package.json
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"
}
}
40 changes: 40 additions & 0 deletions scripts/license-headers.ts
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')
}
})
})
})
146 changes: 146 additions & 0 deletions src/index.ts
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 || '')}`
)
}
22 changes: 22 additions & 0 deletions tsconfig.json
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"]
}
Loading

0 comments on commit af2b08c

Please sign in to comment.