forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
194 lines (152 loc) · 5 KB
/
setup.js
File metadata and controls
194 lines (152 loc) · 5 KB
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
var Task = require('./task').Task,
IonicStats = require('./stats').IonicStats,
fs = require('fs'),
path = require('path'),
argv = require('optimist').argv,
exec = require('child_process').exec,
Q = require('q'),
IonicProject = require('./project'),
IonicAppLib = require('ionic-app-lib'),
Utils = IonicAppLib.utils,
colors = require('colors');
var IonicTask = function() {};
IonicTask.prototype = new Task();
IonicTask.prototype.run = function(ionic) {
if( argv._.length < 2 ) {
return ionic.fail('Missing setup task command.', 'setup');
}
if (!Utils.gulpInstalledGlobally()) {
var gulpMessage = ['You have specified Ionic CLI to set up sass.'.red, '\n', 'However, you do not have Gulp installed globally. Please run '.red, '`npm install -g gulp`'.green].join('');
events.emit('log', gulpMessage);
return Q(gulpMessage);
}
var tasks = argv._.slice(1);
for(var x=0; x<tasks.length; x++) {
if(tasks[x].toLowerCase() == 'sass') {
this.sassSetup(ionic);
} else {
return ionic.fail('Invalid setup task command: ' + tasks[x], 'setup');
}
}
IonicStats.t();
};
IonicTask.prototype.sassSetup = function(ionic) {
var q = Q.defer();
var self = this;
self.ionic = ionic;
this.npmInstall().then(function(){
var indexPath = path.resolve( path.join('www', ionic.getContentSrc()) );
var lines, line, keepLine, activeRemoving;
var cleanedLines = [];
try {
lines = fs.readFileSync(indexPath, 'utf8').split('\n');
} catch(e) {
q.reject(e);
return ionic.fail('Error loading ' + indexPath);
}
try {
activeRemoving = false;
for(x=0; x<lines.length; x++) {
line = lines[x];
keepLine = true;
if( /<!--(.*?) sass (.*?)/gi.test(line) ) {
line = ' <!-- compiled css output -->';
activeRemoving = true;
} else if (activeRemoving && /(.*?)-->(.*?)/gi.test(line) ) {
keepLine = false;
activeRemoving = false;
} else if( /lib\/ionic\/css\/ionic.css|css\/style.css/gi.test(line) ) {
keepLine = false;
}
if(keepLine) {
cleanedLines.push(line);
}
}
var project = null;
try {
project = IonicProject.load();
} catch (ex) {
self.ionic.fail(ex.message);
return
}
var gulpStartupTasks = project.get('gulpStartupTasks') || [];
var hasSass, hasWatch;
gulpStartupTasks.forEach(function(taskName){
if(taskName == 'sass') hasSass = true;
if(taskName == 'watch') hasWatch = true;
});
if(!hasSass) gulpStartupTasks.push('sass');
if(!hasWatch) gulpStartupTasks.push('watch');
project.set('gulpStartupTasks', gulpStartupTasks);
if(!project.get('watchPatterns')){
project.set('watchPatterns', ['www/**/*', '!www/lib/**/*'])
}
project.save();
fs.writeFileSync(indexPath, cleanedLines.join('\n'), 'utf8');
console.log('Updated '.green.bold + indexPath.bold + ' <link href>'.yellow.bold + ' references to sass compiled css'.green.bold);
console.log('\nIonic project ready to use Sass!'.green.bold);
console.log(' * Customize the app using'.yellow.bold, 'scss/ionic.app.scss'.bold);
console.log(' * Run'.yellow.bold, 'ionic serve'.bold, 'to start a local dev server and watch/compile Sass to CSS'.yellow.bold);
console.log('');
self.buildSass().then(function(){
q.resolve();
});
} catch(e) {
q.reject(e);
return ionic.fail('Error parsing ' + indexPath + ': ' + e);
}
}, function(e){
q.reject(e);
});
return q.promise;
};
IonicTask.prototype.buildSass = function() {
var q = Q.defer();
var childProcess = exec('gulp sass');
childProcess.stdout.on('data', function (data) {
process.stdout.write(data);
});
childProcess.stderr.on('data', function (data) {
if(data) {
process.stderr.write(data.toString().yellow);
}
});
childProcess.on('exit', function(code){
process.stderr.write('\n');
if(code === 0) {
console.log('Successful '.green.bold + 'sass build'.bold);
q.resolve();
} else {
console.log( 'Error running '.error.bold + 'gulp sass'.bold );
q.reject();
}
});
return q.promise;
};
IonicTask.prototype.npmInstall = function() {
var q = Q.defer();
var childProcess = exec('npm install');
childProcess.stdout.on('data', function (data) {
process.stdout.write(data);
});
childProcess.stderr.on('data', function (data) {
if(data) {
data = data.toString();
if( !/no repository field/gi.test(data) ) {
process.stderr.write(data.yellow);
}
}
});
childProcess.on('exit', function(code){
process.stderr.write('\n');
if(code === 0) {
console.log('Successful '.green.bold + 'npm install'.bold);
q.resolve();
} else {
console.log( 'Error running '.error.bold + 'npm install'.bold );
q.reject();
}
});
return q.promise;
};
exports.IonicTask = IonicTask;