Skip to content

Commit

Permalink
fix: workaround for npm 8; see #26
Browse files Browse the repository at this point in the history
  • Loading branch information
relu91 committed Feb 22, 2022
1 parent f4cbffe commit 38912e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/commands/init.ts
Expand Up @@ -35,7 +35,14 @@ export async function init(directory: any): Promise<void> {

console.log();
console.log(`${chalk.bold('Thanks!')}; installing packages using ${chalk.bold('npm')}:`);
await install(selectedDirectory);

try {
await install(selectedDirectory);
} catch (error) {
console.log(`${chalk.bold('💥')} npm couldn't install your packages; are you using ${chalk.bold('npm')} 8?`);
console.log(`See https://github.com/UniBO-PRISMLab/wam/issues/26`);
console.log(`As a workaround, you can run ${chalk.bold('npm install')} manually.`);
}

console.log();
console.log();
Expand Down
15 changes: 8 additions & 7 deletions src/utils/npm-install.ts
@@ -1,18 +1,19 @@
import npm from 'global-npm';

export default async function install(projectDir: string): Promise<void> {
// Sadly npm 8 throws an error on import and we need to catch it
// here.
const npm = await import('npm');
// @ts-ignore
const version = npm.version;
if (version.startsWith('6.')) {
return installV6(projectDir);
return installV6(projectDir, npm);
} else if (version.startsWith('7.')) {
return installV7(projectDir);
return installV7(projectDir, npm);
} else {
throw new Error(`Unsupported npm version: ${version}`);
}
}

function installV6(projectDir: string): Promise<void> {
function installV6(projectDir: string, npm: any): Promise<void> {
return new Promise((resolve, reject) => {
// @ts-ignore
npm.load({}, () => {
Expand All @@ -29,11 +30,11 @@ function installV6(projectDir: string): Promise<void> {
});
}

function installV7(projectDir: string): Promise<void> {
function installV7(projectDir: string, npm: any): Promise<void> {
return new Promise((resolve, reject) => {
npm.load(() => {
npm.prefix = projectDir;
npm.commands.install([], error => {
npm.commands.install([], (error: Error) => {
if (error) {
reject(error);
} else {
Expand Down

0 comments on commit 38912e1

Please sign in to comment.