Skip to content

Commit

Permalink
Merge pull request #7 from IgnaceMaes/extract-more-steps
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnaceMaes authored Nov 5, 2023
2 parents 4b2c033 + 143da6d commit c9e68e4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-suns-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ember-codemod-template-tag": patch
---

refactor: extract change extension step
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { changeExtension } from './steps/change-extension.js';
import { convertTests, createOptions } from './steps/index.js';
import { removeHbsImport } from './steps/remove-import.js';
import type { CodemodOptions } from './types/index.js';
Expand All @@ -6,5 +7,6 @@ export function runCodemod(codemodOptions: CodemodOptions): void {
const options = createOptions(codemodOptions);

convertTests(options);
changeExtension(options);
removeHbsImport(options);
}
34 changes: 34 additions & 0 deletions src/steps/change-extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readFileSync, renameSync } from 'node:fs';
import { join } from 'node:path';

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

import type { Options } from '../types/index.js';
import { parse } from '../utils/ast/template-tag.js';
import { replaceExtensionWithGlimmer } from '../utils/general.js';

function isUsingTemplateTag(file: string): boolean {
const contentTags = parse(file);

return contentTags.length > 0;
}

export function changeExtension(options: Options): void {
const { projectRoot } = options;

const filePaths = findFiles('**/*-test.{js,ts}', {
projectRoot,
});

filePaths.forEach((filePath) => {
const file = readFileSync(join(projectRoot, filePath), 'utf8');

const isTemplateTag = isUsingTemplateTag(file);

if (isTemplateTag) {
// Move file to new extension
const newFilePath = replaceExtensionWithGlimmer(filePath);
renameSync(join(projectRoot, filePath), join(projectRoot, newFilePath));
}
});
}
13 changes: 3 additions & 10 deletions src/steps/convert-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync, renameSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { AST as AST_JS } from '@codemod-utils/ast-javascript';
Expand All @@ -11,10 +11,7 @@ import {
getComponentNameFromNestedPath,
} from '../utils/components.js';
import { BUILT_IN_HELPERS } from '../utils/constants.js';
import {
isTypeScriptFile,
replaceExtensionWithGlimmer,
} from '../utils/general.js';
import { isTypeScriptFile } from '../utils/general.js';

function rewriteHbsTemplateString(
file: string,
Expand Down Expand Up @@ -149,17 +146,13 @@ export function convertTests(options: Options): void {
filePaths.map((filePath) => {
let file = readFileSync(join(projectRoot, filePath), 'utf8');

// Move file to new extension
const newFilePath = replaceExtensionWithGlimmer(filePath);
renameSync(join(projectRoot, filePath), join(projectRoot, newFilePath));

// Replace hbs`` template string with a <template> tag
file = rewriteHbsTemplateString(file, {
appName,
isTypeScript: isTypeScriptFile(filePath),
});

return [newFilePath, file];
return [filePath, file];
}),
);

Expand Down
27 changes: 27 additions & 0 deletions tests/steps/change-extension/base-case.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assertFixture, loadFixture, test } from '@codemod-utils/tests';

import { changeExtension } from '../../../src/steps/change-extension.js';
import {
codemodOptions,
options,
} from '../../helpers/shared-test-setups/convert-tests.js';

test('steps | change-extension > base case', function () {
const inputProject = {
'example-test.js': "const foo = 'bar';\n<template>Test</template>\n",
'no-template-tag-test.js': "const foo = 'bar';\nhbs`<foo>Bar</foo>`\n",
'ts-test.ts': "const foo: string = 'bar';\n<template>Test</template>\n",
};

const outputProject = {
'example-test.gjs': "const foo = 'bar';\n<template>Test</template>\n",
'no-template-tag-test.js': "const foo = 'bar';\nhbs`<foo>Bar</foo>`\n",
'ts-test.gts': "const foo: string = 'bar';\n<template>Test</template>\n",
};

loadFixture(inputProject, codemodOptions);

changeExtension(options);

assertFixture(outputProject, codemodOptions);
});

0 comments on commit c9e68e4

Please sign in to comment.