1 | "use strict";
|
2 |
|
3 | var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) {
|
6 | try {
|
7 | step(generator.next(value));
|
8 | } catch (e) {
|
9 | reject(e);
|
10 | }
|
11 | }
|
12 | function rejected(value) {
|
13 | try {
|
14 | step(generator["throw"](value));
|
15 | } catch (e) {
|
16 | reject(e);
|
17 | }
|
18 | }
|
19 | function step(result) {
|
20 | result.done ? resolve(result.value) : new P(function (resolve) {
|
21 | resolve(result.value);
|
22 | }).then(fulfilled, rejected);
|
23 | }
|
24 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
25 | });
|
26 | };
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | const path_1 = require("path");
|
29 | const fs_extra_1 = require("fs-extra");
|
30 | const allPlugins = require("../plugins");
|
31 | const utilities_1 = require("../utilities");
|
32 | const nodeObjectHash = require('node-object-hash');
|
33 | const TYPESCRIPT_CONFIG_FILE_NAME = 'sewing-kit.config.ts';
|
34 | const JAVASCRIPT_CONFIG_FILE_NAME = 'sewing-kit.config.js';
|
35 | class Config {
|
36 | constructor(name, plugins = [], library = false) {
|
37 | this.name = name;
|
38 | this.plugins = plugins;
|
39 | this.library = library;
|
40 | }
|
41 | for(plugin) {
|
42 | const foundPlugin = this.plugins.find(({ plugin: aPlugin }) => aPlugin === plugin);
|
43 | if (!foundPlugin && plugin === 'experiments') {
|
44 | const experiments = allPlugins.experiments({});
|
45 | this.plugins.push(experiments);
|
46 | return experiments;
|
47 | }
|
48 | return foundPlugin;
|
49 | }
|
50 | get hash() {
|
51 | return nodeObjectHash({
|
52 | sort: false,
|
53 | coerce: false,
|
54 | alg: 'md5',
|
55 | enc: 'hex'
|
56 | }).hash(this);
|
57 | }
|
58 | }
|
59 | exports.Config = Config;
|
60 | function loadConfig(configPath, env, project, runner) {
|
61 | return __awaiter(this, void 0, void 0, function* () {
|
62 | const finalConfigPath = configPath && path_1.resolve(configPath) || (yield getDefaultConfigPath(project));
|
63 | let config;
|
64 | if (finalConfigPath) {
|
65 | try {
|
66 | const configCallback = finalConfigPath.endsWith('.ts') ? yield readTypeScriptConfig(finalConfigPath, project) : require(finalConfigPath);
|
67 | config = configCallback(allPlugins, env);
|
68 | } catch (err) {
|
69 | runner.logger.error(err);
|
70 | runner.end();
|
71 | throw err;
|
72 | }
|
73 | }
|
74 | const { name, plugins, library } = config || {
|
75 | name: undefined,
|
76 | plugins: [],
|
77 | library: false
|
78 | };
|
79 | const finalName = name || finalConfigPath && path_1.basename(path_1.dirname(finalConfigPath)) || 'project';
|
80 | return new Config(finalName, plugins, library);
|
81 | });
|
82 | }
|
83 | exports.default = loadConfig;
|
84 | function getDefaultConfigPath({ isRails }) {
|
85 | return __awaiter(this, void 0, void 0, function* () {
|
86 | const cwd = process.cwd();
|
87 | const configLocations = utilities_1.flatten([utilities_1.ifElse(isRails, path_1.join(cwd, 'config', TYPESCRIPT_CONFIG_FILE_NAME)), utilities_1.ifElse(isRails, path_1.join(cwd, 'config', JAVASCRIPT_CONFIG_FILE_NAME)), path_1.join(cwd, TYPESCRIPT_CONFIG_FILE_NAME), path_1.join(cwd, JAVASCRIPT_CONFIG_FILE_NAME)]);
|
88 | for (const configLocation of configLocations) {
|
89 | if (yield fs_extra_1.pathExists(configLocation)) {
|
90 | return configLocation;
|
91 | }
|
92 | }
|
93 | return false;
|
94 | });
|
95 | }
|
96 | function readTypeScriptConfig(configPath, { usesTypeScript }) {
|
97 | return __awaiter(this, void 0, void 0, function* () {
|
98 | if (!usesTypeScript) {
|
99 | const relativeConfigPath = path_1.relative(process.cwd(), configPath);
|
100 | const { default: chalk } = yield Promise.resolve().then(() => require('chalk'));
|
101 | throw new Error(`😿 To read ${chalk.bold(relativeConfigPath)}, sewing-kit needs a TypeScript compiler\n\nTypeScript can be installed using ${chalk.bold('yarn add typescript')}`);
|
102 | }
|
103 | const [fs, sourceMapSupport, ts] = yield Promise.all([Promise.resolve().then(() => require('fs-extra')), Promise.resolve().then(() => require('source-map-support')), Promise.resolve().then(() => require('typescript'))]);
|
104 | sourceMapSupport.install({
|
105 |
|
106 | hookRequire: true
|
107 | });
|
108 | const jsSrc = ts.transpileModule((yield fs.readFile(configPath, 'utf8')), {
|
109 | fileName: configPath,
|
110 | compilerOptions: {
|
111 | inlineSourceMap: true,
|
112 | isolatedModules: true,
|
113 | module: ts.ModuleKind.CommonJS,
|
114 | noEmit: true,
|
115 | skipLibCheck: true,
|
116 | target: ts.ScriptTarget.ES5
|
117 | }
|
118 | });
|
119 | const jsPath = path_1.join(path_1.dirname(configPath), '.sewing-kit.config.transpiled.tmp.js');
|
120 | fs.writeFileSync(jsPath, jsSrc.outputText);
|
121 | const configCallback = require(jsPath);
|
122 | fs.removeSync(jsPath);
|
123 | return configCallback.default || configCallback;
|
124 | });
|
125 | } |
\ | No newline at end of file |