microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
136lines · 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 child_process = require("child_process"); |
| 6 | var fs = require("fs"); |
| 7 | var gutil = require("gulp-util"); |
| 8 | var path = require("path"); |
| 9 | var PluginError = gutil.PluginError; |
| 10 | var through = require("through2"); |
| 11 | |
| 12 | /** |
| 13 | * Pretty logger using gutil.log |
| 14 | * @param {string} pluginName Name of the pluginName |
| 15 | * @param {Object} file A gulp file to report on |
| 16 | * @param {string} message The error message to display |
| 17 | */ |
| 18 | var logError = function(pluginName, file, message) { |
| 19 | var sourcePath = path.relative(__dirname, file.path).replace("../",""); |
| 20 | gutil.log("[" + gutil.colors.cyan(pluginName) + "] " + gutil.colors.red("error") + " " + sourcePath + ": " + message); |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Plugin to verify the Microsoft copyright notice is present |
| 25 | */ |
| 26 | var checkCopyright = function() { |
| 27 | var pluginName = "check-copyright"; |
| 28 | var hadErrors = false; |
| 29 | var copyrightNotice = "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details."; |
| 30 | |
| 31 | return through.obj(function(file, encoding, callback) { |
| 32 | if (file.isBuffer()) { |
| 33 | var fileContents = file.contents.toString(encoding); |
| 34 | fileContents = fileContents.replace("\r\n", "\n"); |
| 35 | |
| 36 | if (fileContents.indexOf(copyrightNotice) !== 0) { |
| 37 | logError(pluginName, file, "missing copyright notice"); |
| 38 | hadErrors = true; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | callback(null, file); |
| 43 | }, |
| 44 | function(callback) { |
| 45 | if (hadErrors) { |
| 46 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); |
| 47 | } |
| 48 | callback(); |
| 49 | }); |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Helper function to check if a file exists case sensitive |
| 54 | * @param {string} filePath The path to check |
| 55 | * @returns {boolean} If the path exists case sensitive |
| 56 | */ |
| 57 | var existsCaseSensitive = function(filePath) { |
| 58 | if (fs.existsSync(filePath)) { |
| 59 | var fileName = path.basename(filePath); |
| 60 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 61 | } |
| 62 | |
| 63 | return false; |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Plugin to verify if import statements use correct casing |
| 68 | */ |
| 69 | var checkImports = function() { |
| 70 | var pluginName = "check-imports"; |
| 71 | var hadErrors = false; |
| 72 | var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/; |
| 73 | |
| 74 | return through.obj(function(file, encoding, callback) { |
| 75 | if (file.isBuffer()) { |
| 76 | var fileContents = file.contents.toString(encoding); |
| 77 | var importStatements = fileContents.match(new RegExp(re.source, "g")) || []; |
| 78 | var workingDirectory = path.dirname(file.path); |
| 79 | |
| 80 | importStatements.forEach(function(importStatement) { |
| 81 | var modulePath = re.exec(importStatement); |
| 82 | if (modulePath && modulePath[1]) { |
| 83 | var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts"); |
| 84 | |
| 85 | if (!existsCaseSensitive(moduleFilePath)) { |
| 86 | logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\""); |
| 87 | hadErrors = true; |
| 88 | } |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | callback(null, file); |
| 94 | }, |
| 95 | function(callback) { |
| 96 | if (hadErrors) { |
| 97 | return this.emit("error", new PluginError(pluginName, "Failed import casing check")); |
| 98 | } |
| 99 | callback(); |
| 100 | }); |
| 101 | }; |
| 102 | |
| 103 | var executeCommand = function(command, args, callback) { |
| 104 | var process = child_process.spawn(command, args); |
| 105 | var errorSignaled = false; |
| 106 | |
| 107 | process.stdout.on("data", function(data) { |
| 108 | console.log("" + data); |
| 109 | }); |
| 110 | |
| 111 | process.stderr.on("data", function(data) { |
| 112 | console.error("" + data); |
| 113 | }); |
| 114 | |
| 115 | process.on("error", function(error) { |
| 116 | if (!errorSignaled) { |
| 117 | callback("An error ocurred. " + error); |
| 118 | errorSignaled = true; |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | process.on("exit", function(code) { |
| 123 | if (code === 0) { |
| 124 | callback(); |
| 125 | } else if (!errorSignaled) { |
| 126 | callback("Error code: " + code); |
| 127 | errorSignaled = true; |
| 128 | } |
| 129 | }); |
| 130 | }; |
| 131 | |
| 132 | module.exports = { |
| 133 | checkCopyright: checkCopyright, |
| 134 | checkImports: checkImports, |
| 135 | executeCommand: executeCommand |
| 136 | } |