microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
101lines · 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 | "use strict"; |
| 4 | |
| 5 | var fs = require("fs"); |
| 6 | var gutil = require("gulp-util"); |
| 7 | var path = require("path"); |
| 8 | var through = require("through2"); |
| 9 | |
| 10 | /** |
| 11 | * Pretty logger using gutil.log |
| 12 | * @param {string} pluginName Name of the pluginName |
| 13 | * @param {Object} file A gulp file to report on |
| 14 | * @param {string} message The error message to display |
| 15 | */ |
| 16 | var logError = function(pluginName, file, message) { |
| 17 | var sourcePath = path.relative(__dirname, file.path).replace("../",""); |
| 18 | gutil.log(`[${gutil.colors.cyan(pluginName)}] ${gutil.colors.red("error")} ${sourcePath}: ${message}`); |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * Helper function to return a list of matches for a given pattern |
| 23 | * @param {string|RegExp} pattern The pattern to match against |
| 24 | * @param {string} text The text to search |
| 25 | * @returns {string[]} An array of matches |
| 26 | */ |
| 27 | var find = function(pattern, text) { |
| 28 | var results = []; |
| 29 | text.toString().replace(pattern, function(match) { |
| 30 | results.push(match); |
| 31 | }); |
| 32 | |
| 33 | return results; |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * Plugin to verify the Microsoft copyright notice is present |
| 38 | */ |
| 39 | var checkCopyright = function() { |
| 40 | var re = /\/\/ Copyright \(c\) Microsoft Corporation. All rights reserved.\s+\/\/ Licensed under the MIT license. See LICENSE file in the project root for details.\s+/ |
| 41 | |
| 42 | return through.obj(function(file, encoding, callback) { |
| 43 | if (file.isBuffer() && !file.path.endsWith(".d.ts")) { |
| 44 | var fileContents = file.contents.toString(encoding); |
| 45 | var matches = re.exec(fileContents); |
| 46 | |
| 47 | if (!matches) { |
| 48 | logError("check-copyright", file, "missing copyright notice"); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | callback(null, file); |
| 53 | }); |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * Helper function to check if a file exists case sensitive |
| 58 | * @param {string} filePath The path to check |
| 59 | * @returns {boolean} If the path exists case sensitive |
| 60 | */ |
| 61 | var existsCaseSensitive = function(filePath) { |
| 62 | if (fs.existsSync(filePath)) { |
| 63 | var fileName = path.basename(filePath); |
| 64 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * Plugin to verify if import statements use correct casing |
| 72 | */ |
| 73 | var checkImports = function() { |
| 74 | var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/g; |
| 75 | |
| 76 | return through.obj(function(file, encoding, callback) { |
| 77 | if (file.isBuffer() && !file.path.endsWith(".d.ts")) { |
| 78 | var fileContents = file.contents.toString(encoding); |
| 79 | var importStatements = find(re, fileContents); |
| 80 | var workingDirectory = path.dirname(file.path); |
| 81 | |
| 82 | importStatements.forEach(function(importStatement) { |
| 83 | var modulePath = re.exec(importStatement); |
| 84 | if (modulePath && modulePath[1]) { |
| 85 | var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts"); |
| 86 | |
| 87 | if (!existsCaseSensitive(moduleFilePath)) { |
| 88 | logError("check-imports", file, `unresolved import: ${modulePath[1]}`); |
| 89 | } |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | callback(null, file); |
| 95 | }); |
| 96 | }; |
| 97 | |
| 98 | module.exports = { |
| 99 | checkCopyright: checkCopyright, |
| 100 | checkImports: checkImports |
| 101 | } |