loader.js
4.31 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
/*
* Jake JavaScript build tool
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
let path = require('path');
let fs = require('fs');
let existsSync = fs.existsSync;
let utils = require('./utils');
// Files like jakelib/foobar.jake.js
const JAKELIB_FILE_PAT = /\.jake$|\.js$/;
const SUPPORTED_EXTENSIONS = {
'js': null,
'coffee': function () {
try {
let cs = require('coffeescript');
if (typeof cs.register == 'function') {
cs.register();
}
}
catch(e) {
throw new Error('You have a CoffeeScript Jakefile, but have not installed CoffeeScript');
}
},
'ls': function () {
try {
require('livescript');
}
catch (e) {
throw new Error('You have a LiveScript Jakefile, but have not installed LiveScript');
}
},
'ts': function () {
try {
require('ts-node/register/transpile-only');
}
catch (e) {
throw new Error('You have a TypeScript Jakefile, but have not installed TypeScript and ts-node');
}
}
};
const IMPLICIT_JAKEFILE_NAMES = [
'Jakefile',
'Gulpfile'
];
let Loader = function () {
// Load a Jakefile, running the code inside -- this may result in
// tasks getting defined using the original Jake API, e.g.,
// `task('foo' ['bar', 'baz']);`, or can also auto-create tasks
// from any functions exported from the file
function loadFile(filePath) {
let exported = require(filePath);
for (let [key, value] of Object.entries(exported)) {
let t;
if (typeof value == 'function') {
t = jake.task(key, value);
t.description = '(Exported function)';
}
}
}
function fileExists(name) {
let nameWithExt = null;
// Support no file extension as well
let exts = Object.keys(SUPPORTED_EXTENSIONS).concat(['']);
exts.some((ext) => {
let fname = ext ? `${name}.${ext}` : name;
if (existsSync(fname)) {
nameWithExt = fname;
return true;
}
});
return nameWithExt;
}
// Recursive
function findImplicitJakefile() {
let cwd = process.cwd();
let names = IMPLICIT_JAKEFILE_NAMES;
let found = null;
names.some((name) => {
let n;
// Prefer all-lowercase
n = name.toLowerCase();
if ((found = fileExists(n))) {
return found;
}
// Check mixed-case as well
n = name;
if ((found = fileExists(n))) {
return found;
}
});
if (found) {
return found;
}
else {
process.chdir("..");
// If we've walked all the way up the directory tree,
// bail out with no result
if (cwd === process.cwd()) {
return null;
}
return findImplicitJakefile();
}
}
this.loadFile = function (fileSpecified) {
let jakefile;
let origCwd = process.cwd();
if (fileSpecified) {
if (existsSync(fileSpecified)) {
jakefile = fileSpecified;
}
}
else {
jakefile = findImplicitJakefile();
}
if (jakefile) {
let ext = jakefile.split('.')[1];
let loaderFunc = SUPPORTED_EXTENSIONS[ext];
loaderFunc && loaderFunc();
loadFile(utils.file.absolutize(jakefile));
return true;
}
else {
if (!fileSpecified) {
// Restore the working directory on failure
process.chdir(origCwd);
}
return false;
}
};
this.loadDirectory = function (d) {
let dirname = d || 'jakelib';
let dirlist;
dirname = utils.file.absolutize(dirname);
if (existsSync(dirname)) {
dirlist = fs.readdirSync(dirname);
dirlist.forEach(function (filePath) {
if (JAKELIB_FILE_PAT.test(filePath)) {
loadFile(path.join(dirname, filePath));
}
});
return true;
}
return false;
};
};
module.exports = function () {
return new Loader();
};