Skip to content

Commit 6728a51

Browse files
author
Ives van Hoorne
committed
Change ATA to fetch from package.json first
1 parent c0f3051 commit 6728a51

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

src/app/components/sandbox/CodeEditor/monaco/workers/fetch-dependency-typings.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const transformFiles = dir =>
112112
: {};
113113

114114
const getFileMetaData = (depUrl, depPath) =>
115-
doFetch(`${depUrl}/${path.dirname(depPath)}?meta`)
115+
doFetch(`${depUrl}/${path.dirname(depPath)}/?meta`)
116116
.then(response => JSON.parse(response))
117117
.then(transformFiles);
118118

@@ -164,45 +164,56 @@ const getFileTypes = (
164164
});
165165
};
166166

167+
function fetchFromTypings(dependency, version, fetchedPaths) {
168+
const depUrl = `${ROOT_URL}${dependency}@${version}`;
169+
return doFetch(`${depUrl}/package.json`)
170+
.then(response => JSON.parse(response))
171+
.then(packageJSON => {
172+
if (packageJSON.typings) {
173+
// Add package.json, since this defines where all types lie
174+
addLib(
175+
`node_modules/${dependency}/package.json`,
176+
JSON.stringify(packageJSON),
177+
fetchedPaths
178+
);
179+
180+
// get all files in the specified directory
181+
getFileMetaData(depUrl, packageJSON.typings).then(fileData => {
182+
getFileTypes(
183+
depUrl,
184+
dependency,
185+
resolveAppropiateFile(fileData, packageJSON.typings),
186+
fetchedPaths,
187+
fileData
188+
);
189+
});
190+
} else {
191+
throw new Error('No typings field in package.json');
192+
}
193+
});
194+
}
195+
167196
function fetchAndAddDependencies(dependencies) {
168197
const fetchedPaths = [];
169198
Object.keys(dependencies).forEach(dep => {
170199
try {
171200
if (loadedTypings.indexOf(dep) === -1) {
172-
loadedTypings.push(dep);
173-
174-
fetchFromDefinitelyTyped(
201+
fetchFromTypings(
175202
dep,
176-
dependencies[dep],
203+
getVersion(dependencies[dep]),
177204
fetchedPaths
178205
).catch(() => {
179-
// Not available as @types, try checking in package.json
180-
181-
const depUrl = `${ROOT_URL}${dep}@${getVersion(dependencies[dep])}`;
182-
doFetch(`${depUrl}/package.json`)
183-
.then(response => JSON.parse(response))
184-
.then(packageJSON => {
185-
if (packageJSON.typings) {
186-
// Add package.json, since this defines where all types lie
187-
addLib(
188-
`node_modules/${dep}/package.json`,
189-
JSON.stringify(packageJSON),
190-
fetchedPaths
191-
);
192-
193-
// get all files in the specified directory
194-
getFileMetaData(depUrl, packageJSON.typings).then(fileData => {
195-
getFileTypes(
196-
depUrl,
197-
dep,
198-
resolveAppropiateFile(fileData, packageJSON.typings),
199-
fetchedPaths,
200-
fileData
201-
);
202-
});
203-
}
204-
});
206+
// Not available in package.json, try checking in @types/
207+
fetchFromDefinitelyTyped(
208+
dep,
209+
dependencies[dep],
210+
fetchedPaths
211+
).catch(() => {
212+
// Do nothing if it still can't be fetched
213+
});
205214
});
215+
216+
loadedTypings.push(dep);
206217
}
207218
} catch (e) {
208219
console.log(`Couldn't find typings for ${dep}`);

0 commit comments

Comments
 (0)