microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
sourcemap-handle-null-sections

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

133lines · modeblame

9adec70dJoshua Skelton10 years ago1// 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 ago5const child_process = require("child_process");
6const fs = require("fs");
3016b3b0Ezio Li9 months ago7const log = require("fancy-log");
8const colors = require("ansi-colors");
f6ac01ceRuslan Bikkinin7 years ago9const path = require("path");
3016b3b0Ezio Li9 months ago10const PluginError = require("plugin-error");
f6ac01ceRuslan Bikkinin7 years ago11const through = require("through2");
9adec70dJoshua Skelton10 years ago12
13/**
4ad1a2dcsemenyakNik7 years ago14* Pretty logger using 'log'
9adec70dJoshua Skelton10 years ago15* @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 ago19function logError(pluginName, file, message) {
825322e3Ezio Li1 years ago20const sourcePath = path.relative(__dirname, file.path).replace(/..\//g, "''");
f6ac01ceRuslan Bikkinin7 years ago21log(`[${colors.cyan(pluginName)}] ${colors.red("error")} ${sourcePath}: ${message}`);
22}
9adec70dJoshua Skelton10 years ago23
24/**
25* Plugin to verify the Microsoft copyright notice is present
26*/
f6ac01ceRuslan Bikkinin7 years ago27function checkCopyright() {
28const pluginName = "check-copyright";
29let hadErrors = false;
3016b3b0Ezio Li9 months ago30const 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
33return through.obj(
34function (file, encoding, callback) {
35if (file.isBuffer()) {
36let fileContents = file.contents.toString(encoding);
37fileContents = fileContents.replace("\r\n", "\n");
38fileContents = fileContents.replace('"use strict";\n', "");
39fileContents = fileContents.replace(
40'Object.defineProperty(exports, "__esModule", { value: true });\n',
41"",
42);
43
44if (fileContents.indexOf(copyrightNotice) !== 0) {
45logError(pluginName, file, "missing copyright notice");
46hadErrors = true;
47}
9adec70dJoshua Skelton10 years ago48}
49
3016b3b0Ezio Li9 months ago50callback(null, file);
51},
f6ac01ceRuslan Bikkinin7 years ago52function (callback) {
53if (hadErrors) {
54return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
55}
56callback();
3016b3b0Ezio Li9 months ago57},
58);
f6ac01ceRuslan Bikkinin7 years ago59}
9adec70dJoshua Skelton10 years ago60
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 ago66function existsCaseSensitive(filePath) {
9adec70dJoshua Skelton10 years ago67if (fs.existsSync(filePath)) {
f6ac01ceRuslan Bikkinin7 years ago68const fileName = path.basename(filePath);
9adec70dJoshua Skelton10 years ago69return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
70}
71
72return false;
f6ac01ceRuslan Bikkinin7 years ago73}
9adec70dJoshua Skelton10 years ago74
f6ac01ceRuslan Bikkinin7 years ago75function executeCommand(command, args, callback, opts) {
3016b3b0Ezio Li9 months ago76const proc = child_process.spawn(
77command + (process.platform === "win32" ? ".cmd" : ""),
78args,
79Object.assign({}, opts, { shell: true }),
80);
f6ac01ceRuslan Bikkinin7 years ago81let errorSignaled = false;
7212311dDaniel Lebu10 years ago82
3016b3b0Ezio Li9 months ago83proc.stdout.on("data", data => {
f6ac01ceRuslan Bikkinin7 years ago84log(`${data}`);
7212311dDaniel Lebu10 years ago85});
86
3016b3b0Ezio Li9 months ago87proc.stderr.on("data", data => {
f6ac01ceRuslan Bikkinin7 years ago88log.error(`${data}`);
7212311dDaniel Lebu10 years ago89});
90
3016b3b0Ezio Li9 months ago91proc.on("error", error => {
c5378ce2Daniel Lebu10 years ago92if (!errorSignaled) {
f6ac01ceRuslan Bikkinin7 years ago93callback(`An error occurred. ${error}`);
c5378ce2Daniel Lebu10 years ago94errorSignaled = true;
95}
96});
97
3016b3b0Ezio Li9 months ago98proc.on("exit", code => {
c5378ce2Daniel Lebu10 years ago99if (code === 0) {
100callback();
101} else if (!errorSignaled) {
f6ac01ceRuslan Bikkinin7 years ago102callback(`Error code: ${code}`);
c5378ce2Daniel Lebu10 years ago103errorSignaled = true;
104}
7212311dDaniel Lebu10 years ago105});
f6ac01ceRuslan Bikkinin7 years ago106}
7212311dDaniel Lebu10 years ago107
3016b3b0Ezio Li9 months ago108async function withTimeout(promise, ms, { onTimeout, fallbackValue } = {}) {
109let timer;
110const guarded = promise
111.then(value => ({ kind: "ok", value }))
112.catch(error => ({ kind: "error", error }));
113
114const timed = new Promise(resolve => {
115timer = setTimeout(() => {
116if (onTimeout) onTimeout();
117resolve({ kind: "timeout", value: fallbackValue });
118}, ms);
119});
120
121const result = await Promise.race([guarded, timed]);
122clearTimeout(timer);
123
124if (result.kind === "error") throw result.error;
125if (result.kind === "timeout") return result.value;
126return result.value;
127}
128
9adec70dJoshua Skelton10 years ago129module.exports = {
f6ac01ceRuslan Bikkinin7 years ago130checkCopyright,
3016b3b0Ezio Li9 months ago131executeCommand,
132withTimeout,
133};