1.0.0 • Published 1 year ago

sscg v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

sscg

The Super Simple Code Generator.

This package prioritizes stability with minimal version updates. Dependencies are regularly updated and bugs are fixed, but new features are not added and existing ones are not changed. This approach results in fewer version bumps, keeping the package simple and its behavior consistent.

Installation

$ npm install --save-dev sscg

Getting started

1. Create base directory

Default base directory is ./templates.

$ mkdir templates

2. Create your first template

Now, let's create a template.

$ mkdir templates/sample

The name of directory under the templates directory (here is sample) is treated as template name.

You can create multiple tpl files in the template directory.

$ touch templates/sample/{{kebabCase[singular[t]]}}.ts.tpl
$ mkdir templates/sample/some-dir
$ touch templates/sample/some-dir/{{kebabCase[plural[t]]}}.ts.tpl

sscg can embed string into the file name by evaluating codes in {{}}, and you can use [] instead of () for calling function in file names.

Then, let's edit your tpl files.

templates/sample/{{kebabCase[singulart]}}.ts.tpl:

export type {{pascalCase(singular(t))}} = {};

templates/sample/some-dir/{{kebabCase[pluralt]}}.ts.tpl:

import type { {{pascalCase(singular(t))}} } from "../{{kebabCase(singular(t))}}";

export type {{pascalCase(plural(t))}} = Array<{{pascalCase(singular(t))}}>;

sscg can embed string into tpl files by evaluating codes in {{}}.

3. Generate codes from the your template

You can generate code with the following command.

$ sscg sample -r sample_record -o outputs
Generate from 'templates/sample'...

outputs/sample-record.ts
outputs/some-dir/sample-records.ts

Done in 11ms

The -r option is replacement, and it becomes t in {{}}.

outputs/sample-record.ts:

export type SampleRecord = {};

outputs/some-dir/sample-records.ts:

import type { SampleRecord } from "../sample-record";

export type SampleRecords = Array<SampleRecord>;

Supported variables

kinddesc
tvalueA string from the -r option
pluralfunctionPluralize a string
singularfunctionSingularize a string
camelCasefunctionTransform a string like sampleRecord
constantCasefunctionTransform a string like SAMPLE_RECORD
kebabCasefunctionTransform a string like sample-record
pascalCasefunctionTransform a string like SampleRecord
snakeCasefunctionTransform a string like sample_record

Usage

The super simple code generator

Usage:
  $ sscg <name> [options]

Options:
  -r, --replacement <text>  The string that replaces tokens in templates
  -o, --out <dir>           The output directory where generated codes are located
  -d, --dir <dir>           The directory where templates are located (default: ./templates)

Example:
  $ sscg model -r user -o ./models