@@ -36,10 +36,6 @@ export const initCommand: CommandModule<object, InitOptions> = {
3636 }
3737} ;
3838
39- // ============================================================================
40- // Path Utilities (Bun-native)
41- // ============================================================================
42-
4339const SEP = '/' ;
4440
4541function basename ( p : string ) : string {
@@ -202,26 +198,18 @@ async function createProjectStructure(projectPath: string, config: ProjectConfig
202198
203199 // Create directories using Bun shell
204200 await $ `mkdir -p ${ projectPath } ` . quiet ( ) ;
205- await $ `mkdir -p ${ join ( projectPath , 'environments ' ) } ` . quiet ( ) ;
201+ await $ `mkdir -p ${ join ( projectPath , '.treq ' ) } ` . quiet ( ) ;
206202 await $ `mkdir -p ${ join ( projectPath , 'collection' , 'auth' ) } ` . quiet ( ) ;
207203 await $ `mkdir -p ${ join ( projectPath , 'collection' , 'users' ) } ` . quiet ( ) ;
208204
209205 // Write root files
210- await Bun . write ( join ( projectPath , 'treq.config.ts ' ) , generateConfig ( ) ) ;
206+ await Bun . write ( join ( projectPath , 'treq.jsonc ' ) , generateConfig ( ) ) ;
211207 await Bun . write ( join ( projectPath , 'run.ts' ) , generateRunScript ( config . runtime ) ) ;
212208 await Bun . write ( join ( projectPath , 'package.json' ) , generatePackageJson ( projectName , config ) ) ;
213209 await Bun . write ( join ( projectPath , '.gitignore' ) , generateGitignore ( ) ) ;
214210
215- // Write environments
216- await Bun . write ( join ( projectPath , 'environments' , 'dev.ts' ) , generateDevEnvironment ( ) ) ;
217- await Bun . write ( join ( projectPath , 'environments' , 'prod.ts' ) , generateProdEnvironment ( ) ) ;
218-
219211 // Write collection
220212 await Bun . write ( join ( projectPath , 'collection' , 'auth' , 'login.http' ) , generateLoginRequest ( ) ) ;
221- await Bun . write (
222- join ( projectPath , 'collection' , 'users' , '_defaults.ts' ) ,
223- generateFolderDefaults ( )
224- ) ;
225213 await Bun . write (
226214 join ( projectPath , 'collection' , 'users' , 'list.http' ) ,
227215 generateListUsersRequest ( )
@@ -230,13 +218,33 @@ async function createProjectStructure(projectPath: string, config: ProjectConfig
230218}
231219
232220export function generateConfig ( ) : string {
233- return `import { defineConfig } from '@t-req/core/config';
234-
235- export default defineConfig({
236- variables: {
237- baseUrl: '{{baseUrl}}',
221+ return `{
222+ "variables": {
223+ // Default base URL for the included sample requests.
224+ // Switch profiles with: treq run ... --profile dev
225+ "baseUrl": "https://jsonplaceholder.typicode.com"
226+ // Example substitutions:
227+ // "apiKey": "{env:API_KEY}",
228+ // "authToken": "{file:./secrets/token.txt}"
238229 },
239- });
230+ "defaults": {
231+ "timeoutMs": 30000
232+ },
233+ // Uncomment to persist cookies between runs:
234+ // "cookies": {
235+ // "enabled": true,
236+ // "jarPath": ".treq/cookies.json"
237+ // },
238+ "profiles": {
239+ "dev": {
240+ "variables": { "baseUrl": "http://localhost:3000" },
241+ "defaults": { "validateSSL": false }
242+ },
243+ "prod": {
244+ "variables": { "baseUrl": "https://api.example.com" }
245+ }
246+ }
247+ }
240248` ;
241249}
242250
@@ -245,11 +253,32 @@ export function generateRunScript(runtime: Runtime): string {
245253
246254 return `${ shebang }
247255import { createClient } from '@t-req/core';
256+ import { resolveProjectConfig } from '@t-req/core/config';
257+ ${ runtime === 'node' ? "import { createNodeIO } from '@t-req/core/runtime';" : '' }
258+
259+ function getFlagValue(name: string): string | undefined {
260+ const idx = process.argv.indexOf(name);
261+ if (idx === -1) return undefined;
262+ const value = process.argv[idx + 1];
263+ return value && !value.startsWith('-') ? value : undefined;
264+ }
265+
266+ const profile = getFlagValue('--profile') ?? process.env.TREQ_PROFILE;
267+ const { config, meta } = await resolveProjectConfig({
268+ startDir: process.cwd(),
269+ profile: profile || undefined,
270+ });
271+
272+ for (const warning of meta.warnings) {
273+ console.warn(\`Warning: \${warning}\`);
274+ }
248275
249276const client = createClient({
250- variables: {
251- baseUrl: 'https://jsonplaceholder.typicode.com',
252- },
277+ ${ runtime === 'node' ? 'io: createNodeIO(),' : '' }
278+ variables: config.variables,
279+ // Map config defaults into the client (timeout + headers/redirects/ssl/proxy).
280+ timeout: config.defaults.timeoutMs,
281+ defaults: config.defaults,
253282});
254283
255284// Example: Get a user
@@ -298,36 +327,9 @@ dist/
298327.env
299328.env.local
300329*.log
301- ` ;
302- }
303-
304- export function generateDevEnvironment ( ) : string {
305- return `// Development environment
306- export default {
307- baseUrl: 'http://localhost:3000',
308- };
309- ` ;
310- }
311-
312- export function generateProdEnvironment ( ) : string {
313- return `// Production environment
314- export default {
315- baseUrl: 'https://api.example.com',
316- };
317- ` ;
318- }
319330
320- export function generateFolderDefaults ( ) : string {
321- return `// Folder defaults - requests in this folder inherit these settings
322- export default {
323- auth: {
324- type: 'bearer',
325- token: '{{authToken}}',
326- },
327- headers: {
328- 'X-Custom-Header': 'value',
329- },
330- };
331+ # t-req local state
332+ .treq/cookies.json
331333` ;
332334}
333335
0 commit comments