-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJakeFile.ts
179 lines (147 loc) · 5.49 KB
/
JakeFile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/// <reference path="typings/jake/jake.d.ts" />
/// <reference path="typings/q/q.d.ts" />
import Q = require('q');
import Promise = Q.Promise;
import seed = require('./source/seed-data/seed');
import iis = require('iis');
import path = require('path');
/* Tasks */
desc('Default task');
task('default', ['compile']);
desc('Compiles All TypeScript Files');
task('compile', ['compile-server', 'compile-client']);
desc('Compiles Server-side NodeJS TypeScript files');
task('compile-server', [], () => {
var files = [
'./source/**/*.ts',
'./server.ts'
];
console.log('Compiling server-side TypeScript files...');
compileTypeScript({
files: files,
moduleType: TsModuleType.CommonJS
})
.then(() => console.log('Done compiling server-side TypeScript files!'))
.catch(() => console.error('Failed to compile server-side TypeScript files!'))
.finally(() => complete());
}, {async: true});
desc('Compiles Client-side NodeJS TypeScript files');
task('compile-client', [], () => {
var files = [
'./public/scripts/**/*.ts'
];
console.log('Compiling client-side TypeScript files...');
compileTypeScript({
files: files,
moduleType: TsModuleType.None
})
.then(() => console.log('Done compiling client-side TypeScript files!'))
.catch(() => console.error('Failed to compile client-side TypeScript files!'))
.finally(() => complete());
}, {async: true});
desc('Seeds test data');
task('seed', [], () => {
seed()
.then(() => console.log('Seeding complete!'))
.catch(err => console.error(`Error occurred seeding: ${err}\n${err.stack}`))
.finally(() => complete());
}, {async: true});
namespace('mongo', () => {
desc('Starts up local MongoDB instance');
task('start', [], () => {
run('net', 'start', 'MongoDB')
.then(() => console.log('MongoDB service started'))
.catch((error) => console.error(`Error starting MongoDB service! - ${error}`))
.finally(() => complete());
}, {async: true});
desc('Stops local MongoDB instance');
task('stop', [], () => {
run('net', 'stop', 'MongoDB')
.then(() => console.log('MongoDB service stopped'))
.catch((error) => console.error(`Error stopping MongoDB service! - ${error}`))
.finally(() => complete());
}, {async: true});
});
namespace('iis', () => {
desc('Installs app into local IIS');
task('install', () => {
var defaultWebsite = 'Default Web Site',
appName = 'TeTra',
fullPath = path.resolve(__dirname);
createIisEntry(defaultWebsite, appName, fullPath)
.then(() => console.log(`Created IIS app '${appName}' successfully!`))
.catch((error) => console.error(`Error creating IIS app - ${error}`))
.finally(() => complete());
}, { async: true });
});
/* Helpers */
enum TsModuleType {
None = 0,
CommonJS = 1,
Amd = 2
}
interface ITypeScriptCompileOptions {
files: string[];
moduleType?: TsModuleType;
generateSourceMaps?: boolean;
}
function compileTypeScript(args: ITypeScriptCompileOptions): Promise<string[]> {
// Error checks
if (!args) {
throw new Error('No args passed');
}
var commandParts = ['--target ES5'];
if (args.moduleType) {
commandParts.push(`--module ${TsModuleType[args.moduleType].toLowerCase()}`);
}
if (args.generateSourceMaps === true) {
commandParts.push('--sourceMap');
}
// Resolve file list
var fileList = new jake.FileList();
fileList.include(args.files);
// Create actual command
var deferred = Q.defer<string[]>(),
files = fileList.toArray(),
command = commandParts.concat(files).join(' '),
exec = jake.createExec([command]),
appDataPath = process.env['APPDATA'],
tsc = appDataPath + '\\npm\\tsc';
return run(tsc, commandParts.concat(files));
}
function run(cmd: string, args: string[]): Promise<any>;
function run(cmd: string, ...args: string[]): Promise<any>;
function run(cmd: string, args: any): Promise<any> {
args = arguments.length === 2 && arguments[1] instanceof Array ? arguments[1] : Array.prototype.slice.call(arguments, 1);
var command = [cmd].concat(args).join(' '),
exec = jake.createExec([command]),
deferred = Q.defer<any>();
exec
.addListener('stdout', output => process.stdout.write(output))
.addListener('stderr', output => process.stderr.write(output))
.addListener('cmdEnd', () => deferred.resolve(undefined))
.addListener('error', error => deferred.reject(error));
exec.run();
return deferred.promise;
}
function createIisEntry(site: string, appName: string, physicalPath: string): Promise<string> {
var dfd = Q.defer<string>(),
appPath = `${site}/${appName}`;
iis.exists('site', site, (err, exists) => {
if (err) return dfd.reject(err);
if (!exists) return dfd.reject(`Site ${site} does not exist`);
iis.exists('app', appPath, (err, exists) => {
if (err) return dfd.reject(err);
if (exists) return dfd.reject(`Application ${appPath} already exists`);
iis.createAppFolder({
site:site,
virtual_path:appName,
physical_path: physicalPath
}, (err, out) => {
if (err) return dfd.reject(err);
else dfd.resolve(out);
});
});
});
return dfd.promise;
}