source: frontend/node_modules/jake/lib/loader.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Jake JavaScript build tool
3 * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17*/
18
19let path = require('path');
20let fs = require('fs');
21let existsSync = fs.existsSync;
22let utils = require('./utils');
23
24// Files like jakelib/foobar.jake.js
25const JAKELIB_FILE_PAT = /\.jake$|\.js$/;
26const SUPPORTED_EXTENSIONS = {
27 'js': null,
28 'cjs': null,
29 'coffee': function () {
30 try {
31 let cs = require('coffeescript');
32 if (typeof cs.register == 'function') {
33 cs.register();
34 }
35 }
36 catch(e) {
37 throw new Error('You have a CoffeeScript Jakefile, but have not installed CoffeeScript');
38 }
39 },
40 'ls': function () {
41 try {
42 require('livescript');
43 }
44 catch (e) {
45 throw new Error('You have a LiveScript Jakefile, but have not installed LiveScript');
46 }
47 },
48 'ts': function () {
49 try {
50 require('ts-node/register/transpile-only');
51 }
52 catch (e) {
53 throw new Error('You have a TypeScript Jakefile, but have not installed TypeScript and ts-node');
54 }
55 }
56};
57const IMPLICIT_JAKEFILE_NAMES = [
58 'Jakefile',
59 'Gulpfile'
60];
61
62let Loader = function () {
63 // Load a Jakefile, running the code inside -- this may result in
64 // tasks getting defined using the original Jake API, e.g.,
65 // `task('foo' ['bar', 'baz']);`, or can also auto-create tasks
66 // from any functions exported from the file
67 function loadFile(filePath) {
68 let exported = require(filePath);
69 for (let [key, value] of Object.entries(exported)) {
70 let t;
71 if (typeof value == 'function') {
72 t = jake.task(key, value);
73 t.description = '(Exported function)';
74 }
75 }
76 }
77
78 function fileExists(name) {
79 let nameWithExt = null;
80 // Support no file extension as well
81 let exts = Object.keys(SUPPORTED_EXTENSIONS).concat(['']);
82 exts.some((ext) => {
83 let fname = ext ? `${name}.${ext}` : name;
84 if (existsSync(fname)) {
85 nameWithExt = fname;
86 return true;
87 }
88 });
89 return nameWithExt;
90 }
91
92 // Recursive
93 function findImplicitJakefile() {
94 let cwd = process.cwd();
95 let names = IMPLICIT_JAKEFILE_NAMES;
96 let found = null;
97 names.some((name) => {
98 let n;
99 // Prefer all-lowercase
100 n = name.toLowerCase();
101 if ((found = fileExists(n))) {
102 return found;
103 }
104 // Check mixed-case as well
105 n = name;
106 if ((found = fileExists(n))) {
107 return found;
108 }
109 });
110 if (found) {
111 return found;
112 }
113 else {
114 process.chdir("..");
115 // If we've walked all the way up the directory tree,
116 // bail out with no result
117 if (cwd === process.cwd()) {
118 return null;
119 }
120 return findImplicitJakefile();
121 }
122 }
123
124 this.loadFile = function (fileSpecified) {
125 let jakefile;
126 let origCwd = process.cwd();
127
128 if (fileSpecified) {
129 if (existsSync(fileSpecified)) {
130 jakefile = fileSpecified;
131 }
132 }
133 else {
134 jakefile = findImplicitJakefile();
135 }
136
137 if (jakefile) {
138 let ext = jakefile.split('.')[1];
139 let loaderFunc = SUPPORTED_EXTENSIONS[ext];
140 loaderFunc && loaderFunc();
141
142 loadFile(utils.file.absolutize(jakefile));
143 return true;
144 }
145 else {
146 if (!fileSpecified) {
147 // Restore the working directory on failure
148 process.chdir(origCwd);
149 }
150 return false;
151 }
152 };
153
154 this.loadDirectory = function (d) {
155 let dirname = d || 'jakelib';
156 let dirlist;
157 dirname = utils.file.absolutize(dirname);
158 if (existsSync(dirname)) {
159 dirlist = fs.readdirSync(dirname);
160 dirlist.forEach(function (filePath) {
161 if (JAKELIB_FILE_PAT.test(filePath)) {
162 loadFile(path.join(dirname, filePath));
163 }
164 });
165 return true;
166 }
167 return false;
168 };
169
170};
171
172module.exports = function () {
173 return new Loader();
174};
Note: See TracBrowser for help on using the repository browser.