Skip to content

Commit

Permalink
cleanup schema removal
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanatkn committed Jan 15, 2024
1 parent fe22ed9 commit 1bb28cd
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 80 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-dolls-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@grogarden/gro": patch
---

cleanup schema removal
42 changes: 0 additions & 42 deletions src/fixtures/some_test_object.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/lib/docs/gen.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ By convention, `gro gen` looks through `src/`
for any TypeScript files with `.gen.` in the file name,
and it outputs a file stripped of `.gen.` to the same directory.
The `*.gen.*` origin files export a `gen` function
that returns the content of the output file.
More flexibility is available when needed
including multiple custom output files.

Expand All @@ -33,15 +32,13 @@ and name the output files accordingly.
Integrating codegen into our development process
is a simple idea with vast potential.
It lets us have a single source of truth for data
that would otherwise be scattered throughout our codebases
without compromising any of our code's runtime characteristics.
We can generate documentation, types,
`index.ts` files exporting directories,
data for a UI,
validators, tests, fakes,
and more by introspecting our data at buildtime,
which speeds up development
and helps us write code that's easier to understand and change.
The goal is to leverage automation to increase the power we wield over our code
with a straightforward developer experience.
Ergonomics are key to unlocking codegen's full potential.
Expand Down Expand Up @@ -73,7 +70,6 @@ gro gen --check # exits with error code 1 if anything is new or different; no-op
```

> in the following examples,
> note that importing the `Gen` type is optional,
> but it makes for a better DX
### generate arbitrary TypeScript
Expand Down Expand Up @@ -208,8 +204,6 @@ and `src/data/thing.json`:
}
```

### check that generated files have not changed

It's often helpful to check if any generated files are new or have changed.
We don't want to forget to regenerate files before committing or publishing!
The `check` CLI argument can be passed to perform this check
Expand Down Expand Up @@ -240,14 +234,8 @@ which is called during `gro publish`, and it's recommended in CI.

- [x] basic functionality
- [x] format output with Prettier
- [x] add type generation for `.schema.` files
- [x] [watch mode and build integration](https://github.com/grogarden/gro/pull/283),
opt out with `watch: false` for expensive gen use cases
- [ ] change the exported `gen` function to an object with a `summary` and other properties like `watch`
- [ ] properly implement the hacks that de-dupe and combine `tsImport` statements for `.schema.` files
- [ ] support gen files authored in languages beyond TypeScript like
Svelte/[MDSveX](https://github.com/pngwn/MDsveX)/etc
to generate html/markdown/etc (similar to the `.schema.` support)
- [ ] support generating non-text files
- [ ] sourcemaps
- [ ] think about how to handle gen file dependency graphs (generated files as inputs to gen files)
4 changes: 2 additions & 2 deletions src/lib/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Gen_Context {
export type Raw_Gen_Result = string | Raw_Gen_File | null | Raw_Gen_Result[];
export interface Raw_Gen_File {
content: string;
// Defaults to file name without the `.gen` or `.schema`, and can be a relative path.
// Defaults to file name without the `.gen`, and can be a relative path.
// TODO maybe support a transform pattern or callback fn? like '[stem].thing.[ext]'
filename?: string;
format?: boolean; // defaults to `true`
Expand Down Expand Up @@ -122,7 +122,7 @@ export const to_output_file_name = (filename: string): string => {
const has_different_ext = gen_pattern_index === parts.length - 3;
const length = has_different_ext ? parts.length - 1 : parts.length;
for (let i = 0; i < length; i++) {
if (i === gen_pattern_index) continue; // skip the `.gen.` or `.schema.` pattern
if (i === gen_pattern_index) continue; // skip the `.gen.` pattern
if (i === length - 1 && parts[i] === '') continue; // allow empty extension
final_parts.push(parts[i]);
}
Expand Down
11 changes: 1 addition & 10 deletions src/lib/gen_module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {test} from 'uvu';
import * as assert from 'uvu/assert';
import {join} from 'node:path';

import {validate_gen_module, find_gen_modules, to_gen_schema_name} from './gen_module.js';
import {validate_gen_module, find_gen_modules} from './gen_module.js';
import {paths} from './paths.js';

test('basic minimal interface', () => {
Expand Down Expand Up @@ -34,13 +34,4 @@ test('finds gen modules in a directory', async () => {
assert.ok(find_gen_modules_result.source_id_path_data_by_input_path.size);
});

test('to_gen_schema_name', () => {
assert.is(to_gen_schema_name('ASchema'), 'A');
assert.is(to_gen_schema_name('A_Schema'), 'A');
assert.is(to_gen_schema_name('A_'), 'A_');
assert.is(to_gen_schema_name('A_SchemaSchema'), 'A_Schema');
assert.is(to_gen_schema_name('A_Schema_Schema'), 'A_Schema');
assert.is(to_gen_schema_name('A__Schema'), 'A_');
});

test.run();
12 changes: 0 additions & 12 deletions src/lib/gen_module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {readFile} from 'node:fs/promises';
import {strip_end} from '@grogarden/util/string.js';

import {
type Module_Meta,
Expand All @@ -24,17 +23,6 @@ export const GEN_SCHEMA_FILE_PATTERN = '.' + GEN_SCHEMA_FILE_PATTERN_TEXT + '.';
export const GEN_SCHEMA_PATH_SUFFIX = GEN_SCHEMA_FILE_PATTERN + 'ts';
export const GEN_SCHEMA_IDENTIFIER_SUFFIX = 'Schema';

/**
* Convert a schema identifer to the final type identifier, e.g. `A_Schema` to `A`.
*/
export const to_gen_schema_name = (identifier: string): string => {
const full_suffix = '_' + GEN_SCHEMA_IDENTIFIER_SUFFIX;
return strip_end(
identifier,
identifier.endsWith(full_suffix) ? full_suffix : GEN_SCHEMA_IDENTIFIER_SUFFIX,
);
};

export type Gen_Module_Type = 'basic';
export type Gen_Module = Basic_Gen_Module | Schema_Gen_Module;
export interface Basic_Gen_Module {
Expand Down
1 change: 0 additions & 1 deletion src/lib/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ export const src_json = {
{name: 'GEN_SCHEMA_FILE_PATTERN', kind: 'variable'},
{name: 'GEN_SCHEMA_PATH_SUFFIX', kind: 'variable'},
{name: 'GEN_SCHEMA_IDENTIFIER_SUFFIX', kind: 'variable'},
{name: 'to_gen_schema_name', kind: 'function'},
{name: 'Gen_Module_Type', kind: 'type'},
{name: 'Gen_Module', kind: 'type'},
{name: 'Basic_Gen_Module', kind: 'type'},
Expand Down
1 change: 0 additions & 1 deletion src/lib/search_fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ test__search_fs('basic behavior', async () => {
'test_file.other.ext',
'test_failing_task_module.ts',
'some_test_side_effect.ts',
'some_test_object.ts',
'some_test_json.json',
'some_test_exports3.ts',
'some_test_exports2.ts',
Expand Down

0 comments on commit 1bb28cd

Please sign in to comment.