@routecraft/cli 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +77 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @routecraft/cli
|
|
2
|
+
|
|
3
|
+
CLI for running RouteCraft routes.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @routecraft/cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
or
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add -g @routecraft/cli
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Run a RouteCraft route file:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
craft run myroute.mjs
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The CLI will:
|
|
26
|
+
- Load your route file
|
|
27
|
+
- Execute the route
|
|
28
|
+
- Keep the process running for continuous processing
|
|
29
|
+
- Handle graceful shutdown on SIGINT/SIGTERM
|
|
30
|
+
|
|
31
|
+
## Options
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
craft run [options] <file>
|
|
35
|
+
|
|
36
|
+
Options:
|
|
37
|
+
-h, --help Display help information
|
|
38
|
+
-V, --version Display version information
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Environment Variables
|
|
42
|
+
|
|
43
|
+
The CLI automatically loads environment variables from a `.env` file in the current directory if present.
|
|
44
|
+
|
|
45
|
+
## Example Route File
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// myroute.mjs
|
|
49
|
+
import { craft, timer, log } from '@routecraft/routecraft';
|
|
50
|
+
|
|
51
|
+
export default craft()
|
|
52
|
+
.id('timer-example')
|
|
53
|
+
.from(timer({ intervalMs: 1000 }))
|
|
54
|
+
.transform((ex) => ({ timestamp: Date.now() }))
|
|
55
|
+
.to(log());
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Run it:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
craft run myroute.mjs
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Documentation
|
|
65
|
+
|
|
66
|
+
For more information, visit [routecraft.dev](https://routecraft.dev).
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
Apache-2.0
|
|
71
|
+
|
|
72
|
+
## Links
|
|
73
|
+
|
|
74
|
+
- [Documentation](https://routecraft.dev)
|
|
75
|
+
- [GitHub Repository](https://github.com/routecraftjs/routecraft)
|
|
76
|
+
- [Issue Tracker](https://github.com/routecraftjs/routecraft/issues)
|
|
77
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {Command}from'commander';import {resolve,extname}from'path';import {logger,ContextBuilder}from'@routecraft/routecraft';import {config}from'dotenv';function f(o){if(o){let s=resolve(process.cwd(),o),r=config({path:s});return r.error?logger.info(`Could not load .env file from ${o}: ${r.error.message}`):r.parsed&&logger.debug(`Loaded ${Object.keys(r.parsed).length} environment variables from ${o}`),r}let e=config({path:resolve(process.cwd(),".env")});e.parsed?logger.debug(`Loaded ${Object.keys(e.parsed).length} environment variables from .env`):e.error&&logger.debug("No .env file found");let n=config({path:resolve(process.cwd(),".env.local"),override:true});return n.parsed&&logger.debug(`Loaded ${Object.keys(n.parsed).length} environment variables from .env.local`),n.parsed?n:e.parsed?e:n}function p(o){process.on("SIGINT",async()=>{o.logger.info("Shutting down (SIGINT)..."),await o.stop(),process.exit(0);}),process.on("SIGTERM",async()=>{o.logger.info("Shutting down (SIGTERM)..."),await o.stop(),process.exit(0);}),process.on("exit",()=>{o.logger.info("Cleanup complete");});}var m=[".mjs",".js",".cjs"];async function g(o){let e=resolve(process.cwd(),o),n=extname(e);if(!m.includes(n))return {success:false,code:1,message:`Error: Only the following file types are supported: ${m.join(", ")}`};if(n===".ts"||n===".tsx")return {success:false,code:1,message:"TypeScript files are not supported by 'craft run'. Compile to .js or use .mjs/.js/.cjs."};try{logger.info(`Loading file: ${e}`);let s=await import(e),r=new ContextBuilder;s.craftConfig&&(logger.info("Found craftConfig export, applying configuration"),r.with(s.craftConfig));let l=w(r,s.default);if(!l.success)return l;let d=r.build();return p(d),await d.start(),{success:!0}}catch(s){return s instanceof Error?(logger.error(`Failed to run ${e}: ${s.message}`),{success:false,code:1,message:s.message}):(logger.error(`Failed to run ${e}: Unknown error occurred`),{success:false,code:1,message:"Unknown error"})}}function w(o,e){let n=r=>typeof r=="object"&&r!==null&&"id"in r,s=r=>typeof r=="object"&&r!==null&&typeof r.build=="function";return e?n(e)||s(e)?(o.routes(e),logger.info("Loaded single route from default export"),{success:true}):Array.isArray(e)?e.every(r=>n(r)||s(r))?(e.forEach(r=>o.routes(r)),logger.info(`Loaded ${e.length} routes from default export array`),{success:true}):(logger.error("All items in default export array must be RouteDefinition or RouteBuilder"),{success:false,code:1,message:"Invalid items in default export array"}):(logger.error("Invalid default export. Expected: RouteDefinition, RouteBuilder, or array of those."),{success:false,code:1,message:"Invalid default export. Expected: RouteDefinition, RouteBuilder, or array of those."}):(logger.error("No default export found. Expected routes as default export."),{success:false,code:1,message:"No default export found"})}var a=new Command;a.name("craft").description("A modern routing framework for TypeScript").version("0.1.1").showSuggestionAfterError().showHelpAfterError().exitOverride(o=>{o.code==="commander.unknownCommand"&&process.exit(0);});process.argv.length<=2&&a.help({error:false});a.command("run").description("Run routes from a single TypeScript/JavaScript file").argument("<file>","Path to a file containing routes").option("--env <path>","Load environment variables from a .env file (default: .env)").action(async(o,e)=>{e.env!==void 0?f(e.env):f();let n=await g(o);n.success||(n.message&&console.error(n.message),process.exit(n.code??1));});a.parse();//# sourceMappingURL=index.js.map
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/util.ts","../src/run.ts","../src/index.ts"],"names":["loadEnvFile","path","envPath","resolve","result","loadDotenv","logger","envResult","envLocalResult","registerContextSignalHandlers","context","SUPPORTED_EXTENSIONS","runCommand","filePath","absFilePath","ext","extname","module","contextBuilder","ContextBuilder","configured","configureRoutes","error","defaultExport","isRouteDefinition","obj","isRouteBuilder","item","routeOrBuilder","program","Command","err","options"],"mappings":";0JAUO,SAASA,CAAAA,CAAYC,CAAAA,CAAe,CACzC,GAAIA,CAAAA,CAAM,CAER,IAAMC,CAAAA,CAAUC,OAAAA,CAAQ,OAAA,CAAQ,GAAA,GAAOF,CAAI,CAAA,CACrCG,CAAAA,CAASC,MAAAA,CAAW,CAAE,IAAA,CAAMH,CAAQ,CAAC,EAE3C,OAAIE,CAAAA,CAAO,KAAA,CACTE,MAAAA,CAAO,IAAA,CACL,CAAA,8BAAA,EAAiCL,CAAI,CAAA,EAAA,EAAKG,EAAO,KAAA,CAAM,OAAO,CAAA,CAChE,CAAA,CACSA,CAAAA,CAAO,MAAA,EAChBE,MAAAA,CAAO,KAAA,CACL,UAAU,MAAA,CAAO,IAAA,CAAKF,CAAAA,CAAO,MAAM,CAAA,CAAE,MAAM,CAAA,4BAAA,EAA+BH,CAAI,EAChF,C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AG,CAAA,CAEvBhC,CAAAA,EAAY,CAGd,IAAMI,CAAAA,CAAS,MAAMQ,CAAAA,CAAWC,CAAQ,EACnCT,CAAAA,CAAO,OAAA,GACNA,CAAAA,CAAO,OAAA,EAET,OAAA,CAAQ,KAAA,CAAMA,CAAAA,CAAO,OAAO,EAE9B,OAAA,CAAQ,IAAA,CAAKA,CAAAA,CAAO,IAAA,EAAQ,CAAC,CAAA,EAEjC,CAAC,CAAA,CAGHyB,EAAQ,KAAA,EAAM","file":"index.js","sourcesContent":["import { logger, CraftContext } from \"@routecraft/routecraft\";\nimport { resolve } from \"node:path\";\nimport { config as loadDotenv } from \"dotenv\";\n\n/**\n * Loads environment variables from .env files\n *\n * @param path Optional path to .env file. If not specified, loads .env and .env.local (if they exist)\n * @returns The parsed dotenv config result\n */\nexport function loadEnvFile(path?: string) {\n if (path) {\n // Explicit path provided - load that file only\n const envPath = resolve(process.cwd(), path);\n const result = loadDotenv({ path: envPath });\n\n if (result.error) {\n logger.info(\n `Could not load .env file from ${path}: ${result.error.message}`,\n );\n } else if (result.parsed) {\n logger.debug(\n `Loaded ${Object.keys(result.parsed).length} environment variables from ${path}`,\n );\n }\n\n return result;\n }\n\n // No path provided - load .env, then .env.local (with override)\n // Load .env first\n const envResult = loadDotenv({ path: resolve(process.cwd(), \".env\") });\n if (envResult.parsed) {\n logger.debug(\n `Loaded ${Object.keys(envResult.parsed).length} environment variables from .env`,\n );\n } else if (envResult.error) {\n logger.debug(`No .env file found`);\n }\n\n // Load .env.local next, allowing it to override .env values\n const envLocalResult = loadDotenv({\n path: resolve(process.cwd(), \".env.local\"),\n override: true,\n });\n if (envLocalResult.parsed) {\n logger.debug(\n `Loaded ${Object.keys(envLocalResult.parsed).length} environment variables from .env.local`,\n );\n }\n\n // Return the most successful result:\n // - If .env.local loaded successfully, return it\n // - If .env loaded successfully but .env.local failed (doesn't exist), return .env result\n // - If both failed, return the last error (from .env.local)\n if (envLocalResult.parsed) {\n return envLocalResult;\n }\n if (envResult.parsed) {\n return envResult;\n }\n return envLocalResult;\n}\n\n/**\n * Registers SIGINT, SIGTERM, and exit handlers for a CraftContext instance.\n * Ensures graceful shutdown and logging.\n *\n * @param context The CraftContext instance\n */\nexport function registerContextSignalHandlers(\n context: InstanceType<typeof CraftContext>,\n) {\n process.on(\"SIGINT\", async () => {\n context.logger.info(\"Shutting down (SIGINT)...\");\n await context.stop();\n process.exit(0);\n });\n process.on(\"SIGTERM\", async () => {\n context.logger.info(\"Shutting down (SIGTERM)...\");\n await context.stop();\n process.exit(0);\n });\n process.on(\"exit\", () => {\n context.logger.info(\"Cleanup complete\");\n });\n}\n","import { resolve, extname } from \"node:path\";\nimport {\n ContextBuilder,\n type RouteDefinition,\n type CraftConfig,\n logger,\n RouteBuilder,\n} from \"@routecraft/routecraft\";\nimport { registerContextSignalHandlers } from \"./util\";\n\nconst SUPPORTED_EXTENSIONS = [\".mjs\", \".js\", \".cjs\"] as const;\n\ntype RunResult =\n | { success: true }\n | { success: false; code?: number; message: string };\n\nexport async function runCommand(filePath: string): Promise<RunResult> {\n const absFilePath = resolve(process.cwd(), filePath);\n const ext = extname(absFilePath);\n\n // Validate file extension\n if (\n !SUPPORTED_EXTENSIONS.includes(ext as (typeof SUPPORTED_EXTENSIONS)[number])\n ) {\n return {\n success: false,\n code: 1,\n message: `Error: Only the following file types are supported: ${SUPPORTED_EXTENSIONS.join(\", \")}`,\n };\n }\n\n // Reject TypeScript files explicitly\n if (ext === \".ts\" || ext === \".tsx\") {\n return {\n success: false,\n code: 1,\n message:\n \"TypeScript files are not supported by 'craft run'. Compile to .js or use .mjs/.js/.cjs.\",\n };\n }\n\n try {\n logger.info(`Loading file: ${absFilePath}`);\n\n // Load the module with both default and named exports\n const module = await import(absFilePath);\n\n // Create context builder\n const contextBuilder = new ContextBuilder();\n\n // Check for optional craftConfig named export\n if (module.craftConfig) {\n logger.info(\"Found craftConfig export, applying configuration\");\n contextBuilder.with(module.craftConfig as CraftConfig);\n }\n\n // Handle routes from the default export\n const configured = configureRoutes(contextBuilder, module.default);\n if (!configured.success) {\n return configured;\n }\n\n // Build and start the context\n const context = contextBuilder.build();\n registerContextSignalHandlers(context);\n await context.start();\n\n return { success: true };\n } catch (error: unknown) {\n if (error instanceof Error) {\n logger.error(`Failed to run ${absFilePath}: ${error.message}`);\n return { success: false, code: 1, message: error.message };\n }\n logger.error(`Failed to run ${absFilePath}: Unknown error occurred`);\n return { success: false, code: 1, message: \"Unknown error\" };\n }\n}\n\nfunction configureRoutes(\n contextBuilder: InstanceType<typeof ContextBuilder>,\n defaultExport: unknown,\n): RunResult {\n // Type guards\n const isRouteDefinition = (obj: unknown): obj is RouteDefinition =>\n typeof obj === \"object\" && obj !== null && \"id\" in obj;\n\n const isRouteBuilder = (\n obj: unknown,\n ): obj is InstanceType<typeof RouteBuilder> =>\n typeof obj === \"object\" &&\n obj !== null &&\n typeof (obj as { build?: unknown }).build === \"function\";\n\n if (!defaultExport) {\n logger.error(\"No default export found. Expected routes as default export.\");\n return { success: false, code: 1, message: \"No default export found\" };\n }\n\n // Handle single route or RouteBuilder\n if (isRouteDefinition(defaultExport) || isRouteBuilder(defaultExport)) {\n contextBuilder.routes(\n defaultExport as RouteDefinition | InstanceType<typeof RouteBuilder>,\n );\n logger.info(\"Loaded single route from default export\");\n return { success: true };\n }\n\n // Handle array of routes\n if (Array.isArray(defaultExport)) {\n if (\n !defaultExport.every(\n (item) => isRouteDefinition(item) || isRouteBuilder(item),\n )\n ) {\n logger.error(\n \"All items in default export array must be RouteDefinition or RouteBuilder\",\n );\n return {\n success: false,\n code: 1,\n message: \"Invalid items in default export array\",\n };\n }\n\n defaultExport.forEach((routeOrBuilder) =>\n contextBuilder.routes(\n routeOrBuilder as RouteDefinition | InstanceType<typeof RouteBuilder>,\n ),\n );\n logger.info(\n `Loaded ${defaultExport.length} routes from default export array`,\n );\n return { success: true };\n }\n\n // Invalid default export\n logger.error(\n \"Invalid default export. Expected: RouteDefinition, RouteBuilder, or array of those.\",\n );\n return {\n success: false,\n code: 1,\n message:\n \"Invalid default export. Expected: RouteDefinition, RouteBuilder, or array of those.\",\n };\n}\n","#!/usr/bin/env node\n\n/**\n * Routecraft CLI Module\n *\n * This module provides the command-line interface for Routecraft.\n */\nimport { Command } from \"commander\";\nimport { runCommand } from \"./run.ts\";\nimport { loadEnvFile } from \"./util.ts\";\n\n/**\n * The main command program for the Routecraft CLI.\n * Built using the commander.js library.\n */\nconst program = new Command();\n\nprogram\n .name(\"craft\")\n .description(\"A modern routing framework for TypeScript\")\n .version(\"0.1.1\")\n .showSuggestionAfterError()\n .showHelpAfterError()\n .exitOverride((err) => {\n if (err.code === \"commander.unknownCommand\") {\n process.exit(0);\n }\n });\n\n// Show help by default if no arguments provided\nif (process.argv.length <= 2) {\n program.help({ error: false });\n}\n\n/**\n * The 'run' command executes routes from a single file.\n *\n * Example:\n * craft run ./my-routes.ts\n */\nprogram\n .command(\"run\")\n .description(\"Run routes from a single TypeScript/JavaScript file\")\n .argument(\"<file>\", \"Path to a file containing routes\")\n .option(\n \"--env <path>\",\n \"Load environment variables from a .env file (default: .env)\",\n )\n .action(async (filePath, options) => {\n // Load environment variables if specified or use default .env\n if (options.env !== undefined) {\n loadEnvFile(options.env);\n } else {\n loadEnvFile();\n }\n\n const result = await runCommand(filePath);\n if (!result.success) {\n if (result.message) {\n // eslint-disable-next-line no-console\n console.error(result.message);\n }\n process.exit(result.code ?? 1);\n }\n });\n\n// Parse the command line arguments and execute the appropriate command\nprogram.parse();\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@routecraft/cli",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "CLI for running RouteCraft routes",
|
|
5
|
+
"private": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@routecraft/routecraft": "^0.1.1",
|
|
20
|
+
"commander": "^14.0.2",
|
|
21
|
+
"dotenv": "^17.2.3"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"repository": "https://github.com/routecraftjs/routecraft",
|
|
27
|
+
"bin": {
|
|
28
|
+
"craft": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"routecraft",
|
|
32
|
+
"cli",
|
|
33
|
+
"routes",
|
|
34
|
+
"automation",
|
|
35
|
+
"typescript"
|
|
36
|
+
],
|
|
37
|
+
"author": "routecraftjs",
|
|
38
|
+
"license": "Apache-2.0",
|
|
39
|
+
"homepage": "https://routecraft.dev",
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
42
|
+
"test": "vitest",
|
|
43
|
+
"test:coverage": "vitest --coverage"
|
|
44
|
+
}
|
|
45
|
+
}
|