2 * Jake JavaScript build tool
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 let path = require('path');
20 let fs = require('fs');
21 let existsSync = fs.existsSync;
22 let utils = require('./utils');
24 // Files like jakelib/foobar.jake.js
25 const JAKELIB_FILE_PAT = /\.jake$|\.js$/;
26 const SUPPORTED_EXTENSIONS = {
28 'coffee': function () {
30 let cs = require('coffeescript');
31 if (typeof cs.register == 'function') {
36 throw new Error('You have a CoffeeScript Jakefile, but have not installed CoffeeScript');
41 require('livescript');
44 throw new Error('You have a LiveScript Jakefile, but have not installed LiveScript');
48 const IMPLICIT_JAKEFILE_NAMES = [
53 let Loader = function () {
54 // Load a Jakefile, running the code inside -- this may result in
55 // tasks getting defined using the original Jake API, e.g.,
56 // `task('foo' ['bar', 'baz']);`, or can also auto-create tasks
57 // from any functions exported from the file
58 function loadFile(filePath) {
59 let exported = require(filePath);
60 for (let [key, value] of Object.entries(exported)) {
62 if (typeof value == 'function') {
63 t = jake.task(key, value);
64 t.description = '(Exported function)';
69 function fileExists(name) {
70 let nameWithExt = null;
71 // Support no file extension as well
72 let exts = Object.keys(SUPPORTED_EXTENSIONS).concat(['']);
74 let fname = ext ? `${name}.${ext}` : name;
75 if (existsSync(fname)) {
84 function findImplicitJakefile() {
85 let cwd = process.cwd();
86 let names = IMPLICIT_JAKEFILE_NAMES;
88 names.some((name) => {
90 // Prefer all-lowercase
91 n = name.toLowerCase();
92 if ((found = fileExists(n))) {
95 // Check mixed-case as well
97 if ((found = fileExists(n))) {
106 // If we've walked all the way up the directory tree,
107 // bail out with no result
108 if (cwd === process.cwd()) {
111 return findImplicitJakefile();
115 this.loadFile = function (fileSpecified) {
117 let origCwd = process.cwd();
120 if (existsSync(fileSpecified)) {
121 jakefile = fileSpecified;
125 jakefile = findImplicitJakefile();
129 let ext = jakefile.split('.')[1];
130 let loaderFunc = SUPPORTED_EXTENSIONS[ext];
131 loaderFunc && loaderFunc();
133 loadFile(utils.file.absolutize(jakefile));
137 if (!fileSpecified) {
138 // Restore the working directory on failure
139 process.chdir(origCwd);
145 this.loadDirectory = function (d) {
146 let dirname = d || 'jakelib';
148 dirname = utils.file.absolutize(dirname);
149 if (existsSync(dirname)) {
150 dirlist = fs.readdirSync(dirname);
151 dirlist.forEach(function (filePath) {
152 if (JAKELIB_FILE_PAT.test(filePath)) {
153 loadFile(path.join(dirname, filePath));
163 module.exports = function () {