1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.findEnvDef = void 0;
|
7 | exports.getEnvDefVersionHash = getEnvDefVersionHash;
|
8 | exports.getEnvDefs = void 0;
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _semver = _interopRequireDefault(require("semver"));
|
13 |
|
14 | var _cacheRepoUtils = require("./cacheRepoUtils");
|
15 |
|
16 | var _node = require("./node");
|
17 |
|
18 | var _ValidationError = require("./ValidationError");
|
19 |
|
20 | var _flowVersion = require("./flowVersion");
|
21 |
|
22 | var _fileUtils = require("./fileUtils");
|
23 |
|
24 | var _libDefs = require("./libDefs");
|
25 |
|
26 | var _git = require("./git");
|
27 |
|
28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
29 |
|
30 | const getEnvDefs = async () => {
|
31 | const definitionsDir = _path.default.join((0, _cacheRepoUtils.getCacheRepoDir)(), 'definitions');
|
32 |
|
33 | const envDefsDirPath = _path.default.join(definitionsDir, 'environments');
|
34 |
|
35 | const dirItems = await _node.fs.readdir(envDefsDirPath);
|
36 | const errors = [];
|
37 | const proms = dirItems.map(async itemName => {
|
38 | if ((0, _fileUtils.isExcludedFile)(itemName)) return;
|
39 |
|
40 | try {
|
41 | return await getSingleEnvDef(itemName, envDefsDirPath);
|
42 | } catch (e) {
|
43 | errors.push(e);
|
44 | }
|
45 | });
|
46 | const settled = await Promise.all(proms);
|
47 |
|
48 | if (errors.length) {
|
49 | throw errors;
|
50 | }
|
51 |
|
52 | return [...settled].filter(Boolean).flat();
|
53 | };
|
54 |
|
55 | exports.getEnvDefs = getEnvDefs;
|
56 |
|
57 | const getSingleEnvDef = async (defName, envDefPath) => {
|
58 | const itemPath = _path.default.join(envDefPath, defName);
|
59 |
|
60 | const itemStat = await _node.fs.stat(itemPath);
|
61 |
|
62 | if (itemStat.isDirectory()) {
|
63 |
|
64 | return await extractEnvDefs(itemPath, defName);
|
65 | } else {
|
66 | throw new _ValidationError.ValidationError(`Expected only directories to be present in this directory.`);
|
67 | }
|
68 | };
|
69 |
|
70 | async function extractEnvDefs(envDirPath, defName) {
|
71 | const envDefFileName = `${defName}.js`;
|
72 | const envDirItems = await _node.fs.readdir(envDirPath);
|
73 | const commonTestFiles = [];
|
74 | const parsedFlowDirs = [];
|
75 | envDirItems.forEach(envDirItem => {
|
76 | const envDirItemPath = _path.default.join(envDirPath, envDirItem);
|
77 |
|
78 | const envDirItemStat = _node.fs.statSync(envDirItemPath);
|
79 |
|
80 | if (envDirItemStat.isFile()) {
|
81 | const isValidTestFile = _libDefs.TEST_FILE_NAME_RE.test(envDirItem);
|
82 |
|
83 | if (isValidTestFile) commonTestFiles.push(envDirItemPath);
|
84 | } else if (envDirItemStat.isDirectory()) {
|
85 | const parsedFlowDir = (0, _flowVersion.parseDirString)(envDirItem);
|
86 | parsedFlowDirs.push([envDirItemPath, parsedFlowDir]);
|
87 | } else {
|
88 | throw new _ValidationError.ValidationError('Unexpected directory item');
|
89 | }
|
90 | });
|
91 |
|
92 | if (!(0, _flowVersion.disjointVersionsAll)(parsedFlowDirs.map(([_, ver]) => ver))) {
|
93 | throw new _ValidationError.ValidationError(`Flow versions not disjoint on ${defName}!`);
|
94 | }
|
95 |
|
96 | if (parsedFlowDirs.length === 0) {
|
97 | throw new _ValidationError.ValidationError('No libdef files found!');
|
98 | }
|
99 |
|
100 | const envDefs = [];
|
101 | await Promise.all(parsedFlowDirs.map(async ([flowDirPath, flowVersion]) => {
|
102 | const testFilePaths = [...commonTestFiles];
|
103 | let libDefFilePath = null;
|
104 | (await _node.fs.readdir(flowDirPath)).forEach(flowDirItem => {
|
105 | const flowDirItemPath = _path.default.join(flowDirPath, flowDirItem);
|
106 |
|
107 | const flowDirItemStat = _node.fs.statSync(flowDirItemPath);
|
108 |
|
109 | if (flowDirItemStat.isFile()) {
|
110 | if (_path.default.extname(flowDirItem) === '.swp') {
|
111 | return;
|
112 | }
|
113 |
|
114 |
|
115 | if (flowDirItem === envDefFileName) {
|
116 | libDefFilePath = _path.default.join(flowDirPath, flowDirItem);
|
117 | return;
|
118 | }
|
119 |
|
120 |
|
121 | const isValidTestFile = _libDefs.TEST_FILE_NAME_RE.test(flowDirItem);
|
122 |
|
123 | if (isValidTestFile) {
|
124 | testFilePaths.push(flowDirItemPath);
|
125 | return;
|
126 | }
|
127 |
|
128 | throw new _ValidationError.ValidationError(`Unexpected file: ${envDefFileName}. This directory can only contain test files ` + `or a env def file named \`${envDefFileName}\`.`);
|
129 | } else {
|
130 | throw new _ValidationError.ValidationError(`Unexpected sub-directory. This directory can only contain test ` + `files or a env def file named \`${envDefFileName}\`.`);
|
131 | }
|
132 | });
|
133 |
|
134 | if (libDefFilePath === null) {
|
135 | libDefFilePath = _path.default.join(flowDirPath, envDefFileName);
|
136 | throw new _ValidationError.ValidationError(`No env def file found. Looking for a file named ${envDefFileName}`);
|
137 | }
|
138 |
|
139 | envDefs.push({
|
140 | name: defName,
|
141 | flowVersion,
|
142 | path: libDefFilePath,
|
143 | testFilePaths
|
144 | });
|
145 | }));
|
146 | return envDefs;
|
147 | }
|
148 |
|
149 | const findEnvDef = (defName, flowVersion, useCacheUntil, envDefs) => {
|
150 | return envDefs.filter(def => {
|
151 | let filterMatch = def.name === defName;
|
152 |
|
153 | if (!filterMatch) {
|
154 | return false;
|
155 | }
|
156 |
|
157 | switch (def.flowVersion.kind) {
|
158 | case 'all':
|
159 | return true;
|
160 |
|
161 | case 'ranged':
|
162 | case 'specific':
|
163 | return _semver.default.satisfies((0, _flowVersion.toSemverString)(flowVersion), (0, _flowVersion.toSemverString)(def.flowVersion));
|
164 |
|
165 | default:
|
166 | return true;
|
167 | }
|
168 | })[0];
|
169 | };
|
170 |
|
171 | exports.findEnvDef = findEnvDef;
|
172 |
|
173 | async function getEnvDefVersionHash(repoDirPath, libDef) {
|
174 | const latestCommitHash = await (0, _git.findLatestFileCommitHash)(repoDirPath, _path.default.relative(repoDirPath, libDef.path));
|
175 | return `${latestCommitHash.substr(0, 10)}/${libDef.name}/flow_${(0, _flowVersion.toSemverString)(libDef.flowVersion)}`;
|
176 | } |
\ | No newline at end of file |