microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
tools/checkCasing.js
46lines · 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 | |
| 4 | var fs = require("fs"); |
| 5 | var path = require("path"); |
| 6 | var os = require("os"); |
| 7 | var child_process = require("child_process"); |
| 8 | |
| 9 | // Recursively find all instances of 'import [...] from "[...]";' |
| 10 | if (os.platform() === "win32") { |
| 11 | child_process.exec("findstr /sir /C:\"import.*from\" *.ts", parseOutput); |
| 12 | } else { |
| 13 | child_process.exec("grep -Ri 'import.*from' .", parseOutput); |
| 14 | } |
| 15 | |
| 16 | function parseOutput(err, out, stderr) { |
| 17 | // Extract out the filename containing the match, |
| 18 | // and the relative path of the file it is searching for |
| 19 | var regex = /(\s|^)([^\n:]*):.*from ["'](\.[^"']*)["'];/g; |
| 20 | var imports = []; |
| 21 | out.replace(regex, function (all, _, file, from) { |
| 22 | imports.push({ path: file, relative: from }); |
| 23 | }); |
| 24 | checkImports(imports); |
| 25 | } |
| 26 | |
| 27 | function checkImports(imports) { |
| 28 | // Check to see if the import references a source file |
| 29 | // with an exact match of a name, and complain otherwise |
| 30 | imports.map(function (i) { |
| 31 | i.resolved = path.resolve(path.dirname(i.path), i.relative + ".ts"); |
| 32 | return i; |
| 33 | }).filter(function (i) { |
| 34 | var dirpath = path.dirname(i.resolved); |
| 35 | try { |
| 36 | var entries = fs.readdirSync(dirpath); |
| 37 | return entries.indexOf(path.basename(i.resolved)) === -1; |
| 38 | } catch (exception) { |
| 39 | console.log("Missing folder for import in " + i.path + ": " + dirpath); |
| 40 | process.exitCode = 1; |
| 41 | } |
| 42 | }).forEach(function (i) { |
| 43 | console.log("Missing file for import in " + i.path + ": " + i.relative); |
| 44 | process.exitCode = 1; |
| 45 | }); |
| 46 | } |
| 47 | |