@@ -6,15 +6,8 @@ import {
66 promptSelect ,
77} from "@std/cli/unstable-prompt-select" ;
88import { fromFileUrl , join , resolve } from "@std/path" ;
9- import {
10- applyEdits as applyJSONCEdits ,
11- modify as modifyJSONC ,
12- parse as parseJSONC ,
13- } from "jsonc-parser" ;
14- import {
15- resolve_config ,
16- resolve_config_with_deploy_config ,
17- } from "./lib/rs_lib.js" ;
9+ import { parse as parseJSONC } from "@david/jsonc-morph" ;
10+ import { resolve_config } from "./lib/rs_lib.js" ;
1811import { ValidationError } from "@cliffy/command" ;
1912import { createFlow } from "./deploy/create/flow.ts" ;
2013import { createApp } from "./deploy/create/mod.ts" ;
@@ -130,12 +123,12 @@ export async function getApp(
130123 }
131124
132125 if ( selectedApp . value === null ) {
133- const data = await createFlow ( context , rootPath ! ) ;
126+ const data = await createFlow ( context , rootPath ! , org ) ;
134127 await createApp (
135128 context ,
129+ config ,
136130 data ,
137131 rootPath ! ,
138- false ,
139132 true ,
140133 ) ;
141134 config . org = data . org ;
@@ -159,6 +152,7 @@ export async function getApp(
159152export interface ConfigContext {
160153 org : undefined | string ;
161154 app : undefined | string ;
155+ files : string [ ] ;
162156 configSaved : boolean ;
163157 doNotCreate : boolean ;
164158 save ( ) : Promise < void > ;
@@ -184,6 +178,8 @@ export function actionHandler<
184178 const config = await readConfig (
185179 rootPath ?.( ...args ) ?? Deno . cwd ( ) ,
186180 context . config ,
181+ context . ignore ?? [ ] ,
182+ context . allowNodeModules ?? false ,
187183 ) ;
188184 const configContext : ConfigContext = {
189185 ...getAppFromConfig ( config ) ,
@@ -230,95 +226,84 @@ export function actionHandler<
230226}
231227
232228interface Config {
233- path : string ;
234- content : string ;
229+ config ?: {
230+ path : string ;
231+ content : string ;
232+ } ;
233+ files : string [ ] ;
235234}
236235
237236async function readConfig (
238237 rootPath : string ,
239238 maybeConfigPath : string | undefined ,
240- ) : Promise < Config | null > {
241- rootPath = resolve ( rootPath ) ;
242- if ( maybeConfigPath ) {
243- const content = await Deno . readTextFile ( maybeConfigPath ) ;
244- return { path : maybeConfigPath , content } ;
245- }
246-
247- // we prefer the configs with the deploy key. then we fallback to a general
248- // config, so when we set the values, it uses existing config files instead
249- // of trying to create a new one (which will still happen if no config file is found)
250-
251- const configUrl = resolve_config_with_deploy_config ( rootPath ) ;
252-
253- if ( configUrl ) {
254- const path = fromFileUrl ( configUrl ) ;
255- const content = await Deno . readTextFile ( path ) ;
256- return { path, content } ;
257- }
258-
259- const configUrlWithoutDeployConfig = resolve_config ( rootPath ) ;
239+ ignorePaths : string [ ] ,
240+ allowNodeModules : boolean ,
241+ ) : Promise < Config > {
242+ const config = resolve_config (
243+ resolve ( maybeConfigPath || rootPath ) ,
244+ ignorePaths ,
245+ allowNodeModules ,
246+ ) ;
260247
261- if ( configUrlWithoutDeployConfig ) {
262- const path = fromFileUrl ( configUrlWithoutDeployConfig ) ;
248+ if ( config . path ) {
249+ const path = fromFileUrl ( config . path ) ;
263250 const content = await Deno . readTextFile ( path ) ;
264- return { path, content } ;
251+ return { config : { path, content } , files : config . files } ;
265252 }
266253
267- return null ;
254+ return { files : config . files } ;
268255}
269256
270257function getAppFromConfig (
271- configContent : Config | null ,
272- ) : { org : undefined | string ; app : undefined | string } {
273- if ( configContent ) {
274- const config = parseJSONC ( configContent . content ) ;
275- if (
276- typeof config === "object" && config !== null && "deploy" in config &&
277- typeof config . deploy === "object" && config . deploy !== null &&
278- ! Array . isArray ( config . deploy )
279- ) {
258+ configContent : Config ,
259+ ) : { org : undefined | string ; app : undefined | string ; files : string [ ] } {
260+ if ( configContent . config ) {
261+ const config = parseJSONC ( configContent . config . content ) ;
262+ const deployObj = config . asObject ( ) ?. getIfObject ( "deploy" ) ;
263+
264+ if ( deployObj ) {
280265 return {
281- org : config . deploy . org ,
282- app : config . deploy . app ,
266+ org : deployObj . get ( "org" ) ?. value ( ) ?. asString ( ) ,
267+ app : deployObj . get ( "app" ) ?. value ( ) ?. asString ( ) ,
268+ files : configContent . files ,
283269 } ;
284270 }
285271 }
286272
287273 return {
288274 org : undefined ,
289275 app : undefined ,
276+ files : configContent . files ,
290277 } ;
291278}
292279
293280async function writeConfig (
294- configContent : Config | null ,
281+ configContent : Config ,
295282 { org, app } : { org : undefined | string ; app : undefined | string } ,
296283) {
297284 if ( ! org ) {
298285 return ;
299286 }
300287
301- const content = configContent ?. content ?? "{}\n" ;
288+ const content = configContent . config ?. content ?? "{}\n" ;
302289
303290 const newConfig : Record < string , string > = { org } ;
304291
305292 if ( app ) {
306293 newConfig . app = app ;
307294 }
308295
309- const edits = modifyJSONC ( content , [ "deploy" ] , newConfig , {
310- formattingOptions : {
311- insertSpaces : true ,
312- tabSize : 2 ,
313- } ,
314- } ) ;
315- const out = applyJSONCEdits ( content , edits ) ;
296+ const config = parseJSONC ( content ) ;
297+ const deployObj = config . asObjectOrForce ( ) . getIfObjectOrForce ( "deploy" ) ;
298+ deployObj . replaceWith ( newConfig ) ;
299+ deployObj . ensureMultiline ( ) ;
300+
316301 await Deno . writeTextFile (
317- configContent ?. path ?? join ( Deno . cwd ( ) , "deno.jsonc" ) ,
318- out ,
302+ configContent . config ?. path ?? join ( Deno . cwd ( ) , "deno.jsonc" ) ,
303+ config . toString ( ) + "\n" ,
319304 ) ;
320305
321- if ( ! configContent ) {
306+ if ( ! configContent . config ) {
322307 console . log (
323308 `Created configuration file at '${ join ( Deno . cwd ( ) , "deno.jsonc" ) } '` ,
324309 ) ;
0 commit comments