microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.9.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

101lines · 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");
7const log = require('fancy-log');
4ad1a2dcsemenyakNik7 years ago8const colors = require('ansi-colors');
f6ac01ceRuslan Bikkinin7 years ago9const path = require("path");
10const PluginError = require('plugin-error');
11const 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) {
20const sourcePath = path.relative(__dirname, file.path).replace("../", "");
21log(`[${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;
30const 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 ago31
f6ac01ceRuslan Bikkinin7 years ago32return through.obj(function (file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago33if (file.isBuffer()) {
f6ac01ceRuslan Bikkinin7 years ago34let fileContents = file.contents.toString(encoding);
4abd7849Joshua Skelton10 years ago35fileContents = fileContents.replace("\r\n", "\n");
5c8365a6Artem Egorov8 years ago36fileContents = fileContents.replace("\"use strict\";\n", "");
078b49a4Artem Egorov7 years ago37fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", "");
9adec70dJoshua Skelton10 years ago38
4abd7849Joshua Skelton10 years ago39if (fileContents.indexOf(copyrightNotice) !== 0) {
6200aa9fJoshua Skelton10 years ago40logError(pluginName, file, "missing copyright notice");
41hadErrors = true;
9adec70dJoshua Skelton10 years ago42}
43}
44
45callback(null, file);
6200aa9fJoshua Skelton10 years ago46},
f6ac01ceRuslan Bikkinin7 years ago47function (callback) {
48if (hadErrors) {
49return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
50}
51callback();
52});
53}
9adec70dJoshua Skelton10 years ago54
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 ago60function existsCaseSensitive(filePath) {
9adec70dJoshua Skelton10 years ago61if (fs.existsSync(filePath)) {
f6ac01ceRuslan Bikkinin7 years ago62const fileName = path.basename(filePath);
9adec70dJoshua Skelton10 years ago63return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
64}
65
66return false;
f6ac01ceRuslan Bikkinin7 years ago67}
9adec70dJoshua Skelton10 years ago68
f6ac01ceRuslan Bikkinin7 years ago69function executeCommand(command, args, callback, opts) {
70const proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
71let errorSignaled = false;
7212311dDaniel Lebu10 years ago72
f6ac01ceRuslan Bikkinin7 years ago73proc.stdout.on("data", (data) => {
74log(`${data}`);
7212311dDaniel Lebu10 years ago75});
76
f6ac01ceRuslan Bikkinin7 years ago77proc.stderr.on("data", (data) => {
78log.error(`${data}`);
7212311dDaniel Lebu10 years ago79});
80
f6ac01ceRuslan Bikkinin7 years ago81proc.on("error", (error) => {
c5378ce2Daniel Lebu10 years ago82if (!errorSignaled) {
f6ac01ceRuslan Bikkinin7 years ago83callback(`An error occurred. ${error}`);
c5378ce2Daniel Lebu10 years ago84errorSignaled = true;
85}
86});
87
f6ac01ceRuslan Bikkinin7 years ago88proc.on("exit", (code) => {
c5378ce2Daniel Lebu10 years ago89if (code === 0) {
90callback();
91} else if (!errorSignaled) {
f6ac01ceRuslan Bikkinin7 years ago92callback(`Error code: ${code}`);
c5378ce2Daniel Lebu10 years ago93errorSignaled = true;
94}
7212311dDaniel Lebu10 years ago95});
f6ac01ceRuslan Bikkinin7 years ago96}
7212311dDaniel Lebu10 years ago97
9adec70dJoshua Skelton10 years ago98module.exports = {
f6ac01ceRuslan Bikkinin7 years ago99checkCopyright,
100executeCommand
9adec70dJoshua Skelton10 years ago101}