Skip to content

Commit 8b46aba

Browse files
committed
Move app creation to coderlib.
1 parent 47fa325 commit 8b46aba

File tree

3 files changed

+70
-80
lines changed

3 files changed

+70
-80
lines changed

coder-apps/common/coder/app/app.js

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ var mustache = require('mustache');
2222
var util = require('util');
2323
var fs = require('fs');
2424
var spawn = require('child_process').spawn;
25-
var ncp = require('ncp').ncp;
2625

2726
//hack to make fs.existsSync work between different node versions
2827
if ( !fs.existsSync ) {
@@ -78,22 +77,6 @@ var getAvailableNewAppID = function( newappid ) {
7877
return newappid;
7978
};
8079

81-
var createLinks = function(name, cb) {
82-
var apppath = "../../apps/" + name + "/";
83-
var viewpath = process.cwd() + "/views/apps/" + name;
84-
var staticpath = process.cwd() + "/static/apps/" + name;
85-
86-
fs.symlink(apppath + "views", viewpath, function(err) {
87-
if (err) {
88-
cb(err);
89-
return;
90-
}
91-
92-
fs.symlink(apppath + "static", staticpath, function(err) {
93-
cb(err);
94-
});
95-
});
96-
}
9780

9881
exports.api_app_create_handler = function( app, req, res ) {
9982

@@ -120,9 +103,7 @@ exports.api_app_create_handler = function( app, req, res ) {
120103

121104
newappid = getAvailableNewAppID( newappid );
122105

123-
var path = process.cwd() + "/apps/";
124-
125-
ncp(path + "boilerplate", path + newappid, function(err) {
106+
coderlib.createApp("boilerplate", newappid, function(err, app) {
126107
if (err) {
127108
res.json({
128109
status: 'error',
@@ -131,49 +112,29 @@ exports.api_app_create_handler = function( app, req, res ) {
131112
return;
132113
}
133114

134-
createLinks(newappid, function(err) {
115+
app.metadata = {
116+
created: getDateString( new Date() ),
117+
modified: getDateString( new Date() ),
118+
color: appcolor,
119+
author: coderlib.device.owner,
120+
name: apptitle,
121+
hidden: false,
122+
public: false
123+
};
124+
125+
app.save(function(err) {
135126
if (err) {
136127
res.json({
137128
status: 'error',
138129
error: err
139130
});
140-
return;
141131
}
142-
143-
coderlib.app(newappid, function(err, app) {
144-
if (err) {
145-
res.json({
146-
status: 'error',
147-
error: err
148-
});
149-
return;
150-
}
151-
152-
app.metadata = {
153-
created: getDateString( new Date() ),
154-
modified: getDateString( new Date() ),
155-
color: appcolor,
156-
author: coderlib.device.owner,
157-
name: apptitle,
158-
hidden: false,
159-
public: false
160-
};
161-
162-
app.save(function(err) {
163-
if (err) {
164-
res.json({
165-
status: 'error',
166-
error: err
167-
});
168-
}
169-
else {
170-
res.json({
171-
status: 'success',
172-
appname: newappid
173-
});
174-
}
132+
else {
133+
res.json({
134+
status: 'success',
135+
appname: newappid
175136
});
176-
});
137+
}
177138
});
178139
});
179140
};

coder-apps/common/coder/static/js/index.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,31 +334,26 @@ var buildAppList = function(apps){
334334
};
335335

336336

337-
338337
//Sort the apps by more recently modified
339-
var sortedapps = [];
340-
for ( var k in apps ) {
341-
sortedapps.push( apps[k] );
342-
}
343-
sortedapps.sort( function(a,b) {
344-
if ( a.ctime < b.ctime ) {
338+
apps.sort( function(a, b) {
339+
if ( a.metadata.ctime < b.metadata.ctime ) {
345340
return 1;
346-
} else if ( b.ctime < a.ctime ) {
341+
} else if ( b.metadata.ctime < a.metadata.ctime ) {
347342
return -1;
348343
} else {
349344
return 0;
350345
}
351346
});
352347

353-
for ( var x=0; x<sortedapps.length; x++ ) {
354-
var app = sortedapps[x];
348+
for ( var x=0; x<apps.length; x++ ) {
349+
var app = apps[x];
355350

356351
var $a = $apptmpl.clone();
357-
$a.find('.appname').text( app.appname );
352+
$a.find('.appname').text( app.name );
358353
if ( app.name && app.name !== "" ) {
359-
$a.find('.appname').text( app.name );
354+
$a.find('.appname').text( app.metadata.name );
360355
}
361-
$a.css('background-color', app.color);
356+
$a.css('background-color', app.metadata.color);
362357

363358
$a.hover(
364359
function() {
@@ -369,11 +364,10 @@ var buildAppList = function(apps){
369364
}
370365
);
371366

372-
$a.click( launchApp( app.appname ) );
373-
$a.find('.editbutton').click( editApp( app.appname ) );
367+
$a.click( launchApp( app.name ) );
368+
$a.find('.editbutton').click( editApp( app.name ) );
374369

375370
$('#applist').append( $a );
376-
377371
}
378372
};
379373

coder-apps/common/coderlib/app/app.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var mustache = require('mustache');
2222
var util = require('util');
2323
var fs = require('fs');
2424
var async = require('async');
25+
var ncp = require('ncp').ncp;
26+
var path = require('path');
2527

2628
exports.get_routes = [
2729
{ path: '/api/app/list', handler: 'api_app_list_handler' },
@@ -65,8 +67,9 @@ exports.app = function(name, callback) {
6567
var viewpath = "apps/" + name + "/";
6668

6769
var userapp = {
70+
name: name,
71+
6872
metadata: {
69-
appname: name,
7073
created: getDateString( new Date() ),
7174
modified: getDateString( new Date() ),
7275
color: "#1abc9c",
@@ -173,6 +176,44 @@ exports.listApps = function(callback, hidden) {
173176
});
174177
}
175178

179+
var createLinks = function(name, cb) {
180+
var apppath = "../../apps/" + name + "/";
181+
var viewpath = process.cwd() + "/views/apps/" + name;
182+
var staticpath = process.cwd() + "/static/apps/" + name;
183+
184+
fs.symlink(apppath + "views", viewpath, function(err) {
185+
if (err) {
186+
cb(err);
187+
return;
188+
}
189+
190+
fs.symlink(apppath + "static", staticpath, function(err) {
191+
cb(err);
192+
});
193+
});
194+
}
195+
196+
exports.createApp = function(template, name, callback) {
197+
var appPath = process.cwd() + "/apps/" + name;
198+
var templatePath = path.resolve("apps/", template);
199+
200+
ncp(templatePath, appPath, function(err) {
201+
if (err) {
202+
callback(err, null);
203+
return;
204+
}
205+
206+
createLinks(name, function(err) {
207+
if (err) {
208+
callback(err, null);
209+
return;
210+
}
211+
212+
coderlib.app(name, callback);
213+
});
214+
});
215+
}
216+
176217
exports.device = function() {
177218
var devicefile = process.cwd() + "/device.json";
178219

@@ -220,14 +261,8 @@ exports.api_app_list_handler = function( app, req, res ) {
220261
res.send(500);
221262
}
222263
else {
223-
var apps = {}
224-
for(var i in results)
225-
{
226-
var m = results[i].metadata;
227-
apps[m.appname] = m;
228-
}
229264
res.json({
230-
apps: apps
265+
apps: results
231266
});
232267
}
233268
});

0 commit comments

Comments
 (0)