Skip to content

Commit

Permalink
Implemented generate commands (Part 2) (#7)
Browse files Browse the repository at this point in the history
* chore: Added fixtures

* feature: Guaranteed idempotency

* refactor: Defined variations of entity name in create-options

* feature: Updated barrel file

* feature: Updated template registry

* bugfix, feature: Created test file

* chore: Updated fixtures

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 15, 2024
1 parent 387368b commit cd6d3de
Show file tree
Hide file tree
Showing 71 changed files with 558 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-numbers-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"blueprints-addon": minor
---

Implemented generate commands (Part 2)
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { <%= data.entity.classifiedName %> } from '<%! options.package.name %>';
import { <%= options.entity.classifiedName %> } from '<%= options.addon.name %>';
import { render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Component | <%= data.entity.name %>', function (hooks) {
module('Integration | Component | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
await render(<template>
<<%= data.entity.classifiedName %> />
<<%= options.entity.classifiedName %> />
</template>);

assert.dom().hasText('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Component | <%= data.entity.name %>', function (hooks) {
module('Integration | Component | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
await render(hbs`
<<%= data.entity.doubleColonizedName %> />
<<%= options.entity.doubleColonizedName %> />
`);

assert.dom().hasText('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import Component from '@glimmer/component';

import styles from './<%= data.localFileName %>.css';

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {};
Blocks: {
default: [];
};
Element: null;
}

export default class <%= data.entity.classifiedName %>Component extends Component<<%= data.entity.classifiedName %>Signature> {
export default class <%= options.entity.classifiedName %>Component extends Component<<%= options.entity.classifiedName %>Signature> {
styles = styles;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import Component from '@glimmer/component';

import styles from './<%= data.localFileName %>.css';

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {};
Blocks: {
default: [];
};
Element: null;
}

export default class <%= data.entity.classifiedName %>Component extends Component<<%= data.entity.classifiedName %>Signature> {
export default class <%= options.entity.classifiedName %>Component extends Component<<%= options.entity.classifiedName %>Signature> {
<template>
<div class={{styles.container}}>
{{yield}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { <%= data.entity.camelizedName %> } from '<%! options.package.name %>';
import { <%= options.entity.camelizedName %> } from '<%= options.addon.name %>';
import { render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Helper | <%= data.entity.name %>', function (hooks) {
module('Integration | Helper | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
this.inputValue = '1234';

await render(<template>>
{{<%= data.entity.camelizedName %> this.inputValue}}
await render(<template>
{{<%= options.entity.camelizedName %> this.inputValue}}
</template>);

assert.dom().hasText('1234');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Helper | <%= data.entity.name %>', function (hooks) {
module('Integration | Helper | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
this.inputValue = '1234';

await render(hbs`
{{<%= data.entity.name %> this.inputValue}}
{{<%= options.entity.name %> this.inputValue}}
`);

assert.dom().hasText('1234');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ type Named = {};
type Positional = [];
type Return = Positional;

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {
Named: Named;
Positional: Positional;
};
Return: Return;
}

export default class <%= data.entity.classifiedName %>Helper extends Helper<<%= data.entity.classifiedName %>Signature> {
export default class <%= options.entity.classifiedName %>Helper extends Helper<<%= options.entity.classifiedName %>Signature> {
compute(positional: Positional /*, named: Named*/): Return {
return positional;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ type Named = {};
type Positional = [];
type Return = Positional;

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {
Named: Named;
Positional: Positional;
};
Return: Return;
}

export default helper<<%= data.entity.classifiedName %>Signature>(
export default helper<<%= options.entity.classifiedName %>Signature>(
(positional /*, named*/) => {
return positional;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { <%= data.entity.camelizedName %> } from '<%! options.package.name %>';
import { <%= options.entity.camelizedName %> } from '<%= options.addon.name %>';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Modifier | <%= data.entity.name %>', function (hooks) {
module('Integration | Modifier | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
await render(hbs`
<div {{<%= data.entity.camelizedName %>}}></div>
`);
await render(<template>
<div {{<%= options.entity.camelizedName %>}}></div>
</template>);

assert.ok(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'test-app/tests/helpers';

module('Integration | Modifier | <%= data.entity.name %>', function (hooks) {
module('Integration | Modifier | <%= options.entity.name %>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
await render(hbs`
<div {{<%= data.entity.name %>}}></div>
<div {{<%= options.entity.name %>}}></div>
`);

assert.ok(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ type Named = {};
type Positional = [];
type Element = Element;

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {
Named: Named;
Positional: Positional;
};
Element: Element;
}

export default class <%= data.entity.classifiedName %>Modifier extends Modifier<<%= data.entity.classifiedName %>Signature> {
export default class <%= options.entity.classifiedName %>Modifier extends Modifier<<%= options.entity.classifiedName %>Signature> {
modify(element: Element /*, positional: Positional, named: Named */) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ type Named = {};
type Positional = [];
type Element = Element;

interface <%= data.entity.classifiedName %>Signature {
interface <%= options.entity.classifiedName %>Signature {
Args: {
Named: Named;
Positional: Positional;
};
Element: Element;
}

export default modifier<<%= data.entity.classifiedName %>Signature>(
function <%= data.entity.camelizedName %>(element /*, positional, named*/) {},
export default modifier<<%= options.entity.classifiedName %>Signature>(
function <%= options.entity.camelizedName %>(element /*, positional, named*/) {},
);
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'test-app/tests/helpers';

module('Unit | Service | <%= data.entity.name %>', function (hooks) {
module('Unit | Service | <%= options.entity.name %>', function (hooks) {
setupTest(hooks);

test('it exists', function (assert) {
const service = this.owner.lookup('service:<%= data.entity.name %>');
const service = this.owner.lookup('service:<%= options.entity.name %>');

assert.ok(service);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Service from '@ember/service';

export default class <%= data.entity.classifiedName %>Service extends Service {}
export default class <%= options.entity.classifiedName %>Service extends Service {}

declare module '@ember/service' {
interface Registry {
'<%= data.entity.name %>': <%= data.entity.classifiedName %>Service;
'<%= options.entity.name %>': <%= options.entity.classifiedName %>Service;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fooBar from '<%= options.addon.name %>/utils/foo/bar';
import <%= options.entity.camelizedName %> from '<%= options.addon.name %>/utils/<%= options.entity.name %>';
import { module, test } from 'qunit';

module('Unit | Utility | <%= data.entity.name %>', function () {
module('Unit | Utility | <%= options.entity.name %>', function () {
test('it works', function (assert) {
const result = fooBar();
const result = <%= options.entity.camelizedName %>();

assert.true(result);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function <%= data.entity.camelizedName %>() {
export default function <%= options.entity.camelizedName %>() {
return true;
}
31 changes: 29 additions & 2 deletions packages/blueprints-addon/src/run-generate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import { createEntity, createOptions } from './steps/run-generate/index.js';
import type { CodemodOptions } from './types/run-generate.js';
import { join } from 'node:path';

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

import {
createEntity,
createOptions,
createTestFile,
updateBarrelFile,
updateTemplateRegistry,
} from './steps/run-generate/index.js';
import type { CodemodOptions, Options } from './types/run-generate.js';

function entityExists(options: Options): boolean {
const { entity, projectRoot } = options;

const files = findFiles(join('src', `${entity.type}s`, `${entity.name}.*`), {
projectRoot,
});

return files.length > 0;
}

export function runGenerate(codemodOptions: CodemodOptions): void {
const options = createOptions(codemodOptions);

if (entityExists(options)) {
return;
}

createEntity(options);
updateBarrelFile(options);
updateTemplateRegistry(options);
createTestFile(options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,15 @@ import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { processTemplate } from '@codemod-utils/blueprints';
import { classify } from '@codemod-utils/ember-cli-string';
import { createFiles, findFiles, parseFilePath } from '@codemod-utils/files';

import type { Options } from '../../types/run-generate.js';
import { blueprintsRoot } from '../../utils/blueprints.js';

function getData(options: Options) {
const entityName = options.entity.name;

const classifiedName = classify(entityName);
const camelizedName = `${classifiedName.charAt(0).toLowerCase()}${classifiedName.substring(1)}`;
const { name: localFileName } = parseFilePath(entityName);
const { name: localFileName } = parseFilePath(options.entity.name);

return {
entity: {
camelizedName,
classifiedName,
name: entityName,
},
localFileName,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { classify, doubleColonize } from '@codemod-utils/ember-cli-string';
import { readPackageJson } from '@codemod-utils/json';

import type { CodemodOptions, Options } from '../../types/run-generate.js';
Expand All @@ -15,11 +16,20 @@ function getAddonName(codemodOptions: CodemodOptions): string {
export function createOptions(codemodOptions: CodemodOptions): Options {
const { entity, projectRoot, testAppLocation } = codemodOptions;

const classifiedName = classify(entity.name);
const camelizedName = `${classifiedName.charAt(0).toLowerCase()}${classifiedName.substring(1)}`;
const doubleColonizedName = doubleColonize(entity.name);

return {
addon: {
name: getAddonName(codemodOptions),
},
entity,
entity: {
...entity,
camelizedName,
classifiedName,
doubleColonizedName,
},
projectRoot,
testApp: {
location: testAppLocation,
Expand Down
Loading

0 comments on commit cd6d3de

Please sign in to comment.