Skip to content

Commit

Permalink
Allowed specifying the component structure (#32)
Browse files Browse the repository at this point in the history
* feature: Normalized the entity name for components with nested structure

* feature: Allowed specifying the component structure

* chore: Updated README

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 29, 2024
1 parent 5c65846 commit 812b84e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-pans-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"analyze-ember-project-dependencies": minor
---

Allowed specifying the component structure
14 changes: 13 additions & 1 deletion packages/analyze-ember-project-dependencies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ Step 2. Check the output for true positives.

<details>

<summary>Optional: Specify the component structure</summary>

By default, apps and addons follow the flat component structure for components. Pass `--component-structure` to indicate otherwise.

```sh
npx analyze-ember-project-dependencies --component-structure nested
```

</details>

<details>

<summary>Optional: Specify the project root</summary>

Pass `--root` to run the codemod somewhere else (i.e. not in the current directory).

```sh
npx ember-codemod-remove-inject-as-service --root <path/to/your/project>
npx analyze-ember-project-dependencies --root <path/to/your/project>
```

</details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ process.title = 'analyze-ember-project-dependencies';

// Set codemod options
const argv = yargs(hideBin(process.argv))
.option('component-structure', {
choices: ['flat', 'nested'] as const,
describe: 'Component structure (how your components are colocated)',
type: 'string',
})
.option('root', {
describe: 'Where to run the codemod',
type: 'string',
})
.parseSync();

const codemodOptions: CodemodOptions = {
componentStructure: argv['component-structure'] ?? 'flat',
projectRoot: argv['root'] ?? process.cwd(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export function analyzeProject(options: Options) {
const { dependencies, devDependencies, packageType } =
analyzePackageJson(packageJson);

const entities = analyzeEntities({
packageRoot,
packageType,
});
const entities = analyzeEntities(packageRoot, packageType, options);

projectData.set(packageJson['name'], {
dependencies,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { doubleColonize } from '@codemod-utils/ember';
import { findFiles } from '@codemod-utils/files';

import type { PackageType, ProjectDataEntities } from '../../types/index.js';
import type {
Options,
PackageType,
ProjectDataEntities,
} from '../../types/index.js';

const SOURCE = {
app: 'app',
Expand All @@ -17,12 +21,11 @@ const ENTITY_TYPES = [
'services',
] as const;

export function analyzeEntities(options: {
packageRoot: string;
packageType: PackageType;
}): ProjectDataEntities {
const { packageRoot, packageType } = options;

export function analyzeEntities(
packageRoot: string,
packageType: PackageType,
options: Options,
): ProjectDataEntities {
const source = SOURCE[packageType];

const entities: ProjectDataEntities = {
Expand All @@ -37,6 +40,8 @@ export function analyzeEntities(options: {
return entities;
}

const { componentStructure } = options;

ENTITY_TYPES.forEach((entityType) => {
const filePaths = findFiles(
`${source}/${entityType}/**/*.{gjs,gts,hbs,js,ts}`,
Expand All @@ -55,6 +60,12 @@ export function analyzeEntities(options: {
if (entityType === 'components') {
entityNames = Array.from(new Set(entityNames));

if (componentStructure === 'nested') {
entityNames = entityNames.map((entityName) => {
return entityName.replace(/\/index$/, '');
});
}

entities['components'] = entityNames;
entities['componentsDoubleColonized'] = entityNames.map(doubleColonize);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { CodemodOptions, Options } from '../types/index.js';

export function createOptions(codemodOptions: CodemodOptions): Options {
const { projectRoot } = codemodOptions;
const { componentStructure, projectRoot } = codemodOptions;

return {
componentStructure,
projectRoot,
};
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
type CodemodOptions = {
componentStructure: 'flat' | 'nested';
projectRoot: string;
};

type Options = {
componentStructure: 'flat' | 'nested';
projectRoot: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { analyzeEmberProjectDependencies } from '../../src/index.js';
import { inputProject, outputProject } from '../fixtures/my-project/index.js';

const codemodOptions = {
componentStructure: 'flat' as const,
projectRoot: 'tmp/my-project',
};

Expand Down

0 comments on commit 812b84e

Please sign in to comment.