microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/checkCopyright.ts
78lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | var FindFiles = require("node-find-files"); |
| 5 | var fs = require("fs"); |
| 6 | import Q = require("q"); |
| 7 | import path_module = require("path"); |
| 8 | |
| 9 | class CopyrightVerifier { |
| 10 | private foundMissing = false; |
| 11 | |
| 12 | private static UTB_BYTE_ORDER_MARKER = "\ufeff"; |
| 13 | private static SOURCE_CODE_FILE_PATTERN = /.*\.(ts|js)$/; |
| 14 | private static EXCLUDED_FILE_PATTERN = /.*\.d\.ts/; |
| 15 | private static COPYRIGHT_NOTICE = "// Copyright (c) Microsoft Corporation. All rights reserved.\n" + |
| 16 | "// Licensed under the MIT license. See LICENSE file in the project root for details.\n"; |
| 17 | |
| 18 | private findIn(path: string): Q.Promise<string[]> { |
| 19 | const defer = Q.defer<string[]>(); |
| 20 | const foundFiles: string[] = []; |
| 21 | const finder = new FindFiles({ |
| 22 | rootFolder: path, |
| 23 | filterFunction: (path: string, stat: any) => { |
| 24 | // match only filename |
| 25 | return path.match(CopyrightVerifier.SOURCE_CODE_FILE_PATTERN) && !path.match(CopyrightVerifier.EXCLUDED_FILE_PATTERN); |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | finder.on("match", (filePath: string, fileStats: any) => { |
| 30 | let contents = fs.readFileSync(filePath).toString().replace(/\r\n/g, "\n"); |
| 31 | if (contents.startsWith(CopyrightVerifier.UTB_BYTE_ORDER_MARKER)) { |
| 32 | contents = contents.substr(1); |
| 33 | } |
| 34 | if (!contents.startsWith(CopyrightVerifier.COPYRIGHT_NOTICE)) { |
| 35 | foundFiles.push(filePath); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | finder.on("complete", function() { |
| 40 | return defer.resolve(foundFiles); |
| 41 | }); |
| 42 | |
| 43 | finder.on("patherror", (err: any, strPath: string) => { |
| 44 | // defer.reject("Error for Path " + strPath + " " + err); |
| 45 | }) |
| 46 | |
| 47 | finder.on("error", (err: any) => { |
| 48 | defer.reject("Global Error " + err); |
| 49 | }) |
| 50 | |
| 51 | finder.startSearch(); |
| 52 | |
| 53 | return defer.promise; |
| 54 | } |
| 55 | |
| 56 | private findInAll(paths: string[]): Q.Promise<string[]> { |
| 57 | return Q.all(paths.map(path => this.findIn(path_module.resolve(path)))).then(invalids => { |
| 58 | return [].concat.apply([], invalids); |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | public verify(...paths: string[]): void { |
| 63 | return this.findInAll(paths).done(filePaths => { |
| 64 | if (filePaths.length !== 0) { |
| 65 | process.stderr.write("Found files which don't match the expected copyright notice:\n"); |
| 66 | filePaths.forEach(filePath => process.stderr.write(`\t${filePath}\n`)); |
| 67 | process.exit(1); |
| 68 | } else { |
| 69 | process.exit(0); |
| 70 | } |
| 71 | }, (reason: any) => { |
| 72 | process.stderr.write(`Uknown error while trying to check files copyright: ${reason}`); |
| 73 | process.exit(1); |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | new CopyrightVerifier().verify("../src", "../tools"); |