|
| 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 | +} |
0 commit comments