1
1
import path = require( "path" ) ;
2
2
import * as vscode from "vscode" ;
3
3
import { 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" ;
6
13
import { NodeBase } from "../explorer/models/nodeBase" ;
7
14
8
15
export const getCategory = ( fileName : string , addCategory : any | boolean ) : string => {
@@ -98,6 +105,7 @@ export async function exportFile(
98
105
const api = new AtelierAPI ( workspaceFolder ) ;
99
106
api . setNamespace ( namespace ) ;
100
107
const log = ( status ) => outputChannel . appendLine ( `export "${ name } " as "${ fileName } " - ${ status } ` ) ;
108
+ const fileUri = vscode . Uri . file ( fileName ) ;
101
109
const foldersUri = vscode . Uri . file ( path . dirname ( fileName ) ) ;
102
110
try {
103
111
if ( ! ( await fileExists ( foldersUri ) ) ) {
@@ -110,6 +118,16 @@ export async function exportFile(
110
118
throw new Error ( "Received malformed JSON object from server fetching document" ) ;
111
119
}
112
120
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
+
113
131
const { noStorage, dontExportIfNoChanges } = config ( "export" ) ;
114
132
115
133
const storageResult : { found : boolean ; content ?: string } = await new Promise ( ( resolve , reject ) => {
@@ -142,19 +160,20 @@ export async function exportFile(
142
160
}
143
161
} ) ;
144
162
145
- const fileUri = vscode . Uri . file ( fileName ) ;
146
163
if ( Buffer . isBuffer ( content ) ) {
147
164
// This is a binary file
148
165
let isSkipped = "" ;
149
166
if ( dontExportIfNoChanges && ( await fileExists ( fileUri ) ) ) {
150
167
const existingContent = await vscode . workspace . fs . readFile ( fileUri ) ;
151
- if ( content . equals ( existingContent ) ) {
168
+ if ( ! content . equals ( existingContent ) ) {
152
169
await vscode . workspace . fs . writeFile ( fileUri , content ) ;
170
+ await recordMtime ( ) ;
153
171
} else {
154
172
isSkipped = " => skipped - no changes." ;
155
173
}
156
174
} else {
157
175
await vscode . workspace . fs . writeFile ( fileUri , content ) ;
176
+ await recordMtime ( ) ;
158
177
}
159
178
log ( `Success ${ isSkipped } ` ) ;
160
179
} else {
@@ -171,11 +190,13 @@ export async function exportFile(
171
190
// stringify to harmonise the text encoding.
172
191
if ( JSON . stringify ( joinedContent ) !== JSON . stringify ( existingContent ) ) {
173
192
await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
193
+ await recordMtime ( ) ;
174
194
} else {
175
195
isSkipped = " => skipped - no changes." ;
176
196
}
177
197
} else {
178
198
await vscode . workspace . fs . writeFile ( fileUri , new TextEncoder ( ) . encode ( joinedContent ) ) ;
199
+ await recordMtime ( ) ;
179
200
}
180
201
181
202
log ( `Success ${ isSkipped } ` ) ;
0 commit comments