microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
tools/gulp-extras.js
101lines · 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"); | |
| 7 | const log = require('fancy-log'); | |
4ad1a2dcsemenyakNik7 years ago | 8 | const colors = require('ansi-colors'); |
f6ac01ceRuslan Bikkinin7 years ago | 9 | const path = require("path"); |
| 10 | const PluginError = require('plugin-error'); | |
| 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; | |
| 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."; | |
9adec70dJoshua Skelton10 years ago | 31 | |
f6ac01ceRuslan Bikkinin7 years ago | 32 | return through.obj(function (file, encoding, callback) { |
a3fd5ee9Joshua Skelton10 years ago | 33 | if (file.isBuffer()) { |
f6ac01ceRuslan Bikkinin7 years ago | 34 | let 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 | }, |
f6ac01ceRuslan Bikkinin7 years ago | 47 | function (callback) { |
| 48 | if (hadErrors) { | |
| 49 | return this.emit("error", new PluginError(pluginName, "Failed copyright check")); | |
| 50 | } | |
| 51 | callback(); | |
| 52 | }); | |
| 53 | } | |
9adec70dJoshua Skelton10 years ago | 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 | */ | |
f6ac01ceRuslan Bikkinin7 years ago | 60 | function existsCaseSensitive(filePath) { |
9adec70dJoshua Skelton10 years ago | 61 | if (fs.existsSync(filePath)) { |
f6ac01ceRuslan Bikkinin7 years ago | 62 | const fileName = path.basename(filePath); |
9adec70dJoshua Skelton10 years ago | 63 | return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1; |
| 64 | } | |
| 65 | | |
| 66 | return false; | |
f6ac01ceRuslan Bikkinin7 years ago | 67 | } |
9adec70dJoshua Skelton10 years ago | 68 | |
f6ac01ceRuslan Bikkinin7 years ago | 69 | function executeCommand(command, args, callback, opts) { |
77e86943lexie0111 years ago | 70 | const proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, Object.assign({}, opts, { shell: true })); |
f6ac01ceRuslan Bikkinin7 years ago | 71 | let errorSignaled = false; |
7212311dDaniel Lebu10 years ago | 72 | |
f6ac01ceRuslan Bikkinin7 years ago | 73 | proc.stdout.on("data", (data) => { |
| 74 | log(`${data}`); | |
7212311dDaniel Lebu10 years ago | 75 | }); |
| 76 | | |
f6ac01ceRuslan Bikkinin7 years ago | 77 | proc.stderr.on("data", (data) => { |
| 78 | log.error(`${data}`); | |
7212311dDaniel Lebu10 years ago | 79 | }); |
| 80 | | |
f6ac01ceRuslan Bikkinin7 years ago | 81 | proc.on("error", (error) => { |
c5378ce2Daniel Lebu10 years ago | 82 | if (!errorSignaled) { |
f6ac01ceRuslan Bikkinin7 years ago | 83 | callback(`An error occurred. ${error}`); |
c5378ce2Daniel Lebu10 years ago | 84 | errorSignaled = true; |
| 85 | } | |
| 86 | }); | |
| 87 | | |
f6ac01ceRuslan Bikkinin7 years ago | 88 | proc.on("exit", (code) => { |
c5378ce2Daniel Lebu10 years ago | 89 | if (code === 0) { |
| 90 | callback(); | |
| 91 | } else if (!errorSignaled) { | |
f6ac01ceRuslan Bikkinin7 years ago | 92 | callback(`Error code: ${code}`); |
c5378ce2Daniel Lebu10 years ago | 93 | errorSignaled = true; |
| 94 | } | |
7212311dDaniel Lebu10 years ago | 95 | }); |
f6ac01ceRuslan Bikkinin7 years ago | 96 | } |
7212311dDaniel Lebu10 years ago | 97 | |
9adec70dJoshua Skelton10 years ago | 98 | module.exports = { |
f6ac01ceRuslan Bikkinin7 years ago | 99 | checkCopyright, |
| 100 | executeCommand | |
9adec70dJoshua Skelton10 years ago | 101 | } |