-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGruntfile.js
199 lines (180 loc) · 7.58 KB
/
Gruntfile.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Remove the quotes below and fill in <proxy-ip> and <proxy-port> if you use a proxy
//require('./http-proxy')('<proxy-ip>',<proxy-port>);
var sqlite3 = require('sqlite3').verbose();
var dbName = require('path').join(__dirname, 'nest.db');
var serverLocation = 'ssh://<ip-address>';
module.exports = function ( grunt ) {
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
all: [
'Gruntfile.js',
'bowers-nest.js',
'server.js'
],
options: {
jshintrc: '.jshintrc'
}
},
jasmine_node: {
projectRoot: 'server',
src: 'server.js',
spec: 'server.spec.js',
requirejs: false,
forceExit: true
},
clean: {
all: 'git_repositories/all_bower_packages.json'
}
});
grunt.registerTask('test', ['jshint', 'jasmine_node']);
grunt.registerTask('sync', ['clean', 'get_bower_packages', 'sync_db']);
grunt.registerTask('default', ['test', 'sync']);
grunt.registerTask('init_db', 'Sync database to Bower official registry', function(){
var done = this.async();
var db = new sqlite3.Database(dbName);
db.run('CREATE table IF NOT EXISTS packages(id integer primary key, ' +
'name varchar(500) UNIQUE, url varchar(500), created_at date);', function(){
done();
});
});
grunt.registerTask('get_bower_packages', 'Get official bower packages', function(){
var fs = require('fs');
var request = require('request');
var progress = require('request-progress');
var util = require('util');
var done = this.async();
var url = 'http://bower.herokuapp.com/packages';
if(!fs.existsSync('git_repositories')){
fs.mkdir('git_repositories');
}
//request = request.defaults({'proxy':'http://<proxy-ip>:<proxy-port>'});
var total = '';
progress(request(url), {
throttle: 1000,
delay: 500
})
.on('progress', function (state) {
util.print('Receiving ' + state.received + ' of ' + state.total +
' bytes. ' + state.percent + '% done.\r');
total = state.total;
})
.on('error', function (err) {
console.log('Could not read '+ url +' - ' + err);
})
.pipe(fs.createWriteStream('git_repositories/all_bower_packages.json'))
.on('error', function (err) {
console.log('Could not save git_repositories/all_bower_packages.json - ' + err);
})
.on('close', function (err) {
if(total !== ''){
util.print('Receiving ' + total + ' of ' + total +
' bytes. 100 % done.\r');
}
else {
util.print('100 % done.\r');
}
done();
});
});
grunt.registerTask('sync_db', 'Sync database to Bower official registry', function(){
var done = this.async();
var bowerDB = grunt.file.readJSON('git_repositories/all_bower_packages.json');
var db = new sqlite3.Database(dbName);
var util = require('util');
db.run('CREATE table IF NOT EXISTS packages(id integer primary key, ' +
'name varchar(500) UNIQUE, url varchar(500), created_at date);', function(){
var count = 1;
bowerDB.forEach(function(e){
count++;
if(e.name !== ''){
var temp = count;
db.run('INSERT INTO packages ("name", "url", "created_at") VALUES ($name, $url, $date)',
{
$name: e.name,
$url: e.url,
$date: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
}, function(err, row){
util.print('Syncing with database ' + (100.0 * temp / bowerDB.length).toFixed(2) + '% ' + ' done.\r');
if(temp >= bowerDB.length) { // check if all callbacks have been called
console.log('');
done();
}
});
}
});
});
});
grunt.registerTask('register', function(name){
var fs = require('fs');
if(!fs.existsSync('git_repositories/'+name+'.git') && name !== undefined && name !== ''){
var done = this.async();
var db = new sqlite3.Database(dbName);
var cp = require('child_process');
cp.exec('git init --bare git_repositories/'+name+'.git');
var url = serverLocation + __dirname +'/git_repositories/'+name+'.git';
db.run('CREATE table IF NOT EXISTS packages(id integer primary key, ' +
'name varchar(500) UNIQUE, url varchar(500), created_at date);', function(){
db.run('INSERT INTO packages ("name", "url", "created_at") VALUES ($name, $url, $date)',
{
$name: name,
$url: url,
$date: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')
}, function(err, row){
console.log('\nModule \"'+name+'\" is registered.\n');
console.log('Run these commands in your package directory to finalize the process: \n');
console.log(' git remote add bower '+url);
console.log(' git push bower master');
done();
});
});
}
else{
if(name === undefined || name === ''){
console.log('\nModule name can\'t be undefined.');
}
else{
console.log('\nModule \"'+name+'\" already exists.');
}
console.log('\nShutting down...\n');
}
});
grunt.registerTask('unregister', function(name){
if(name !== undefined && name !== ''){
var done = this.async();
var db = new sqlite3.Database(dbName);
db.run('CREATE table IF NOT EXISTS packages(id integer primary key, ' +
'name varchar(500) UNIQUE, url varchar(500), created_at date);', function(){
db.run('DELETE FROM packages WHERE name = $name', {$name: name}, function(err, row){
console.log('\nModule \"'+name+'\" is unregistered.\n');
console.log('Dont forget to remove the git_repository.');
done();
});
});
}
else{
console.log('\nModule name can\'t be undefined.');
console.log('\nShutting down...\n');
}
});
grunt.registerTask('config', function(ip,port){
var gruntTextReplace = require('grunt-text-replace/lib/grunt-text-replace');
console.log(ip);
console.log(port);
gruntTextReplace.replace({
src: ['Gruntfile.js', 'bowers-nest.js', 'templates/.bowerrc'],
overwrite: true,
replacements: [
{
from:/<ip-address>/g,
to: ip
},
{
from:/<port>/g,
to: port
}
]
});
});
};