123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- /*
- * 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();
- };
|