@arkenv/cli 0.2.8 → 0.2.9
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/dist/index.cjs +15 -15
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -108,10 +108,10 @@ ${t?(0,a.styleText)(`cyan`,V):``}
|
|
|
108
108
|
outdir: "./dist",
|
|
109
109
|
${y.default.green(`plugins: [arkenv]`)}
|
|
110
110
|
});
|
|
111
|
-
`),{success:!0,instructions:a.trim()}}async function Dt(e,t){let n=t||await J();if(!n)return{status:`not_found`};let i=r.default.basename(n);try{let t=await e.readFile(n);if((0,u.parse)(t)?.compilerOptions?.strict===!0)return{status:`already_strict`,file:i};let r=(0,u.applyEdits)(t,(0,u.modify)(t,[`compilerOptions`,`strict`],!0,{formattingOptions:{insertSpaces:!0,tabSize:2}}));return await e.writeFile(n,r),{status:`updated`,file:i}}catch{return{status:`error`,file:i}}}var Ot=class{constructor(e,t){this.isQuiet=e,this.stdio=t}async exists(e){try{return await n.default.access(e),!0}catch{return!1}}async readFile(e){return n.default.readFile(e,`utf-8`)}async writeFile(e,t){await n.default.writeFile(e,t,`utf-8`)}async mkdir(e,t){await n.default.mkdir(e,{recursive:t})}async execute(e,t=[],n){return new Promise((r,i)=>{let a=(0,d.spawn)(e,t,{cwd:n,stdio:this.isQuiet?`pipe`:this.stdio,shell:!1}),o=``,s=``,c=1e4;this.isQuiet&&(a.stdout?.on(`data`,e=>{o=(o+e.toString()).slice(-c)}),a.stderr?.on(`data`,e=>{s=(s+e.toString()).slice(-c)})),a.on(`close`,(e,t)=>{if(e===0)r();else{let n=e===null?`Command terminated by signal ${t}`:`Command failed with code ${e}`;this.isQuiet&&(o&&(n+=`\n${y.default.dim(`STDOUT:`)}\n${o}`),s&&(n+=`\n${y.default.red(`STDERR:`)}\n${s}`)),i(Error(n))}}),a.on(`error`,i)})}async updateTsConfigToStrict(e){return Dt(this,e)}async findViteConfig(e){return Ct(e)}async findBunConfig(e){return wt(e)}async bootstrapViteConfig(e,t){return Tt(this,e,t)}async bootstrapBunConfig(e,t){return Et(e,t)}async safeAppend(e,t,n){let{safeAppend:r}=await Promise.resolve().then(()=>p);return r(e,t,n)}};async function kt(e){let t=e.defaultPath||`./src/env.ts`;if(e.hasEnvSchemaFile){let e=await U({message:y.default.yellow(`An existing ArkEnv configuration was found at ${Y(t)}. Do you want to overwrite it?`),initialValue:!1,active:`Yes (override my configuration)`,inactive:`No (abort)`});return I(e)||!e?null:e}return!0}async function At(e){let t=e.detectedKeys,n=e.keysSource||`.env.example`;if(t&&t.length>0){let e=t.length,r=e===1,i=await U({message:n===`.env.example`?`Detected ${Y(`.env.example`)} with ${e} ${r?`key`:`keys`}. Use ${r?`it`:`them`} for your schema?`:`Detected ${e} environment variable${r?``:`s`} used in your project. Use ${r?`it`:`them`} for your schema?`,active:`Yes (Recommended)`,initialValue:!0});return I(i)?null:i}return!1}async function jt(e){if(e.framework===`vite`||e.framework===`bun-fullstack`){let t=e.framework===`vite`?`vite-env.d.ts`:`bun-env.d.ts`;if(e.hasTypeFile)return!0;let n=await U({message:`Establish ${Y(t)} for typesafe environment variables?`,initialValue:!0,active:`Yes (Recommended)`,inactive:`No`});return I(n)?null:n}return!0}async function Mt(e){if(!e.installTypeDefinitions||e.framework!==`vite`&&e.framework!==`bun-fullstack`)return`skip`;let t=e.framework===`vite`?`vite-env.d.ts`:`bun-env.d.ts`;if(e.hasTypeFile){let e=await K({message:`Found existing ${Y(t)}. How should we handle ArkEnv types?`,options:[{value:`append`,label:`Append types safely (if needed)`,hint:`Recommended`},{value:`overwrite`,label:`Overwrite entirely`,hint:`Destructive`},{value:`skip`,label:`Skip`}]});return I(e)?null:e}return`overwrite`}async function Nt(e){let t=await K({message:`Select an example:`,options:e.examples.map(e=>({value:e.id,label:e.name,...e.description?{hint:e.description}:{}}))});return I(t)?null:t}async function Pt(e){let t=await K({message:`Select your framework or build tool:`,initialValue:e.framework,options:[{value:`vanilla`,label:`Vanilla${e.framework===`vanilla`?` (Detected)`:``}`,hint:`Node.js, Bun, server-side or runtime-only validation`},{value:`vite`,label:`Vite${e.framework===`vite`?` (Detected)`:``}`,hint:`Client-side and static inlining validation`},{value:`bun-fullstack`,label:`Bun fullstack dev server${e.framework===`bun-fullstack`?` (Detected)`:``}`,hint:`Client-side bundling and Bun.serve integration`},{value:`nextjs`,label:`Next.js${e.framework===`nextjs`?` (Detected)`:``}`,hint:`Next.js App or Pages Router with build and runtime validation`}]});return I(t)?null:t}async function Ft(e){let t=await U({message:`Optional: Would you also like to bootstrap a custom Bun.build script for programmatic bundling?`,initialValue:e.initialValue??!1});return I(t)?null:t}async function It(){let e=await K({message:`Select your preferred validator library:`,options:[{value:`arktype`,label:`ArkType (Recommended)`,hint:`TypeScript's 1:1 validator, optimized from editor to runtime`},{value:`zod`,label:`Zod`,hint:`TypeScript-first schema validation with static type inference`},{value:`valibot`,label:`Valibot`,hint:`The modular and type safe schema library`}]});return I(e)?null:e}async function Lt(){let e=await K({message:`How would you like to structure your environment variables?`,options:[{value:`simple`,label:`Simple`,hint:`(Recommended) A single env.ts file for the best DX`},{value:`strict`,label:`Strict`,hint:`Separate shared, client, and server files for hard bundle boundaries.`}]});return I(e)?null:e}async function Rt(e){let t=await U({message:`Enable automated Next.js code generation for environment variables?`,initialValue:e.initialValue??!0,active:`Yes (Recommended)`,inactive:`No`});return I(t)?null:t}async function zt(e){let t=await U({message:`Use default config path (${Y(e.defaultEnvPath||`./src/env.ts`)})?`,initialValue:!0,active:`Yes (Recommended)`,inactive:`No, let me customize it`});return I(t)?null:t}async function Bt(e){let t=e.defaultEnvPath||`./src/env.ts`;if(!e.useDefaultPath){let e=await $e({message:`Where should we create the ArkEnv config?`,placeholder:t,initialValue:t,defaultValue:t});if(I(e))return null;let n=typeof e==`string`?e.trim():``;return n===``?t:n}return t}const X={example:Nt,overwriteEnvSchemaFile:kt,framework:Pt,bunBuild:Ft,layout:Lt,nextjsCodegen:Rt,useDefaultPath:zt,path:Bt,installTypeDefinitions:jt,envDtsHandling:Mt,validator:It,useEnvExample:At};async function Vt(e,t=!1){return(e?.mode||`existing`)===`new`?Ht(e,t):Ut(e,t)}async function Ht(e,t=!1){let n=e?.examples||[],i=`arkenv-project`;try{let a;if(e?.name){let t=e.name.trim();a=t===`.`||t===`./`||r.default.resolve(process.cwd(),t)===process.cwd()?`.`:t}else if(t)a=i;else{let e=(Z(await $e({message:`Project name:`,placeholder:i,defaultValue:i}))||i).trim();a=e===`.`||e===`./`||r.default.resolve(process.cwd(),e)===process.cwd()?`.`:e}let o=e?.example;!o&&!t?o=Z(await X.example({examples:n})):!o&&t&&(o=`basic`);let s=n.find(e=>e.id===o);if(!s){let e=n.map(e=>e.id).join(`, `);throw Error(`Unknown example ${o}. Available examples: ${e}`)}return{mode:`new`,example:s.id,name:a,path:`./src/env.ts`,validator:`arktype`,framework:s.framework,language:`ts`,installSkill:!1}}catch(e){if(e instanceof Gt)return null;throw e}}async function Ut(e,n=!1){let r=e?.defaultEnvPath||`./src/env.ts`,i=e?.envKeys||null,a=e?.envKeysSource||`.env.example`;if(n){let n=e?.framework||`vanilla`,a;n===`nextjs`&&(a=e?.isStrict?`strict`:(e?.isSimple,`simple`));let o=await Wt(e,n,r),s;return(n===`vite`||n===`bun-fullstack`)&&(s=o?`append`:`overwrite`),(0,t.shake)({mode:`existing`,path:r,validator:`arktype`,framework:n,layout:a,bunFeatures:n===`bun-fullstack`?e?.bunFeatures??[`serve`]:void 0,language:`ts`,overwriteEnvSchemaFile:!0,installTypeDefinitions:n===`vite`||n===`bun-fullstack`,installSkill:!1,envDtsHandling:s,envKeys:i??void 0,disableCodegen:e?.disableCodegen??!1})}try{let n=Z(await X.overwriteEnvSchemaFile({hasEnvSchemaFile:e?.hasEnvSchemaFile??!1,defaultPath:r})),o=Z(await X.framework({framework:e?.framework})),s;o===`nextjs`&&(s=e?.isStrict?`strict`:e?.isSimple?`simple`:Z(await X.layout()));let c;if(o===`bun-fullstack`){let t=!!e?.bunFeatures?.includes(`build`);c=Z(await X.bunBuild({initialValue:t}))}let l=e?.disableCodegen;o===`nextjs`&&l===void 0&&(l=!Z(await X.nextjsCodegen({initialValue:!0})));let u=Z(await X.useDefaultPath({defaultEnvPath:r})),d=Z(await X.path({useDefaultPath:u,defaultEnvPath:r})),f=await Wt(e,o,d),p=Z(await X.installTypeDefinitions({framework:o,hasTypeFile:f})),m=Z(await X.envDtsHandling({framework:o,installTypeDefinitions:p,hasTypeFile:f})),h=Z(await X.validator()),g=Z(await X.useEnvExample({detectedKeys:i,keysSource:a}));return(0,t.shake)({mode:`existing`,overwriteEnvSchemaFile:n,framework:o,layout:s,path:d,installTypeDefinitions:p,envDtsHandling:m,validator:h,bunFeatures:o===`bun-fullstack`?c?[`serve`,`build`]:[`serve`]:void 0,language:`ts`,installSkill:!1,envKeys:g?i??void 0:void 0,disableCodegen:l})}catch(e){if(e instanceof Gt)return null;throw e}}async function Wt(e,t,n){return await e?.hasTypeFileAtPath?.({framework:t,envPath:n})??e?.hasTypeFile??!1}var Gt=class extends Error{constructor(){super(`Operation cancelled`)}};function Z(e){if(e===null||I(e))throw Ge(`Operation cancelled`),new Gt;return e}var Kt=class{async confirm(e,n=!0,r,i){let a=await U((0,t.shake)({message:e,initialValue:n,active:r,inactive:i}));return I(a)?null:a}async runWizard(e,t=!1){return Vt(e,t)}};const qt={isYes:{long:`--yes`,short:`-y`,kind:`boolean`},isForce:{long:`--force`,short:`-f`,kind:`boolean`},isQuiet:{long:`--quiet`,short:`-q`,kind:`boolean`},isJson:{long:`--json`,short:`-j`,kind:`boolean`},isAgent:{long:`--agent`,short:`-a`,kind:`boolean`},helpRequested:{long:`--help`,short:`-h`,kind:`boolean`},example:{long:`--example`,short:`-e`,kind:`value`},isStrict:{long:`--strict`,short:``,kind:`boolean`},isSimple:{long:`--simple`,short:``,kind:`boolean`},noCodegen:{long:`--no-codegen`,short:`-C`,kind:`boolean`}},Jt=new Set(Object.values(qt).flatMap(e=>[e.long,e.short].filter(Boolean))),Yt=new Set(Object.values(qt).filter(e=>e.kind===`value`).flatMap(e=>[e.long,e.short].filter(Boolean)));var Xt=class{constructor(e,t={}){let n=e.slice(2),r=[],i=!1;for(let e of n){if(i){r.push(e),i=!1;continue}if(Yt.has(e)){r.push(e),i=!0;continue}if(/^-[a-zA-Z]{2,}$/.test(e)){let t=e.slice(1).split(``);for(let e of t)r.push(`-${e}`);Yt.has(`-${t[t.length-1]}`)&&(i=!0)}else r.push(e)}this.args=r,this.command=this.args[0];let a=1,o=[];for(this.validationError=void 0;a<this.args.length;){let e=this.args[a];if(e.startsWith(`-`)){if(!Jt.has(e)){this.validationError=`Unknown argument: ${e}`;break}if(Yt.has(e))if(a+1<this.args.length&&!this.args[a+1].startsWith(`-`))a+=2;else{this.validationError=`Missing value for option: ${e}`;break}else a+=1}else o.push(e),a+=1}this.validationError||(o.length>1?this.validationError=`Unknown argument: ${o[1]}`:this.name=o[0]),this.logger=t.logger||new tt({isQuiet:this.isQuiet,isJson:this.isJson,isYes:this.isYes})}get isAgent(){return this.hasFlag(`isAgent`)}get isYes(){return this.isAgent||this.hasFlag(`isYes`)}get isQuiet(){return this.isAgent||this.hasFlag(`isQuiet`)}get isJson(){return this.isAgent||this.hasFlag(`isJson`)}get isForce(){return this.hasFlag(`isForce`)}get helpRequested(){return this.hasFlag(`helpRequested`)}get example(){let e=qt.example;return this.getFlagValue(e.long,e.short)}get isStrict(){return this.hasFlag(`isStrict`)}get isSimple(){return this.hasFlag(`isSimple`)}get noCodegen(){return this.hasFlag(`noCodegen`)}hasFlag(e){let t=qt[e];return this.args.includes(t.long)||!!t.short&&this.args.includes(t.short)}get initInput(){let e={isYes:this.isYes,isForce:this.isForce,isQuiet:this.isQuiet,isAgent:this.isAgent,isStrict:this.isStrict,isSimple:this.isSimple};return this.example!==void 0&&(e.example=this.example),this.name!==void 0&&(e.name=this.name),this.noCodegen&&(e.noCodegen=!0),e}getFlagValue(e,t){let n=this.args.findIndex(n=>n===e||n===t);if(n!==-1&&this.args[n+1]&&!this.args[n+1].startsWith(`-`))return this.args[n+1]}},Zt=`0.2.8`;function Qt(e,t=2,n=4){let r=Math.max(...e.map(e=>e.left.length),0),i=` `.repeat(t);return e.map(e=>{let t=` `.repeat(r-e.left.length+n);return`${i}${e.left}${t}${e.right}`})}var $t=class{constructor(e){this.logger=e}async execute(){let e=[{left:`arkenv init [project-name]`,right:`Set up ArkEnv in your project`}],t=[{left:`--yes, -y`,right:`Skip prompts and use defaults (also passed to subprocesses)`},{left:`--force, -f`,right:`Bypass technical requirement checks and force scaffolding`},{left:`--agent, -a`,right:`Enable non-interactive, machine-readable mode for AI agents. Bypasses all prompts and outputs structured JSON. Macro for --yes --quiet --json`},{left:`--example, -e`,right:`Specify an example name when creating a new project`},{left:`--quiet, -q`,right:`Quiet mode: Suppress output, capture logs on failure`},{left:`--json, -j`,right:`Output structured JSON to stdout`},{left:`--no-codegen, -C`,right:`Disable automatic env.gen.ts code generation for Next.js`},{left:`--help, -h`,right:`Show this help message`}];this.logger.log(`ArkEnv CLI v${Zt}`),this.logger.log(`\n${y.default.bold(`Usage:`)}`);for(let t of Qt(e))this.logger.log(t);this.logger.log(`\n${y.default.bold(`Options:`)}`);for(let e of Qt(t))this.logger.log(e)}};function en(e,t,n,r){let{extraImports:i,serverField:a,clientField:o,sharedField:s,defaultServerFields:c,defaultClientFields:l,defaultSharedFields:u}=t,d=[],f=[],p=[];if(e&&e.length>0)for(let t of e)t.startsWith(`NEXT_PUBLIC_`)?f.push(o(t)):t===`NODE_ENV`||t===`PORT`?p.push(s(t,t===`PORT`)):d.push(a(t));else d.push(...c),f.push(...l),p.push(...u);let m=[];if(d.length>0&&m.push(`\tserver: {\n${d.join(`
|
|
111
|
+
`),{success:!0,instructions:a.trim()}}async function Dt(e,t){let n=t||await J();if(!n)return{status:`not_found`};let i=r.default.basename(n);try{let t=await e.readFile(n);if((0,u.parse)(t)?.compilerOptions?.strict===!0)return{status:`already_strict`,file:i};let r=(0,u.applyEdits)(t,(0,u.modify)(t,[`compilerOptions`,`strict`],!0,{formattingOptions:{insertSpaces:!0,tabSize:2}}));return await e.writeFile(n,r),{status:`updated`,file:i}}catch{return{status:`error`,file:i}}}var Ot=class{constructor(e,t){this.isQuiet=e,this.stdio=t}async exists(e){try{return await n.default.access(e),!0}catch{return!1}}async readFile(e){return n.default.readFile(e,`utf-8`)}async writeFile(e,t){await n.default.writeFile(e,t,`utf-8`)}async mkdir(e,t){await n.default.mkdir(e,{recursive:t})}async execute(e,t=[],n){return new Promise((r,i)=>{let a=(0,d.spawn)(e,t,{cwd:n,stdio:this.isQuiet?`pipe`:this.stdio,shell:!1}),o=``,s=``,c=1e4;this.isQuiet&&(a.stdout?.on(`data`,e=>{o=(o+e.toString()).slice(-c)}),a.stderr?.on(`data`,e=>{s=(s+e.toString()).slice(-c)})),a.on(`close`,(e,t)=>{if(e===0)r();else{let n=e===null?`Command terminated by signal ${t}`:`Command failed with code ${e}`;this.isQuiet&&(o&&(n+=`\n${y.default.dim(`STDOUT:`)}\n${o}`),s&&(n+=`\n${y.default.red(`STDERR:`)}\n${s}`)),i(Error(n))}}),a.on(`error`,i)})}async updateTsConfigToStrict(e){return Dt(this,e)}async findViteConfig(e){return Ct(e)}async findBunConfig(e){return wt(e)}async bootstrapViteConfig(e,t){return Tt(this,e,t)}async bootstrapBunConfig(e,t){return Et(e,t)}async safeAppend(e,t,n){let{safeAppend:r}=await Promise.resolve().then(()=>p);return r(e,t,n)}};async function kt(e){let t=e.defaultPath||`./src/env.ts`;if(e.hasEnvSchemaFile){let e=await U({message:y.default.yellow(`An existing ArkEnv configuration was found at ${Y(t)}. Do you want to overwrite it?`),initialValue:!1,active:`Yes (override my configuration)`,inactive:`No (abort)`});return I(e)||!e?null:e}return!0}async function At(e){let t=e.detectedKeys,n=e.keysSource||`.env.example`;if(t&&t.length>0){let e=t.length,r=e===1,i=await U({message:n===`.env.example`?`Detected ${Y(`.env.example`)} with ${e} ${r?`key`:`keys`}. Use ${r?`it`:`them`} for your schema?`:`Detected ${e} environment variable${r?``:`s`} used in your project. Use ${r?`it`:`them`} for your schema?`,active:`Yes (Recommended)`,initialValue:!0});return I(i)?null:i}return!1}async function jt(e){if(e.framework===`vite`||e.framework===`bun-fullstack`){let t=e.framework===`vite`?`vite-env.d.ts`:`bun-env.d.ts`;if(e.hasTypeFile)return!0;let n=await U({message:`Establish ${Y(t)} for typesafe environment variables?`,initialValue:!0,active:`Yes (Recommended)`,inactive:`No`});return I(n)?null:n}return!0}async function Mt(e){if(!e.installTypeDefinitions||e.framework!==`vite`&&e.framework!==`bun-fullstack`)return`skip`;let t=e.framework===`vite`?`vite-env.d.ts`:`bun-env.d.ts`;if(e.hasTypeFile){let e=await K({message:`Found existing ${Y(t)}. How should we handle ArkEnv types?`,options:[{value:`append`,label:`Append types safely (if needed)`,hint:`Recommended`},{value:`overwrite`,label:`Overwrite entirely`,hint:`Destructive`},{value:`skip`,label:`Skip`}]});return I(e)?null:e}return`overwrite`}async function Nt(e){let t=await K({message:`Select an example:`,options:e.examples.map(e=>({value:e.id,label:e.name,...e.description?{hint:e.description}:{}}))});return I(t)?null:t}async function Pt(e){let t=await K({message:`Select your framework or build tool:`,initialValue:e.framework,options:[{value:`vanilla`,label:`Vanilla${e.framework===`vanilla`?` (Detected)`:``}`,hint:`Node.js, Bun, server-side or runtime-only validation`},{value:`vite`,label:`Vite${e.framework===`vite`?` (Detected)`:``}`,hint:`Client-side and static inlining validation`},{value:`bun-fullstack`,label:`Bun fullstack dev server${e.framework===`bun-fullstack`?` (Detected)`:``}`,hint:`Client-side bundling and Bun.serve integration`},{value:`nextjs`,label:`Next.js${e.framework===`nextjs`?` (Detected)`:``}`,hint:`Next.js App or Pages Router with build and runtime validation`}]});return I(t)?null:t}async function Ft(e){let t=await U({message:`Optional: Would you also like to bootstrap a custom Bun.build script for programmatic bundling?`,initialValue:e.initialValue??!1});return I(t)?null:t}async function It(){let e=await K({message:`Select your preferred validator library:`,options:[{value:`arktype`,label:`ArkType (Recommended)`,hint:`TypeScript's 1:1 validator, optimized from editor to runtime`},{value:`zod`,label:`Zod`,hint:`TypeScript-first schema validation with static type inference`},{value:`valibot`,label:`Valibot`,hint:`The modular and type safe schema library`}]});return I(e)?null:e}async function Lt(){let e=await K({message:`How would you like to structure your environment variables?`,options:[{value:`simple`,label:`Simple (Recommended)`,hint:`A single env.ts file for the best DX`},{value:`strict`,label:`Strict`,hint:`Separate shared, client, and server files for hard bundle boundaries.`}]});return I(e)?null:e}async function Rt(e){let t=await U({message:`Enable automated Next.js code generation for environment variables?`,initialValue:e.initialValue??!0,active:`Yes (Recommended)`,inactive:`No`});return I(t)?null:t}async function zt(e){let t=await U({message:`Use default config path (${Y(e.defaultEnvPath||`./src/env.ts`)})?`,initialValue:!0,active:`Yes (Recommended)`,inactive:`No, let me customize it`});return I(t)?null:t}async function Bt(e){let t=e.defaultEnvPath||`./src/env.ts`;if(!e.useDefaultPath){let e=await $e({message:`Where should we create the ArkEnv config?`,placeholder:t,initialValue:t,defaultValue:t});if(I(e))return null;let n=typeof e==`string`?e.trim():``;return n===``?t:n}return t}const X={example:Nt,overwriteEnvSchemaFile:kt,framework:Pt,bunBuild:Ft,layout:Lt,nextjsCodegen:Rt,useDefaultPath:zt,path:Bt,installTypeDefinitions:jt,envDtsHandling:Mt,validator:It,useEnvExample:At};async function Vt(e,t=!1){return(e?.mode||`existing`)===`new`?Ht(e,t):Ut(e,t)}async function Ht(e,t=!1){let n=e?.examples||[],i=`arkenv-project`;try{let a;if(e?.name){let t=e.name.trim();a=t===`.`||t===`./`||r.default.resolve(process.cwd(),t)===process.cwd()?`.`:t}else if(t)a=i;else{let e=(Z(await $e({message:`Project name:`,placeholder:i,defaultValue:i}))||i).trim();a=e===`.`||e===`./`||r.default.resolve(process.cwd(),e)===process.cwd()?`.`:e}let o=e?.example;!o&&!t?o=Z(await X.example({examples:n})):!o&&t&&(o=`basic`);let s=n.find(e=>e.id===o);if(!s){let e=n.map(e=>e.id).join(`, `);throw Error(`Unknown example ${o}. Available examples: ${e}`)}return{mode:`new`,example:s.id,name:a,path:`./src/env.ts`,validator:`arktype`,framework:s.framework,language:`ts`,installSkill:!1}}catch(e){if(e instanceof Gt)return null;throw e}}async function Ut(e,n=!1){let r=e?.defaultEnvPath||`./src/env.ts`,i=e?.envKeys||null,a=e?.envKeysSource||`.env.example`;if(n){let n=e?.framework||`vanilla`,a;n===`nextjs`&&(a=e?.isStrict?`strict`:(e?.isSimple,`simple`));let o=await Wt(e,n,r),s;return(n===`vite`||n===`bun-fullstack`)&&(s=o?`append`:`overwrite`),(0,t.shake)({mode:`existing`,path:r,validator:`arktype`,framework:n,layout:a,bunFeatures:n===`bun-fullstack`?e?.bunFeatures??[`serve`]:void 0,language:`ts`,overwriteEnvSchemaFile:!0,installTypeDefinitions:n===`vite`||n===`bun-fullstack`,installSkill:!1,envDtsHandling:s,envKeys:i??void 0,disableCodegen:e?.disableCodegen??!1})}try{let n=Z(await X.overwriteEnvSchemaFile({hasEnvSchemaFile:e?.hasEnvSchemaFile??!1,defaultPath:r})),o=Z(await X.framework({framework:e?.framework})),s;o===`nextjs`&&(s=e?.isStrict?`strict`:e?.isSimple?`simple`:Z(await X.layout()));let c;if(o===`bun-fullstack`){let t=!!e?.bunFeatures?.includes(`build`);c=Z(await X.bunBuild({initialValue:t}))}let l=e?.disableCodegen;o===`nextjs`&&l===void 0&&(l=!Z(await X.nextjsCodegen({initialValue:!0})));let u=Z(await X.useDefaultPath({defaultEnvPath:r})),d=Z(await X.path({useDefaultPath:u,defaultEnvPath:r})),f=await Wt(e,o,d),p=Z(await X.installTypeDefinitions({framework:o,hasTypeFile:f})),m=Z(await X.envDtsHandling({framework:o,installTypeDefinitions:p,hasTypeFile:f})),h=Z(await X.validator()),g=Z(await X.useEnvExample({detectedKeys:i,keysSource:a}));return(0,t.shake)({mode:`existing`,overwriteEnvSchemaFile:n,framework:o,layout:s,path:d,installTypeDefinitions:p,envDtsHandling:m,validator:h,bunFeatures:o===`bun-fullstack`?c?[`serve`,`build`]:[`serve`]:void 0,language:`ts`,installSkill:!1,envKeys:g?i??void 0:void 0,disableCodegen:l})}catch(e){if(e instanceof Gt)return null;throw e}}async function Wt(e,t,n){return await e?.hasTypeFileAtPath?.({framework:t,envPath:n})??e?.hasTypeFile??!1}var Gt=class extends Error{constructor(){super(`Operation cancelled`)}};function Z(e){if(e===null||I(e))throw Ge(`Operation cancelled`),new Gt;return e}var Kt=class{async confirm(e,n=!0,r,i){let a=await U((0,t.shake)({message:e,initialValue:n,active:r,inactive:i}));return I(a)?null:a}async runWizard(e,t=!1){return Vt(e,t)}};const qt={isYes:{long:`--yes`,short:`-y`,kind:`boolean`},isForce:{long:`--force`,short:`-f`,kind:`boolean`},isQuiet:{long:`--quiet`,short:`-q`,kind:`boolean`},isJson:{long:`--json`,short:`-j`,kind:`boolean`},isAgent:{long:`--agent`,short:`-a`,kind:`boolean`},helpRequested:{long:`--help`,short:`-h`,kind:`boolean`},example:{long:`--example`,short:`-e`,kind:`value`},isStrict:{long:`--strict`,short:``,kind:`boolean`},isSimple:{long:`--simple`,short:``,kind:`boolean`},noCodegen:{long:`--no-codegen`,short:`-C`,kind:`boolean`}},Jt=new Set(Object.values(qt).flatMap(e=>[e.long,e.short].filter(Boolean))),Yt=new Set(Object.values(qt).filter(e=>e.kind===`value`).flatMap(e=>[e.long,e.short].filter(Boolean)));var Xt=class{constructor(e,t={}){let n=e.slice(2),r=[],i=!1;for(let e of n){if(i){r.push(e),i=!1;continue}if(Yt.has(e)){r.push(e),i=!0;continue}if(/^-[a-zA-Z]{2,}$/.test(e)){let t=e.slice(1).split(``);for(let e of t)r.push(`-${e}`);Yt.has(`-${t[t.length-1]}`)&&(i=!0)}else r.push(e)}this.args=r,this.command=this.args[0];let a=1,o=[];for(this.validationError=void 0;a<this.args.length;){let e=this.args[a];if(e.startsWith(`-`)){if(!Jt.has(e)){this.validationError=`Unknown argument: ${e}`;break}if(Yt.has(e))if(a+1<this.args.length&&!this.args[a+1].startsWith(`-`))a+=2;else{this.validationError=`Missing value for option: ${e}`;break}else a+=1}else o.push(e),a+=1}this.validationError||(o.length>1?this.validationError=`Unknown argument: ${o[1]}`:this.name=o[0]),this.logger=t.logger||new tt({isQuiet:this.isQuiet,isJson:this.isJson,isYes:this.isYes})}get isAgent(){return this.hasFlag(`isAgent`)}get isYes(){return this.isAgent||this.hasFlag(`isYes`)}get isQuiet(){return this.isAgent||this.hasFlag(`isQuiet`)}get isJson(){return this.isAgent||this.hasFlag(`isJson`)}get isForce(){return this.hasFlag(`isForce`)}get helpRequested(){return this.hasFlag(`helpRequested`)}get example(){let e=qt.example;return this.getFlagValue(e.long,e.short)}get isStrict(){return this.hasFlag(`isStrict`)}get isSimple(){return this.hasFlag(`isSimple`)}get noCodegen(){return this.hasFlag(`noCodegen`)}hasFlag(e){let t=qt[e];return this.args.includes(t.long)||!!t.short&&this.args.includes(t.short)}get initInput(){let e={isYes:this.isYes,isForce:this.isForce,isQuiet:this.isQuiet,isAgent:this.isAgent,isStrict:this.isStrict,isSimple:this.isSimple};return this.example!==void 0&&(e.example=this.example),this.name!==void 0&&(e.name=this.name),this.noCodegen&&(e.noCodegen=!0),e}getFlagValue(e,t){let n=this.args.findIndex(n=>n===e||n===t);if(n!==-1&&this.args[n+1]&&!this.args[n+1].startsWith(`-`))return this.args[n+1]}},Zt=`0.2.9`;function Qt(e,t=2,n=4){let r=Math.max(...e.map(e=>e.left.length),0),i=` `.repeat(t);return e.map(e=>{let t=` `.repeat(r-e.left.length+n);return`${i}${e.left}${t}${e.right}`})}var $t=class{constructor(e){this.logger=e}async execute(){let e=[{left:`arkenv init [project-name]`,right:`Set up ArkEnv in your project`}],t=[{left:`--yes, -y`,right:`Skip prompts and use defaults (also passed to subprocesses)`},{left:`--force, -f`,right:`Bypass technical requirement checks and force scaffolding`},{left:`--agent, -a`,right:`Enable non-interactive, machine-readable mode for AI agents. Bypasses all prompts and outputs structured JSON. Macro for --yes --quiet --json`},{left:`--example, -e`,right:`Specify an example name when creating a new project`},{left:`--quiet, -q`,right:`Quiet mode: Suppress output, capture logs on failure`},{left:`--json, -j`,right:`Output structured JSON to stdout`},{left:`--no-codegen, -C`,right:`Disable automatic env.gen.ts code generation for Next.js`},{left:`--help, -h`,right:`Show this help message`}];this.logger.log(`ArkEnv CLI v${Zt}`),this.logger.log(`\n${y.default.bold(`Usage:`)}`);for(let t of Qt(e))this.logger.log(t);this.logger.log(`\n${y.default.bold(`Options:`)}`);for(let e of Qt(t))this.logger.log(e)}};function en(e,t,n,r){let{extraImports:i,serverField:a,clientField:o,sharedField:s,defaultServerFields:c,defaultClientFields:l,defaultSharedFields:u}=t,d=[],f=[],p=[];if(e&&e.length>0)for(let t of e)t.startsWith(`NEXT_PUBLIC_`)?f.push(o(t)):t===`NODE_ENV`?p.push(s(t)):d.push(a(t));else d.push(...c),f.push(...l),p.push(...u);let m=[];if(d.length>0&&m.push(`\tserver: {\n${d.join(`
|
|
112
112
|
`)}\n\t}`),f.length>0&&m.push(`\tclient: {\n${f.join(`
|
|
113
113
|
`)}\n\t}`),p.length>0&&m.push(`\tshared: {\n${p.join(`
|
|
114
|
-
`)}\n\t}`),r){let t=[];if(e&&e.length>0)for(let n of e)(n.startsWith(`NEXT_PUBLIC_`)||n===`NODE_ENV
|
|
114
|
+
`)}\n\t}`),r){let t=[];if(e&&e.length>0)for(let n of e)(n.startsWith(`NEXT_PUBLIC_`)||n===`NODE_ENV`)&&t.push(`\t\t${n}: process.env.${n},`);else t.push(` NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,`,` NODE_ENV: process.env.NODE_ENV,`);return m.push(`\truntimeEnv: {\n${t.join(`
|
|
115
115
|
`)}\n\t}`),`${[`import arkenv from "@arkenv/nextjs";`,...i?[i]:[]].join(`
|
|
116
116
|
`)}
|
|
117
117
|
|
|
@@ -124,15 +124,15 @@ export const env = arkenv({
|
|
|
124
124
|
${m.join(`,
|
|
125
125
|
`)},
|
|
126
126
|
});
|
|
127
|
-
`}return`${[`import
|
|
127
|
+
`}return`${[`import arkenv from "${n||`./generated/env.gen`}";`,...i?[i]:[]].join(`
|
|
128
128
|
`)}
|
|
129
129
|
|
|
130
130
|
/**
|
|
131
131
|
* Environment variable schema.
|
|
132
|
-
* In Next.js, use the generated \`
|
|
132
|
+
* In Next.js, use the generated \`arkenv\` from \`env.gen.ts\` to validate variables.
|
|
133
133
|
* Enforces client/server separation and prevents secret leaks.
|
|
134
134
|
*/
|
|
135
|
-
export const env =
|
|
135
|
+
export const env = arkenv({
|
|
136
136
|
${m.join(`,
|
|
137
137
|
`)},
|
|
138
138
|
});
|
|
@@ -160,7 +160,7 @@ ${m.join(`,
|
|
|
160
160
|
export const Env = type({
|
|
161
161
|
${a}
|
|
162
162
|
});
|
|
163
|
-
`:t===`nextjs`?en(e,{serverField:e=>`\t\t${e}: "string?",`,clientField:e=>`\t\t${e}: "string?",`,sharedField:
|
|
163
|
+
`:t===`nextjs`?en(e,{serverField:e=>`\t\t${e}: "string?",`,clientField:e=>`\t\t${e}: "string?",`,sharedField:e=>`\t\t${e}: "'development' | 'production' | 'test' = 'development'",`,defaultServerFields:[` DATABASE_URL: "string = 'postgres://localhost:5432/mydb'",`],defaultClientFields:[` NEXT_PUBLIC_API_URL: "string = 'https://api.example.com'",`],defaultSharedFields:[` NODE_ENV: "'development' | 'production' | 'test' = 'development'",`]},n,r):i.default`
|
|
164
164
|
import arkenv, { type } from "arkenv";
|
|
165
165
|
|
|
166
166
|
/**
|
|
@@ -220,7 +220,7 @@ ${a}
|
|
|
220
220
|
export const Env = type({
|
|
221
221
|
${a}
|
|
222
222
|
});
|
|
223
|
-
`:t===`nextjs`?en(e,{extraImports:`import * as v from "valibot";`,serverField:e=>`\t\t${e}: v.optional(v.string()),`,clientField:e=>`\t\t${e}: v.optional(v.string()),`,sharedField:
|
|
223
|
+
`:t===`nextjs`?en(e,{extraImports:`import * as v from "valibot";`,serverField:e=>`\t\t${e}: v.optional(v.string()),`,clientField:e=>`\t\t${e}: v.optional(v.string()),`,sharedField:e=>`\t\t${e}: v.optional(v.picklist(["development", "production", "test"]), "development"),`,defaultServerFields:[` DATABASE_URL: v.optional(v.pipe(v.string(), v.url()), "postgres://localhost:5432/mydb"),`],defaultClientFields:[` NEXT_PUBLIC_API_URL: v.optional(v.pipe(v.string(), v.url()), "https://api.example.com"),`],defaultSharedFields:[` NODE_ENV: v.optional(v.picklist(["development", "production", "test"]), "development"),`]},n,r):i.default`
|
|
224
224
|
import arkenv from "arkenv/standard";
|
|
225
225
|
import * as v from "valibot";
|
|
226
226
|
|
|
@@ -256,7 +256,7 @@ ${a}
|
|
|
256
256
|
export const Env = type({
|
|
257
257
|
${a}
|
|
258
258
|
});
|
|
259
|
-
`:t===`nextjs`?en(e,{extraImports:`import { z } from "zod";`,serverField:e=>`\t\t${e}: z.string().optional(),`,clientField:e=>`\t\t${e}: z.string().optional(),`,sharedField:
|
|
259
|
+
`:t===`nextjs`?en(e,{extraImports:`import { z } from "zod";`,serverField:e=>`\t\t${e}: z.string().optional(),`,clientField:e=>`\t\t${e}: z.string().optional(),`,sharedField:e=>`\t\t${e}: z.enum(["development", "production", "test"]).default("development"),`,defaultServerFields:[` DATABASE_URL: z.string().url().default("postgres://localhost:5432/mydb"),`],defaultClientFields:[` NEXT_PUBLIC_API_URL: z.string().url().default("https://api.example.com"),`],defaultSharedFields:[` NODE_ENV: z.enum(["development", "production", "test"]).default("development"),`]},n,r):i.default`
|
|
260
260
|
import arkenv from "arkenv/standard";
|
|
261
261
|
import { z } from "zod";
|
|
262
262
|
|
|
@@ -266,7 +266,7 @@ ${a}
|
|
|
266
266
|
export const env = arkenv({
|
|
267
267
|
${a}
|
|
268
268
|
});
|
|
269
|
-
`};function sn(e,t){let{validator:n,envKeys:r,framework:i,disableCodegen:a}=e;switch(n){case`arktype`:return`${tn(r,i,t,a)}\n`;case`zod`:return`${on(r,i,t,a)}\n`;case`valibot`:return`${an(r,i,t,a)}\n`;default:throw Error(`Unsupported validator: ${n}`)}}function Q(e,t=` `){return e.length===0?`{}`:`{\n${t}${e.map(e=>e.trim()).join(`\n${t}`)}\n${t.slice(1)}}`}function cn(e,t){let{validator:n,envKeys:r,disableCodegen:i}=e,a=[],o=[],s=[],c=[];if(r&&r.length>0)for(let e of r)e.startsWith(`NEXT_PUBLIC_`)?(n===`arktype`?o.push(`${e}: "string?",`):n===`zod`?o.push(`${e}: z.string().optional(),`):n===`valibot`&&o.push(`${e}: v.optional(v.string()),`),c.push(`${e}: process.env.${e},`)):e===`NODE_ENV
|
|
269
|
+
`};function sn(e,t){let{validator:n,envKeys:r,framework:i,disableCodegen:a}=e;switch(n){case`arktype`:return`${tn(r,i,t,a)}\n`;case`zod`:return`${on(r,i,t,a)}\n`;case`valibot`:return`${an(r,i,t,a)}\n`;default:throw Error(`Unsupported validator: ${n}`)}}function Q(e,t=` `){return e.length===0?`{}`:`{\n${t}${e.map(e=>e.trim()).join(`\n${t}`)}\n${t.slice(1)}}`}function cn(e,t){let{validator:n,envKeys:r,disableCodegen:i}=e,a=[],o=[],s=[],c=[];if(r&&r.length>0)for(let e of r)e.startsWith(`NEXT_PUBLIC_`)?(n===`arktype`?o.push(`${e}: "string?",`):n===`zod`?o.push(`${e}: z.string().optional(),`):n===`valibot`&&o.push(`${e}: v.optional(v.string()),`),c.push(`${e}: process.env.${e},`)):e===`NODE_ENV`?(n===`arktype`?s.push(`${e}: "'development' | 'production' | 'test' = 'development'",`):n===`zod`?s.push(`${e}: z.enum(["development", "production", "test"]).default("development"),`):n===`valibot`&&s.push(`${e}: v.optional(v.picklist(["development", "production", "test"]), "development"),`),c.push(`${e}: process.env.${e},`)):e===`PORT`?n===`arktype`?a.push(`PORT: "number.port = 3000",`):n===`zod`?a.push(`PORT: z.coerce.number().int().min(1).max(65535).default(3000),`):n===`valibot`&&a.push(`PORT: v.optional(v.pipe(v.string(), v.transform(Number), v.number(), v.integer(), v.minValue(1), v.maxValue(65535)), 3000),`):n===`arktype`?a.push(`${e}: "string?",`):n===`zod`?a.push(`${e}: z.string().optional(),`):n===`valibot`&&a.push(`${e}: v.optional(v.string()),`);else n===`arktype`?(a.push(`DATABASE_URL: "string = 'postgres://localhost:5432/mydb'",`),o.push(`NEXT_PUBLIC_API_URL: "string = 'https://api.example.com'",`),s.push(`NODE_ENV: "'development' | 'production' | 'test' = 'development'",`)):n===`zod`?(a.push(`DATABASE_URL: z.string().url().default("postgres://localhost:5432/mydb"),`),o.push(`NEXT_PUBLIC_API_URL: z.string().url().default("https://api.example.com"),`),s.push(`NODE_ENV: z.enum(["development", "production", "test"]).default("development"),`)):n===`valibot`&&(a.push(`DATABASE_URL: v.optional(v.pipe(v.string(), v.url()), "postgres://localhost:5432/mydb"),`),o.push(`NEXT_PUBLIC_API_URL: v.optional(v.pipe(v.string(), v.url()), "https://api.example.com"),`),s.push(`NODE_ENV: v.optional(v.picklist(["development", "production", "test"]), "development"),`)),c.push(`NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL,`,`NODE_ENV: process.env.NODE_ENV,`);let l=``,u=``,d=``;return n===`arktype`?(l=`import { type } from "@arkenv/nextjs/shared";
|
|
270
270
|
|
|
271
271
|
/**
|
|
272
272
|
* @internal 🛑 INTERNAL SCHEMA ONLY.
|
|
@@ -284,10 +284,10 @@ export const env = arkenv(
|
|
|
284
284
|
`)}
|
|
285
285
|
},
|
|
286
286
|
},
|
|
287
|
-
);`:`import
|
|
287
|
+
);`:`import arkenv from "${t||`./generated/env.gen`}";
|
|
288
288
|
import { SharedSchema } from "./internal/shared";
|
|
289
289
|
|
|
290
|
-
export const env =
|
|
290
|
+
export const env = arkenv(
|
|
291
291
|
${Q(o,` `)},
|
|
292
292
|
{
|
|
293
293
|
extends: [SharedSchema],
|
|
@@ -319,11 +319,11 @@ export const env = arkenv(
|
|
|
319
319
|
`)}
|
|
320
320
|
},
|
|
321
321
|
},
|
|
322
|
-
);`:`import
|
|
322
|
+
);`:`import arkenv from "${t||`./generated/env.gen`}";
|
|
323
323
|
import { z } from "zod";
|
|
324
324
|
import { SharedSchema } from "./internal/shared";
|
|
325
325
|
|
|
326
|
-
export const env =
|
|
326
|
+
export const env = arkenv(
|
|
327
327
|
${Q(o,` `)},
|
|
328
328
|
{
|
|
329
329
|
extends: [SharedSchema],
|
|
@@ -356,11 +356,11 @@ export const env = arkenv(
|
|
|
356
356
|
`)}
|
|
357
357
|
},
|
|
358
358
|
},
|
|
359
|
-
);`:`import
|
|
359
|
+
);`:`import arkenv from "${t||`./generated/env.gen`}";
|
|
360
360
|
import * as v from "valibot";
|
|
361
361
|
import { SharedSchema } from "./internal/shared";
|
|
362
362
|
|
|
363
|
-
export const env =
|
|
363
|
+
export const env = arkenv(
|
|
364
364
|
${Q(o,` `)},
|
|
365
365
|
{
|
|
366
366
|
extends: [SharedSchema],
|