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 · modecode

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
5var child_process = require("child_process");
6var fs = require("fs");
7var log = require('fancy-log');
8const colors = require('ansi-colors');
9var path = require("path");
10var PluginError = require('plugin-error');
11var through = require("through2");
12
13/**
14 * Pretty logger using 'log'
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 */
19var logError = function(pluginName, file, message) {
20 var sourcePath = path.relative(__dirname, file.path).replace("../","");
21 log("[" + colors.cyan(pluginName) + "] " + colors.red("error") + " " + sourcePath + ": " + message);
22};
23
24/**
25 * Plugin to verify the Microsoft copyright notice is present
26 */
27var checkCopyright = function() {
28 var pluginName = "check-copyright";
29 var hadErrors = false;
30 var copyrightNotice = "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details.";
31
32 return through.obj(function(file, encoding, callback) {
33 if (file.isBuffer()) {
34 var fileContents = file.contents.toString(encoding);
35 fileContents = fileContents.replace("\r\n", "\n");
36 fileContents = fileContents.replace("\"use strict\";\n", "");
37 fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", "");
38
39 if (fileContents.indexOf(copyrightNotice) !== 0) {
40 logError(pluginName, file, "missing copyright notice");
41 hadErrors = true;
42 }
43 }
44
45 callback(null, file);
46 },
47 function(callback) {
48 if (hadErrors) {
49 return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
50 }
51 callback();
52 });
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) {
61 if (fs.existsSync(filePath)) {
62 var fileName = path.basename(filePath);
63 return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
64 }
65
66 return false;
67};
68
69/**
70 * Plugin to verify if import statements use correct casing
71 */
72var checkImports = function() {
73 var pluginName = "check-imports";
74 var hadErrors = false;
75 var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;
76
77 return through.obj(function(file, encoding, callback) {
78 if (file.isBuffer()) {
79 var fileContents = file.contents.toString(encoding);
80 var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
81 var workingDirectory = path.dirname(file.path);
82
83 importStatements.forEach(function(importStatement) {
84
85 var modulePath = re.exec(importStatement);
86 if (modulePath && modulePath[1]) {
87 var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
88
89 if (!existsCaseSensitive(moduleFilePath)) {
90 logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\"");
91 hadErrors = true;
92 }
93 }
94 });
95 }
96
97 callback(null, file);
98 },
99 function(callback) {
100 if (hadErrors) {
101 return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
102 }
103 callback();
104 });
105};
106
107var executeCommand = function(command, args, callback, opts) {
108 var proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
109 var errorSignaled = false;
110
111 proc.stdout.on("data", function(data) {
112 console.log("" + data);
113 });
114
115 proc.stderr.on("data", function(data) {
116 console.error("" + data);
117 });
118
119 proc.on("error", function(error) {
120 if (!errorSignaled) {
121 callback("An error occurred. " + error);
122 errorSignaled = true;
123 }
124 });
125
126 proc.on("exit", function(code) {
127 if (code === 0) {
128 callback();
129 } else if (!errorSignaled) {
130 callback("Error code: " + code);
131 errorSignaled = true;
132 }
133 });
134};
135
136module.exports = {
137 checkCopyright: checkCopyright,
138 checkImports: checkImports,
139 executeCommand: executeCommand
140}