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