-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
36 lines (31 loc) · 1.25 KB
/
Copy pathdebug.js
File metadata and controls
36 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const path = require('path');
const fs = require('fs');
const ROOT = __dirname;
let pathname = '/dashboard';
let rel = pathname.replace(/^\//, '');
if (rel === '' || rel === '/') rel = 'index.html';
if (rel === 'dashboard') rel = 'dashboard.html';
console.log('rel:', rel);
let filePath = path.join(ROOT, rel);
console.log('filePath:', filePath, 'exists:', fs.existsSync(filePath));
if (!fs.existsSync(filePath)) {
const docsPath = path.join(ROOT, 'docs', rel);
console.log('docsPath:', docsPath, 'exists:', fs.existsSync(docsPath));
if (fs.existsSync(docsPath)) filePath = docsPath;
}
const resolvedPath = path.resolve(filePath);
console.log('resolvedPath:', resolvedPath);
console.log('startsWith ROOT:', resolvedPath.startsWith(path.resolve(ROOT)));
console.log('exists:', fs.existsSync(resolvedPath));
// Test /
pathname = '/';
rel = pathname.replace(/^\//, '');
console.log('\nrel for /:', JSON.stringify(rel));
if (rel === '' || rel === '/') rel = 'index.html';
console.log('rel after check:', rel);
filePath = path.join(ROOT, rel);
console.log('filePath:', filePath, 'exists:', fs.existsSync(filePath));
if (!fs.existsSync(filePath)) {
const docsPath = path.join(ROOT, 'docs', rel);
console.log('docsPath:', docsPath, 'exists:', fs.existsSync(docsPath));
}