Skip to content

Commit dfc6db4

Browse files
authored
Added files from addon-test-support to addon.publicEntrypoints() (#11)
* refactor: Renamed changeDirectory to renameDirectory. Updated its API. * chore: Simplified tests for mapFilePaths. Added failing tests. * bugfix: Updated renameDirectory to provide a new path only when there is a match * refactor: Used renameDirectory to better separate concerns from the glob package * feature: Added the files from test-support to publicEntrypoints * refactor: Added tests for blueprints and test-support Co-authored-by: ijlee2 <[email protected]>
1 parent eb68f1f commit dfc6db4

File tree

6 files changed

+218
-95
lines changed

6 files changed

+218
-95
lines changed

src/migration/ember-addon/steps/analyze-addon.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import { join } from 'node:path';
2-
31
import glob from 'glob';
42

53
import { decideVersion } from '../../../utils/blueprints.js';
4+
import { renameDirectory } from '../../../utils/files.js';
65

76
function getAppReexports(options) {
87
const { projectRoot } = options;
98

10-
const filePaths = glob.sync('**/*.js', {
11-
cwd: join(projectRoot, 'app'),
9+
const filePaths = glob.sync('app/**/*.js', {
10+
cwd: projectRoot,
1211
});
1312

14-
return filePaths;
13+
return filePaths.map((filePath) => {
14+
return renameDirectory(filePath, {
15+
from: 'app',
16+
to: '',
17+
});
18+
});
1519
}
1620

1721
function getProjectRootDevDependencies(options) {
@@ -24,11 +28,26 @@ function getProjectRootDevDependencies(options) {
2428
function getPublicEntrypoints(options) {
2529
const { projectRoot } = options;
2630

27-
const filePaths = glob.sync('**/*.{js,ts}', {
28-
cwd: join(projectRoot, 'addon'),
31+
const filePaths = glob.sync('{addon,addon-test-support}/**/*.{js,ts}', {
32+
cwd: projectRoot,
2933
});
3034

31-
return filePaths.map((filePath) => filePath.replace(/ts$/, 'js'));
35+
return filePaths
36+
.map((filePath) => {
37+
return renameDirectory(filePath, {
38+
from: 'addon',
39+
to: '',
40+
});
41+
})
42+
.map((filePath) => {
43+
return renameDirectory(filePath, {
44+
from: 'addon-test-support',
45+
to: 'test-support',
46+
});
47+
})
48+
.map((filePath) => {
49+
return filePath.replace(/ts$/, 'js');
50+
});
3251
}
3352

3453
export function analyzeAddon(options) {

src/utils/files/map-file-paths.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
function changeDirectory(oldPath, from, to) {
2-
if (!oldPath.startsWith(from)) {
3-
throw new RangeError(
4-
`The provided path \`${oldPath}\` does not start with \`${from}\`.`
5-
);
1+
import { join } from 'node:path';
2+
3+
export function renameDirectory(oldPath, { from, to }) {
4+
if (from === '') {
5+
return join(to, oldPath);
66
}
77

8-
const relativePath = oldPath.replace(new RegExp(`^${from}/`), '');
9-
const newPath = `${to}/${relativePath}`;
8+
if (!oldPath.startsWith(`${from}/`)) {
9+
return oldPath;
10+
}
1011

11-
return [oldPath, newPath];
12+
return join(to, oldPath.replace(`${from}/`, ''));
1213
}
1314

1415
export function mapFilePaths(filePaths, directory) {
1516
const { from, to } = directory;
1617

1718
return new Map(
18-
filePaths.map((filePath) => changeDirectory(filePath, from, to))
19+
filePaths.map((filePath) => {
20+
const newPath = renameDirectory(filePath, { from, to });
21+
22+
return [filePath, newPath];
23+
})
1924
);
2025
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { analyzeAddon } from '../../../../../src/migration/ember-addon/steps/index.js';
2+
import {
3+
codemodOptions,
4+
options,
5+
} from '../../../../helpers/shared-test-setups/typescript.js';
6+
import { assert, loadFixture, test } from '../../../../helpers/testing.js';
7+
8+
test('migration | ember-addon | steps | analyze-addon > blueprints and test-support', function () {
9+
const inputProject = {
10+
'addon-test-support': {
11+
components: {
12+
'container-query.ts': '',
13+
},
14+
'index.ts': `export * from './components/container-query';\n`,
15+
},
16+
blueprints: {
17+
'ember-container-query': {
18+
files: {
19+
'some-folder': {
20+
'some-file.ts': '',
21+
},
22+
},
23+
'index.ts': [
24+
`module.exports = {`,
25+
` normalizeEntityName() {},`,
26+
`};`,
27+
``,
28+
].join('\n'),
29+
},
30+
},
31+
};
32+
33+
loadFixture(inputProject, codemodOptions);
34+
35+
assert.deepEqual(analyzeAddon(options), {
36+
addon: {
37+
appReexports: [],
38+
publicEntrypoints: [
39+
'test-support/components/container-query.js',
40+
'test-support/index.js',
41+
],
42+
},
43+
projectRoot: {
44+
devDependencies: {
45+
concurrently: '^7.6.0',
46+
prettier: '^2.8.1',
47+
},
48+
},
49+
});
50+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { moveAddonFiles } from '../../../../../src/migration/ember-addon/steps/index.js';
2+
import {
3+
codemodOptions,
4+
options,
5+
} from '../../../../helpers/shared-test-setups/typescript.js';
6+
import {
7+
assertFixture,
8+
loadFixture,
9+
test,
10+
} from '../../../../helpers/testing.js';
11+
12+
test('migration | ember-addon | steps | move-addon-files > blueprints and test-support', function () {
13+
const inputProject = {
14+
'addon-test-support': {
15+
components: {
16+
'container-query.ts': '',
17+
},
18+
'index.ts': `export * from './components/container-query';\n`,
19+
},
20+
blueprints: {
21+
'ember-container-query': {
22+
files: {
23+
'some-folder': {
24+
'some-file.ts': '',
25+
},
26+
},
27+
'index.ts': [
28+
`module.exports = {`,
29+
` normalizeEntityName() {},`,
30+
`};`,
31+
``,
32+
].join('\n'),
33+
},
34+
},
35+
};
36+
37+
const outputProject = {
38+
'ember-container-query': {
39+
blueprints: {
40+
'ember-container-query': {
41+
files: {
42+
'some-folder': {
43+
'some-file.ts': '',
44+
},
45+
},
46+
'index.ts': [
47+
`module.exports = {`,
48+
` normalizeEntityName() {},`,
49+
`};`,
50+
``,
51+
].join('\n'),
52+
},
53+
},
54+
src: {
55+
'test-support': {
56+
components: {
57+
'container-query.ts': '',
58+
},
59+
'index.ts': `export * from './components/container-query';\n`,
60+
},
61+
},
62+
},
63+
};
64+
65+
loadFixture(inputProject, codemodOptions);
66+
67+
moveAddonFiles(options);
68+
69+
assertFixture(outputProject, codemodOptions);
70+
});

tests/migration/ember-addon/steps/move-addon-files/other-files.test.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/utils/files/map-file-paths.test.js

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,73 @@
11
import { mapFilePaths } from '../../../src/utils/files.js';
22
import { assert, test } from '../../helpers/testing.js';
33

4-
test('utils | files | map-file-paths', function () {
5-
const filePaths = [
6-
'addon/components/container-query.hbs',
7-
'addon/components/container-query.ts',
8-
'addon/helpers/aspect-ratio.ts',
9-
'addon/helpers/height.ts',
10-
'addon/helpers/width.ts',
11-
'addon/modifiers/container-query.ts',
12-
'addon/styles/container-query.css',
13-
'addon/.gitkeep',
14-
'addon/index.ts',
15-
'addon/template-registry.ts',
16-
];
4+
test('utils | files | map-file-paths > base case', function () {
5+
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];
176

187
const pathMapping = mapFilePaths(filePaths, {
198
from: 'addon',
20-
to: 'ember-container-query/src',
9+
to: 'new-location/src',
2110
});
2211

2312
const expectedValue = new Map([
2413
[
25-
'addon/components/container-query.hbs',
26-
'ember-container-query/src/components/container-query.hbs',
14+
'addon/some-folder/some-file.ts',
15+
'new-location/src/some-folder/some-file.ts',
2716
],
17+
['addon/.gitkeep', 'new-location/src/.gitkeep'],
18+
]);
19+
20+
assert.deepStrictEqual(pathMapping, expectedValue);
21+
});
22+
23+
test('utils | files | map-file-paths > file paths are mapped from the project root', function () {
24+
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];
25+
26+
const pathMapping = mapFilePaths(filePaths, {
27+
from: '',
28+
to: 'new-location/src',
29+
});
30+
31+
const expectedValue = new Map([
2832
[
29-
'addon/components/container-query.ts',
30-
'ember-container-query/src/components/container-query.ts',
31-
],
32-
[
33-
'addon/helpers/aspect-ratio.ts',
34-
'ember-container-query/src/helpers/aspect-ratio.ts',
35-
],
36-
['addon/helpers/height.ts', 'ember-container-query/src/helpers/height.ts'],
37-
['addon/helpers/width.ts', 'ember-container-query/src/helpers/width.ts'],
38-
[
39-
'addon/modifiers/container-query.ts',
40-
'ember-container-query/src/modifiers/container-query.ts',
41-
],
42-
[
43-
'addon/styles/container-query.css',
44-
'ember-container-query/src/styles/container-query.css',
45-
],
46-
['addon/.gitkeep', 'ember-container-query/src/.gitkeep'],
47-
['addon/index.ts', 'ember-container-query/src/index.ts'],
48-
[
49-
'addon/template-registry.ts',
50-
'ember-container-query/src/template-registry.ts',
33+
'addon/some-folder/some-file.ts',
34+
'new-location/src/addon/some-folder/some-file.ts',
5135
],
36+
['addon/.gitkeep', 'new-location/src/addon/.gitkeep'],
37+
]);
38+
39+
assert.deepStrictEqual(pathMapping, expectedValue);
40+
});
41+
42+
test('utils | files | map-file-paths > file paths are mapped to the project root', function () {
43+
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];
44+
45+
const pathMapping = mapFilePaths(filePaths, {
46+
from: 'addon',
47+
to: '',
48+
});
49+
50+
const expectedValue = new Map([
51+
['addon/some-folder/some-file.ts', 'some-folder/some-file.ts'],
52+
['addon/.gitkeep', '.gitkeep'],
53+
]);
54+
55+
assert.deepStrictEqual(pathMapping, expectedValue);
56+
});
57+
58+
test('utils | files | map-file-paths > file paths remain when there is no match', function () {
59+
const filePaths = ['.addon/index.js', 'addon', 'addon.js', 'app/index.js'];
60+
61+
const pathMapping = mapFilePaths(filePaths, {
62+
from: 'addon',
63+
to: 'new-location/src',
64+
});
65+
66+
const expectedValue = new Map([
67+
['.addon/index.js', '.addon/index.js'],
68+
['addon', 'addon'],
69+
['addon.js', 'addon.js'],
70+
['app/index.js', 'app/index.js'],
5271
]);
5372

5473
assert.deepStrictEqual(pathMapping, expectedValue);

0 commit comments

Comments
 (0)