@@ -20,7 +20,7 @@ function parse_cli_args(deno_args: string[]) {
2020 if ( args [ 'develop' ] ) args = { ...args , watch : true , preview : true , open : true }
2121 const positional_args = args . _ . map ( ( a ) => a . toString ( ) )
2222 const template_filepath = positional_args [ 0 ]
23- const { dir, name } = path . parse ( args . template_filepath )
23+ const { dir, name } = path . parse ( template_filepath )
2424 let output_folder = path . join ( dir , 'ffmpeg-templates-projects' , dir , `${ name } ` )
2525 if ( positional_args [ 1 ] ) output_folder
2626 return {
@@ -69,6 +69,23 @@ async function try_render_video(template_filepath: string, sample_frame: boolean
6969}
7070
7171
72+ async function watch ( filepath : string , fn : ( ) => Promise < void > ) {
73+ let lock = false
74+ for await ( const event of Deno . watchFs ( filepath ) ) {
75+ if ( event . kind === 'modify' && lock === false ) {
76+ lock = true
77+ setTimeout ( ( ) => {
78+ fn ( ) . then ( ( ) => {
79+ lock = false
80+ } )
81+ } , 50 ) // assume that all file modifications are completed in 50ms
82+ }
83+ if ( event . kind === 'remove' ) break
84+ }
85+ watch ( filepath , fn )
86+ }
87+
88+
7289export default async function ( ...deno_args : string [ ] ) {
7390 const args = parse_cli_args ( deno_args )
7491 const { template_filepath } = args
@@ -89,20 +106,10 @@ export default async function (...deno_args: string[]) {
89106
90107 if ( args . watch ) {
91108 logger . info ( `watching ${ template_filepath } for changes` )
92- let lock = false
93- for await ( const event of Deno . watchFs ( template_filepath ) ) {
94- console . log ( { event, lock} )
95- if ( event . kind === 'modify' && ! lock ) {
96- lock = true
97- setTimeout ( ( ) => {
98- logger . info ( `template ${ template_filepath } was changed. Starting render.` )
99- try_render_video ( template_filepath , args . preview , context_options ) . then ( ( ) => {
100- lock = false
101- logger . info ( `watching ${ template_filepath } for changes` )
102- } )
103- // assume that all file modifications are completed in 50ms
104- } , 50 )
105- }
106- }
109+ await watch ( template_filepath , async ( ) => {
110+ logger . info ( `template ${ template_filepath } was changed. Starting render.` )
111+ await try_render_video ( template_filepath , args . preview , context_options )
112+ logger . info ( `watching ${ template_filepath } for changes` )
113+ } )
107114 }
108115}
0 commit comments