preamble.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. const cStyleComment = `
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. `;
  39. const hashComment = `
  40. # Licensed to the Apache Software Foundation (ASF) under one
  41. # or more contributor license agreements. See the NOTICE file
  42. # distributed with this work for additional information
  43. # regarding copyright ownership. The ASF licenses this file
  44. # to you under the Apache License, Version 2.0 (the
  45. # "License"); you may not use this file except in compliance
  46. # with the License. You may obtain a copy of the License at
  47. #
  48. # http://www.apache.org/licenses/LICENSE-2.0
  49. #
  50. # Unless required by applicable law or agreed to in writing,
  51. # software distributed under the License is distributed on an
  52. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  53. # KIND, either express or implied. See the License for the
  54. # specific language governing permissions and limitations
  55. # under the License.
  56. `;
  57. const mlComment = `
  58. <!--
  59. Licensed to the Apache Software Foundation (ASF) under one
  60. or more contributor license agreements. See the NOTICE file
  61. distributed with this work for additional information
  62. regarding copyright ownership. The ASF licenses this file
  63. to you under the Apache License, Version 2.0 (the
  64. "License"); you may not use this file except in compliance
  65. with the License. You may obtain a copy of the License at
  66. http://www.apache.org/licenses/LICENSE-2.0
  67. Unless required by applicable law or agreed to in writing,
  68. software distributed under the License is distributed on an
  69. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  70. KIND, either express or implied. See the License for the
  71. specific language governing permissions and limitations
  72. under the License.
  73. -->
  74. `;
  75. function hasPreamble(fileExt) {
  76. return fileExt && preambleMap[fileExt];
  77. }
  78. function addPreamble(fileStr, fileExt) {
  79. if (fileStr && fileExt) {
  80. const addFn = addFns[fileExt];
  81. const headStr = preambleMap[fileExt];
  82. return addFn && headStr && addFn(headStr, fileStr);
  83. }
  84. }
  85. const addFns = {
  86. ts: function (headStr, fileStr) {
  87. return headStr + fileStr;
  88. },
  89. js: function (headStr, fileStr) {
  90. return headStr + fileStr;
  91. },
  92. css: function (headStr, fileStr) {
  93. return headStr + fileStr;
  94. },
  95. java: function (headStr, fileStr) {
  96. return headStr + fileStr;
  97. },
  98. yml: addShellLikeHeader,
  99. yaml: addShellLikeHeader,
  100. sh: addShellLikeHeader,
  101. html: function (headStr, fileStr) {
  102. // Git diff enables manual check.
  103. let resultStr = fileStr.replace(/^\s*<!DOCTYPE\s[^<>]+>/i, '$&' + headStr);
  104. // If no doctype
  105. if (resultStr.length === fileStr.length) {
  106. resultStr = headStr + fileStr;
  107. }
  108. return resultStr;
  109. },
  110. xml: xmlAddFn,
  111. xsl: xmlAddFn
  112. };
  113. function addShellLikeHeader(headStr, fileStr) {
  114. // Git diff enables manual check.
  115. if (/^#\!/.test(fileStr)) {
  116. const lines = fileStr.split('\n');
  117. lines.splice(1, 0, headStr);
  118. return lines.join('\n');
  119. }
  120. else {
  121. return headStr + fileStr;
  122. }
  123. }
  124. function xmlAddFn(headStr, fileStr) {
  125. // Git diff enables manual check.
  126. let resultStr = fileStr.replace(/^\s*<\?xml\s[^<>]+\?>/i, '$&' + headStr);
  127. // If no <?xml version='1.0' ?>
  128. if (resultStr.length === fileStr.length) {
  129. resultStr = headStr + fileStr;
  130. }
  131. return resultStr;
  132. }
  133. const preambleMap = {
  134. ts: cStyleComment,
  135. js: cStyleComment,
  136. css: cStyleComment,
  137. java: cStyleComment,
  138. yml: hashComment,
  139. yaml: hashComment,
  140. sh: hashComment,
  141. html: mlComment,
  142. xml: mlComment,
  143. xsl: mlComment
  144. };
  145. const licenseReg = [
  146. {name: 'Apache', reg: /apache (license|commons)/i},
  147. {name: 'BSD', reg: /BSD/},
  148. {name: 'LGPL', reg: /LGPL/},
  149. {name: 'GPL', reg: /GPL/},
  150. {name: 'Mozilla', reg: /mozilla public/i},
  151. {name: 'MIT', reg: /mit license/i},
  152. {name: 'BSD-d3', reg: /Copyright\s+\(c\)\s+2010-2015,\s+Michael\s+Bostock/i}
  153. ];
  154. function extractLicense(fileStr, fileExt) {
  155. let commentText = extractComment(fileStr.trim(), fileExt);
  156. if (!commentText) {
  157. return;
  158. }
  159. for (let i = 0; i < licenseReg.length; i++) {
  160. if (licenseReg[i].reg.test(commentText)) {
  161. return licenseReg[i].name;
  162. }
  163. }
  164. }
  165. const cStyleCommentReg = /\/\*[\S\s]*?\*\//;
  166. const hashCommentReg = /^\s*#.*$/gm;
  167. const mlCommentReg = /<\!\-\-[\S\s]*?\-\->/;
  168. const commentReg = {
  169. ts: cStyleCommentReg,
  170. js: cStyleCommentReg,
  171. css: cStyleCommentReg,
  172. java: cStyleCommentReg,
  173. yml: hashCommentReg,
  174. yaml: hashCommentReg,
  175. sh: hashCommentReg,
  176. html: mlCommentReg,
  177. xml: mlCommentReg,
  178. xsl: mlCommentReg
  179. };
  180. function extractComment(str, fileExt) {
  181. const reg = commentReg[fileExt];
  182. if (!fileExt || !reg || !str) {
  183. return;
  184. }
  185. reg.lastIndex = 0;
  186. if (fileExt === 'sh' || fileExt === 'yml' || fileExt === 'yaml') {
  187. let result = str.match(reg);
  188. return result && result.join('\n');
  189. }
  190. else {
  191. let result = reg.exec(str);
  192. return result && result[0];
  193. }
  194. }
  195. module.exports = Object.assign({
  196. extractLicense,
  197. hasPreamble,
  198. addPreamble
  199. }, preambleMap);