0.9.9 • Published 2 years ago

generate-ts v0.9.9

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

generate-ts

Typescript NPM

The purpose of this repository is to provide a simple, Typescript native way to dynamically generate Typescript code based on a pattern from a markdown file.

Getting started

Typescript version ^4.8.4 needs to be installed globally via:

npm install -g typescript

Then you can simply run this package via an npx command:

npx generate-ts

It will pick up any <NAME>.generator.md files in your project and generate patterns based on a particular format.

How to structure the generator.md files

Create some .md files with the name <NAME>.generator.md and structure them like so:

filePath
`src/example/test.ts`

fileNamePattern
`.screen.ts`

codePatterns

```TypeScript
import { $nameScreen } from './$importName'; // pattern
```

```TypeScript
const test = () => {
  // variable assignment
  const $nameVariable = "$name"; // pattern
  return {
    $nameVariable, // pattern
  };
};
```

Run npx generate-ts, and the resulting test.ts file will look like this:

import { ExampleScreen } from './Example.screen';
import { SecondScreen } from './Second.screen';

const test = () => {
  // variable assignment
  const ExampleVariable = 'Example';
  const SecondVariable = 'Second';
  return {
    ExampleVariable,
    SecondVariable,
  };
};

API

Markdown file structure

FlagDescription
filePathThe path to the file to be generated
fileNamePatternThe pattern used to pick up files by their extensions
codePatternsAny code blocks after this will be used to generate code

Code patterns

TypeDescriptionExample
StaticCode that is generated onceconst test = "test";
PatternMust be appended with a // pattern comment. This is code that is generated for each file that matches the fileNamePatternimport { $nameScreen } from './$importPath'; // pattern

Pattern Variables

VariableDescription
$nameGets the first section of the file name: "MyScreen"
$fileNameGets the entire file name: "MyScreen.screen.ts"
$importPathGets the file path without the last extension: "src/screens/MyScreen.screen"
$pathGets the full path: "src/screens/MyScreen.screen.ts"

Using the library programmatically

You can install this package and use it programmatically instead of using npx:

npm install generate-ts

And then use it like so:

import { generatePattern, GeneratePatternOptions } from 'generate-ts';

const options: GeneratePatternOptions = {
  filePath: 'src/Example/test.ts',
  fileNamePattern: '.screen.ts',
  codePatterns: [
    {
      pattern: `// import code block`
      static: true,
    },
    {
      pattern: `import { $nameScreen } from './$importName';`
      static: false,
    }
  ],
};

generatePattern(options);

Options

PropertyTypeDescription
folderstring (optional)Limits file search to a provided directory. Defaults to './'

Motivation

Over a year ago I wrote a blog post going over how the vscode-generate-index plugin has helped solve a lot of tedious problems we may face in the Typescript world. However, if I wasn't following my own advice about continuous improvement, I would have never thought to start working on this project.

The vscode-generate-index plugin has been helpful in the short-term. However, in the long-term we identified several issues that this plugin's approach created. Here is a breakdown of the problems we faced, and how this project solves them:

vscode-generate-indexTypescript-Code-Generation
Creating and maintaining the patterns is cumbersome. The patterns exist as single line commentsPatterns are defined with simple, overt syntax. Documentation is also encouraged and can be added anywhere in the markdown file
Onboarding new developers creates learning curve issues. They need to learn the minimatch pattern, and the @index flag syntax, among other thingsAPI is interacted with via native Typescript. In fact, the entire backend is run via the Typescript Compiler API
Makes it too easy/tempting to combine with manual code, adding to confusionEach code generation result is placed in its own file, overwriting itself on each invocation
When not left in check, creates monolithic generated filesWhile any file can become monolithic, this project makes it difficult because it is not possible to have different file searches in the same output, only different patterns as a result of a single file search
No type safety or compiler checks until after the code is generatedThe resulting code from each pattern is run through the native Typescript compiler API, and any errors and type issues are outputted to the console and blocks generation of the file
0.9.9

2 years ago

0.9.8

2 years ago

0.9.7

2 years ago

0.9.6

2 years ago

0.9.5

2 years ago

0.9.4

2 years ago

0.9.3

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.9.0

2 years ago