Skip to content

Commit dc2ee67

Browse files
author
Neriya Cohen
committed
Fix installing types with namespaces and versions
1 parent 46d27b0 commit dc2ee67

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/index.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,33 @@ const isProduction = (terminal.args.prod || terminal.args.production) && true;
3232

3333
const packageSyntax = `${packageManager} ${packageManager === 'npm' ? 'install' : 'add'} {package} ${terminal.commands.map(arg => arg + ' ')}`;
3434

35-
function httpsGet(options: string | https.RequestOptions | URL) {
36-
return new Promise<IncomingMessage>((resolve, reject) => {
37-
https.get(options, (res) => {
38-
resolve(res);
39-
}).on('error', (err) => {
40-
reject(err);
41-
}).end();
42-
});
35+
function httpsRequest(options: https.RequestOptions | string | URL): Promise<IncomingMessage>;
36+
function httpsRequest(url: string | URL, options: https.RequestOptions): Promise<IncomingMessage>;
37+
function httpsRequest(url: https.RequestOptions | string | URL, options?: https.RequestOptions) {
38+
if (typeof url === 'string') {
39+
return new Promise<IncomingMessage>((resolve, reject) => {
40+
https.request(url, options, (res) => {
41+
resolve(res);
42+
}).on('error', (err) => {
43+
reject(err);
44+
}).end();
45+
});
46+
} else {
47+
return new Promise<IncomingMessage>((resolve, reject) => {
48+
https.request(url, (res) => {
49+
resolve(res);
50+
}).on('error', (err) => {
51+
reject(err);
52+
}).end();
53+
});
54+
}
55+
56+
}
57+
58+
async function packageExists(name: string, version?: string) {
59+
version = version && version.match(/([\d+.?]+)/g)?.[0];
60+
const url = `https://registry.${packageManager === 'yarn' ? 'yarnpkg.com' : 'npmjs.org'}/${name}${version ? `/${version}` : ''}`;
61+
return (await httpsRequest(url, { method: 'HEAD'})).statusCode === 200;
4362
}
4463

4564
function readPackageJson() {
@@ -110,7 +129,7 @@ function installPackage(packageName: string, version?: string, dev = false) {
110129
});
111130
}
112131

113-
async function installTypes(packageName: string) {
132+
async function installTypes(packageName: string, version?: string) {
114133
if(packageName.startsWith('@types/')) {
115134
return;
116135
}
@@ -119,10 +138,11 @@ async function installTypes(packageName: string) {
119138
packageName = packageName.substring(1).split("/").join("__");
120139
}
121140

122-
if ((await httpsGet(`https://registry.npmjs.org/@types/${packageName}`)).statusCode === 200) {
123-
return installPackage(`@types/${packageName}`, '', true);
141+
const typesName = `@types/${packageName}`;
142+
if ((await packageExists(typesName, version))) {
143+
return installPackage(typesName, version, true);
124144
} else {
125-
console.log(`Types package for ${packageName} does not exist, skipping`.style(colors.FgYellow));
145+
console.log(`The types package for ${packageName}${version ? `@${version}` : ''} does not exist, skipping`.style(colors.FgYellow));
126146
}
127147
}
128148

@@ -150,7 +170,7 @@ async function install() {
150170

151171
for (const dependency of dependencies) {
152172
const [packageName, version] = Object.entries(dependency)[0];
153-
await installPackage(packageName, version) && await installTypes(packageName);
173+
await installPackage(packageName, version) && await installTypes(packageName, version);
154174
}
155175
}
156176

0 commit comments

Comments
 (0)