@coreframe/preview 0.1.5 → 0.1.7
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.js +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{preparePreviewTunnel as e}from"./cloudflare.js";import{copyFile as t,glob as n,mkdir as r,readFile as i,stat as a}from"node:fs/promises";import o from"node:path";import s from"node:process";import{parseEnv as c}from"node:util";import{coreframeLocalWorkerVariables as l,readProjectConfig as u}from"@coreframe/config";import{migrateLocalDb as d,runProjectSeed as f,startProjectPostgres as p,startProjectTurso as m}from"@coreframe/db";import h from"nano-spawn";import{supervise as g}from"procband";const _=[`DATABASE_URL`,`PGDATABASE`,`PGHOST`,`PGPASSWORD`,`PGPORT`,`PGUSER`,`TURSO_AUTH_TOKEN`,`TURSO_DATABASE_URL`],v=[`POSTHOG_`,`VITE_POSTHOG_`,`VITE_GA_`],y=[`CI`,`COLORTERM`,`COMSPEC`,`ComSpec`,`FORCE_COLOR`,`HOME`,`NODE_EXTRA_CA_CERTS`,`NODE_OPTIONS`,`NO_COLOR`,`PATH`,`PATHEXT`,`PNPM_HOME`,`SHELL`,`SSL_CERT_DIR`,`SSL_CERT_FILE`,`SYSTEMROOT`,`SystemRoot`,`TEMP`,`TERM`,`TMP`,`TMPDIR`,`USERPROFILE`],b=s.platform===`win32`?`pnpm.cmd`:`pnpm`;async function x({cwd:e=s.cwd(),env:t=s.env,host:n,port:r},i={}){let a=await u({cwd:e}),o=S(a,{host:n,port:r}),c=await C({appOrigin:o.appOrigin,config:a,cwd:e,env:t}),l=E({config:a,cwd:e,env:c}),d,f,p,m=new Set,h=!1,g,_=()=>{if(!h){h=!0;for(let e of m)e.nodeChildProcess.then(e=>e.kill(`SIGTERM`)).catch(()=>void 0);d?.kill(`SIGTERM`),f?.kill(`SIGTERM`),l.stop()}},v=[`SIGINT`,`SIGTERM`].map(e=>{let t=()=>{g??=e,_()};return s.on(e,t),{handler:t,signal:e}});try{await l.start();let n=w({config:o,cwd:e,database:l,env:c,processEnvironment:t,commands:m,onServe(e){d=e.server,f=e.tunnel,p=e.disposeTunnel},isStopping:()=>h,onStop:_});await n.database.migrate(),await n.database.seed(),await i.beforeBuild?.(n),await n.build(),await i.afterBuild?.(n),await n.serve()}finally{let e=[...m];_();try{await l.dispose()}finally{await Promise.allSettled([...e,d?.wait(),f?.wait()].filter(Boolean)),await p?.();for(let{handler:e,signal:t}of v)s.off(t,e);g&&(s.exitCode??=g===`SIGINT`?130:143)}}}function S(e,t={}){let n=e.preview??{},r=t.host??n.host??`127.0.0.1`,i=t.port??n.port??4173,a=n.strictPort??!0,o=n.appOrigin;if(!Number.isInteger(i)||i<1||i>65535)throw Error(`Preview port must be an integer between 1 and 65535.`);if(!r.trim())throw Error(`Preview host must not be empty.`);return o&&A(o),n.tunnel&&k(n.tunnel,o,a),{appOrigin:o,host:r,port:i,strictPort:a,tunnel:n.tunnel}}async function C({appOrigin:e,config:t,cwd:n,env:r}){let i=await D(n),a=Object.keys(t.worker?.environment??{}).filter(e=>!v.some(t=>e.startsWith(t))),o=new Set([...l,..._,...a]),s={},c=new Set([...Object.keys(r),...Object.keys(i)].filter(e=>e.startsWith(`VITE_`)&&!v.some(t=>e.startsWith(t))));for(let e of
|
|
1
|
+
import{preparePreviewTunnel as e}from"./cloudflare.js";import{copyFile as t,glob as n,mkdir as r,readFile as i,stat as a}from"node:fs/promises";import o from"node:path";import s from"node:process";import{parseEnv as c}from"node:util";import{coreframeLocalWorkerVariables as l,readProjectConfig as u}from"@coreframe/config";import{migrateLocalDb as d,runProjectSeed as f,startProjectPostgres as p,startProjectTurso as m}from"@coreframe/db";import h from"nano-spawn";import{supervise as g}from"procband";const _=[`DATABASE_URL`,`PGDATABASE`,`PGHOST`,`PGPASSWORD`,`PGPORT`,`PGUSER`,`TURSO_AUTH_TOKEN`,`TURSO_DATABASE_URL`],v=[`POSTHOG_`,`VITE_POSTHOG_`,`VITE_GA_`],y=[`CI`,`COLORTERM`,`COMSPEC`,`ComSpec`,`FORCE_COLOR`,`HOME`,`LANG`,`NODE_EXTRA_CA_CERTS`,`NODE_OPTIONS`,`NO_COLOR`,`PATH`,`PATHEXT`,`PNPM_HOME`,`SHELL`,`SSL_CERT_DIR`,`SSL_CERT_FILE`,`SYSTEMROOT`,`SystemRoot`,`TEMP`,`TERM`,`TMP`,`TMPDIR`,`USERPROFILE`],b=s.platform===`win32`?`pnpm.cmd`:`pnpm`;async function x({cwd:e=s.cwd(),env:t=s.env,host:n,port:r},i={}){let a=await u({cwd:e}),o=S(a,{host:n,port:r}),c=await C({appOrigin:o.appOrigin,config:a,cwd:e,env:t}),l=E({config:a,cwd:e,env:c}),d,f,p,m=new Set,h=!1,g,_=()=>{if(!h){h=!0;for(let e of m)e.nodeChildProcess.then(e=>e.kill(`SIGTERM`)).catch(()=>void 0);d?.kill(`SIGTERM`),f?.kill(`SIGTERM`),l.stop()}},v=[`SIGINT`,`SIGTERM`].map(e=>{let t=()=>{g??=e,_()};return s.on(e,t),{handler:t,signal:e}});try{await l.start();let n=w({config:o,cwd:e,database:l,env:c,processEnvironment:t,commands:m,onServe(e){d=e.server,f=e.tunnel,p=e.disposeTunnel},isStopping:()=>h,onStop:_});await n.database.migrate(),await n.database.seed(),await i.beforeBuild?.(n),await n.build(),await i.afterBuild?.(n),await n.serve()}finally{let e=[...m];_();try{await l.dispose()}finally{await Promise.allSettled([...e,d?.wait(),f?.wait()].filter(Boolean)),await p?.();for(let{handler:e,signal:t}of v)s.off(t,e);g&&(s.exitCode??=g===`SIGINT`?130:143)}}}function S(e,t={}){let n=e.preview??{},r=t.host??n.host??`127.0.0.1`,i=t.port??n.port??4173,a=n.strictPort??!0,o=n.appOrigin;if(!Number.isInteger(i)||i<1||i>65535)throw Error(`Preview port must be an integer between 1 and 65535.`);if(!r.trim())throw Error(`Preview host must not be empty.`);return o&&A(o),n.tunnel&&k(n.tunnel,o,a),{appOrigin:o,host:r,port:i,strictPort:a,tunnel:n.tunnel}}async function C({appOrigin:e,config:t,cwd:n,env:r}){let i=await D(n),a=Object.keys(t.worker?.environment??{}).filter(e=>!v.some(t=>e.startsWith(t))),o=new Set([...l,..._,...a]),s={},c=new Set([...Object.keys(r),...Object.keys(i)].filter(e=>e.startsWith(`VITE_`)&&!v.some(t=>e.startsWith(t))));for(let e of[...y,...Object.keys(r).filter(e=>e.startsWith(`LC_`))])r[e]!==void 0&&(s[e]=r[e]);for(let e of o){let t=r[e]??i[e];t!==void 0&&(s[e]=t)}for(let e of c){let t=r[e]??i[e];t!==void 0&&(s[e]=t)}return e!==void 0&&(o.add(`APP_ORIGIN`),s.APP_ORIGIN=e),s.CLOUDFLARE_ENV=`preview`,s.COREFRAME_ENV=`preview`,s.COREFRAME_PLATFORM=`web`,s.COREFRAME_PREVIEW_WORKER_VARIABLES=JSON.stringify([...o].filter(e=>!v.some(t=>e.startsWith(t)))),s}function w({config:i,cwd:c,database:l,env:u,processEnvironment:d,commands:f,onServe:p,isStopping:m,onStop:_}){let v=!1,y={cwd:c,database:l,env:u,host:i.host,origin:i.appOrigin??M(i.host,i.port),port:i.port,async build(){await y.run(b,[`run`,`build`,`--mode`,`preview`])},report(e,t){console.log(t===void 0?e:`${e}: ${t}`)},async run(e,t=[]){let n=h(e,t,{cwd:c,env:u,stdio:`inherit`});f.add(n);try{await n}finally{f.delete(n)}},async serve(){if(v)throw Error(`Preview serving can only be started once.`);v=!0;let t,n,r;try{if(t=i.tunnel?await e({config:i.tunnel,cwd:c,env:d}):void 0,m()){await t?.dispose();return}n=g({name:`preview`,command:b,args:[`exec`,`vite`,`preview`,`--mode`,`preview`,`--outDir`,`dist/client`,`--host`,i.host,`--port`,String(i.port),...i.strictPort?[`--strictPort`]:[]],cwd:c,env:u,prefix:!1,stdin:s.stdin}),r=t?g({name:`cloudflared`,command:s.platform===`win32`?`cloudflared.exe`:`cloudflared`,args:[`tunnel`,`run`,`--url`,`http://${P(N(i.host))}:${i.port}`,`--credentials-file`,t.credentialsFile,t.tunnel.tunnelId],cwd:c,env:u,prefix:!1}):void 0}catch(e){throw n?.kill(`SIGTERM`),await n?.wait(),await t?.dispose(),e}try{p({disposeTunnel:t?.dispose,server:n,tunnel:r}),y.report(`Preview`,y.url(`/`)),t&&y.report(`Cloudflare tunnel`,`${t.tunnel.name} (${t.tunnel.tunnelId})`),await Promise.race(r?[n.expectSuccess(),r.expectSuccess()]:[n.expectSuccess()])}catch(e){if(m()||_(),await t?.dispose(),m())return;throw e}},async stageArtifact({label:e,pathname:i,source:s}){let l=T(c,i),u=[];for await(let e of n(s,{cwd:c}))(await a(o.resolve(c,e))).isFile()&&u.push(e);if(u.length===0)throw Error(`Preview artifact source ${JSON.stringify(s)} did not match a file.`);if(u.length>1)throw Error(`Preview artifact source ${JSON.stringify(s)} matched multiple files: ${u.sort().join(`, `)}.`);await r(o.dirname(l),{recursive:!0}),await t(o.resolve(c,u[0]),l);let d=y.url(i);return y.report(e,d),d},url(e=`/`){return new URL(e,`${y.origin.replace(/\/$/u,``)}/`).toString()}};return y}function T(e,t){let n=new URL(t,`https://coreframe.invalid`);if(!t.startsWith(`/`)||t===`/`||t.includes(`\\`)||t.includes(`?`)||t.includes(`#`)||n.origin!==`https://coreframe.invalid`||n.pathname!==t||o.posix.normalize(t)!==t)throw Error(`Preview artifact pathname must be a normalized absolute URL path.`);return o.resolve(e,`dist/client`,`.${t}`)}function E({config:e,cwd:t,env:n}){let r=e.database?.dialect??`postgres`,i,a,o,s,c=!1,l=n,u=()=>a?(s??=a[Symbol.asyncDispose](),s):Promise.resolve(),h=()=>{c||(c=!0,u().catch(()=>void 0))};return{dialect:r,get env(){return l},async migrate(){await d({config:e,cwd:t,env:l})},async seed(){await f(`default`,{cwd:t,env:l,seedEntryPoint:e.database?.seed})},async start(){if(r===`sqlite`){a=await m({config:e,cwd:t,env:l,useSupervisorLease:!0}),Object.assign(l,a.env);return}i=await p({config:e,cwd:t,env:l,failIfRunning:!0,postgresOutput:`on-error`,useSupervisorLease:!0}),Object.assign(l,i.env)},stop:h,async dispose(){o??=(async()=>{h(),await Promise.allSettled([i?.[Symbol.asyncDispose](),u()]),i=void 0,a=void 0})(),await o}}}async function D(e){let t={};for(let n of[`.env`,`.env.local`,`.env.preview`,`.env.preview.local`]){let r=await O(o.resolve(e,n));r&&(t={...t,...r})}let n=await O(o.resolve(e,`.dev.vars`));n&&(t={...t,...n});let r=await O(o.resolve(e,`.dev.vars.preview`));return r&&(t={...t,...r}),t}async function O(e){try{return Object.fromEntries(Object.entries(c(await i(e,`utf8`))).filter(e=>e[1]!==void 0))}catch(e){if(e.code===`ENOENT`)return;throw e}}function k(e,t,n){if(!t)throw Error(`preview.appOrigin is required when preview.tunnel is configured.`);if(!n)throw Error(`preview.strictPort must be true when preview.tunnel is configured.`);let r=[`credentialsFile`,`tokenEnv`,`tokenFile`],i=e,a=r.find(e=>e in i);if(a)throw Error(`preview.tunnel.${a} is no longer supported. Coreframe preview now provisions Cloudflare credentials through the operating system credential store.`);if(!e.name.trim())throw Error(`preview.tunnel.name must not be empty.`);if(!/^[a-z0-9][a-z0-9._-]*$/u.test(e.profile))throw Error(`preview.tunnel.profile must start with a lowercase letter or number and contain only lowercase letters, numbers, dots, underscores, or hyphens.`);if(e.dns!==`managed`&&e.dns!==`external`)throw Error(`preview.tunnel.dns must be either "managed" or "external".`);let o=j(e.hostname);if(new URL(t).hostname!==o)throw Error(`preview.appOrigin hostname must match preview.tunnel.hostname.`)}function A(e){let t;try{t=new URL(e)}catch{throw Error(`preview.appOrigin must be an exact HTTPS browser origin.`)}if(t.protocol!==`https:`||t.origin!==e||t.pathname!==`/`||t.search||t.hash||t.username||t.password)throw Error(`preview.appOrigin must be an exact HTTPS browser origin.`);return t}function j(e){let t;try{t=new URL(`https://${e}`)}catch{throw Error(`preview.tunnel.hostname must be an exact hostname.`)}if(t.hostname!==e||t.origin!==`https://${e}`)throw Error(`preview.tunnel.hostname must be an exact hostname.`);return t.hostname}function M(e,t){return`http://${P(N(e))}:${t}`}function N(e){return e===`0.0.0.0`||e===`::`?`127.0.0.1`:e}function P(e){return e.includes(`:`)&&!e.startsWith(`[`)?`[${e}]`:e}export{C as createPreviewEnvironment,S as resolvePreviewConfig,x as runPreview};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coreframe/preview",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Production-like local preview lifecycle utilities for Coreframe apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"@napi-rs/keyring": "1.3.0",
|
|
22
22
|
"nano-spawn": "^2.1.0",
|
|
23
23
|
"procband": "^0.3.8",
|
|
24
|
-
"@coreframe/config": "^0.1.
|
|
25
|
-
"@coreframe/db": "^0.1.
|
|
24
|
+
"@coreframe/config": "^0.1.7",
|
|
25
|
+
"@coreframe/db": "^0.1.8"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@types/node": "^26.1.0",
|