Skip to content

Commit

Permalink
feat: implemented post-export hook in routes-gen (#18)
Browse files Browse the repository at this point in the history
* feat: implemented post-export hook in routes-gen

* chore: added post-export to changelog
  • Loading branch information
sandulat committed Apr 30, 2022
1 parent c41ff6d commit 8e5f629
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ export default function Product() {

## CLI Options

| Option | Alias | Description |
|-----------|-------|---------------------------------------|
| --help | | Print the help message and exit |
| --version | -v | Print the CLI version and exit |
| --output | -o | The path for routes export |
| --driver | -d | The driver of handling routes parsing |
| --watch | -w | Watch for changes |
| Option | Alias | Description |
|-----------------|-------|---------------------------------------|
| --help | | Print the help message and exit |
| --version | -v | Print the CLI version and exit |
| --output | -o | The path for routes export |
| --driver | -d | The driver of handling routes parsing |
| --watch | -w | Watch for changes |
| --post-export | | Execute a command after routes export |

## Writing Your Driver

Expand All @@ -113,7 +114,7 @@ module.exports = {

// The paths to be watched for changes. Must return and array of relative paths.
watchPaths: async () => {
return ["/my-routes"];
return ["/my-routes/**/*.{ts,tsx,js,jsx}"];
},
}
```
Expand Down
6 changes: 6 additions & 0 deletions packages/routes-gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# routes-gen

## 0.5.0

### Minor Changes

- Implemented post-export hook.

## 0.4.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/routes-gen/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "routes-gen",
"version": "0.4.0",
"version": "0.5.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
Expand Down
21 changes: 19 additions & 2 deletions packages/routes-gen/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from "child_process";
import chokidar from "chokidar";
import meow from "meow";
import * as path from "path";
Expand All @@ -21,6 +22,7 @@ Options
--output, -o The path for routes export
--driver, -d The driver of handling routes parsing
--watch, -w Watch for changes
--post-export Execute a command after routes export
Official Drivers
${driversHelpText}
Expand All @@ -46,6 +48,9 @@ const cli = meow(helpText, {
type: "boolean",
alias: "w",
},
postExport: {
type: "string",
},
},
});

Expand All @@ -65,6 +70,18 @@ const markAsFailed = (message: string) => {
process.exit(1);
};

const processRoutes: typeof exportRoutes = (...args) => {
const result = exportRoutes(...args);

if (typeof cli.flags.postExport === "undefined") {
return;
}

execSync(cli.flags.postExport, { stdio: "inherit" });

return result;
};

const getRoutes = async (driver: Driver) => {
const routes = await driver.routes();

Expand Down Expand Up @@ -121,7 +138,7 @@ const bootstrap = async () => {

const outputPath = cli.flags.output ?? driver.defaultOutputPath;

exportRoutes({
processRoutes({
routes: await getRoutes(driver),
outputPath,
});
Expand Down Expand Up @@ -160,7 +177,7 @@ const bootstrap = async () => {
}
)
.on("all", async () =>
exportRoutes({
processRoutes({
routes: await getRoutes(driver!),
outputPath,
})
Expand Down

0 comments on commit 8e5f629

Please sign in to comment.