From 4718d3aaf9f1beada9b235d6753783128d270cbf Mon Sep 17 00:00:00 2001 From: ijlee2 Date: Tue, 26 Dec 2023 18:18:37 +0100 Subject: [PATCH 1/4] bugfix: Removed unused keys --- tests/steps/update-addon-package-json/public-assets.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/steps/update-addon-package-json/public-assets.test.ts b/tests/steps/update-addon-package-json/public-assets.test.ts index 8e297900..c4e7847e 100644 --- a/tests/steps/update-addon-package-json/public-assets.test.ts +++ b/tests/steps/update-addon-package-json/public-assets.test.ts @@ -6,6 +6,7 @@ import { } from '@codemod-utils/tests'; import { updateAddonPackageJson } from '../../../src/steps/index.js'; +import type { Context } from '../../../src/types/index.js'; import { codemodOptions, options, @@ -20,14 +21,12 @@ test('steps | update-addon-package-json > public-assets', function () { 'steps/update-addon-package-json/public-assets/output', ); - const context = { + const context: Context = { addon: { - appReexports: [], publicAssets: [ 'assets/documents/some-file.pdf', 'assets/images/v1/some-file.svg', ], - publicEntrypoints: [], }, projectRoot: { devDependencies: { From bf8f415a956b3d22be382d1f5201cf700874d7f8 Mon Sep 17 00:00:00 2001 From: ijlee2 Date: Tue, 26 Dec 2023 18:31:38 +0100 Subject: [PATCH 2/4] chore: Removed unnecessary sorting --- src/steps/analyze-addon.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/steps/analyze-addon.ts b/src/steps/analyze-addon.ts index c1a43d21..98bda787 100644 --- a/src/steps/analyze-addon.ts +++ b/src/steps/analyze-addon.ts @@ -18,14 +18,12 @@ function getPublicAssets(options: Options): string[] { projectRoot, }); - return filePaths - .map((filePath) => { - return renamePathByDirectory(filePath, { - from: 'public', - to: '', - }); - }) - .sort(); + return filePaths.map((filePath) => { + return renamePathByDirectory(filePath, { + from: 'public', + to: '', + }); + }); } export function analyzeAddon(options: Options): Context { From 4feaea4020f53f2da1959556fa36095c09acad32 Mon Sep 17 00:00:00 2001 From: ijlee2 Date: Tue, 26 Dec 2023 18:49:54 +0100 Subject: [PATCH 3/4] refactor: Allowed the analyze-addon step to define ember-addon.public-assets --- src/steps/analyze-addon.ts | 23 ++++++++++------- .../update-other-fields.ts | 25 ++++++------------- src/types/index.ts | 2 +- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/steps/analyze-addon.ts b/src/steps/analyze-addon.ts index 98bda787..396b401b 100644 --- a/src/steps/analyze-addon.ts +++ b/src/steps/analyze-addon.ts @@ -1,4 +1,4 @@ -import { findFiles, renamePathByDirectory } from '@codemod-utils/files'; +import { findFiles } from '@codemod-utils/files'; import type { Context, Options } from '../types/index.js'; import { getVersion } from '../utils/blueprints.js'; @@ -11,19 +11,24 @@ function getProjectRootDevDependencies( }; } -function getPublicAssets(options: Options): string[] { - const { projectRoot } = options; +function getPublicAssets(options: Options): Record { + const { packages, projectRoot } = options; const filePaths = findFiles('public/**/*', { projectRoot, }); - return filePaths.map((filePath) => { - return renamePathByDirectory(filePath, { - from: 'public', - to: '', - }); - }); + return filePaths.reduce( + (accumulator, filePath) => { + const from = `./${filePath}`; + const to = `/${packages.addon.name}/${filePath.replace(/^public\//, '')}`; + + accumulator[from] = to; + + return accumulator; + }, + {} as Record, + ); } export function analyzeAddon(options: Options): Context { diff --git a/src/steps/update-addon-package-json/update-other-fields.ts b/src/steps/update-addon-package-json/update-other-fields.ts index 43abc77d..1f5aae87 100644 --- a/src/steps/update-addon-package-json/update-other-fields.ts +++ b/src/steps/update-addon-package-json/update-other-fields.ts @@ -3,16 +3,17 @@ import type { Context, Options, PackageJson } from '../../types/index.js'; type Data = { hasPublicAssets: boolean; hasTypeScript: boolean; - publicAssetMapping: Record; + publicAssets: Record; }; function updateEmberAddon(packageJson: PackageJson, data: Data): void { - const { hasPublicAssets, publicAssetMapping } = data; + const { hasPublicAssets, publicAssets } = data; - if (!hasPublicAssets) { + if (hasPublicAssets) { packageJson['ember-addon'] = { 'app-js': {}, main: 'addon-main.cjs', + 'public-assets': publicAssets, type: 'addon', version: 2, }; @@ -23,7 +24,6 @@ function updateEmberAddon(packageJson: PackageJson, data: Data): void { packageJson['ember-addon'] = { 'app-js': {}, main: 'addon-main.cjs', - 'public-assets': publicAssetMapping, type: 'addon', version: 2, }; @@ -93,26 +93,15 @@ export function updateOtherFields( context: Context, options: Options, ): void { + const { addon } = context; const { packages } = options; - const publicAssetMapping = context.addon.publicAssets.reduce( - (accumulator, filePath) => { - const from = `./public/${filePath}`; - const to = `/${packages.addon.name}/${filePath}`; - - accumulator[from] = to; - - return accumulator; - }, - {} as Record, - ); - - const hasPublicAssets = Object.keys(publicAssetMapping).length > 0; + const hasPublicAssets = Object.keys(addon.publicAssets).length > 0; const data = { hasPublicAssets, hasTypeScript: packages.addon.hasTypeScript, - publicAssetMapping, + publicAssets: addon.publicAssets, }; updateEmberAddon(packageJson, data); diff --git a/src/types/index.ts b/src/types/index.ts index 72d23bc2..05ca2267 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -9,7 +9,7 @@ type CodemodOptions = { type Context = { addon: { - publicAssets: string[]; + publicAssets: Record; }; projectRoot: { devDependencies: Record; From d656ea6a7fac7140f5892ddef42336f09b78d7d0 Mon Sep 17 00:00:00 2001 From: ijlee2 Date: Tue, 26 Dec 2023 18:50:09 +0100 Subject: [PATCH 4/4] chore: Updated test setups --- tests/helpers/shared-test-setups/customizations.ts | 2 +- tests/helpers/shared-test-setups/glint.ts | 2 +- tests/helpers/shared-test-setups/javascript.ts | 2 +- tests/helpers/shared-test-setups/scoped.ts | 2 +- tests/helpers/shared-test-setups/typescript.ts | 2 +- tests/steps/analyze-addon/blueprints.test.ts | 2 +- .../edge-case-folders-are-missing.test.ts | 2 +- tests/steps/analyze-addon/public-assets.test.ts | 10 ++++++---- tests/steps/analyze-addon/test-support.test.ts | 2 +- .../update-addon-package-json/public-assets.test.ts | 10 ++++++---- 10 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/helpers/shared-test-setups/customizations.ts b/tests/helpers/shared-test-setups/customizations.ts index 86d7dbb2..769fdd53 100644 --- a/tests/helpers/shared-test-setups/customizations.ts +++ b/tests/helpers/shared-test-setups/customizations.ts @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = { const context: Context = { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/helpers/shared-test-setups/glint.ts b/tests/helpers/shared-test-setups/glint.ts index 6d52f3f9..942e4c30 100644 --- a/tests/helpers/shared-test-setups/glint.ts +++ b/tests/helpers/shared-test-setups/glint.ts @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = { const context: Context = { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/helpers/shared-test-setups/javascript.ts b/tests/helpers/shared-test-setups/javascript.ts index 7b0a9f37..302ac645 100644 --- a/tests/helpers/shared-test-setups/javascript.ts +++ b/tests/helpers/shared-test-setups/javascript.ts @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = { const context: Context = { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/helpers/shared-test-setups/scoped.ts b/tests/helpers/shared-test-setups/scoped.ts index c6701422..c2093cc0 100644 --- a/tests/helpers/shared-test-setups/scoped.ts +++ b/tests/helpers/shared-test-setups/scoped.ts @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = { const context: Context = { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/helpers/shared-test-setups/typescript.ts b/tests/helpers/shared-test-setups/typescript.ts index 90b674c5..0c1b5301 100644 --- a/tests/helpers/shared-test-setups/typescript.ts +++ b/tests/helpers/shared-test-setups/typescript.ts @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = { const context: Context = { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/steps/analyze-addon/blueprints.test.ts b/tests/steps/analyze-addon/blueprints.test.ts index 37837322..541d09fd 100644 --- a/tests/steps/analyze-addon/blueprints.test.ts +++ b/tests/steps/analyze-addon/blueprints.test.ts @@ -29,7 +29,7 @@ test('steps | analyze-addon > blueprints', function () { assert.deepStrictEqual(analyzeAddon(options), { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/steps/analyze-addon/edge-case-folders-are-missing.test.ts b/tests/steps/analyze-addon/edge-case-folders-are-missing.test.ts index 18cc6392..4016f1a0 100644 --- a/tests/steps/analyze-addon/edge-case-folders-are-missing.test.ts +++ b/tests/steps/analyze-addon/edge-case-folders-are-missing.test.ts @@ -13,7 +13,7 @@ test('steps | analyze-addon > edge case (folders are missing)', function () { assert.deepStrictEqual(analyzeAddon(options), { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/steps/analyze-addon/public-assets.test.ts b/tests/steps/analyze-addon/public-assets.test.ts index 3b2096d3..81410467 100644 --- a/tests/steps/analyze-addon/public-assets.test.ts +++ b/tests/steps/analyze-addon/public-assets.test.ts @@ -26,10 +26,12 @@ test('steps | analyze-addon > public-assets', function () { assert.deepStrictEqual(analyzeAddon(options), { addon: { - publicAssets: [ - 'assets/documents/some-file.pdf', - 'assets/images/v1/some-file.svg', - ], + publicAssets: { + './public/assets/documents/some-file.pdf': + '/ember-container-query/assets/documents/some-file.pdf', + './public/assets/images/v1/some-file.svg': + '/ember-container-query/assets/images/v1/some-file.svg', + }, }, projectRoot: { devDependencies: { diff --git a/tests/steps/analyze-addon/test-support.test.ts b/tests/steps/analyze-addon/test-support.test.ts index fa6dff86..199deb69 100644 --- a/tests/steps/analyze-addon/test-support.test.ts +++ b/tests/steps/analyze-addon/test-support.test.ts @@ -20,7 +20,7 @@ test('steps | analyze-addon > test-support', function () { assert.deepStrictEqual(analyzeAddon(options), { addon: { - publicAssets: [], + publicAssets: {}, }, projectRoot: { devDependencies: { diff --git a/tests/steps/update-addon-package-json/public-assets.test.ts b/tests/steps/update-addon-package-json/public-assets.test.ts index c4e7847e..56d35a2b 100644 --- a/tests/steps/update-addon-package-json/public-assets.test.ts +++ b/tests/steps/update-addon-package-json/public-assets.test.ts @@ -23,10 +23,12 @@ test('steps | update-addon-package-json > public-assets', function () { const context: Context = { addon: { - publicAssets: [ - 'assets/documents/some-file.pdf', - 'assets/images/v1/some-file.svg', - ], + publicAssets: { + './public/assets/documents/some-file.pdf': + '/ember-container-query/assets/documents/some-file.pdf', + './public/assets/images/v1/some-file.svg': + '/ember-container-query/assets/images/v1/some-file.svg', + }, }, projectRoot: { devDependencies: {