microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.13

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

139lines · 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");
7var gutil = require("gulp-util");
8var path = require("path");
6200aa9fJoshua Skelton10 years ago9var PluginError = gutil.PluginError;
9adec70dJoshua Skelton10 years ago10var 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) {
19var sourcePath = path.relative(__dirname, file.path).replace("../","");
fefdba64Joshua Skelton10 years ago20gutil.log("[" + gutil.colors.cyan(pluginName) + "] " + gutil.colors.red("error") + " " + sourcePath + ": " + message);
9adec70dJoshua Skelton10 years ago21};
22
23/**
24* Plugin to verify the Microsoft copyright notice is present
25*/
26var checkCopyright = function() {
6200aa9fJoshua Skelton10 years ago27var pluginName = "check-copyright";
28var hadErrors = false;
4abd7849Joshua Skelton10 years ago29var 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 ago30
31return through.obj(function(file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago32if (file.isBuffer()) {
9adec70dJoshua Skelton10 years ago33var fileContents = file.contents.toString(encoding);
4abd7849Joshua Skelton10 years ago34fileContents = fileContents.replace("\r\n", "\n");
5c8365a6Artem Egorov8 years ago35fileContents = fileContents.replace("\"use strict\";\n", "");
078b49a4Artem Egorov7 years ago36fileContents = fileContents.replace("Object.defineProperty(exports, \"__esModule\", { value: true });\n", "");
9adec70dJoshua Skelton10 years ago37
4abd7849Joshua Skelton10 years ago38if (fileContents.indexOf(copyrightNotice) !== 0) {
6200aa9fJoshua Skelton10 years ago39logError(pluginName, file, "missing copyright notice");
40hadErrors = true;
9adec70dJoshua Skelton10 years ago41}
42}
43
44callback(null, file);
6200aa9fJoshua Skelton10 years ago45},
46function(callback) {
47if (hadErrors) {
48return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
49}
50callback();
9adec70dJoshua Skelton10 years ago51});
52};
53
54/**
55* Helper function to check if a file exists case sensitive
56* @param {string} filePath The path to check
57* @returns {boolean} If the path exists case sensitive
58*/
59var existsCaseSensitive = function(filePath) {
60if (fs.existsSync(filePath)) {
61var fileName = path.basename(filePath);
62return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
63}
64
65return false;
66};
67
68/**
69* Plugin to verify if import statements use correct casing
70*/
71var checkImports = function() {
6200aa9fJoshua Skelton10 years ago72var pluginName = "check-imports";
73var hadErrors = false;
e8486e58Joshua Skelton10 years ago74var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;
9adec70dJoshua Skelton10 years ago75
76return through.obj(function(file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago77if (file.isBuffer()) {
9adec70dJoshua Skelton10 years ago78var fileContents = file.contents.toString(encoding);
0d7e2dc5Joshua Skelton10 years ago79var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
9adec70dJoshua Skelton10 years ago80var workingDirectory = path.dirname(file.path);
81
82importStatements.forEach(function(importStatement) {
547ca187max-mironov8 years ago83
9adec70dJoshua Skelton10 years ago84var modulePath = re.exec(importStatement);
9425034eJoshua Skelton10 years ago85if (modulePath && modulePath[1]) {
9adec70dJoshua Skelton10 years ago86var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
87
88if (!existsCaseSensitive(moduleFilePath)) {
30d12ab8Joshua Skelton10 years ago89logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\"");
6200aa9fJoshua Skelton10 years ago90hadErrors = true;
9adec70dJoshua Skelton10 years ago91}
92}
93});
94}
95
96callback(null, file);
6200aa9fJoshua Skelton10 years ago97},
98function(callback) {
99if (hadErrors) {
100return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
101}
102callback();
9adec70dJoshua Skelton10 years ago103});
104};
105
92f13422Jimmy Thomson9 years ago106var executeCommand = function(command, args, callback, opts) {
107var proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
c5378ce2Daniel Lebu10 years ago108var errorSignaled = false;
7212311dDaniel Lebu10 years ago109
92f13422Jimmy Thomson9 years ago110proc.stdout.on("data", function(data) {
7212311dDaniel Lebu10 years ago111console.log("" + data);
112});
113
92f13422Jimmy Thomson9 years ago114proc.stderr.on("data", function(data) {
7212311dDaniel Lebu10 years ago115console.error("" + data);
116});
117
92f13422Jimmy Thomson9 years ago118proc.on("error", function(error) {
c5378ce2Daniel Lebu10 years ago119if (!errorSignaled) {
8e5ee0d7Chris Bala10 years ago120callback("An error occurred. " + error);
c5378ce2Daniel Lebu10 years ago121errorSignaled = true;
122}
123});
124
92f13422Jimmy Thomson9 years ago125proc.on("exit", function(code) {
c5378ce2Daniel Lebu10 years ago126if (code === 0) {
127callback();
128} else if (!errorSignaled) {
129callback("Error code: " + code);
130errorSignaled = true;
131}
7212311dDaniel Lebu10 years ago132});
133};
134
9adec70dJoshua Skelton10 years ago135module.exports = {
136checkCopyright: checkCopyright,
7212311dDaniel Lebu10 years ago137checkImports: checkImports,
138executeCommand: executeCommand
9adec70dJoshua Skelton10 years ago139}