blob: 85090a6eeaf32a1c4cc6c3faae5757580e5964d6 (
plain)
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
37
38
39
40
41
42
|
'use strict';
const path = require('path');
const {readFileSync} = require('fs');
const isNodeModules = require('./is-node-modules.cjs');
const resultsCache = require('./cache.cjs');
function getDirectoryTypeActual(directory) {
if (isNodeModules(directory)) {
return 'commonjs';
}
try {
return JSON.parse(readFileSync(path.resolve(directory, 'package.json'))).type || 'commonjs';
} catch (_) {
}
const parent = path.dirname(directory);
if (parent === directory) {
return 'commonjs';
}
return getDirectoryType(parent);
}
function getDirectoryType(directory) {
if (resultsCache.has(directory)) {
return resultsCache.get(directory);
}
const result = getDirectoryTypeActual(directory);
resultsCache.set(directory, result);
return result;
}
function getPackageTypeSync(filename) {
return getDirectoryType(path.resolve(path.dirname(filename)));
}
module.exports = getPackageTypeSync;
|