Skip to content

Commit

Permalink
Added blueprints for create-v2-addon-repo (Part 3) (#14)
Browse files Browse the repository at this point in the history
* refactor: Simplified scripts

* refactor: Created a script to update blueprints-addon

* bugfix: Added a suffix to guarantee unique names (prevent runtime error from recast)

* chore: Updated blueprints

* chore: Hid blueprint for *-test.gts behind a feature flag

* chore: Renamed examples (want to avoid numbers for components)

* chore: Renamed --blueprint-type to --blueprint

* chore: Updated fixtures (blueprints-addon)

* chore: Ran update-blueprints-addon

* chore: Updated blueprints

* chore: Updated fixtures (create-v2-addon-repo)

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Aug 16, 2024
1 parent b8dc54a commit aa9a93b
Show file tree
Hide file tree
Showing 300 changed files with 2,102 additions and 2,041 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-weeks-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"create-v2-addon-repo": minor
"blueprints-addon": minor
---

Added blueprints for create-v2-addon-repo (Part 3)
4 changes: 1 addition & 3 deletions packages/blueprints-addon/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@
/.prettierignore
/.prettierrc.cjs
/build.sh
/codemod-test-fixture.sh
/codemod-test-fixtures.sh
/create-test-fixture.sh
/tests/
/update-test-fixtures.sh
30 changes: 15 additions & 15 deletions packages/blueprints-addon/bin/blueprints-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ yargs(hideBin(process.argv))
describe: 'Entity name',
type: 'string',
})
.option('blueprint-type', {
.option('blueprint', {
choices: ['glimmer', 'template-tag'] as const,
describe: 'Blueprint type',
describe: 'Which blueprint to run',
type: 'string',
})
.demandOption(['name']);
Expand All @@ -98,7 +98,7 @@ yargs(hideBin(process.argv))
handler: (argv) => {
runGenerate({
entity: {
blueprintType: argv['blueprint-type'] ?? 'glimmer',
blueprint: argv['blueprint'] ?? 'glimmer',
name: argv['name'],
type: 'component',
},
Expand All @@ -115,9 +115,9 @@ yargs(hideBin(process.argv))
describe: 'Entity name',
type: 'string',
})
.option('blueprint-type', {
.option('blueprint', {
choices: ['class', 'function'] as const,
describe: 'Blueprint type',
describe: 'Which blueprint to run',
type: 'string',
})
.demandOption(['name']);
Expand All @@ -127,7 +127,7 @@ yargs(hideBin(process.argv))
handler: (argv) => {
runGenerate({
entity: {
blueprintType: argv['blueprint-type'] ?? 'class',
blueprint: argv['blueprint'] ?? 'class',
name: argv['name'],
type: 'helper',
},
Expand All @@ -144,9 +144,9 @@ yargs(hideBin(process.argv))
describe: 'Entity name',
type: 'string',
})
.option('blueprint-type', {
.option('blueprint', {
choices: ['class', 'function'] as const,
describe: 'Blueprint type',
describe: 'Which blueprint to run',
type: 'string',
})
.demandOption(['name']);
Expand All @@ -156,7 +156,7 @@ yargs(hideBin(process.argv))
handler: (argv) => {
runGenerate({
entity: {
blueprintType: argv['blueprint-type'] ?? 'class',
blueprint: argv['blueprint'] ?? 'class',
name: argv['name'],
type: 'modifier',
},
Expand All @@ -173,9 +173,9 @@ yargs(hideBin(process.argv))
describe: 'Entity name',
type: 'string',
})
.option('blueprint-type', {
.option('blueprint', {
choices: ['class'] as const,
describe: 'Blueprint type',
describe: 'Which blueprint to run',
type: 'string',
})
.demandOption(['name']);
Expand All @@ -185,7 +185,7 @@ yargs(hideBin(process.argv))
handler: (argv) => {
runGenerate({
entity: {
blueprintType: argv['blueprint-type'] ?? 'class',
blueprint: argv['blueprint'] ?? 'class',
name: argv['name'],
type: 'service',
},
Expand All @@ -202,9 +202,9 @@ yargs(hideBin(process.argv))
describe: 'Entity name',
type: 'string',
})
.option('blueprint-type', {
.option('blueprint', {
choices: ['function'] as const,
describe: 'Blueprint type',
describe: 'Which blueprint to run',
type: 'string',
})
.demandOption(['name']);
Expand All @@ -214,7 +214,7 @@ yargs(hideBin(process.argv))
handler: (argv) => {
runGenerate({
entity: {
blueprintType: argv['blueprint-type'] ?? 'function',
blueprint: argv['blueprint'] ?? 'function',
name: argv['name'],
type: 'util',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface <%= options.entity.classifiedName %>Signature {
Element: null;
}

// eslint-disable-next-line ember/no-empty-glimmer-component-classes
export default class <%= options.entity.classifiedName %>Component extends Component<<%= options.entity.classifiedName %>Signature> {
<template>
<div class={{styles.container}}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface <%= options.entity.classifiedName %>Signature {
}

export default helper<<%= options.entity.classifiedName %>Signature>(
(positional /*, named*/) => {
function <%= options.entity.camelizedName %>(positional /*, named*/) {
return positional[0];
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@

## Local development

<details>

<summary>Lint and build</summary>

```sh
# Lint files
pnpm lint
pnpm lint:fix

# Build addon
pnpm build

# Live reload ("keep building")
pnpm start
```

</details>

<details>

<summary>Generate and destroy</summary>

From the package root, you can create (or remove) the source code and its corresponding test file in `test-app`.

```sh
pnpm addon <generate|destroy> <component|helper|modifier|service|util> <name> [options]

# Examples
pnpm addon g component hello-world
pnpm addon d component hello-world
```

There may be more than 1 blueprint available. You can pass `--blueprint` to select the right one.

For more information, pass `--help`.

</details>


## Compatibility

* Node.js v18 or above
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createEntity(options: Options): void {
blueprintsRoot,
'run-generate',
options.entity.type,
options.entity.blueprintType,
options.entity.blueprint,
);

const blueprintFilePaths = findFiles('**/*', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import { createFiles, findFiles } from '@codemod-utils/files';
import type { Options } from '../../types/run-generate.js';
import { blueprintsRoot } from '../../utils/blueprints.js';

const ENABLE_TEMPLATE_TAG = false;

function getFilesToSkip(): string[] {
const files = new Set<string>();

if (ENABLE_TEMPLATE_TAG) {
files.add('__fileName__.ts');
} else {
files.add('__fileName__.gts');
}

return Array.from(files);
}

function resolveBlueprintFilePath(
blueprintFilePath: string,
options: Options,
Expand Down Expand Up @@ -34,6 +48,8 @@ function resolveBlueprintFilePath(
}

export function createTestFile(options: Options): void {
const filesToSkip = getFilesToSkip();

const cwd = join(
blueprintsRoot,
'run-generate',
Expand All @@ -42,6 +58,7 @@ export function createTestFile(options: Options): void {
);

const blueprintFilePaths = findFiles('**/*', {
ignoreList: filesToSkip,
projectRoot: cwd,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function addExportStatement(file: string, options: Options): string {

switch (entity.type) {
case 'component': {
if (entity.blueprintType === 'template-tag') {
if (entity.blueprint === 'template-tag') {
line = `export { default as ${entity.classifiedName} } from './${entity.type}s/${entity.name}.gts';\n`;
} else {
line = `export { default as ${entity.classifiedName} } from './${entity.type}s/${entity.name}.ts';\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import type { Options } from '../../types/run-generate.js';

function addImportStatement(file: string, options: Options): string {
const { entity } = options;
const localName = getLocalName(options);

let line = '';

switch (entity.type) {
case 'component': {
if (entity.blueprintType === 'template-tag') {
line = `import type ${entity.classifiedName} from './${entity.type}s/${entity.name}.gts';\n`;
if (entity.blueprint === 'template-tag') {
line = `import type ${localName} from './${entity.type}s/${entity.name}.gts';\n`;
} else {
line = `import type ${entity.classifiedName} from './${entity.type}s/${entity.name}.ts';\n`;
line = `import type ${localName} from './${entity.type}s/${entity.name}.ts';\n`;
}

break;
}

case 'helper':
case 'modifier': {
line = `import type ${entity.classifiedName} from './${entity.type}s/${entity.name}.ts';\n`;
line = `import type ${localName} from './${entity.type}s/${entity.name}.ts';\n`;

break;
}
Expand All @@ -31,8 +33,31 @@ function addImportStatement(file: string, options: Options): string {
return [line, file].join('');
}

function getLocalName(options: Options): string {
const { entity } = options;

switch (entity.type) {
case 'component': {
return `${entity.classifiedName}Component`;
}

case 'helper': {
return `${entity.classifiedName}Helper`;
}

case 'modifier': {
return `${entity.classifiedName}Modifier`;
}

default: {
return entity.classifiedName;
}
}
}

function updateRegistry(file: string, options: Options): string {
const { entity } = options;
const localName = getLocalName(options);

const traverse = AST.traverse(true);

Expand All @@ -47,9 +72,7 @@ function updateRegistry(file: string, options: Options): string {
AST.builders.tsPropertySignature(
AST.builders.stringLiteral(entity.name),
AST.builders.tsTypeAnnotation(
AST.builders.tsTypeQuery(
AST.builders.identifier(entity.classifiedName),
),
AST.builders.tsTypeQuery(AST.builders.identifier(localName)),
),
),
);
Expand All @@ -61,9 +84,7 @@ function updateRegistry(file: string, options: Options): string {
AST.builders.tsPropertySignature(
AST.builders.stringLiteral(entity.doubleColonizedName),
AST.builders.tsTypeAnnotation(
AST.builders.tsTypeQuery(
AST.builders.identifier(entity.classifiedName),
),
AST.builders.tsTypeQuery(AST.builders.identifier(localName)),
),
),
);
Expand Down
10 changes: 5 additions & 5 deletions packages/blueprints-addon/src/types/run-generate.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
type EntityType =
| {
blueprintType: 'glimmer' | 'template-tag';
blueprint: 'glimmer' | 'template-tag';
type: 'component';
}
| {
blueprintType: 'class' | 'function';
blueprint: 'class' | 'function';
type: 'helper';
}
| {
blueprintType: 'class' | 'function';
blueprint: 'class' | 'function';
type: 'modifier';
}
| {
blueprintType: 'class';
blueprint: 'class';
type: 'service';
}
| {
blueprintType: 'function';
blueprint: 'function';
type: 'util';
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Component from '@glimmer/component';

import styles from './glimmer.css';

interface MyExampleGlimmerSignature {
Args: {};
Blocks: {
default: [];
};
Element: null;
}

export default class MyExampleGlimmerComponent extends Component<MyExampleGlimmerSignature> {
styles = styles;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Component from '@glimmer/component';

import styles from './template-tag.css';

interface MyExampleTemplateTagSignature {
Args: {};
Blocks: {
default: [];
};
Element: null;
}

// eslint-disable-next-line ember/no-empty-glimmer-component-classes
export default class MyExampleTemplateTagComponent extends Component<MyExampleTemplateTagSignature> {
<template>
<div class={{styles.container}}>
{{yield}}
</div>
</template>
}
Loading

0 comments on commit aa9a93b

Please sign in to comment.