preamble.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. js: function (headStr, fileStr) {
  87. return headStr + fileStr;
  88. },
  89. css: function (headStr, fileStr) {
  90. return headStr + fileStr;
  91. },
  92. java: function (headStr, fileStr) {
  93. return headStr + fileStr;
  94. },
  95. sh: function (headStr, fileStr) {
  96. // Git diff enables manual check.
  97. if (/^#\!/.test(fileStr)) {
  98. const lines = fileStr.split('\n');
  99. lines.splice(1, 0, headStr);
  100. return lines.join('\n');
  101. }
  102. else {
  103. return headStr + fileStr;
  104. }
  105. },
  106. html: function (headStr, fileStr) {
  107. // Git diff enables manual check.
  108. let resultStr = fileStr.replace(/^\s*<!DOCTYPE\s[^<>]+>/i, '$&' + headStr);
  109. // If no doctype
  110. if (resultStr.length === fileStr.length) {
  111. resultStr = headStr + fileStr;
  112. }
  113. return resultStr;
  114. },
  115. xml: xmlAddFn,
  116. xsl: xmlAddFn
  117. };
  118. function xmlAddFn(headStr, fileStr) {
  119. // Git diff enables manual check.
  120. let resultStr = fileStr.replace(/^\s*<\?xml\s[^<>]+\?>/i, '$&' + headStr);
  121. // If no <?xml version='1.0' ?>
  122. if (resultStr.length === fileStr.length) {
  123. resultStr = headStr + fileStr;
  124. }
  125. return resultStr;
  126. }
  127. const preambleMap = {
  128. js: cStyleComment,
  129. css: cStyleComment,
  130. java: cStyleComment,
  131. sh: hashComment,
  132. html: mlComment,
  133. xml: mlComment,
  134. xsl: mlComment
  135. };
  136. const licenseReg = [
  137. {name: 'Apache', reg: /apache (license|commons)/i},
  138. {name: 'BSD', reg: /BSD/},
  139. {name: 'LGPL', reg: /LGPL/},
  140. {name: 'GPL', reg: /GPL/},
  141. {name: 'Mozilla', reg: /mozilla public/i},
  142. {name: 'MIT', reg: /mit license/i},
  143. {name: 'BSD-d3', reg: /Copyright\s+\(c\)\s+2010-2015,\s+Michael\s+Bostock/i}
  144. ];
  145. function extractLicense(fileStr, fileExt) {
  146. let commentText = extractComment(fileStr.trim(), fileExt);
  147. if (!commentText) {
  148. return;
  149. }
  150. for (let i = 0; i < licenseReg.length; i++) {
  151. if (licenseReg[i].reg.test(commentText)) {
  152. return licenseReg[i].name;
  153. }
  154. }
  155. }
  156. const cStyleCommentReg = /\/\*[\S\s]*?\*\//;
  157. const hashCommentReg = /^\s*#.*$/gm;
  158. const mlCommentReg = /<\!\-\-[\S\s]*?\-\->/;
  159. const commentReg = {
  160. js: cStyleCommentReg,
  161. css: cStyleCommentReg,
  162. java: cStyleCommentReg,
  163. sh: hashCommentReg,
  164. html: mlCommentReg,
  165. xml: mlCommentReg,
  166. xsl: mlCommentReg
  167. };
  168. function extractComment(str, fileExt) {
  169. const reg = commentReg[fileExt];
  170. if (!fileExt || !reg || !str) {
  171. return;
  172. }
  173. reg.lastIndex = 0;
  174. if (fileExt === 'sh') {
  175. let result = str.match(reg);
  176. return result && result.join('\n');
  177. }
  178. else {
  179. let result = reg.exec(str);
  180. return result && result[0];
  181. }
  182. }
  183. module.exports = Object.assign({
  184. extractLicense,
  185. hasPreamble,
  186. addPreamble
  187. }, preambleMap);