microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

140lines · 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
7212311dDaniel Lebu10 years ago5var child_process = require("child_process");
9adec70dJoshua Skelton10 years ago6var fs = require("fs");
4ad1a2dcsemenyakNik7 years ago7var log = require('fancy-log');
8const colors = require('ansi-colors');
9adec70dJoshua Skelton10 years ago9var path = require("path");
4ad1a2dcsemenyakNik7 years ago10var PluginError = require('plugin-error');
9adec70dJoshua Skelton10 years ago11var through = require("through2");
12
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*/
19var logError = function(pluginName, file, message) {
20var sourcePath = path.relative(__dirname, file.path).replace("../","");
4ad1a2dcsemenyakNik7 years ago21log("[" + colors.cyan(pluginName) + "] " + colors.red("error") + " " + sourcePath + ": " + message);
9adec70dJoshua Skelton10 years ago22};
23
24/**
25* Plugin to verify the Microsoft copyright notice is present
26*/
27var checkCopyright = function() {
6200aa9fJoshua Skelton10 years ago28var pluginName = "check-copyright";
29var hadErrors = false;
4abd7849Joshua Skelton10 years ago30var 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
32return through.obj(function(file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago33if (file.isBuffer()) {
9adec70dJoshua Skelton10 years ago34var 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},
47function(callback) {
48if (hadErrors) {
49return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
50}
51callback();
9adec70dJoshua Skelton10 years ago52});
53};
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*/
60var existsCaseSensitive = function(filePath) {
61if (fs.existsSync(filePath)) {
62var fileName = path.basename(filePath);
63return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
64}
65
66return false;
67};
68
69/**
70* Plugin to verify if import statements use correct casing
71*/
72var checkImports = function() {
6200aa9fJoshua Skelton10 years ago73var pluginName = "check-imports";
74var hadErrors = false;
e8486e58Joshua Skelton10 years ago75var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;
9adec70dJoshua Skelton10 years ago76
77return through.obj(function(file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago78if (file.isBuffer()) {
9adec70dJoshua Skelton10 years ago79var fileContents = file.contents.toString(encoding);
0d7e2dc5Joshua Skelton10 years ago80var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
9adec70dJoshua Skelton10 years ago81var workingDirectory = path.dirname(file.path);
82
83importStatements.forEach(function(importStatement) {
547ca187max-mironov8 years ago84
9adec70dJoshua Skelton10 years ago85var modulePath = re.exec(importStatement);
9425034eJoshua Skelton10 years ago86if (modulePath && modulePath[1]) {
9adec70dJoshua Skelton10 years ago87var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
88
89if (!existsCaseSensitive(moduleFilePath)) {
30d12ab8Joshua Skelton10 years ago90logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\"");
6200aa9fJoshua Skelton10 years ago91hadErrors = true;
9adec70dJoshua Skelton10 years ago92}
93}
94});
95}
96
97callback(null, file);
6200aa9fJoshua Skelton10 years ago98},
99function(callback) {
100if (hadErrors) {
101return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
102}
103callback();
9adec70dJoshua Skelton10 years ago104});
105};
106
92f13422Jimmy Thomson9 years ago107var executeCommand = function(command, args, callback, opts) {
108var proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
c5378ce2Daniel Lebu10 years ago109var errorSignaled = false;
7212311dDaniel Lebu10 years ago110
92f13422Jimmy Thomson9 years ago111proc.stdout.on("data", function(data) {
7212311dDaniel Lebu10 years ago112console.log("" + data);
113});
114
92f13422Jimmy Thomson9 years ago115proc.stderr.on("data", function(data) {
7212311dDaniel Lebu10 years ago116console.error("" + data);
117});
118
92f13422Jimmy Thomson9 years ago119proc.on("error", function(error) {
c5378ce2Daniel Lebu10 years ago120if (!errorSignaled) {
8e5ee0d7Chris Bala10 years ago121callback("An error occurred. " + error);
c5378ce2Daniel Lebu10 years ago122errorSignaled = true;
123}
124});
125
92f13422Jimmy Thomson9 years ago126proc.on("exit", function(code) {
c5378ce2Daniel Lebu10 years ago127if (code === 0) {
128callback();
129} else if (!errorSignaled) {
130callback("Error code: " + code);
131errorSignaled = true;
132}
7212311dDaniel Lebu10 years ago133});
134};
135
9adec70dJoshua Skelton10 years ago136module.exports = {
137checkCopyright: checkCopyright,
7212311dDaniel Lebu10 years ago138checkImports: checkImports,
139executeCommand: executeCommand
9adec70dJoshua Skelton10 years ago140}