11import path = require( "path" ) ;
22import * as vscode from "vscode" ;
33import { AtelierAPI } from "../api" ;
4- import { config , explorerProvider , OBJECTSCRIPT_FILE_SCHEMA , schemas } from "../extension" ;
5- import { currentFile , fileExists , notNull , outputChannel , uriOfWorkspaceFolder } from "../utils" ;
4+ import { config , explorerProvider , OBJECTSCRIPT_FILE_SCHEMA , schemas , workspaceState } from "../extension" ;
5+ import {
6+ currentFile ,
7+ currentFileFromContent ,
8+ fileExists ,
9+ notNull ,
10+ outputChannel ,
11+ uriOfWorkspaceFolder ,
12+ } from "../utils" ;
613import { NodeBase } from "../explorer/models/nodeBase" ;
714
815export const getCategory = ( fileName : string , addCategory : any | boolean ) : string => {
@@ -98,6 +105,7 @@ export async function exportFile(
98105 const api = new AtelierAPI ( workspaceFolder ) ;
99106 api . setNamespace ( namespace ) ;
100107 const log = ( status ) => outputChannel . appendLine ( `export "${ name } " as "${ fileName } " - ${ status } ` ) ;
108+ const fileUri = vscode . Uri . file ( fileName ) ;
101109 const foldersUri = vscode . Uri . file ( path . dirname ( fileName ) ) ;
102110 try {
103111 if ( ! ( await fileExists ( foldersUri ) ) ) {
@@ -110,6 +118,16 @@ export async function exportFile(
110118 throw new Error ( "Received malformed JSON object from server fetching document" ) ;
111119 }
112120 const content = data . result . content ;
121+
122+ // Local function to update local record of mtime
123+ const recordMtime = async ( ) => {
124+ const contentString = Buffer . isBuffer ( content ) ? "" : content . join ( "\n" ) ;
125+ const file = currentFileFromContent ( fileUri , contentString ) ;
126+ const serverTime = Number ( new Date ( data . result . ts + "Z" ) ) ;
127+ await workspaceState . update ( `${ file . uniqueId } :mtime` , serverTime ) ;
128+ return ;
129+ } ;
130+
113131 const { noStorage, dontExportIfNoChanges } = config ( "export" ) ;
114132
115133 const storageResult : { found : boolean ; content ?: string } = await new Promise ( ( resolve , reject ) => {
@@ -142,19 +160,20 @@ export async function exportFile(
142160 }
143161 } ) ;
144162
145- const fileUri = vscode . Uri . file ( fileName ) ;
146163 if ( Buffer . isBuffer ( content ) ) {
147164 // This is a binary file
148165 let isSkipped = "" ;
149166 if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
150167 const existingContent = await vscode . workspace . fs . readFile ( fileUri ) ;
151- if ( content . equals ( existingContent ) ) {
168+ if ( ! content . equals ( existingContent ) ) {
152169 await vscode . workspace . fs . writeFile ( fileUri , content ) ;
170+ await recordMtime ( ) ;
153171 } else {
154172 isSkipped = " => skipped - no changes." ;
155173 }
156174 } else {
157175 await vscode . workspace . fs . writeFile ( fileUri , content ) ;
176+ await recordMtime ( ) ;
158177 }
159178 log ( `Success ${ isSkipped } ` ) ;
160179 } else {
@@ -171,11 +190,13 @@ export async function exportFile(
171190 // stringify to harmonise the text encoding.
172191 if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
173192 await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
193+ await recordMtime ( ) ;
174194 } else {
175195 isSkipped = " => skipped - no changes." ;
176196 }
177197 } else {
178198 await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
199+ await recordMtime ( ) ;
179200 }
180201
181202 log ( `Success ${ isSkipped } ` ) ;
0 commit comments