forked from strongloop/loopback-example-offline-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
170 lines (143 loc) · 4.23 KB
/
Copy pathgulpfile.js
File metadata and controls
170 lines (143 loc) · 4.23 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
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var sh = require('shelljs');
var async = require('async');
var path = require('path');
var fs = require('fs');
var currentEnv = process.env.NODE_ENV || 'development';
var config = {
global: {},
local: {},
build: {}
};
var webProcess;
// add the current directory to the NODE_PATH
process.env.NODE_PATH = __dirname + ':' + process.env.NODE_PATH;
// install deps of the packages
gulp.task('install', function() {
var pwd = process.cwd();
findPackages().forEach(function(pkg) {
process.chdir(path.join(pwd, pkg));
sh.exec('npm install');
});
process.chdir(pwd);
});
// run the entire project
gulp.task('run', function(cb) {
nodemon({
script: 'app.js',
ext: 'html js ejs',
ignore: ['node_modules/**', 'node_modules/**/node_modules', 'build/**'],
watch: findSrc(),
cwd: path.join(__dirname, 'web'),
env: {NODE_PATH: process.env.NODE_PATH},
nodeArgs: ['--debug']
})
.on('change', function() {
// TODO(ritch) only build the package that changed
findAndBuild('*');
})
.on('restart', function () {
console.log('restarted!');
});
});
// build a package
gulp.task('build', function(cb) {
findAndBuild('*', cb);
});
// default task (for dev)
gulp.task('default', ['build', 'run']);
function findAndBuild(packageName, cb) {
var packages = findPackages(packageName);
// clean
// packages.forEach(clean);
// configure all globally
packages.forEach(globalConfigure);
// configure locally
packages.forEach(localConfigure);
// write local config modules
packages.forEach(writeLocalConfigModule);
// write global config module
writeGlobalConfigModule();
// build each
async.each(packages, build, cb);
}
function findSrc(package) {
var paths = [];
findPackages(package).forEach(function(pkg) {
paths.push(path.join(__dirname, pkg, '*', '*') + '.js');
paths.push(path.join(__dirname, pkg, '*.js'));
});
return paths;
}
var packageCache;
function findPackages(packageName) {
var matchAll = packageName === '*' || !packageName;
var packages = packageCache || (packageCache = listPackages());
return packages.filter(function(directoryName) {
if(matchAll) {
return true;
} else if(directoryName === packageName) {
return true;
}
return false;
});
}
function listPackages() {
return sh
.find(__dirname)
.filter(function(filePath) {
return path.dirname(path.dirname(filePath)) === __dirname
&& path.dirname(filePath) !== __dirname
&& path.basename(filePath) === 'package.json'
})
.map(function(filePath) {
return path.basename(path.dirname(filePath));
});
}
function build(package, cb) {
var buildConfig = config.build[package];
console.log('building', package);
configure(package, 'build', cb);
}
function configure(package, scope, cb) {
var configurePackage = packageScript(package, 'configure');
var localConfig = config.local[package] || (config.local[package] = {});
var globalConfig = config.global;
if(configurePackage && configurePackage[scope]) {
configurePackage[scope](currentEnv, globalConfig, localConfig, cb);
} else if(cb) {
cb();
}
}
function globalConfigure(package) {
configure(package, 'global');
}
function localConfigure(package) {
configure(package, 'local');
}
function clean(package) {
removeConfigModule(package);
}
function packageScript(package, name) {
var file = path.join(__dirname, package, name + '.js');
if(fs.existsSync(file)) {
return require(file);
}
}
function writeLocalConfigModule(package) {
var localConfig = config.local[package];
writeConfigModule(path.join(__dirname, package), 'local.config.js', localConfig);
}
function writeGlobalConfigModule() {
writeConfigModule(__dirname, 'global.config.js', config.global);
}
function writeConfigModule(root, name, obj) {
var type = ~name.indexOf('global') ? 'GLOBAL' : 'LOCAL';
sh.mkdir('-p', path.join(root, 'node_modules'));
var src = 'module.exports = process.' + type + '_CONFIG = ' + JSON.stringify(obj);
src.to(path.join(root, 'node_modules', name));
}
function removeConfigModule(package) {
fs.unlinkSync(path.join(__dirname, package, 'node_modules', 'local.config.js'));
}