microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.5

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

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