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