microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e414fcd5d5ad07a33490f6e98232b61ecf7d4971

Branches

Tags

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

Clone

HTTPS

Download ZIP

tools/gulp-extras.js

105lines · 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 fs = require("fs");
6var gutil = require("gulp-util");
7var path = require("path");
8var PluginError = gutil.PluginError;
9var through = require("through2");
10
11/**
12 * Pretty logger using gutil.log
13 * @param {string} pluginName Name of the pluginName
14 * @param {Object} file A gulp file to report on
15 * @param {string} message The error message to display
16 */
17var logError = function(pluginName, file, message) {
18 var sourcePath = path.relative(__dirname, file.path).replace("../","");
19 gutil.log(`[${gutil.colors.cyan(pluginName)}] ${gutil.colors.red("error")} ${sourcePath}: ${message}`);
20};
21
22/**
23 * Plugin to verify the Microsoft copyright notice is present
24 */
25var checkCopyright = function() {
26 var pluginName = "check-copyright";
27 var hadErrors = false;
28 var copyrightNotice = "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for details.";
29
30 return through.obj(function(file, encoding, callback) {
31 if (file.isBuffer()) {
32 var fileContents = file.contents.toString(encoding);
33 fileContents = fileContents.replace("\r\n", "\n");
34
35 if (fileContents.indexOf(copyrightNotice) !== 0) {
36 logError(pluginName, file, "missing copyright notice");
37 hadErrors = true;
38 }
39 }
40
41 callback(null, file);
42 },
43 function(callback) {
44 if (hadErrors) {
45 return this.emit("error", new PluginError(pluginName, "Failed copyright check"));
46 }
47 callback();
48 });
49};
50
51/**
52 * Helper function to check if a file exists case sensitive
53 * @param {string} filePath The path to check
54 * @returns {boolean} If the path exists case sensitive
55 */
56var existsCaseSensitive = function(filePath) {
57 if (fs.existsSync(filePath)) {
58 var fileName = path.basename(filePath);
59 return fs.readdirSync(path.dirname(filePath)).indexOf(fileName) !== -1;
60 }
61
62 return false;
63};
64
65/**
66 * Plugin to verify if import statements use correct casing
67 */
68var checkImports = function() {
69 var pluginName = "check-imports";
70 var hadErrors = false;
71 var re = /(?:\s|^)(?:[^\n:]*).*from ["'](\.[^"']*)["'];/;
72
73 return through.obj(function(file, encoding, callback) {
74 if (file.isBuffer()) {
75 var fileContents = file.contents.toString(encoding);
76 var importStatements = fileContents.match(new RegExp(re.source, "g")) || [];
77 var workingDirectory = path.dirname(file.path);
78
79 importStatements.forEach(function(importStatement) {
80 var modulePath = re.exec(importStatement);
81 if (modulePath && modulePath[1]) {
82 var moduleFilePath = path.resolve(workingDirectory, modulePath[1] + ".ts");
83
84 if (!existsCaseSensitive(moduleFilePath)) {
85 logError(pluginName, file, `unresolved import: "${modulePath[1]}"`);
86 hadErrors = true;
87 }
88 }
89 });
90 }
91
92 callback(null, file);
93 },
94 function(callback) {
95 if (hadErrors) {
96 return this.emit("error", new PluginError(pluginName, "Failed import casing check"));
97 }
98 callback();
99 });
100};
101
102module.exports = {
103 checkCopyright: checkCopyright,
104 checkImports: checkImports
105}