@@ -23,6 +23,11 @@ const ConfigSchema = require('../config-schema')
2323
2424const LocationSuffixRegExp = / ( : \d + ) ( : \d + ) ? $ /
2525
26+ // Increment this when changing the serialization format of `${ATOM_HOME}/storage/application.json` used by
27+ // AtomApplication::saveCurrentWindowOptions() and AtomApplication::loadPreviousWindowOptions() in a backward-
28+ // incompatible way.
29+ const APPLICATION_STATE_VERSION = '1'
30+
2631const getDefaultPath = ( ) => {
2732 const editor = atom . workspace . getActiveTextEditor ( )
2833 if ( ! editor || ! editor . getPath ( ) ) {
@@ -247,8 +252,7 @@ class AtomApplication extends EventEmitter {
247252 this . config . onDidChange ( 'core.colorProfile' , ( ) => this . promptForRestart ( ) )
248253 }
249254
250- const optionsForWindowsToOpen = [ ]
251-
255+ let optionsForWindowsToOpen = [ ]
252256 let shouldReopenPreviousWindows = false
253257
254258 if ( options . test || options . benchmark || options . benchmarkTest ) {
@@ -264,9 +268,7 @@ class AtomApplication extends EventEmitter {
264268 }
265269
266270 if ( shouldReopenPreviousWindows ) {
267- for ( const previousOptions of await this . loadPreviousWindowOptions ( ) ) {
268- optionsForWindowsToOpen . push ( previousOptions )
269- }
271+ optionsForWindowsToOpen = [ ...await this . loadPreviousWindowOptions ( ) , ...optionsForWindowsToOpen ]
270272 }
271273
272274 if ( optionsForWindowsToOpen . length === 0 ) {
@@ -1139,27 +1141,58 @@ class AtomApplication extends EventEmitter {
11391141 async saveCurrentWindowOptions ( allowEmpty = false ) {
11401142 if ( this . quitting ) return
11411143
1142- const states = [ ]
1143- for ( let window of this . getAllWindows ( ) ) {
1144- if ( ! window . isSpec ) states . push ( { initialPaths : window . projectRoots } )
1144+ const state = {
1145+ version : APPLICATION_STATE_VERSION ,
1146+ windows : this . getAllWindows ( )
1147+ . filter ( window => ! window . isSpec )
1148+ . map ( window => ( { projectRoots : window . projectRoots } ) )
11451149 }
1146- states . reverse ( )
1150+ state . windows . reverse ( )
11471151
1148- if ( states . length > 0 || allowEmpty ) {
1149- await this . storageFolder . store ( 'application.json' , states )
1152+ if ( state . windows . length > 0 || allowEmpty ) {
1153+ await this . storageFolder . store ( 'application.json' , state )
11501154 this . emit ( 'application:did-save-state' )
11511155 }
11521156 }
11531157
11541158 async loadPreviousWindowOptions ( ) {
1155- const states = await this . storageFolder . load ( 'application.json' )
1156- if ( states ) {
1157- return states . map ( state => ( {
1158- foldersToOpen : state . initialPaths ,
1159+ const state = await this . storageFolder . load ( 'application.json' )
1160+ if ( ! state ) {
1161+ return [ ]
1162+ }
1163+
1164+ if ( state . version === APPLICATION_STATE_VERSION ) {
1165+ // Atom >=1.36.1
1166+ // Schema: {version: '1', windows: [{projectRoots: ['<root-dir>', ...]}, ...] }
1167+ return state . windows . map ( each => ( {
1168+ foldersToOpen : each . projectRoots ,
11591169 devMode : this . devMode ,
1160- safeMode : this . safeMode
1170+ safeMode : this . safeMode ,
1171+ newWindow : true
11611172 } ) )
1173+ } else if ( state . version === undefined ) {
1174+ // Atom <= 1.36.0
1175+ // Schema: [{initialPaths: ['<root-dir>', ...]}, ...]
1176+ return await Promise . all (
1177+ state . map ( async windowState => {
1178+ // Classify each window's initialPaths as directories or non-directories
1179+ const classifiedPaths = await Promise . all (
1180+ windowState . initialPaths . map ( initialPath => new Promise ( resolve => {
1181+ fs . isDirectory ( initialPath , isDir => resolve ( { initialPath, isDir} ) )
1182+ } ) )
1183+ )
1184+
1185+ // Only accept initialPaths that are existing directories
1186+ return {
1187+ foldersToOpen : classifiedPaths . filter ( ( { isDir} ) => isDir ) . map ( ( { initialPath} ) => initialPath ) ,
1188+ devMode : this . devMode ,
1189+ safeMode : this . safeMode ,
1190+ newWindow : true
1191+ }
1192+ } )
1193+ )
11621194 } else {
1195+ // Unrecognized version (from a newer Atom?)
11631196 return [ ]
11641197 }
11651198 }
0 commit comments