UNPKG

2.4 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5const path_1 = __importDefault(require("path"));
6const findup_sync_1 = __importDefault(require("findup-sync"));
7const esmRequire = require('esm')(module);
8/**
9 * Require a brocfile via either ESM or TypeScript
10 *
11 * @param {String} brocfilePath The path to the brocfile
12 * @returns {*}
13 */
14function requireBrocfile(brocfilePath) {
15 let brocfile;
16 if (brocfilePath.match(/\.ts$/)) {
17 try {
18 require.resolve('ts-node');
19 }
20 catch (e) {
21 throw new Error(`Cannot find module 'ts-node', please install`);
22 }
23 try {
24 require.resolve('typescript');
25 }
26 catch (e) {
27 throw new Error(`Cannot find module 'typescript', please install`);
28 }
29 // Register ts-node typescript compiler
30 require('ts-node').register(); // eslint-disable-line node/no-unpublished-require
31 // Load brocfile via ts-node
32 brocfile = require(brocfilePath);
33 }
34 else {
35 // Load brocfile via esm shim
36 brocfile = esmRequire(brocfilePath);
37 }
38 // ESM `export default X` is represented as module.exports = { default: X }
39 if (brocfile !== null && typeof brocfile === 'object' && brocfile.hasOwnProperty('default')) {
40 brocfile = brocfile.default;
41 }
42 return brocfile;
43}
44module.exports = function loadBrocfile(options = {}) {
45 let brocfilePath;
46 if (options.brocfilePath) {
47 brocfilePath = path_1.default.resolve(options.brocfilePath);
48 }
49 else {
50 brocfilePath = findup_sync_1.default('Brocfile.{ts,js}', {
51 nocase: true,
52 });
53 }
54 if (!brocfilePath) {
55 throw new Error('Brocfile.[js|ts] not found');
56 }
57 const baseDir = options.cwd || path_1.default.dirname(brocfilePath);
58 // The chdir should perhaps live somewhere else and not be a side effect of
59 // this function, or go away entirely
60 process.chdir(baseDir);
61 const brocfile = requireBrocfile(brocfilePath);
62 // Brocfile should export a function, if it did, return now
63 if (typeof brocfile === 'function') {
64 return brocfile;
65 }
66 // Wrap brocfile result in a function for backwards compatibility
67 return () => brocfile;
68};
69//# sourceMappingURL=load_brocfile.js.map
\No newline at end of file