-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathget-module-path-for.js
More file actions
126 lines (106 loc) · 3.71 KB
/
get-module-path-for.js
File metadata and controls
126 lines (106 loc) · 3.71 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const fs = require('fs-extra');
const path = require('path');
const walkSync = require('walk-sync');
const ADDON_PATHS = {};
const APP_PATHS = {};
const cwd = process.cwd();
let packagePaths = walkSync(cwd, {
globs: ['**/package.json'],
ignore: ['**/tmp/**', '**/node_modules/**'],
});
for (let packagePath of packagePaths) {
let pkg = fs.readJsonSync(packagePath);
let packageDir = path.dirname(path.resolve(cwd, packagePath));
if (pkg.keywords && pkg.keywords.includes('ember-addon')) {
// build addon instance
ADDON_PATHS[packageDir] = getAddonPackageName(packagePath);
} else if (isEmberCliProject(pkg)) {
APP_PATHS[packageDir] = pkg.name;
}
}
/**
* takes a package path and returns the runtime name of the addon.
*
* @param {String} packagePath the path on disk (from current working directory)
* @returns {String} The runtime name of the addon
*/
function getAddonPackageName(packagePath) {
const indexPath = path.resolve(path.dirname(packagePath), 'index.js');
if (fs.existsSync(indexPath)) {
const { name: packageName, moduleName } = require(indexPath);
// this is bad, fix later
return moduleName && typeof moduleName === 'function' ? moduleName() : packageName;
}
const { name: packageName } = require(packagePath);
return packageName;
}
function isEmberCliProject(pkg) {
return (
pkg &&
((pkg.dependencies && Object.keys(pkg.dependencies).indexOf('ember-cli') !== -1) ||
(pkg.devDependencies && Object.keys(pkg.devDependencies).indexOf('ember-cli') !== -1))
);
}
/**
* Transforms a literal "on disk" path to a "module path".
*
* @param {String} filePath the path on disk (from current working directory)
* @returns {String} The in-browser module path for the specified filePath
*/
function getModulePathFor(filePath, addonPaths = ADDON_PATHS, appPaths = APP_PATHS) {
let bestMatch = '';
let moduleNameRoot, relativePath, isApp, result;
for (let addonPath in addonPaths) {
if (filePath.startsWith(addonPath) && addonPath.length > bestMatch.length) {
bestMatch = addonPath;
moduleNameRoot = addonPaths[addonPath];
relativePath = filePath.slice(
addonPath.length + 1 /* for slash */,
-path.extname(filePath).length
);
}
}
for (let appPath in appPaths) {
if (filePath.startsWith(appPath) && appPath.length > bestMatch.length) {
bestMatch = appPath;
moduleNameRoot = appPaths[appPath];
relativePath = filePath.slice(
appPath.length + 1 /* for slash */,
-path.extname(filePath).length
);
isApp = true;
}
}
// this is pretty odd, but our tests in
// transforms/ember-object/__testfixtures__ don't actually live in an ember
// app or addon, so the standard logic above doesn't work for them
//
// this works by passing through the input file name when we are operating
// on the local ember-es6-class-codemod repo **and** we were not able to
// resolve a relativePath via normal means
let isLocallyTesting = process.cwd() === path.resolve(__dirname, '../../..');
if (!relativePath || isLocallyTesting) {
let result = filePath.replace(/\.[^/.]+$/, '');
return result;
}
if (!relativePath) {
return;
}
if (isApp) {
if (relativePath.startsWith('app')) {
result = `${moduleNameRoot}${relativePath.slice(3)}`;
} else if (relativePath.startsWith('tests')) {
result = `${moduleNameRoot}/${relativePath}`;
}
} else {
if (relativePath.startsWith('addon-test-support')) {
result = `${moduleNameRoot}/test-support${relativePath.slice(18)}`;
} else if (relativePath.startsWith('addon')) {
result = `${moduleNameRoot}${relativePath.slice(5)}`;
}
}
return result;
}
module.exports = {
getModulePathFor,
};