-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (133 loc) · 3.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
const path = require('path');
const fs = require('fs-extra');
const { execSync } = require('child_process');
const readline = require('readline');
const commandMappings = {
install: {
yarn: 'add',
npm: 'install',
pnpm: 'add',
},
add: {
yarn: 'add',
npm: 'install',
pnpm: 'add',
},
remove: {
yarn: 'remove',
npm: 'uninstall',
pnpm: 'remove',
},
start: {
yarn: 'start',
npm: 'start',
pnpm: 'start',
},
build: {
yarn: 'build',
npm: 'run build',
pnpm: 'build',
},
test: {
yarn: 'test',
npm: 'test',
pnpm: 'test',
},
init: {
yarn: 'init',
npm: 'init',
pnpm: 'init',
},
dev: {
yarn: 'dev',
npm: 'run dev',
pnpm: 'dev',
},
publish: {
yarn: 'publish',
npm: 'publish',
pnpm: 'publish',
},
};
function detectPackageManager() {
const currentDirectory = process.cwd();
if (fs.existsSync(path.join(currentDirectory, 'yarn.lock'))) {
return 'yarn';
}
if (fs.existsSync(path.join(currentDirectory, 'package-lock.json'))) {
return 'npm';
}
return 'npm';
}
function runCommand(command) {
try {
const output = execSync(command, { stdio: 'inherit' });
if (output) {
console.log(output.toString().trim());
}
} catch (error) {
console.error(`Command "${command}" failed with error: ${error.message}`);
process.exit(1);
}
}
function installPackages(packages, isDev, packageManager) {
if (packageManager === 'yarn') {
runCommand(`yarn add${isDev ? ' --dev' : ''} ${packages}`);
} else if (packageManager === 'npm') {
runCommand(`npm install${isDev ? ' --save-dev' : ''} ${packages}`);
}
}
function promptPackageManager(callback) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
'No lockfile found. Choose a package manager (yarn/npm) [npm]: ',
(answer) => {
rl.close();
const packageManager = answer.trim().toLowerCase() || 'npm';
callback(packageManager);
},
);
}
function suggestCommand(command) {
console.log(`Did you mean "${command}"?`);
}
function remapCommand(command, packageManager) {
const mappedCommand = commandMappings[command]?.[packageManager];
return mappedCommand || command;
}
function pompa(command) {
const packageManager = detectPackageManager();
if (packageManager === 'yarn' && command === 'install') {
console.error('Error: "pompa install" command is not supported with Yarn.');
process.exit(1);
}
if (command.startsWith('install ')) {
const commandParts = command.split(' ');
const isDev = commandParts[1] === '-D' || commandParts[1] === '--save-dev';
const packages = commandParts.slice(2).join(' ');
if (packageManager === null) {
promptPackageManager((answer) => {
installPackages(packages, isDev, answer);
});
} else {
installPackages(packages, isDev, packageManager);
}
} else if (packageManager === 'yarn') {
const remappedCommand = remapCommand(command, packageManager);
runCommand(`yarn ${remappedCommand}`);
} else if (packageManager === 'npm') {
const remappedCommand = remapCommand(command, packageManager);
runCommand(`npm ${remappedCommand}`);
} else {
suggestCommand(command);
process.exit(1);
}
}
const [, , ...args] = process.argv;
const command = args.join(' ');
pompa(command);
module.exports = pompa;