|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 4 | +/* Copyright (c) 2017 Mobify Research & Development Inc. All rights reserved. */ |
| 5 | +/* * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 6 | + |
| 7 | +const glob = require('glob') |
| 8 | +const fs = require('fs') |
| 9 | +const path = require('path') |
| 10 | + |
| 11 | +const red = '\x1b[31m' |
| 12 | +const green = '\x1b[32m' |
| 13 | +const yellow = '\x1b[33m' |
| 14 | +const magenta = '\x1b[35m' |
| 15 | +const cyan = '\x1b[36m' |
| 16 | +const blackBG = '\x1b[40m' |
| 17 | +const defaultBG = '\x1b[49m' |
| 18 | +const defaultFG = '\x1b[39m' |
| 19 | + |
| 20 | +const currentYear = new Date().getFullYear() |
| 21 | +const langs = {} |
| 22 | + |
| 23 | +let lintMode = true |
| 24 | +let updateMode = false |
| 25 | +let error = false |
| 26 | + |
| 27 | +// we don't want to pass node and copyright.js directories to the glob |
| 28 | +const args = process.argv.filter((arg) => { |
| 29 | + return !/node|copyright/.test(arg) |
| 30 | +}) |
| 31 | + |
| 32 | +/** |
| 33 | + * getHeaderText - retrieve appropriate header file context from langs{} object |
| 34 | + * @param {String} ext file extension of desired header file |
| 35 | + * @return {String} content of appropriate header file |
| 36 | + */ |
| 37 | +const getHeaderText = (ext) => { |
| 38 | + if (!langs[ext]) { |
| 39 | + console.log(`${red}${blackBG}ERROR${defaultBG} - ${ext} is not supported (yet)`) |
| 40 | + process.exit(1) |
| 41 | + } |
| 42 | + return langs[ext] |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * buildSupportedExtensions - initializes the langs{} object with the supported |
| 47 | + * extensions, as well as their respective (c) header content |
| 48 | + * @return {undefined} - populates the langs{} object |
| 49 | + */ |
| 50 | +const buildSupportedExtensions = () => { |
| 51 | + const headerDir = path.join(__dirname, './headers') |
| 52 | + fs |
| 53 | + .readdirSync(headerDir) |
| 54 | + .forEach((file) => { |
| 55 | + const extension = file.match(/\.[0-9a-z]+$/i)[0] |
| 56 | + const textPath = path.join(headerDir, file) |
| 57 | + const content = fs |
| 58 | + .readFileSync(textPath) |
| 59 | + .toString() |
| 60 | + .replace('year', currentYear) |
| 61 | + |
| 62 | + langs[extension] = content |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +if (args.length === 0 || args.indexOf('--help') >= 0) { |
| 68 | + |
| 69 | + console.log(` |
| 70 | + Usage: node copyright.js [options] 'glob' ['additional globs'] |
| 71 | +
|
| 72 | + If your glob is not targetting all nested directories, ensure that the glob string is wrapped in single quotes |
| 73 | +
|
| 74 | + Example: |
| 75 | + ${yellow}node copyright.js --fix${defaultFG} 'src/**/*.js' |
| 76 | +
|
| 77 | + Options: |
| 78 | +
|
| 79 | + --fix run in fix mode |
| 80 | + --update update the year in existing headers |
| 81 | +
|
| 82 | + Visit ${cyan}https://github.com/mobify/mobify-code-style${defaultFG} to learn more. |
| 83 | +`) |
| 84 | + |
| 85 | + process.exit(0) |
| 86 | +} |
| 87 | + |
| 88 | +// Sets fix flag if the user provides --fix command line arg |
| 89 | +if (args.indexOf('--fix') >= 0) { |
| 90 | + args.splice(args.indexOf('--fix'), 1) |
| 91 | + lintMode = false |
| 92 | +} |
| 93 | + |
| 94 | +if (args.indexOf('--update') >= 0) { |
| 95 | + args.splice(args.indexOf('--update'), 1) |
| 96 | + updateMode = true |
| 97 | +} |
| 98 | + |
| 99 | +buildSupportedExtensions() |
| 100 | + |
| 101 | +args |
| 102 | + .map((folder) => glob.sync(folder)) // build array of files matching glob pattern |
| 103 | + .reduce((a, b) => a.concat(b)) // flatten array of arrays |
| 104 | + .forEach((file) => { |
| 105 | + const content = fs.readFileSync(file) |
| 106 | + const hasCopyrightHeader = content.includes('Copyright (c)') |
| 107 | + const ext = file.match(/\.[0-9a-z]+$/i)[0] |
| 108 | + |
| 109 | + let newData = '' |
| 110 | + |
| 111 | + if (hasCopyrightHeader && updateMode) { |
| 112 | + newData = content.toString().replace(/(\(c\)\s)(\d{4})/, `$1 ${currentYear}`) |
| 113 | + fs.writeFileSync(file, newData) |
| 114 | + console.log(`${green}Copyright header succesfully updated to ${currentYear} in ${magenta}${file}`) |
| 115 | + } |
| 116 | + |
| 117 | + if (!hasCopyrightHeader) { |
| 118 | + if (lintMode) { |
| 119 | + console.log(`${yellow}${file} ${red}missing copyright header`) |
| 120 | + error = true |
| 121 | + } else { |
| 122 | + let contentStr = content.toString().split('\n') |
| 123 | + |
| 124 | + // accomodate for shebang and insert before header |
| 125 | + if (contentStr[0].indexOf('#!') >= 0) { |
| 126 | + const shebang = contentStr.shift() |
| 127 | + contentStr = contentStr.join('\n') |
| 128 | + newData = shebang + '\n\n' + getHeaderText(ext) + '\n' + contentStr // eslint-disable-line prefer-template |
| 129 | + } else { |
| 130 | + newData = getHeaderText(ext) + `\n${content}` // eslint-disable-line prefer-template |
| 131 | + } |
| 132 | + |
| 133 | + fs.writeFileSync(file, newData) |
| 134 | + console.log(`${green}Copyright header succesfully written into ${magenta}${file}`) |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | +if (error) { |
| 140 | + console.log(`${red}${blackBG}ERROR${defaultBG} - Please run the copyright headers tool in this project`) |
| 141 | + process.exit(1) |
| 142 | +} else { |
| 143 | + console.log(`${cyan}Copyright headers are present in target files`) |
| 144 | +} |
0 commit comments