1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports._cacheRepoEnsureToken = exports._REMOTE_REPO_URL = exports._CACHE_REPO_EXPIRY = exports.CACHE_REPO_EXPIRY = void 0;
|
7 | exports._clearCustomCacheDir = clearCustomCacheDir;
|
8 | exports._getCacheRepoGitDir = getCacheRepoGitDir;
|
9 | exports._getLastUpdatedFile = getLastUpdatedFile;
|
10 | exports._setCustomCacheDir = setCustomCacheDir;
|
11 | exports.ensureCacheRepo = ensureCacheRepo;
|
12 | exports.getCacheRepoDir = getCacheRepoDir;
|
13 | exports.verifyCLIVersion = verifyCLIVersion;
|
14 |
|
15 | var _fileUtils = require("./fileUtils");
|
16 |
|
17 | var _git = require("./git");
|
18 |
|
19 | var _node = require("./node");
|
20 |
|
21 | var _semver = _interopRequireDefault(require("semver"));
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | const CACHE_REPO_EXPIRY = 1000 * 60;
|
26 |
|
27 | exports._CACHE_REPO_EXPIRY = exports.CACHE_REPO_EXPIRY = CACHE_REPO_EXPIRY;
|
28 | const REMOTE_REPO_URL = 'https://github.com/flowtype/flow-typed.git';
|
29 | exports._REMOTE_REPO_URL = REMOTE_REPO_URL;
|
30 |
|
31 | async function cloneCacheRepo() {
|
32 | await (0, _fileUtils.mkdirp)(getCacheRepoDir());
|
33 |
|
34 | try {
|
35 | await (0, _git.cloneInto)(REMOTE_REPO_URL, getCacheRepoDir());
|
36 | } catch (e) {
|
37 | console.error('ERROR: Unable to clone local cache repo!');
|
38 | throw e;
|
39 | }
|
40 |
|
41 | await _node.fs.writeFile(getLastUpdatedFile(), String(Date.now()));
|
42 | }
|
43 |
|
44 | let customCacheDir = null;
|
45 |
|
46 | function getCacheDir() {
|
47 | return customCacheDir === null ? _node.path.join(_node.os.homedir(), '.flow-typed') : customCacheDir;
|
48 | }
|
49 |
|
50 | function clearCustomCacheDir() {
|
51 | customCacheDir = null;
|
52 | }
|
53 |
|
54 | function setCustomCacheDir(dir) {
|
55 | customCacheDir = dir;
|
56 | }
|
57 |
|
58 | function getCacheRepoGitDir() {
|
59 | return _node.path.join(getCacheRepoDir(), '.git');
|
60 | }
|
61 |
|
62 | function getLastUpdatedFile() {
|
63 | return _node.path.join(getCacheRepoDir(), 'lastUpdated');
|
64 | }
|
65 |
|
66 | async function rebaseCacheRepo() {
|
67 | if ((await _node.fs.exists(getCacheRepoDir())) && (await _node.fs.exists(getCacheRepoGitDir()))) {
|
68 | try {
|
69 | await (0, _git.rebaseRepoMainline)(getCacheRepoDir());
|
70 | } catch (e) {
|
71 | console.error('ERROR: Unable to rebase the local cache repo. ' + e.message);
|
72 | return false;
|
73 | }
|
74 |
|
75 | await _node.fs.writeFile(getLastUpdatedFile(), String(Date.now()));
|
76 | return true;
|
77 | } else {
|
78 | await cloneCacheRepo();
|
79 | return true;
|
80 | }
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | const cacheRepoEnsureToken = {
|
89 | lastEnsured: 0,
|
90 | pendingEnsurance: Promise.resolve()
|
91 | };
|
92 | exports._cacheRepoEnsureToken = cacheRepoEnsureToken;
|
93 |
|
94 | async function ensureCacheRepo(cacheRepoExpiry = CACHE_REPO_EXPIRY) {
|
95 |
|
96 | if (cacheRepoEnsureToken.lastEnsured + 5 * 1000 * 60 >= Date.now()) {
|
97 | return cacheRepoEnsureToken.pendingEnsurance;
|
98 | }
|
99 |
|
100 | cacheRepoEnsureToken.lastEnsured = Date.now();
|
101 | const prevEnsurance = cacheRepoEnsureToken.pendingEnsurance;
|
102 | return cacheRepoEnsureToken.pendingEnsurance = prevEnsurance.then(() => async function () {
|
103 | const repoDirExists = _node.fs.exists(getCacheRepoDir());
|
104 |
|
105 | const repoGitDirExists = _node.fs.exists(getCacheRepoGitDir());
|
106 |
|
107 | if (!(await repoDirExists) || !(await repoGitDirExists)) {
|
108 | console.log(`• flow-typed cache not found, fetching from GitHub...`);
|
109 | await cloneCacheRepo();
|
110 | } else {
|
111 | let lastUpdated = 0;
|
112 |
|
113 | if (await _node.fs.exists(getLastUpdatedFile())) {
|
114 |
|
115 |
|
116 | const lastUpdatedRaw = await _node.fs.readFile(getLastUpdatedFile());
|
117 | const lastUpdatedNum = parseInt(lastUpdatedRaw, 10);
|
118 |
|
119 | if (String(lastUpdatedNum) === String(lastUpdatedRaw)) {
|
120 | lastUpdated = lastUpdatedNum;
|
121 | }
|
122 | }
|
123 |
|
124 | if (lastUpdated + cacheRepoExpiry < Date.now()) {
|
125 | console.log('• rebasing flow-typed cache...');
|
126 | const rebaseSuccessful = await rebaseCacheRepo();
|
127 |
|
128 | if (!rebaseSuccessful) {
|
129 | console.log("\nNOTE: Unable to rebase local cache! If you don't currently " + "have internet connectivity, no worries -- we'll update the " + 'local cache the next time you do.\n');
|
130 | }
|
131 | }
|
132 | }
|
133 | }());
|
134 | }
|
135 |
|
136 | function getCacheRepoDir() {
|
137 | return _node.path.join(getCacheDir(), 'repo');
|
138 | }
|
139 |
|
140 | async function verifyCLIVersion() {
|
141 | var _semver$coerce;
|
142 |
|
143 | const metadataPath = _node.path.join(getCacheRepoDir(), 'definitions', '.cli-metadata.json');
|
144 |
|
145 | const metadata = await _node.fs.readJson(metadataPath);
|
146 | const compatibleCLIRange = metadata.compatibleCLIRange;
|
147 |
|
148 | if (!compatibleCLIRange) {
|
149 | throw new Error(`Unable to find the 'compatibleCLIRange' property in ${metadataPath}. ` + `You might need to update your flow-typed CLI to the latest version.`);
|
150 | }
|
151 |
|
152 | const thisCLIPkgJsonPath = _node.path.join(__dirname, '..', '..', 'package.json');
|
153 |
|
154 | const thisCLIPkgJson = await _node.fs.readJson(thisCLIPkgJsonPath);
|
155 | const thisCLIVersion = thisCLIPkgJson.version;
|
156 |
|
157 | if (!_semver.default.satisfies((_semver$coerce = _semver.default.coerce(thisCLIVersion)) !== null && _semver$coerce !== void 0 ? _semver$coerce : thisCLIVersion, compatibleCLIRange)) {
|
158 | throw new Error(`Please upgrade your flow-typed CLI! This CLI is version ` + `${thisCLIVersion}, but the latest flow-typed definitions are only ` + `compatible with flow-typed@${compatibleCLIRange}`);
|
159 | }
|
160 | } |
\ | No newline at end of file |