microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
133lines · 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(/..\//g, "''"); |
| 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 = |
| 31 | "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details."; |
| 32 | |
| 33 | return through.obj( |
| 34 | function (file, encoding, callback) { |
| 35 | if (file.isBuffer()) { |
| 36 | let fileContents = file.contents.toString(encoding); |
| 37 | fileContents = fileContents.replace("\r\n", "\n"); |
| 38 | fileContents = fileContents.replace('"use strict";\n', ""); |
| 39 | fileContents = fileContents.replace( |
| 40 | 'Object.defineProperty(exports, "__esModule", { value: true });\n', |
| 41 | "", |
| 42 | ); |
| 43 | |
| 44 | if (fileContents.indexOf(copyrightNotice) !== 0) { |
| 45 | logError(pluginName, file, "missing copyright notice"); |
| 46 | hadErrors = true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | callback(null, file); |
| 51 | }, |
| 52 | function (callback) { |
| 53 | if (hadErrors) { |
| 54 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); |
| 55 | } |
| 56 | callback(); |
| 57 | }, |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Helper function to check if a file exists case sensitive |
| 63 | * @param {string} filePath The path to check |
| 64 | * @returns {boolean} If the path exists case sensitive |
| 65 | */ |
| 66 | function existsCaseSensitive(filePath) { |
| 67 | if (fs.existsSync(filePath)) { |
| 68 | const fileName = path.basename(filePath); |
| 69 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | function executeCommand(command, args, callback, opts) { |
| 76 | const proc = child_process.spawn( |
| 77 | command + (process.platform === "win32" ? ".cmd" : ""), |
| 78 | args, |
| 79 | Object.assign({}, opts, { shell: true }), |
| 80 | ); |
| 81 | let errorSignaled = false; |
| 82 | |
| 83 | proc.stdout.on("data", data => { |
| 84 | log(`${data}`); |
| 85 | }); |
| 86 | |
| 87 | proc.stderr.on("data", data => { |
| 88 | log.error(`${data}`); |
| 89 | }); |
| 90 | |
| 91 | proc.on("error", error => { |
| 92 | if (!errorSignaled) { |
| 93 | callback(`An error occurred. ${error}`); |
| 94 | errorSignaled = true; |
| 95 | } |
| 96 | }); |
| 97 | |
| 98 | proc.on("exit", code => { |
| 99 | if (code === 0) { |
| 100 | callback(); |
| 101 | } else if (!errorSignaled) { |
| 102 | callback(`Error code: ${code}`); |
| 103 | errorSignaled = true; |
| 104 | } |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | async function withTimeout(promise, ms, { onTimeout, fallbackValue } = {}) { |
| 109 | let timer; |
| 110 | const guarded = promise |
| 111 | .then(value => ({ kind: "ok", value })) |
| 112 | .catch(error => ({ kind: "error", error })); |
| 113 | |
| 114 | const timed = new Promise(resolve => { |
| 115 | timer = setTimeout(() => { |
| 116 | if (onTimeout) onTimeout(); |
| 117 | resolve({ kind: "timeout", value: fallbackValue }); |
| 118 | }, ms); |
| 119 | }); |
| 120 | |
| 121 | const result = await Promise.race([guarded, timed]); |
| 122 | clearTimeout(timer); |
| 123 | |
| 124 | if (result.kind === "error") throw result.error; |
| 125 | if (result.kind === "timeout") return result.value; |
| 126 | return result.value; |
| 127 | } |
| 128 | |
| 129 | module.exports = { |
| 130 | checkCopyright, |
| 131 | executeCommand, |
| 132 | withTimeout, |
| 133 | }; |
| 134 | |