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 | const child_process = require("child_process"); |
| 6 | const fs = require("fs"); |
| 7 | const log = require('fancy-log'); |
| 8 | const colors = require('ansi-colors'); |
| 9 | const path = require("path"); |
| 10 | const PluginError = require('plugin-error'); |
| 11 | const through = require("through2"); |
| 12 | |
| 13 | /** |
| 14 | * Pretty logger using 'log' |
| 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 | function logError(pluginName, file, message) { |
| 20 | const sourcePath = path.relative(__dirname, file.path).replace("../", ""); |
| 21 | log(`[${colors.cyan(pluginName)}] ${colors.red("error")} ${sourcePath}: ${message}`); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Plugin to verify the Microsoft copyright notice is present |
| 26 | */ |
| 27 | function checkCopyright() { |
| 28 | const pluginName = "check-copyright"; |
| 29 | let hadErrors = false; |
| 30 | const copyrightNotice = "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details."; |
| 31 | |
| 32 | return through.obj(function (file, encoding, callback) { |
| 33 | if (file.isBuffer()) { |
| 34 | let fileContents = file.contents.toString(encoding); |
| 35 | fileContents = fileContents.replace("\r\n", "\n"); |
| 36 | fileContents = fileContents.replace("\"use strict\";\n", ""); |
| 37 | fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", ""); |
| 38 | |
| 39 | if (fileContents.indexOf(copyrightNotice) !== 0) { |
| 40 | logError(pluginName, file, "missing copyright notice"); |
| 41 | hadErrors = true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | callback(null, file); |
| 46 | }, |
| 47 | function (callback) { |
| 48 | if (hadErrors) { |
| 49 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); |
| 50 | } |
| 51 | callback(); |
| 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 | function existsCaseSensitive(filePath) { |
| 61 | if (fs.existsSync(filePath)) { |
| 62 | const fileName = path.basename(filePath); |
| 63 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | function executeCommand(command, args, callback, opts) { |
| 70 | const proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts); |
| 71 | let errorSignaled = false; |
| 72 | |
| 73 | proc.stdout.on("data", (data) => { |
| 74 | log(`${data}`); |
| 75 | }); |
| 76 | |
| 77 | proc.stderr.on("data", (data) => { |
| 78 | log.error(`${data}`); |
| 79 | }); |
| 80 | |
| 81 | proc.on("error", (error) => { |
| 82 | if (!errorSignaled) { |
| 83 | callback(`An error occurred. ${error}`); |
| 84 | errorSignaled = true; |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | proc.on("exit", (code) => { |
| 89 | if (code === 0) { |
| 90 | callback(); |
| 91 | } else if (!errorSignaled) { |
| 92 | callback(`Error code: ${code}`); |
| 93 | errorSignaled = true; |
| 94 | } |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | module.exports = { |
| 99 | checkCopyright, |
| 100 | executeCommand |
| 101 | } |