microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
133lines · 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 | | |
f6ac01ceRuslan Bikkinin7 years ago | 5 | const child_process = require("child_process"); |
| 6 | const fs = require("fs"); | |
3016b3b0Ezio Li9 months ago | 7 | const log = require("fancy-log"); |
| 8 | const colors = require("ansi-colors"); | |
f6ac01ceRuslan Bikkinin7 years ago | 9 | const path = require("path"); |
3016b3b0Ezio Li9 months ago | 10 | const PluginError = require("plugin-error"); |
f6ac01ceRuslan Bikkinin7 years ago | 11 | const through = require("through2"); |
9adec70dJoshua Skelton10 years ago | 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 | */ | |
f6ac01ceRuslan Bikkinin7 years ago | 19 | function logError(pluginName, file, message) { |
825322e3Ezio Li1 years ago | 20 | const sourcePath = path.relative(__dirname, file.path).replace(/..\//g, "''"); |
f6ac01ceRuslan Bikkinin7 years ago | 21 | log(`[${colors.cyan(pluginName)}] ${colors.red("error")} ${sourcePath}: ${message}`); |
| 22 | } | |
9adec70dJoshua Skelton10 years ago | 23 | |
| 24 | /** | |
| 25 | * Plugin to verify the Microsoft copyright notice is present | |
| 26 | */ | |
f6ac01ceRuslan Bikkinin7 years ago | 27 | function checkCopyright() { |
| 28 | const pluginName = "check-copyright"; | |
| 29 | let hadErrors = false; | |
3016b3b0Ezio Li9 months ago | 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 | } | |
9adec70dJoshua Skelton10 years ago | 48 | } |
| 49 | | |
3016b3b0Ezio Li9 months ago | 50 | callback(null, file); |
| 51 | }, | |
f6ac01ceRuslan Bikkinin7 years ago | 52 | function (callback) { |
| 53 | if (hadErrors) { | |
| 54 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); | |
| 55 | } | |
| 56 | callback(); | |
3016b3b0Ezio Li9 months ago | 57 | }, |
| 58 | ); | |
f6ac01ceRuslan Bikkinin7 years ago | 59 | } |
9adec70dJoshua Skelton10 years ago | 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 | */ | |
f6ac01ceRuslan Bikkinin7 years ago | 66 | function existsCaseSensitive(filePath) { |
9adec70dJoshua Skelton10 years ago | 67 | if (fs.existsSync(filePath)) { |
f6ac01ceRuslan Bikkinin7 years ago | 68 | const fileName = path.basename(filePath); |
9adec70dJoshua Skelton10 years ago | 69 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 70 | } | |
| 71 | | |
| 72 | return false; | |
f6ac01ceRuslan Bikkinin7 years ago | 73 | } |
9adec70dJoshua Skelton10 years ago | 74 | |
f6ac01ceRuslan Bikkinin7 years ago | 75 | function executeCommand(command, args, callback, opts) { |
3016b3b0Ezio Li9 months ago | 76 | const proc = child_process.spawn( |
| 77 | command + (process.platform === "win32" ? ".cmd" : ""), | |
| 78 | args, | |
| 79 | Object.assign({}, opts, { shell: true }), | |
| 80 | ); | |
f6ac01ceRuslan Bikkinin7 years ago | 81 | let errorSignaled = false; |
7212311dDaniel Lebu10 years ago | 82 | |
3016b3b0Ezio Li9 months ago | 83 | proc.stdout.on("data", data => { |
f6ac01ceRuslan Bikkinin7 years ago | 84 | log(`${data}`); |
7212311dDaniel Lebu10 years ago | 85 | }); |
| 86 | | |
3016b3b0Ezio Li9 months ago | 87 | proc.stderr.on("data", data => { |
f6ac01ceRuslan Bikkinin7 years ago | 88 | log.error(`${data}`); |
7212311dDaniel Lebu10 years ago | 89 | }); |
| 90 | | |
3016b3b0Ezio Li9 months ago | 91 | proc.on("error", error => { |
c5378ce2Daniel Lebu10 years ago | 92 | if (!errorSignaled) { |
f6ac01ceRuslan Bikkinin7 years ago | 93 | callback(`An error occurred. ${error}`); |
c5378ce2Daniel Lebu10 years ago | 94 | errorSignaled = true; |
| 95 | } | |
| 96 | }); | |
| 97 | | |
3016b3b0Ezio Li9 months ago | 98 | proc.on("exit", code => { |
c5378ce2Daniel Lebu10 years ago | 99 | if (code === 0) { |
| 100 | callback(); | |
| 101 | } else if (!errorSignaled) { | |
f6ac01ceRuslan Bikkinin7 years ago | 102 | callback(`Error code: ${code}`); |
c5378ce2Daniel Lebu10 years ago | 103 | errorSignaled = true; |
| 104 | } | |
7212311dDaniel Lebu10 years ago | 105 | }); |
f6ac01ceRuslan Bikkinin7 years ago | 106 | } |
7212311dDaniel Lebu10 years ago | 107 | |
3016b3b0Ezio Li9 months ago | 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 | | |
9adec70dJoshua Skelton10 years ago | 129 | module.exports = { |
f6ac01ceRuslan Bikkinin7 years ago | 130 | checkCopyright, |
3016b3b0Ezio Li9 months ago | 131 | executeCommand, |
| 132 | withTimeout, | |
| 133 | }; |