From dfc6db4e2576229d1f44d758ec224ac6ac8c3679 Mon Sep 17 00:00:00 2001 From: Isaac Lee <16869656+ijlee2@users.noreply.github.com> Date: Fri, 27 Jan 2023 10:18:12 +0100 Subject: [PATCH] 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 --- .../ember-addon/steps/analyze-addon.js | 35 +++++-- src/utils/files/map-file-paths.js | 23 +++-- .../blueprints-and-test-support.test.js | 50 ++++++++++ .../blueprints-and-test-support.test.js | 70 ++++++++++++++ .../move-addon-files/other-files.test.js | 40 -------- tests/utils/files/map-file-paths.test.js | 95 +++++++++++-------- 6 files changed, 218 insertions(+), 95 deletions(-) create mode 100644 tests/migration/ember-addon/steps/analyze-addon/blueprints-and-test-support.test.js create mode 100644 tests/migration/ember-addon/steps/move-addon-files/blueprints-and-test-support.test.js delete mode 100644 tests/migration/ember-addon/steps/move-addon-files/other-files.test.js diff --git a/src/migration/ember-addon/steps/analyze-addon.js b/src/migration/ember-addon/steps/analyze-addon.js index 12f2284e..2257e3c7 100644 --- a/src/migration/ember-addon/steps/analyze-addon.js +++ b/src/migration/ember-addon/steps/analyze-addon.js @@ -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) { @@ -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) { diff --git a/src/utils/files/map-file-paths.js b/src/utils/files/map-file-paths.js index cb7a2034..09102e32 100644 --- a/src/utils/files/map-file-paths.js +++ b/src/utils/files/map-file-paths.js @@ -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]; + }) ); } diff --git a/tests/migration/ember-addon/steps/analyze-addon/blueprints-and-test-support.test.js b/tests/migration/ember-addon/steps/analyze-addon/blueprints-and-test-support.test.js new file mode 100644 index 00000000..a7208f13 --- /dev/null +++ b/tests/migration/ember-addon/steps/analyze-addon/blueprints-and-test-support.test.js @@ -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', + }, + }, + }); +}); diff --git a/tests/migration/ember-addon/steps/move-addon-files/blueprints-and-test-support.test.js b/tests/migration/ember-addon/steps/move-addon-files/blueprints-and-test-support.test.js new file mode 100644 index 00000000..8971dae8 --- /dev/null +++ b/tests/migration/ember-addon/steps/move-addon-files/blueprints-and-test-support.test.js @@ -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); +}); diff --git a/tests/migration/ember-addon/steps/move-addon-files/other-files.test.js b/tests/migration/ember-addon/steps/move-addon-files/other-files.test.js deleted file mode 100644 index e1018608..00000000 --- a/tests/migration/ember-addon/steps/move-addon-files/other-files.test.js +++ /dev/null @@ -1,40 +0,0 @@ -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 > other files', function () { - const inputProject = { - 'addon-test-support': { - '.gitkeep': '', - }, - blueprints: { - '.gitkeep': '', - }, - }; - - const outputProject = { - 'ember-container-query': { - blueprints: { - '.gitkeep': '', - }, - src: { - 'test-support': { - '.gitkeep': '', - }, - }, - }, - }; - - loadFixture(inputProject, codemodOptions); - - moveAddonFiles(options); - - assertFixture(outputProject, codemodOptions); -}); diff --git a/tests/utils/files/map-file-paths.test.js b/tests/utils/files/map-file-paths.test.js index 1da71249..52210101 100644 --- a/tests/utils/files/map-file-paths.test.js +++ b/tests/utils/files/map-file-paths.test.js @@ -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);