Skip to content

Commit

Permalink
Added files from addon-test-support to addon.publicEntrypoints() (#11)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
ijlee2 and ijlee2 authored Jan 27, 2023
1 parent eb68f1f commit dfc6db4
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 95 deletions.
35 changes: 27 additions & 8 deletions src/migration/ember-addon/steps/analyze-addon.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { join } from 'node:path';

import glob from 'glob';

import { decideVersion } from '../../../utils/blueprints.js';
import { renameDirectory } from '../../../utils/files.js';

function getAppReexports(options) {
const { projectRoot } = options;

const filePaths = glob.sync('**/*.js', {
cwd: join(projectRoot, 'app'),
const filePaths = glob.sync('app/**/*.js', {
cwd: projectRoot,
});

return filePaths;
return filePaths.map((filePath) => {
return renameDirectory(filePath, {
from: 'app',
to: '',
});
});
}

function getProjectRootDevDependencies(options) {
Expand All @@ -24,11 +28,26 @@ function getProjectRootDevDependencies(options) {
function getPublicEntrypoints(options) {
const { projectRoot } = options;

const filePaths = glob.sync('**/*.{js,ts}', {
cwd: join(projectRoot, 'addon'),
const filePaths = glob.sync('{addon,addon-test-support}/**/*.{js,ts}', {
cwd: projectRoot,
});

return filePaths.map((filePath) => filePath.replace(/ts$/, 'js'));
return filePaths
.map((filePath) => {
return renameDirectory(filePath, {
from: 'addon',
to: '',
});
})
.map((filePath) => {
return renameDirectory(filePath, {
from: 'addon-test-support',
to: 'test-support',
});
})
.map((filePath) => {
return filePath.replace(/ts$/, 'js');
});
}

export function analyzeAddon(options) {
Expand Down
23 changes: 14 additions & 9 deletions src/utils/files/map-file-paths.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
function changeDirectory(oldPath, from, to) {
if (!oldPath.startsWith(from)) {
throw new RangeError(
`The provided path \`${oldPath}\` does not start with \`${from}\`.`
);
import { join } from 'node:path';

export function renameDirectory(oldPath, { from, to }) {
if (from === '') {
return join(to, oldPath);
}

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

return [oldPath, newPath];
return join(to, oldPath.replace(`${from}/`, ''));
}

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

return new Map(
filePaths.map((filePath) => changeDirectory(filePath, from, to))
filePaths.map((filePath) => {
const newPath = renameDirectory(filePath, { from, to });

return [filePath, newPath];
})
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { analyzeAddon } from '../../../../../src/migration/ember-addon/steps/index.js';
import {
codemodOptions,
options,
} from '../../../../helpers/shared-test-setups/typescript.js';
import { assert, loadFixture, test } from '../../../../helpers/testing.js';

test('migration | ember-addon | steps | analyze-addon > blueprints and test-support', function () {
const inputProject = {
'addon-test-support': {
components: {
'container-query.ts': '',
},
'index.ts': `export * from './components/container-query';\n`,
},
blueprints: {
'ember-container-query': {
files: {
'some-folder': {
'some-file.ts': '',
},
},
'index.ts': [
`module.exports = {`,
` normalizeEntityName() {},`,
`};`,
``,
].join('\n'),
},
},
};

loadFixture(inputProject, codemodOptions);

assert.deepEqual(analyzeAddon(options), {
addon: {
appReexports: [],
publicEntrypoints: [
'test-support/components/container-query.js',
'test-support/index.js',
],
},
projectRoot: {
devDependencies: {
concurrently: '^7.6.0',
prettier: '^2.8.1',
},
},
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { moveAddonFiles } from '../../../../../src/migration/ember-addon/steps/index.js';
import {
codemodOptions,
options,
} from '../../../../helpers/shared-test-setups/typescript.js';
import {
assertFixture,
loadFixture,
test,
} from '../../../../helpers/testing.js';

test('migration | ember-addon | steps | move-addon-files > blueprints and test-support', function () {
const inputProject = {
'addon-test-support': {
components: {
'container-query.ts': '',
},
'index.ts': `export * from './components/container-query';\n`,
},
blueprints: {
'ember-container-query': {
files: {
'some-folder': {
'some-file.ts': '',
},
},
'index.ts': [
`module.exports = {`,
` normalizeEntityName() {},`,
`};`,
``,
].join('\n'),
},
},
};

const outputProject = {
'ember-container-query': {
blueprints: {
'ember-container-query': {
files: {
'some-folder': {
'some-file.ts': '',
},
},
'index.ts': [
`module.exports = {`,
` normalizeEntityName() {},`,
`};`,
``,
].join('\n'),
},
},
src: {
'test-support': {
components: {
'container-query.ts': '',
},
'index.ts': `export * from './components/container-query';\n`,
},
},
},
};

loadFixture(inputProject, codemodOptions);

moveAddonFiles(options);

assertFixture(outputProject, codemodOptions);
});

This file was deleted.

95 changes: 57 additions & 38 deletions tests/utils/files/map-file-paths.test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,73 @@
import { mapFilePaths } from '../../../src/utils/files.js';
import { assert, test } from '../../helpers/testing.js';

test('utils | files | map-file-paths', function () {
const filePaths = [
'addon/components/container-query.hbs',
'addon/components/container-query.ts',
'addon/helpers/aspect-ratio.ts',
'addon/helpers/height.ts',
'addon/helpers/width.ts',
'addon/modifiers/container-query.ts',
'addon/styles/container-query.css',
'addon/.gitkeep',
'addon/index.ts',
'addon/template-registry.ts',
];
test('utils | files | map-file-paths > base case', function () {
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];

const pathMapping = mapFilePaths(filePaths, {
from: 'addon',
to: 'ember-container-query/src',
to: 'new-location/src',
});

const expectedValue = new Map([
[
'addon/components/container-query.hbs',
'ember-container-query/src/components/container-query.hbs',
'addon/some-folder/some-file.ts',
'new-location/src/some-folder/some-file.ts',
],
['addon/.gitkeep', 'new-location/src/.gitkeep'],
]);

assert.deepStrictEqual(pathMapping, expectedValue);
});

test('utils | files | map-file-paths > file paths are mapped from the project root', function () {
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];

const pathMapping = mapFilePaths(filePaths, {
from: '',
to: 'new-location/src',
});

const expectedValue = new Map([
[
'addon/components/container-query.ts',
'ember-container-query/src/components/container-query.ts',
],
[
'addon/helpers/aspect-ratio.ts',
'ember-container-query/src/helpers/aspect-ratio.ts',
],
['addon/helpers/height.ts', 'ember-container-query/src/helpers/height.ts'],
['addon/helpers/width.ts', 'ember-container-query/src/helpers/width.ts'],
[
'addon/modifiers/container-query.ts',
'ember-container-query/src/modifiers/container-query.ts',
],
[
'addon/styles/container-query.css',
'ember-container-query/src/styles/container-query.css',
],
['addon/.gitkeep', 'ember-container-query/src/.gitkeep'],
['addon/index.ts', 'ember-container-query/src/index.ts'],
[
'addon/template-registry.ts',
'ember-container-query/src/template-registry.ts',
'addon/some-folder/some-file.ts',
'new-location/src/addon/some-folder/some-file.ts',
],
['addon/.gitkeep', 'new-location/src/addon/.gitkeep'],
]);

assert.deepStrictEqual(pathMapping, expectedValue);
});

test('utils | files | map-file-paths > file paths are mapped to the project root', function () {
const filePaths = ['addon/some-folder/some-file.ts', 'addon/.gitkeep'];

const pathMapping = mapFilePaths(filePaths, {
from: 'addon',
to: '',
});

const expectedValue = new Map([
['addon/some-folder/some-file.ts', 'some-folder/some-file.ts'],
['addon/.gitkeep', '.gitkeep'],
]);

assert.deepStrictEqual(pathMapping, expectedValue);
});

test('utils | files | map-file-paths > file paths remain when there is no match', function () {
const filePaths = ['.addon/index.js', 'addon', 'addon.js', 'app/index.js'];

const pathMapping = mapFilePaths(filePaths, {
from: 'addon',
to: 'new-location/src',
});

const expectedValue = new Map([
['.addon/index.js', '.addon/index.js'],
['addon', 'addon'],
['addon.js', 'addon.js'],
['app/index.js', 'app/index.js'],
]);

assert.deepStrictEqual(pathMapping, expectedValue);
Expand Down

0 comments on commit dfc6db4

Please sign in to comment.