microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

136lines · 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");
9adec70dJoshua Skelton10 years ago35
4abd7849Joshua Skelton10 years ago36if (fileContents.indexOf(copyrightNotice) !== 0) {
6200aa9fJoshua Skelton10 years ago37logError(pluginName, file, "missing copyright notice");
38hadErrors = true;
9adec70dJoshua Skelton10 years ago39}
40}
41
42callback(null, file);
6200aa9fJoshua Skelton10 years ago43},
44function(callback) {
45if (hadErrors) {
46return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
47}
48callback();
9adec70dJoshua Skelton10 years ago49});
50};
51
52/**
53* Helper function to check if a file exists case sensitive
54* @param {string} filePath The path to check
55* @returns {boolean} If the path exists case sensitive
56*/
57var existsCaseSensitive = function(filePath) {
58if (fs.existsSync(filePath)) {
59var fileName = path.basename(filePath);
60return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
61}
62
63return false;
64};
65
66/**
67* Plugin to verify if import statements use correct casing
68*/
69var checkImports = function() {
6200aa9fJoshua Skelton10 years ago70var pluginName = "check-imports";
71var hadErrors = false;
e8486e58Joshua Skelton10 years ago72var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;
9adec70dJoshua Skelton10 years ago73
74return through.obj(function(file, encoding, callback) {
a3fd5ee9Joshua Skelton10 years ago75if (file.isBuffer()) {
9adec70dJoshua Skelton10 years ago76var fileContents = file.contents.toString(encoding);
0d7e2dc5Joshua Skelton10 years ago77var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
9adec70dJoshua Skelton10 years ago78var workingDirectory = path.dirname(file.path);
79
80importStatements.forEach(function(importStatement) {
81var modulePath = re.exec(importStatement);
9425034eJoshua Skelton10 years ago82if (modulePath && modulePath[1]) {
9adec70dJoshua Skelton10 years ago83var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
84
85if (!existsCaseSensitive(moduleFilePath)) {
30d12ab8Joshua Skelton10 years ago86logError(pluginName, file, "unresolved import: \"" + modulePath[1] + "\"");
6200aa9fJoshua Skelton10 years ago87hadErrors = true;
9adec70dJoshua Skelton10 years ago88}
89}
90});
91}
92
93callback(null, file);
6200aa9fJoshua Skelton10 years ago94},
95function(callback) {
96if (hadErrors) {
97return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
98}
99callback();
9adec70dJoshua Skelton10 years ago100});
101};
102
92f13422Jimmy Thomson9 years ago103var executeCommand = function(command, args, callback, opts) {
104var proc = child_process.spawn(command + (process.platform === "win32" ? ".cmd" : ""), args, opts);
c5378ce2Daniel Lebu10 years ago105var errorSignaled = false;
7212311dDaniel Lebu10 years ago106
92f13422Jimmy Thomson9 years ago107proc.stdout.on("data", function(data) {
7212311dDaniel Lebu10 years ago108console.log("" + data);
109});
110
92f13422Jimmy Thomson9 years ago111proc.stderr.on("data", function(data) {
7212311dDaniel Lebu10 years ago112console.error("" + data);
113});
114
92f13422Jimmy Thomson9 years ago115proc.on("error", function(error) {
c5378ce2Daniel Lebu10 years ago116if (!errorSignaled) {
8e5ee0d7Chris Bala10 years ago117callback("An error occurred. " + error);
c5378ce2Daniel Lebu10 years ago118errorSignaled = true;
119}
120});
121
92f13422Jimmy Thomson9 years ago122proc.on("exit", function(code) {
c5378ce2Daniel Lebu10 years ago123if (code === 0) {
124callback();
125} else if (!errorSignaled) {
126callback("Error code: " + code);
127errorSignaled = true;
128}
7212311dDaniel Lebu10 years ago129});
130};
131
9adec70dJoshua Skelton10 years ago132module.exports = {
133checkCopyright: checkCopyright,
7212311dDaniel Lebu10 years ago134checkImports: checkImports,
135executeCommand: executeCommand
9adec70dJoshua Skelton10 years ago136}