Skip to content

Commit

Permalink
feat: add dir_base template parameter for emitTemplate (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsafalpiya authored Sep 26, 2023
1 parent 4b0ee03 commit 36cdf28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ Config file format (`config.json`):

By default, PgTyped saves generated files in the same folder as the source files it parses.
This behavior can be customized using the `emitTemplate` config parameter.
In that template, four parameters are available for interpolation: `root`, `dir`, `base`, `name` and `ext`.
In that template, following parameters are available for interpolation: `root`, `dir`, `dir_base`, `base`, `name` and `ext`.
For example, when parsing source/query file `/home/user/dir/file.sql`, these parameters are assigned the following values:

```
┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
│ root │ │ name │ ext │
" / home/user/dir / file .sql "
└──────┴──────────────┴──────┴─────┘
┌───────────────────────────┬────────────┐
│ dir │ base │
├──────┬─────────┬──────────├──────┬─────┤
│ root │ │ dir_base │ name │ ext │
" / home/user/ dir / file .sql "
└──────┴────────────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)
```

Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ let connected = false;
const connection = new AsyncQueue();
const config: ParsedConfig = worker.workerData;

interface extendedParsedPath extends path.ParsedPath {
dir_base: string,
}

export default async function processFile({
fileName,
transform,
Expand All @@ -30,7 +34,9 @@ export default async function processFile({
await startup(config.db, connection);
connected = true;
}
const ppath = path.parse(fileName);
const ppath = path.parse(fileName) as extendedParsedPath;
ppath.dir_base = path.basename(ppath.dir);

let decsFileName;
if (transform.emitTemplate) {
decsFileName = nun.renderString(transform.emitTemplate, ppath);
Expand Down

0 comments on commit 36cdf28

Please sign in to comment.