|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +/** |
| 4 | + * A script to automatically update the Visual Studio links |
| 5 | + * based on the latest *stable* version of TypeScript shipped |
| 6 | + * to npm. |
| 7 | + */ |
| 8 | + |
| 9 | + |
| 10 | +const axios = require('axios').default; |
| 11 | +const {writeFileSync} = require("fs") |
| 12 | + |
| 13 | +/** |
| 14 | + * Gets the NPM package JSON for a module |
| 15 | + * @param {string} name |
| 16 | + */ |
| 17 | +const getLatestNPMPackage = async (name) => { |
| 18 | + const packageJSON = await axios({ |
| 19 | + url: `https://registry.npmjs.org/${name}` |
| 20 | + }) |
| 21 | + |
| 22 | + return packageJSON.data |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Queries the VS markplace for typescript extensions, returns |
| 27 | + * only official extensions |
| 28 | + */ |
| 29 | +const getLatestVSExtensions = async () => { |
| 30 | + var headers = { |
| 31 | + 'Content-Type': 'application/json', |
| 32 | + 'Accept': 'application/json;api-version=5.2-preview.1;excludeUrls=true', |
| 33 | + 'Host': 'marketplace.visualstudio.com', |
| 34 | + 'Origin': 'https://marketplace.visualstudio.com', |
| 35 | + 'Referer': 'https://marketplace.visualstudio.com/search?term=typescript&target=VS&category=All%20categories&vsVersion=vs2019&sortBy=Relevance', |
| 36 | + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15', |
| 37 | + 'Content-Length': '1082', |
| 38 | + 'Connection': 'keep-alive', |
| 39 | + 'X-TFS-Session': 'e16c1b5b-850f-42ee-ab7c-519c79f6e356', |
| 40 | + 'X-Requested-With': 'XMLHttpRequest', |
| 41 | + 'X-VSS-ReauthenticationAction': 'Suppress', |
| 42 | + }; |
| 43 | + |
| 44 | + const query = (name) => |
| 45 | + `{"assetTypes":["Microsoft.VisualStudio.Services.Icons.Default","Microsoft.VisualStudio.Services.Icons.Branding","Microsoft.VisualStudio.Services.Icons.Small"],"filters":[{"criteria":[{"filterType":8,"value":"Microsoft.VisualStudio.Ide"},{"filterType":10,"value":"${name}"},{"filterType":12,"value":"37888"}],"direction":2,"pageSize":54,"pageNumber":1,"sortBy":0,"sortOrder":0,"pagingToken":null}],"flags":870}` |
| 46 | + |
| 47 | + |
| 48 | + const extensionSearchResults = await axios({ |
| 49 | + url: 'https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery', |
| 50 | + method: 'POST', |
| 51 | + headers: headers, |
| 52 | + data: query("typescript") |
| 53 | + }) |
| 54 | + |
| 55 | + if (!extensionSearchResults.data || !extensionSearchResults.data.results) { |
| 56 | + throw new Error("Got a bad response from VS marketplace") |
| 57 | + } |
| 58 | + |
| 59 | + const extensions = extensionSearchResults.data.results[0].extensions |
| 60 | + const officialExtensions = extensions.filter(e => e.publisher.publisherId === "4f0355d2-4a53-4ab1-a8ea-507f4a333a6f") |
| 61 | + return officialExtensions |
| 62 | +} |
| 63 | + |
| 64 | +const go = async () => { |
| 65 | + // curl https://registry.npmjs.org/typescript | jq |
| 66 | + console.log("Grabbing the TypeScript package from NPM, this takes about 10-15s") |
| 67 | + const tsPackage = await getLatestNPMPackage("typescript") |
| 68 | + const latest = tsPackage["dist-tags"].latest |
| 69 | + |
| 70 | + console.log(`Grabbing the VS TypeScript extension for ${latest} from the marketplace`) |
| 71 | + const extensions = await getLatestVSExtensions() |
| 72 | + const currentExtension = extensions.find(e => e.versions[0].version === latest) |
| 73 | + |
| 74 | + // @ts-ignore |
| 75 | + const currentURLs = require("../src/_data/urls") |
| 76 | + |
| 77 | + if (currentExtension) { |
| 78 | + const extensionURL = `https://marketplace.visualstudio.com/items?itemName=TypeScriptTeam.${currentExtension.extensionName}` |
| 79 | + currentURLs.vs2017_download = extensionURL |
| 80 | + currentURLs.vs2019_download = extensionURL |
| 81 | + } |
| 82 | + |
| 83 | + writeFileSync("src/_data/urls.json", JSON.stringify(currentURLs, null, " ")) |
| 84 | + console.log(`Updated src/_data/urls.json`) |
| 85 | +} |
| 86 | + |
| 87 | +go() |
0 commit comments