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 |
|
13 | function rejected(value) {
|
14 | try {
|
15 | step(generator["throw"](value));
|
16 | } catch (e) {
|
17 | reject(e);
|
18 | }
|
19 | }
|
20 |
|
21 | function step(result) {
|
22 | result.done ? resolve(result.value) : new P(function (resolve) {
|
23 | resolve(result.value);
|
24 | }).then(fulfilled, rejected);
|
25 | }
|
26 |
|
27 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
28 | });
|
29 | };
|
30 |
|
31 | Object.defineProperty(exports, "__esModule", {
|
32 | value: true
|
33 | });
|
34 |
|
35 | const util_1 = require("util");
|
36 |
|
37 | const path_1 = require("path");
|
38 |
|
39 | const rawGlob = require("glob");
|
40 |
|
41 | const fs_extra_1 = require("fs-extra");
|
42 |
|
43 | const js_yaml_1 = require("js-yaml");
|
44 |
|
45 | const glob = util_1.promisify(rawGlob);
|
46 |
|
47 | const get = require('lodash/get');
|
48 |
|
49 | class Project {
|
50 | constructor(isRails, packageJSON, dependencies, devYaml, railgunYaml, hasPostCSSConfig, hasProcfile = false, hasServiceWorker = false) {
|
51 | this.isRails = isRails;
|
52 | this.packageJSON = packageJSON;
|
53 | this.dependencies = dependencies;
|
54 | this.devYaml = devYaml;
|
55 | this.railgunYaml = railgunYaml;
|
56 | this.hasPostCSSConfig = hasPostCSSConfig;
|
57 | this.hasProcfile = hasProcfile;
|
58 | this.hasServiceWorker = hasServiceWorker;
|
59 | }
|
60 |
|
61 | get isNode() {
|
62 | return !this.isRails;
|
63 | }
|
64 |
|
65 | get usesDev() {
|
66 | return Boolean(this.devYaml);
|
67 | }
|
68 |
|
69 | get usesTypeScript() {
|
70 | return this.uses('typescript');
|
71 | }
|
72 |
|
73 | get usesPolaris() {
|
74 | return this.hasDependency('@shopify/polaris');
|
75 | }
|
76 |
|
77 | get usesReact() {
|
78 | return this.hasDependency('react') || this.usesPreactCompat;
|
79 | }
|
80 |
|
81 | get usesPreact() {
|
82 | return this.hasDependency('preact') && !this.usesPreactCompat;
|
83 | }
|
84 |
|
85 | get usesPreactCompat() {
|
86 | return this.hasDependency('preact-compat');
|
87 | }
|
88 |
|
89 | get devType() {
|
90 | return this.getDevKey('type');
|
91 | }
|
92 |
|
93 | get devYamlPort() {
|
94 | return this.getDevKey('server.port');
|
95 | }
|
96 |
|
97 | get devProxyHosts() {
|
98 | const hostnames = this.getRailgunKey('hostnames') || [];
|
99 | const proxyHosts = hostnames.filter(hostname => typeof hostname === 'object');
|
100 | const hostsMap = Object.values(proxyHosts).reduce((acc, host) => {
|
101 | const hostName = Object.keys(host)[0];
|
102 | acc.push({
|
103 | host: hostName,
|
104 | port: host[hostName].proxy_to_host_port
|
105 | });
|
106 | return acc;
|
107 | }, []);
|
108 |
|
109 | if (hostsMap.length === 0) {
|
110 | return [];
|
111 | }
|
112 |
|
113 | const firstHost = hostsMap[0];
|
114 | const port = firstHost.port;
|
115 | return hostsMap.filter(host => {
|
116 | return host.port === port;
|
117 | });
|
118 | }
|
119 |
|
120 | get devPort() {
|
121 |
|
122 | const hosts = this.devProxyHosts;
|
123 | return hosts.length > 0 ? hosts[0].port : undefined;
|
124 | }
|
125 |
|
126 | uses(dependency, versionCondition) {
|
127 | return this.hasDependency(dependency, versionCondition) || this.hasDevDependency(dependency, versionCondition);
|
128 | }
|
129 |
|
130 | version(dependency) {
|
131 | return this.dependencies.get(dependency);
|
132 | }
|
133 |
|
134 | hasDependency(dependency, versionCondition) {
|
135 | if (this.packageJSON.dependencies[dependency] == null) {
|
136 | return false;
|
137 | }
|
138 |
|
139 | if (versionCondition == null) {
|
140 | return true;
|
141 | }
|
142 |
|
143 | const version = this.version(dependency);
|
144 | return version != null && versionCondition.test(dependency);
|
145 | }
|
146 |
|
147 | hasDevDependency(dependency, versionCondition) {
|
148 | if (this.packageJSON.devDependencies[dependency] == null) {
|
149 | return false;
|
150 | }
|
151 |
|
152 | if (versionCondition == null) {
|
153 | return true;
|
154 | }
|
155 |
|
156 | const version = this.version(dependency);
|
157 | return version != null && versionCondition.test(dependency);
|
158 | }
|
159 |
|
160 | getDevKey(keyPath) {
|
161 | if (!this.usesDev) {
|
162 | return undefined;
|
163 | }
|
164 |
|
165 | return get(this.devYaml, keyPath, undefined);
|
166 | }
|
167 |
|
168 | getRailgunKey(keyPath) {
|
169 | if (!this.railgunYaml) {
|
170 | return undefined;
|
171 | }
|
172 |
|
173 | return get(this.railgunYaml, keyPath, undefined);
|
174 | }
|
175 |
|
176 | }
|
177 |
|
178 | exports.Project = Project;
|
179 |
|
180 | function readYaml(path) {
|
181 | return __awaiter(this, void 0, void 0, function* () {
|
182 | if (!(yield fs_extra_1.pathExists(path))) {
|
183 | return false;
|
184 | }
|
185 |
|
186 | const result = yield js_yaml_1.safeLoad((yield fs_extra_1.readFile(path, 'utf8')));
|
187 | return result || false;
|
188 | });
|
189 | }
|
190 |
|
191 | class DependencyInfoRequire {
|
192 | has(dependency) {
|
193 | try {
|
194 | require.resolve(dependency);
|
195 |
|
196 | return true;
|
197 | } catch (_a) {
|
198 | return false;
|
199 | }
|
200 | }
|
201 |
|
202 | get(dependency) {
|
203 | try {
|
204 | return require(`${dependency}/package.json`).version;
|
205 | } catch (_a) {
|
206 | throw new Error(`Could not resolve version for dependency '${dependency}'`);
|
207 | }
|
208 | }
|
209 |
|
210 | }
|
211 |
|
212 | function loadProject(root) {
|
213 | return __awaiter(this, void 0, void 0, function* () {
|
214 | const devPath = path_1.join(root, 'dev.yml');
|
215 | const devYaml = yield readYaml(devPath);
|
216 | const railgunPath = path_1.join(root, 'railgun.yml');
|
217 | const railgunYaml = Boolean(devYaml) && (yield readYaml(railgunPath));
|
218 | const packageJSON = Object.assign({
|
219 | dependencies: {},
|
220 | devDependencies: {}
|
221 | }, (yield fs_extra_1.readJSON(path_1.join(root, 'package.json'))));
|
222 | const dependencies = new DependencyInfoRequire();
|
223 | const isRails = yield fs_extra_1.pathExists(path_1.join(root, 'Gemfile'));
|
224 | const hasPostCSSConfig = yield fs_extra_1.pathExists(path_1.join(root, './postcss.config.js'));
|
225 | const hasServiceWorker = yield fs_extra_1.pathExists(path_1.join(root, 'service-worker'));
|
226 | const hasProcfile = isRails && (yield glob('Procfile*(.*|)')).length > 0;
|
227 | return new Project(isRails, packageJSON, dependencies, devYaml, railgunYaml, hasPostCSSConfig, hasProcfile, hasServiceWorker);
|
228 | });
|
229 | }
|
230 |
|
231 | exports.default = loadProject; |
\ | No newline at end of file |