Skip to content

Commit

Permalink
Removed duplicated code (#1)
Browse files Browse the repository at this point in the history
* refactor: Allowed createOptions() to define src

* refactor: Simplified the folder structure

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Apr 24, 2024
1 parent dc0cbdf commit a887494
Show file tree
Hide file tree
Showing 21 changed files with 76 additions and 103 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-emus-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ember-codemod-remove-inject-as-service": patch
---

Removed duplicated code
23 changes: 3 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
import {
migrateEmberApp,
migrateEmberV1Addon,
migrateEmberV2Addon,
} from './migration/index.js';
import { createOptions, updateProject } from './steps/index.js';
import type { CodemodOptions } from './types/index.js';

export function runCodemod(codemodOptions: CodemodOptions): void {
switch (codemodOptions.projectType) {
case 'app': {
migrateEmberApp(codemodOptions);
break;
}
const options = createOptions(codemodOptions);

case 'v1-addon': {
migrateEmberV1Addon(codemodOptions);
break;
}

case 'v2-addon': {
migrateEmberV2Addon(codemodOptions);
break;
}
}
updateProject(options);
}
9 changes: 0 additions & 9 deletions src/migration/ember-app/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/migration/ember-v1-addon/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/migration/ember-v2-addon/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/migration/index.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/steps/create-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { CodemodOptions, Options } from '../types/index.js';

function getSrc(projectType: CodemodOptions['projectType']): string[] {
switch (projectType) {
case 'app': {
return ['app/**/*.{js,ts}'];
}

case 'v1-addon': {
return ['addon/**/*.{js,ts}', 'tests/dummy/app/**/*.{js,ts}'];
}

case 'v2-addon': {
return ['src/**/*.{js,ts}'];
}
}
}

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

return {
projectRoot,
src: getSrc(projectType),
};
}
2 changes: 2 additions & 0 deletions src/steps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './create-options.js';
export * from './update-project.js';
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { join } from 'node:path';

import { findFiles } from '@codemod-utils/files';

import { Options } from '../../types/index.js';
import { Options } from '../types/index.js';
import { updateClass } from './update-project/update-class.js';

export function updateProject(src: string[], options: Options): void {
const { projectRoot } = options;
export function updateProject(options: Options): void {
const { projectRoot, src } = options;

const filePaths = findFiles(src, {
projectRoot,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type CodemodOptions = {

type Options = {
projectRoot: string;
projectType: 'app' | 'v1-addon' | 'v2-addon';
src: string[];
};

export type { CodemodOptions, Options };
10 changes: 0 additions & 10 deletions src/utils/steps/create-options.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/my-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const codemodOptions: CodemodOptions = {

const options: Options = {
projectRoot: 'tmp/my-app',
projectType: 'app',
src: ['app/**/*.{js,ts}'],
};

export { codemodOptions, options };
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/my-v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const codemodOptions: CodemodOptions = {

const options: Options = {
projectRoot: 'tmp/my-v1-addon',
projectType: 'v1-addon',
src: ['addon/**/*.{js,ts}', 'tests/dummy/app/**/*.{js,ts}'],
};

export { codemodOptions, options };
2 changes: 1 addition & 1 deletion tests/helpers/shared-test-setups/my-v2-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const codemodOptions: CodemodOptions = {

const options: Options = {
projectRoot: 'tmp/my-v2-addon',
projectType: 'v2-addon',
src: ['src/**/*.{js,ts}'],
};

export { codemodOptions, options };
11 changes: 11 additions & 0 deletions tests/steps/create-options/my-app.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { createOptions } from '../../../src/steps/index.js';
import {
codemodOptions,
options,
} from '../../helpers/shared-test-setups/my-app.js';

test('steps | create-options > my-app', function () {
assert.deepStrictEqual(createOptions(codemodOptions), options);
});
11 changes: 11 additions & 0 deletions tests/steps/create-options/my-v1-addon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { createOptions } from '../../../src/steps/index.js';
import {
codemodOptions,
options,
} from '../../helpers/shared-test-setups/my-v1-addon.js';

test('steps | create-options > my-v1-addon', function () {
assert.deepStrictEqual(createOptions(codemodOptions), options);
});
11 changes: 11 additions & 0 deletions tests/steps/create-options/my-v2-addon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert, test } from '@codemod-utils/tests';

import { createOptions } from '../../../src/steps/index.js';
import {
codemodOptions,
options,
} from '../../helpers/shared-test-setups/my-v2-addon.js';

test('steps | create-options > my-v2-addon', function () {
assert.deepStrictEqual(createOptions(codemodOptions), options);
});
11 changes: 0 additions & 11 deletions tests/utils/steps/create-options/my-app.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions tests/utils/steps/create-options/my-v1-addon.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions tests/utils/steps/create-options/my-v2-addon.test.ts

This file was deleted.

0 comments on commit a887494

Please sign in to comment.