microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
139lines · modeblame
9adec70dJoshua Skelton10 years ago | 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 | | |
7212311dDaniel Lebu10 years ago | 5 | var child_process = require("child_process"); |
9adec70dJoshua Skelton10 years ago | 6 | var fs = require("fs"); |
| 7 | var gutil = require("gulp-util"); | |
| 8 | var path = require("path"); | |
6200aa9fJoshua Skelton10 years ago | 9 | var PluginError = gutil.PluginError; |
9adec70dJoshua Skelton10 years ago | 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("../",""); | |
fefdba64Joshua Skelton10 years ago | 20 | gutil.log("[" + gutil.colors.cyan(pluginName) + "] " + gutil.colors.red("error") + " " + sourcePath + ": " + message); |
9adec70dJoshua Skelton10 years ago | 21 | }; |
| 22 | | |
| 23 | /** | |
| 24 | * Plugin to verify the Microsoft copyright notice is present | |
| 25 | */ | |
| 26 | var checkCopyright = function() { | |
6200aa9fJoshua Skelton10 years ago | 27 | var pluginName = "check-copyright"; |
| 28 | var hadErrors = false; | |
4abd7849Joshua Skelton10 years ago | 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."; |
9adec70dJoshua Skelton10 years ago | 30 | |
| 31 | return through.obj(function(file, encoding, callback) { | |
a3fd5ee9Joshua Skelton10 years ago | 32 | if (file.isBuffer()) { |
9adec70dJoshua Skelton10 years ago | 33 | var fileContents = file.contents.toString(encoding); |
4abd7849Joshua Skelton10 years ago | 34 | fileContents = fileContents.replace("\r\n", "\n"); |
5c8365a6Artem Egorov8 years ago | 35 | fileContents = fileContents.replace("\"use strict\";\n", ""); |
078b49a4Artem Egorov7 years ago | 36 | fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", ""); |
9adec70dJoshua Skelton10 years ago | 37 | |
4abd7849Joshua Skelton10 years ago | 38 | if (fileContents.indexOf(copyrightNotice) !== 0) { |
6200aa9fJoshua Skelton10 years ago | 39 | logError(pluginName, file, "missing copyright notice"); |
| 40 | hadErrors = true; | |
9adec70dJoshua Skelton10 years ago | 41 | } |
| 42 | } | |
| 43 | | |
| 44 | callback(null, file); | |
6200aa9fJoshua Skelton10 years ago | 45 | }, |
| 46 | function(callback) { | |
| 47 | if (hadErrors) { | |
| 48 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); | |
| 49 | } | |
| 50 | callback(); | |
9adec70dJoshua Skelton10 years ago | 51 | }); |
| 52 | }; | |
| 53 | | |
| 54 | /** | |
| 55 | * Helper function to check if a file exists case sensitive | |
| 56 | * @param {string} filePath The path to check | |
| 57 | * @returns {boolean} If the path exists case sensitive | |
| 58 | */ | |
| 59 | var existsCaseSensitive = function(filePath) { | |
| 60 | if (fs.existsSync(filePath)) { | |
| 61 | var fileName = path.basename(filePath); | |
| 62 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; | |
| 63 | } | |
| 64 | | |
| 65 | return false; | |
| 66 | }; | |
| 67 | | |
| 68 | /** | |
| 69 | * Plugin to verify if import statements use correct casing | |
| 70 | */ | |
| 71 | var checkImports = function() { | |
6200aa9fJoshua Skelton10 years ago | 72 | var pluginName = "check-imports"; |
| 73 | var hadErrors = false; | |
e8486e58Joshua Skelton10 years ago | 74 | var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/; |
9adec70dJoshua Skelton10 years ago | 75 | |
| 76 | return through.obj(function(file, encoding, callback) { | |
a3fd5ee9Joshua Skelton10 years ago | 77 | if (file.isBuffer()) { |
9adec70dJoshua Skelton10 years ago | 78 | var fileContents = file.contents.toString(encoding); |
0d7e2dc5Joshua Skelton10 years ago | 79 | var importStatements = fileContents.match(new RegExp(re.source, "g")) || []; |
9adec70dJoshua Skelton10 years ago | 80 | var workingDirectory = path.dirname(file.path); |
| 81 | | |
| 82 | importStatements.forEach(function(importStatement) { | |
547ca187max-mironov8 years ago | 83 | |
9adec70dJoshua Skelton10 years ago | 84 | var modulePath = re.exec(importStatement); |
9425034eJoshua Skelton10 years ago | 85 | if (modulePath && modulePath[1]) { |
9adec70dJoshua Skelton10 years ago | 86 | var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts"); |
| 87 | | |
| 88 | if (!existsCaseSensitive(moduleFilePath)) { | |
30d12ab8Joshua Skelton10 years ago | 89 | logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\""); |
6200aa9fJoshua Skelton10 years ago | 90 | hadErrors = true; |
9adec70dJoshua Skelton10 years ago | 91 | } |
| 92 | } | |
| 93 | }); | |
| 94 | } | |
| 95 | | |
| 96 | callback(null, file); | |
6200aa9fJoshua Skelton10 years ago | 97 | }, |
| 98 | function(callback) { | |
| 99 | if (hadErrors) { | |
| 100 | return this.emit("error", new PluginError(pluginName, "Failed import casing check")); | |
| 101 | } | |
| 102 | callback(); | |
9adec70dJoshua Skelton10 years ago | 103 | }); |
| 104 | }; | |
| 105 | | |
92f13422Jimmy Thomson9 years ago | 106 | var executeCommand = function(command, args, callback, opts) { |
| 107 | var proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts); | |
c5378ce2Daniel Lebu10 years ago | 108 | var errorSignaled = false; |
7212311dDaniel Lebu10 years ago | 109 | |
92f13422Jimmy Thomson9 years ago | 110 | proc.stdout.on("data", function(data) { |
7212311dDaniel Lebu10 years ago | 111 | console.log("" + data); |
| 112 | }); | |
| 113 | | |
92f13422Jimmy Thomson9 years ago | 114 | proc.stderr.on("data", function(data) { |
7212311dDaniel Lebu10 years ago | 115 | console.error("" + data); |
| 116 | }); | |
| 117 | | |
92f13422Jimmy Thomson9 years ago | 118 | proc.on("error", function(error) { |
c5378ce2Daniel Lebu10 years ago | 119 | if (!errorSignaled) { |
8e5ee0d7Chris Bala10 years ago | 120 | callback("An error occurred. " + error); |
c5378ce2Daniel Lebu10 years ago | 121 | errorSignaled = true; |
| 122 | } | |
| 123 | }); | |
| 124 | | |
92f13422Jimmy Thomson9 years ago | 125 | proc.on("exit", function(code) { |
c5378ce2Daniel Lebu10 years ago | 126 | if (code === 0) { |
| 127 | callback(); | |
| 128 | } else if (!errorSignaled) { | |
| 129 | callback("Error code: " + code); | |
| 130 | errorSignaled = true; | |
| 131 | } | |
7212311dDaniel Lebu10 years ago | 132 | }); |
| 133 | }; | |
| 134 | | |
9adec70dJoshua Skelton10 years ago | 135 | module.exports = { |
| 136 | checkCopyright: checkCopyright, | |
7212311dDaniel Lebu10 years ago | 137 | checkImports: checkImports, |
| 138 | executeCommand: executeCommand | |
9adec70dJoshua Skelton10 years ago | 139 | } |