UNPKG

3.97 kBJavaScriptView Raw
1'use strict';
2
3var util = require('util');
4
5var when = require('when');
6var nodefn = require('when/node');
7var fs = nodefn.liftAll(require('fs-extra'));
8
9var Workspace = require('./workspace');
10var helpers = require('./helpers');
11var temp = helpers.temp;
12
13function TempWorkspace(){
14 Workspace.call(this);
15}
16util.inherits(TempWorkspace, Workspace);
17
18TempWorkspace.prototype.updateContent = function(content, cb){
19 var self = this;
20
21 var filename = this.filename.deref();
22
23 var update = Workspace.prototype.updateContent.call(self, content);
24
25 var promise = update;
26
27 if(filename){
28 promise = update.tap(function(){
29 return self._updateTemp(self.filename, content);
30 })
31 .tap(function(){
32 return self._updateDirectory();
33 });
34 }
35
36 return nodefn.bindCallback(promise, cb);
37};
38
39TempWorkspace.prototype.loadFile = function(filepath, cb){
40 var self = this;
41
42 var resolvedFilepath = this._resolveToCwd(filepath);
43
44 var promise = temp.get(resolvedFilepath)
45 // if found, set the temp as current and return
46 .then(function(contents){
47 // TODO: dedupe with Workspace
48 self.filename.update(function(){
49 return filepath;
50 });
51 self.current.update(function(){
52 return contents;
53 });
54
55 return self.current;
56 })
57 // if not found, defer to original
58 .catch(temp.notFound, function(){
59 return Workspace.prototype.loadFile.call(self, filepath);
60 });
61
62 return nodefn.bindCallback(promise, cb);
63};
64
65TempWorkspace.prototype.saveFile = function(filepath, content, cb){
66 var self = this;
67
68 var promise = Workspace.prototype.saveFile.call(self, filepath, content)
69 .tap(function(){
70 return self._updateTemp(filepath, content);
71 })
72 .tap(function(){
73 return self._updateDirectory();
74 });
75
76 return nodefn.bindCallback(promise, cb);
77};
78
79TempWorkspace.prototype.deleteFile = function(filepath, cb){
80 var self = this;
81
82 if(typeof filepath.deref === 'function'){
83 filepath = filepath.deref();
84 }
85
86 var resolvedFilepath = this._resolveToCwd(filepath);
87
88 var promise = Workspace.prototype.deleteFile.call(self, filepath)
89 .tap(function(){
90 return temp.del(resolvedFilepath);
91 })
92 .tap(function(){
93 return self._updateDirectory();
94 });
95
96 return nodefn.bindCallback(promise, cb);
97};
98
99TempWorkspace.prototype.changeDir = function(dirpath, cb){
100 var self = this;
101
102 var promise = Workspace.prototype.changeDir.call(self, dirpath)
103 .tap(function(){
104 return self._updateDirectory();
105 });
106
107 return nodefn.bindCallback(promise, cb);
108};
109
110// internal methods don't need to accept callbacks
111TempWorkspace.prototype._updateTemp = function(filepath, content){
112 if(typeof filepath.deref === 'function'){
113 filepath = filepath.deref();
114 }
115
116 if(typeof content.deref === 'function'){
117 content = content.deref();
118 }
119
120 var resolvedFilepath = this._resolveToCwd(filepath);
121
122 var promise = fs.readFile(resolvedFilepath, 'utf8')
123 .then(function(fileContents){
124 if(content === fileContents){
125 return temp.del(resolvedFilepath);
126 } else {
127 return temp.put(resolvedFilepath, content);
128 }
129 })
130 .otherwise(function(){
131 return temp.put(resolvedFilepath, content);
132 });
133
134 return promise;
135};
136
137// internal methods don't need to accept callbacks
138TempWorkspace.prototype._updateDirectory = function(){
139 var cwd = this.cwd.deref();
140 var directory = this.directory;
141
142 // don't update a directory at root
143 // currently not supported
144 if(cwd === helpers.root){
145 return when.resolve();
146 }
147
148 var resolvedDir = this._resolveToRoot(cwd);
149
150 var promise = fs.readdir(resolvedDir)
151 .then(function(filenames){
152 return helpers.createFiles(filenames, { cwd: cwd, checkTemp: true });
153 })
154 .tap(function(files){
155 directory.update(function(list){
156 var empty = list.clear();
157 return empty.concat(files);
158 });
159 });
160
161 return promise;
162};
163
164module.exports = TempWorkspace;