Skip to content

Commit fd8ba6c

Browse files
author
Robert Jackson
authored
Fix issues identifying in-repo addon paths in getTelemetryFor. (#129)
Fix issues identifying in-repo addon paths in getTelemetryFor.
2 parents 172fbb1 + 6ec165b commit fd8ba6c

3 files changed

Lines changed: 119 additions & 27 deletions

File tree

transforms/ember-object/test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const path = require('path');
34
const { runTransformTest } = require('codemod-cli');
45

56
// bootstrap the mock telemetry data
@@ -18,9 +19,7 @@ for (let testFile of testFiles) {
1819
let moduleName = testFile.replace(/\.[^/.]+$/, '');
1920
let value = mockTelemetryData[moduleName] || {};
2021

21-
mockTelemetry[
22-
`ember-es6-class-codemod/transforms/ember-object/__testfixtures__/${moduleName}`
23-
] = value;
22+
mockTelemetry[path.resolve(__dirname, `./__testfixtures__/${moduleName}`)] = value;
2423
}
2524

2625
cache.set('telemetry', JSON.stringify(mockTelemetry));

transforms/helpers/util/get-telemetry-for.js

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,31 @@ const cache = require('../../../lib/cache');
55

66
const telemetry = cache.has('telemetry') ? JSON.parse(cache.get('telemetry').value) : {};
77
const ADDON_PATHS = {};
8+
const APP_PATHS = {};
89

910
let packagePaths = walkSync('./', {
1011
globs: ['**/package.json'],
1112
ignore: ['node_modules/**'],
1213
});
1314

1415
for (let packagePath of packagePaths) {
15-
let { name } = fs.readJsonSync(packagePath);
16+
let pkg = fs.readJsonSync(packagePath);
1617

17-
ADDON_PATHS[path.dirname(path.resolve('.', packagePath))] = name;
18+
let packageDir = path.dirname(path.resolve('.', packagePath));
19+
20+
if (pkg.keywords && pkg.keywords.includes('ember-addon')) {
21+
ADDON_PATHS[packageDir] = pkg.name;
22+
} else if (isEmberCliProject(pkg)) {
23+
APP_PATHS[packageDir] = pkg.name;
24+
}
25+
}
26+
27+
function isEmberCliProject(pkg) {
28+
return (
29+
pkg &&
30+
((pkg.dependencies && Object.keys(pkg.dependencies).indexOf('ember-cli') !== -1) ||
31+
(pkg.devDependencies && Object.keys(pkg.devDependencies).indexOf('ember-cli') !== -1))
32+
);
1833
}
1934

2035
/**
@@ -23,27 +38,63 @@ for (let packagePath of packagePaths) {
2338
* @param {String} filePath the path on disk (from current working directory)
2439
* @returns {String} The in-browser module path for the specified filePath
2540
*/
26-
function getModulePathFor(filePath, addonPaths = ADDON_PATHS) {
27-
let fileSegments = filePath.split('/');
28-
let addonSegments = [];
41+
function getModulePathFor(filePath, addonPaths = ADDON_PATHS, appPaths = APP_PATHS) {
42+
let bestMatch = '';
43+
let moduleNameRoot, relativePath, isApp, result;
2944

30-
while (fileSegments.length > 0) {
31-
addonSegments.push(fileSegments.shift());
45+
for (let addonPath in addonPaths) {
46+
if (filePath.startsWith(addonPath) && addonPath.length > bestMatch.length) {
47+
bestMatch = addonPath;
48+
moduleNameRoot = addonPaths[addonPath];
49+
relativePath = filePath.slice(
50+
addonPath.length + 1 /* for slash */,
51+
-path.extname(filePath).length
52+
);
53+
}
54+
}
3255

33-
if (addonPaths[addonSegments.join('/')]) {
34-
break;
56+
for (let appPath in appPaths) {
57+
if (filePath.startsWith(appPath) && appPath.length > bestMatch.length) {
58+
bestMatch = appPath;
59+
moduleNameRoot = appPaths[appPath];
60+
relativePath = filePath.slice(
61+
appPath.length + 1 /* for slash */,
62+
-path.extname(filePath).length
63+
);
64+
isApp = true;
3565
}
3666
}
3767

38-
let addonFilePath = addonSegments.join('/');
39-
let addonName = addonPaths[addonFilePath];
68+
if (!relativePath && process.cwd() === path.resolve(__dirname, '../../..')) {
69+
// this is pretty odd, but our tests in
70+
// transforms/ember-object/__testfixtures__ don't actually live in an ember
71+
// app or addon, so the standard logic above doesn't work for them
72+
//
73+
// this works by passing through the input file name when we are operating
74+
// on the local ember-es6-class-codemod repo **and** we were not able to
75+
// resolve a relativePath via normal means
76+
return filePath.replace(/\.[^/.]+$/, '');
77+
}
78+
79+
if (!relativePath) {
80+
return;
81+
}
4082

41-
let relativeFilePath = fileSegments
42-
.join('/')
43-
.replace(/^(addon|app)\//, '')
44-
.replace(/\.[^/.]+$/, '');
83+
if (isApp) {
84+
if (relativePath.startsWith('app')) {
85+
result = `${moduleNameRoot}${relativePath.slice(3)}`;
86+
} else if (relativePath.startsWith('tests')) {
87+
result = `${moduleNameRoot}/${relativePath}`;
88+
}
89+
} else {
90+
if (relativePath.startsWith('addon-test-support')) {
91+
result = `${moduleNameRoot}/test-support${relativePath.slice(18)}`;
92+
} else if (relativePath.startsWith('addon')) {
93+
result = `${moduleNameRoot}${relativePath.slice(5)}`;
94+
}
95+
}
4596

46-
return `${addonName}/${relativeFilePath}`;
97+
return result;
4798
}
4899

49100
/**
@@ -54,8 +105,9 @@ function getModulePathFor(filePath, addonPaths = ADDON_PATHS) {
54105
*/
55106
function getTelemetryFor(filePath) {
56107
let modulePath = getModulePathFor(filePath);
108+
let data = telemetry[modulePath];
57109

58-
return telemetry[modulePath];
110+
return data;
59111
}
60112

61113
module.exports = {
Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,57 @@
11
const { getModulePathFor } = require('./get-telemetry-for');
22

33
describe('getModulePathFor', () => {
4-
test('accesses telemetry data for the specified app module', () => {
5-
const addonPaths = {
6-
'/User/whomever/voyager-web/lib/invitation-platform': 'invitation-platform',
7-
};
4+
const addonPaths = {
5+
'/User/whomever/some-app/lib/special-sauce': 'special-sauce',
6+
};
7+
8+
const appPaths = {
9+
'/User/whomever/some-app': 'some-app',
10+
};
11+
12+
test('can determine the runtime module id for a specific on disk in-repo addon file', () => {
13+
expect(
14+
getModulePathFor(
15+
'/User/whomever/some-app/lib/special-sauce/addon/components/fire-sauce.js',
16+
addonPaths,
17+
appPaths
18+
)
19+
).toEqual('special-sauce/components/fire-sauce');
20+
21+
expect(
22+
getModulePathFor(
23+
'/User/whomever/some-app/lib/special-sauce/addon-test-support/services/whatever.js',
24+
addonPaths,
25+
appPaths
26+
)
27+
).toEqual('special-sauce/test-support/services/whatever');
28+
});
29+
30+
test("does not process files in an in-repo addon's app/ folder", () => {
31+
expect(
32+
getModulePathFor(
33+
'/User/whomever/some-app/lib/special-sauce/app/services/whatever.js',
34+
addonPaths,
35+
appPaths
36+
)
37+
).toEqual(undefined);
38+
});
39+
40+
test('can determine the runtime module id for a file in the app itself', () => {
41+
expect(
42+
getModulePathFor(
43+
'/User/whomever/some-app/app/services/something-here.js',
44+
addonPaths,
45+
appPaths
46+
)
47+
).toEqual('some-app/services/something-here');
848

949
expect(
1050
getModulePathFor(
11-
'/User/whomever/voyager-web/lib/invitation-platform/addon/components/fuse-limit-alert',
12-
addonPaths
51+
'/User/whomever/some-app/tests/mocks/something-here.js',
52+
addonPaths,
53+
appPaths
1354
)
14-
).toEqual('invitation-platform/components/fuse-limit-alert');
55+
).toEqual('some-app/tests/mocks/something-here');
1556
});
1657
});

0 commit comments

Comments
 (0)