UNPKG

5.43 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports._cacheRepoEnsureToken = exports._REMOTE_REPO_URL = exports._CACHE_REPO_EXPIRY = exports.CACHE_REPO_EXPIRY = void 0;
7exports._clearCustomCacheDir = clearCustomCacheDir;
8exports._getCacheRepoGitDir = getCacheRepoGitDir;
9exports._getLastUpdatedFile = getLastUpdatedFile;
10exports._setCustomCacheDir = setCustomCacheDir;
11exports.ensureCacheRepo = ensureCacheRepo;
12exports.getCacheRepoDir = getCacheRepoDir;
13exports.verifyCLIVersion = verifyCLIVersion;
14
15var _fileUtils = require("./fileUtils");
16
17var _git = require("./git");
18
19var _node = require("./node");
20
21var _semver = _interopRequireDefault(require("semver"));
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25const CACHE_REPO_EXPIRY = 1000 * 60; // 1 minute
26
27exports._CACHE_REPO_EXPIRY = exports.CACHE_REPO_EXPIRY = CACHE_REPO_EXPIRY;
28const REMOTE_REPO_URL = 'https://github.com/flowtype/flow-typed.git';
29exports._REMOTE_REPO_URL = REMOTE_REPO_URL;
30
31async 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
44let customCacheDir = null;
45
46function getCacheDir() {
47 return customCacheDir === null ? _node.path.join(_node.os.homedir(), '.flow-typed') : customCacheDir;
48}
49
50function clearCustomCacheDir() {
51 customCacheDir = null;
52}
53
54function setCustomCacheDir(dir) {
55 customCacheDir = dir;
56}
57
58function getCacheRepoGitDir() {
59 return _node.path.join(getCacheRepoDir(), '.git');
60}
61
62function getLastUpdatedFile() {
63 return _node.path.join(getCacheRepoDir(), 'lastUpdated');
64}
65
66async 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 * Ensure that the CACHE_REPO_DIR exists and is recently rebased.
84 * (else: create/rebase it)
85 */
86
87
88const cacheRepoEnsureToken = {
89 lastEnsured: 0,
90 pendingEnsurance: Promise.resolve()
91};
92exports._cacheRepoEnsureToken = cacheRepoEnsureToken;
93
94async function ensureCacheRepo(cacheRepoExpiry = CACHE_REPO_EXPIRY) {
95 // Only re-run rebase checks if a check hasn't been run in the last 5 minutes
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 // If the LAST_UPDATED_FILE has anything other than just a number in
115 // it, just assume we need to update.
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
136function getCacheRepoDir() {
137 return _node.path.join(getCacheDir(), 'repo');
138}
139
140async 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