UNPKG

3.03 kBJavaScriptView Raw
1#!/usr/bin/env node
2const fs = require('fs');
3const util = require('util');
4// eslint-disable-next-line import/extensions
5const supportedExtensions = require('./src/extensions.js');
6/**
7 * As this is a mixture of ES6 and ES5 we require module that might
8 * be exported as default or using the old module.exports
9 * @param m array | object | any
10 * @returns {*}
11 */
12/* global getDefault */
13global.getDefault = global.getDefault || (m => (m.default ? m.default : m));
14
15/**
16 * We need to resolve the files as per the extensions at many places
17 * for example we do not want to restrict people to just .js or .jsx extension
18 * we need ability like fileExistsSync to compare for all extensions we have defined
19 * in `src/extensions`
20 * @type {*|Function}
21 */
22global.pawExistsSync = global.pawExistsSync || ((filePath, fileSystem = fs) => {
23 if (fileSystem.existsSync(filePath)) return filePath;
24 let resolvedFilePath = '';
25 supportedExtensions.javascript.forEach((jsExt) => {
26 if (resolvedFilePath) {
27 return;
28 }
29 if (fileSystem.existsSync(filePath + jsExt)) {
30 resolvedFilePath = filePath + jsExt;
31 }
32 });
33 return resolvedFilePath;
34});
35
36// eslint-disable-next-line no-unused-vars
37/* global pawDebug */
38global.pawDebug = global.pawDebug || ((data, options = {}) => {
39 // eslint-disable-next-line
40 console.log(util.inspect(data, { depth: 10, ...options }));
41});
42
43/**
44 * @desc Set cache to enabled by default,
45 * at this moment we need this cache to determine if we would like to use babel cache
46 * @type {boolean}
47 */
48let cacheEnabled = true;
49
50/**
51 * Traverse through all the arguments and check if the user has
52 * indicated if he does not want to use cache!
53 */
54Array.from(process.argv).forEach((arg) => {
55 // Convert argument to lowercase
56 const lArg = arg.toLowerCase();
57 if (
58 lArg.indexOf('-nc') !== false
59 || lArg.indexOf('--no-cache') !== false
60 ) {
61 process.env.BABEL_DISABLE_CACHE = 1;
62 cacheEnabled = false;
63 }
64});
65
66// Get babel configuration for nodejs server
67const babelServerOptions = getDefault(require('./src/babel/node.js'))({
68 cacheDirectory: cacheEnabled,
69 hot: false,
70 noChunk: true,
71 cache: cacheEnabled,
72}).use.options;
73
74/**
75 * Use babel register so that we can use latest EcmaScript & TypeScript version
76 * in included files. Also we need to make sure that any plugins for pawjs or pawjs core
77 * modules needs to be access with new code directly and there should be no need for
78 * compiled code even if it lies in node_modules
79 */
80require('@babel/register')({
81 presets: babelServerOptions.presets,
82 plugins: babelServerOptions.plugins,
83 cache: cacheEnabled,
84 ignore: [
85 // Allow @pawjs core & pawjs- plugins to be of es6 or TS format
86 /node_modules\/(?!(@pawjs|pawjs-)).*/,
87 ],
88 extensions: supportedExtensions.resolveExtensions,
89});
90
91/**
92 * After registering babel we can include typescript (TS) files as well
93 */
94const CliHandler = getDefault(require('./src/scripts/cli.ts'));
95
96const handler = new CliHandler();
97handler.run();
98
99module.exports = handler;