Skip to content

Commit

Permalink
Allowed the analyze-addon step to define ember-addon.public-assets (#73)
Browse files Browse the repository at this point in the history
* bugfix: Removed unused keys

* chore: Removed unnecessary sorting

* refactor: Allowed the analyze-addon step to define ember-addon.public-assets

* chore: Updated test setups

---------

Co-authored-by: ijlee2 <[email protected]>
  • Loading branch information
ijlee2 and ijlee2 authored Dec 26, 2023
1 parent 626298e commit 7175c1f
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 49 deletions.
25 changes: 14 additions & 11 deletions src/steps/analyze-addon.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,21 +11,24 @@ function getProjectRootDevDependencies(
};
}

function getPublicAssets(options: Options): string[] {
const { projectRoot } = options;
function getPublicAssets(options: Options): Record<string, string> {
const { packages, projectRoot } = options;

const filePaths = findFiles('public/**/*', {
projectRoot,
});

return filePaths
.map((filePath) => {
return renamePathByDirectory(filePath, {
from: 'public',
to: '',
});
})
.sort();
return filePaths.reduce(
(accumulator, filePath) => {
const from = `./${filePath}`;
const to = `/${packages.addon.name}/${filePath.replace(/^public\//, '')}`;

accumulator[from] = to;

return accumulator;
},
{} as Record<string, string>,
);
}

export function analyzeAddon(options: Options): Context {
Expand Down
25 changes: 7 additions & 18 deletions src/steps/update-addon-package-json/update-other-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import type { Context, Options, PackageJson } from '../../types/index.js';
type Data = {
hasPublicAssets: boolean;
hasTypeScript: boolean;
publicAssetMapping: Record<string, string>;
publicAssets: Record<string, string>;
};

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,
};
Expand All @@ -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,
};
Expand Down Expand Up @@ -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<string, string>,
);

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);
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CodemodOptions = {

type Context = {
addon: {
publicAssets: string[];
publicAssets: Record<string, string>;
};
projectRoot: {
devDependencies: Record<string, string>;
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/customizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = {

const context: Context = {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/glint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = {

const context: Context = {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = {

const context: Context = {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/scoped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = {

const context: Context = {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const codemodOptions: CodemodOptions = {

const context: Context = {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/analyze-addon/blueprints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('steps | analyze-addon > blueprints', function () {

assert.deepStrictEqual(analyzeAddon(options), {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('steps | analyze-addon > edge case (folders are missing)', function () {

assert.deepStrictEqual(analyzeAddon(options), {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
10 changes: 6 additions & 4 deletions tests/steps/analyze-addon/public-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/analyze-addon/test-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('steps | analyze-addon > test-support', function () {

assert.deepStrictEqual(analyzeAddon(options), {
addon: {
publicAssets: [],
publicAssets: {},
},
projectRoot: {
devDependencies: {
Expand Down
15 changes: 8 additions & 7 deletions tests/steps/update-addon-package-json/public-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,14 +21,14 @@ 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: [],
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: {
Expand Down

0 comments on commit 7175c1f

Please sign in to comment.