-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.js
43 lines (35 loc) · 1.55 KB
/
helper.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
'use strict';
var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
exports.STATE_NEEDLE = '/* STATES-NEEDLE - DO NOT REMOVE THIS */';
// inspired by https://github.com/cgross/generator-cg-angular/blob/master/utils.js
exports.addToFile = function(filename, lineToAdd, beforeMarker) {
try {
var fullPath = path.resolve(process.cwd(), filename);
var fileSrc = fs.readFileSync(fullPath, 'utf8');
var indexOf = fileSrc.indexOf(beforeMarker);
var lineStart = fileSrc.substring(0, indexOf)
.lastIndexOf('\n') + 1;
var indent = fileSrc.substring(lineStart, indexOf);
fileSrc = fileSrc.substring(0, indexOf) + lineToAdd + '\n' + indent + fileSrc.substring(indexOf);
fs.writeFileSync(fullPath, fileSrc);
} catch (e) {
throw e;
}
};
exports.injectRoute = function(routesFile, name, url, tplUrl, ctrl, that) {
var IND = ' ';
var template = tplUrl ? ',\n' + IND + IND + IND + IND + 'templateUrl: \'' + tplUrl + '\'' : '';
ctrl = ctrl ? ',\n' + IND + IND + IND + IND + 'controller: \'' + ctrl + '\'' : '';
var ctrlAs = ctrl ? ',\n' + IND + IND + IND + IND + 'controllerAs: \'vm\'' : '';
var code = '' +
'.state(\'' + name + '\', {' +
'\n' + IND + IND + IND + IND + 'url: \'' + url + '\'' +
ctrl +
ctrlAs +
template +
'\n' + IND + IND + IND + '})';
exports.addToFile(routesFile, code, exports.STATE_NEEDLE);
that.log.writeln(chalk.green(' updating') + ' %s', path.basename(routesFile));
};