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.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
inyono committed Nov 18, 2018
2 parents 72e77cb + a900b9f commit 875dc94
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 21 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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.2] - 2018-11-18

### Fixed

- Handle multiple multiline comments without newline in between correctly

## [0.0.1] - 2018-11-08

### Added
Expand All @@ -23,6 +29,7 @@ All notable changes to this project will be documented in this file. The format

- Add initial `updateLicenseHeader` function supporting JavaScript, TypeScript, PHP, PHTML and Twig.

[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.1...HEAD
[unreleased]: https://github.com/splish-me/copyright-headers/compare/0.0.2...HEAD
[0.0.2]: https://github.com/splish-me/copyright-headers/compare/0.0.1...0.0.2
[0.0.1]: https://github.com/splish-me/copyright-headers/compare/0.0.0...0.0.1
[0.0.0]: https://github.com/splish-me/copyright-headers/compare/df12fe2868efc66641034590c3ffd37e0896afbb...HEAD
85 changes: 85 additions & 0 deletions __tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ echo "foo bar";
*
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
*/
echo "foo bar";
`)
})

test('does start with <?php (w/ unnecessary whitespace after opening tag)', () => {
const input = `<?php
echo "foo bar";
`
const [status, output] = getUpdatedCopyrightHeader(input, php, options)

expect(status).toEqual(CopyrightHeaderStatus.Added)
expect(output).toEqual(`<?php
/**
* This file is part of @splish-me/copyright-headers
*
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
*/
echo "foo bar";
`)
})
Expand Down Expand Up @@ -115,6 +136,37 @@ echo "foo bar";
* Copyright (c) 2016 Max Mustermann Corporation
*/
echo "foo bar";
`)
})

test('multiple copyright headers (w/o newlines in between)', () => {
const input = `<?php
/**
* This file is part of @splish-me/copyright-headers
*
* Copyright (c) 2016 Splish UG (haftungsbeschränkt)
*/
/**
* Copyright (c) 2016 Max Mustermann Corporation
*/
echo "foo bar";
`
const [status, output] = getUpdatedCopyrightHeader(input, php, {
...options,
shouldUpdate: () => true
})

expect(status).toEqual(CopyrightHeaderStatus.Changed)
expect(output).toEqual(`<?php
/**
* This file is part of @splish-me/copyright-headers
*
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
*/
/**
* Copyright (c) 2016 Max Mustermann Corporation
*/
echo "foo bar";
`)
})
})
Expand Down Expand Up @@ -236,6 +288,39 @@ echo "foo bar";
*/
echo "foo bar";
?>
`)
})

test('multiple copyright headers (w/o newlines in between)', () => {
const input = `<?php
/**
* This file is part of @splish-me/copyright-headers
*
* Copyright (c) 2016 Splish UG (haftungsbeschränkt)
*/
/**
* Copyright (c) 2016 Max Mustermann Corporation
*/
echo "foo bar";
?>
`
const [status, output] = getUpdatedCopyrightHeader(input, php, {
...options,
shouldUpdate: () => true
})

expect(status).toEqual(CopyrightHeaderStatus.Changed)
expect(output).toEqual(`<?php
/**
* This file is part of @splish-me/copyright-headers
*
* Copyright (c) 2018 Splish UG (haftungsbeschränkt)
*/
/**
* Copyright (c) 2016 Max Mustermann Corporation
*/
echo "foo bar";
?>
`)
})
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splish-me/copyright-headers",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "https://github.com/splish-me/copyright-headers",
"bugs": {
"url": "https://github.com/splish-me/copyright-headers/issues"
Expand All @@ -26,12 +26,14 @@
"_prettier": "prettier \"{src/**/*,*}.{js,jsx,ts,tsx,css,scss,sass,less,json,md,markdown,yaml,yml}\""
},
"dependencies": {
"ramda": "^0.26.0",
"signale": "^1.3.0"
},
"devDependencies": {
"@types/glob": "^7.0.0",
"@types/jest": "^23.3.9",
"@types/node": "^10.0.0",
"@types/ramda": "^0.25.41",
"@types/signale": "^1.2.0",
"glob": "^7.0.0",
"jest": "^23.6.0",
Expand Down
49 changes: 30 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @link https://github.com/splish-me/copyright-headers for the canonical source repository
*/
import * as fs from 'fs'
import * as R from 'ramda'
import * as signale from 'signale'
import * as util from 'util'

Expand Down Expand Up @@ -120,33 +121,42 @@ export function getUpdatedCopyrightHeader(
options: CopyrightHeaderOptions
): [CopyrightHeaderStatus, string] {
const header = getLicenseHeader(language, options.lines)
const re = getLicenseHeaderRegExp(language)
const match = content.match(re)
const existingCopyrightHeader = getFirstCopyrightHeader(content, language)

if (!match) {
if (!existingCopyrightHeader) {
let newHeader = header
let newContent = content

if (language.before && newContent.startsWith(language.before)) {
newContent = content.substring(language.before.length)
} else {
if (language.after) {
newHeader += `${language.after}`
const contentStartsWithOpeningTag =
language.before && content.startsWith(language.before)

if (language.before) {
newHeader = language.before + newHeader

if (contentStartsWithOpeningTag) {
newContent = content.substring(language.before.length)
}
}

if (language.after && !contentStartsWithOpeningTag) {
newHeader += language.after
}

return [CopyrightHeaderStatus.Added, `${newHeader}\n${newContent}`]
}

if (match[0] === header) {
if (existingCopyrightHeader === header) {
return [CopyrightHeaderStatus.Unchanged, '']
}

if (
typeof options.shouldUpdate === 'function' &&
options.shouldUpdate(match[0])
options.shouldUpdate(existingCopyrightHeader)
) {
return [CopyrightHeaderStatus.Changed, content.replace(re, header)]
return [
CopyrightHeaderStatus.Changed,
content.replace(existingCopyrightHeader, header)
]
}

return [CopyrightHeaderStatus.External, content]
Expand All @@ -167,19 +177,20 @@ function getSourceLanguage(filePath: string) {

function getLicenseHeader(language: SourceLanguage, lines: string[]) {
return (
(language.before || '') +
`${language.begin}\n` +
lines.map(language.buildLine).join('\n') +
`\n${language.end}`
)
}

function getLicenseHeaderRegExp(language: SourceLanguage) {
const forRe = (s: string) => s.replace(/(\?|\/|\*)/g, match => `\\${match}`)
function getFirstCopyrightHeader(content: string, language: SourceLanguage) {
const after = R.tail(R.split(language.begin, content))

return new RegExp(
`${forRe(language.before || '')}${forRe(language.begin)}\n(.+\n)*${forRe(
language.end
)}`
)
if (R.isEmpty(after)) {
return ''
}

const [comment] = R.split(language.end, after.join(''))

return `${language.begin}${comment}${language.end}`
}
11 changes: 11 additions & 0 deletions wallaby.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = () => {
return {
files: ['src/**/*.ts'],
tests: ['__tests__/**/*.ts'],
env: {
type: 'node',
runner: 'node'
},
testFramework: 'jest'
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.3.tgz#3918b73ceed484e58367be5acb79d1775239e393"
integrity sha512-sfGmOtSMSbQ/AKG8V9xD1gmjquC9awIIZ/Kj309pHb2n3bcRAcGMQv5nJ6gCXZVsneGE4+ve8DXKRCsrg3TFzg==

"@types/ramda@^0.25.41":
version "0.25.41"
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.25.41.tgz#c965160129d7e8345e40508858514f8ec7af706f"
integrity sha512-R6IkySFkrCN6Xd3exKX1PI2hvkmpbDZBKYz7u4uuYas8NeUCmcHkccYo6OWKtWIkYipjGBQP2ZzlTT+R3bUY6Q==

"@types/signale@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/signale/-/signale-1.2.0.tgz#575719e586a1352cf1de04478b5fc0920e221000"
Expand Down Expand Up @@ -2960,6 +2965,11 @@ qs@~6.5.2:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
integrity sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==

ramda@^0.26.0:
version "0.26.0"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.26.0.tgz#3cbe059f5c18fbc0eb86f2396ab1f8320705424c"
integrity sha512-maK1XqpgsOo5DwjhROjqDvpm1vkphLQbpleVv+b3t5Y9uOQ0t8hTHT582+mDs7RLrex1kd4lWYizNXWLVjsq9w==

randomatic@^3.0.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.1.1.tgz#b776efc59375984e36c537b2f51a1f0aff0da1ed"
Expand Down

0 comments on commit 875dc94

Please sign in to comment.