2018-07-03 16:43:08 +02:00
|
|
|
const fs = require('fs');
|
2018-06-21 15:07:50 +02:00
|
|
|
const shell = require('shelljs');
|
2018-06-22 15:07:12 +02:00
|
|
|
const { includes } = require('lodash');
|
2018-06-21 15:07:50 +02:00
|
|
|
|
2018-06-22 16:46:48 +02:00
|
|
|
// let isStrapiInstalledWithNPM = true;
|
|
|
|
// let skipCheck = false;
|
2018-06-21 15:07:50 +02:00
|
|
|
|
|
|
|
const watcher = (cmd) => {
|
|
|
|
const data = shell.exec(cmd, {
|
|
|
|
silent: true,
|
|
|
|
});
|
|
|
|
|
2018-06-22 15:07:12 +02:00
|
|
|
if (includes(data.stderr, 'command not found') && data.code !== 0) {
|
2018-06-21 15:07:50 +02:00
|
|
|
throw new Error('Command not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.stdout.toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2018-06-22 16:46:48 +02:00
|
|
|
isStrapiInstalledWithNPM: () => {
|
|
|
|
let isNPM = true;
|
|
|
|
let skipCheck = false;
|
|
|
|
|
|
|
|
// Check if we are in development mode (working with the monorepo)
|
|
|
|
// So we don't run `npm -g ls` which takes time
|
|
|
|
if (process.argv.indexOf('new') !== -1 && process.argv.indexOf('--dev') !== -1) {
|
|
|
|
skipCheck = true;
|
|
|
|
}
|
2018-07-03 16:43:08 +02:00
|
|
|
|
2018-06-22 16:46:48 +02:00
|
|
|
if (!skipCheck) {
|
|
|
|
try {
|
|
|
|
// Retrieve all the packages installed with NPM
|
2018-07-03 16:43:08 +02:00
|
|
|
const npmPath = watcher('npm root -g');
|
|
|
|
|
|
|
|
const data = fs.readdirSync(npmPath.trim());
|
|
|
|
|
2018-06-22 16:46:48 +02:00
|
|
|
// Check if strapi is installed with NPM
|
|
|
|
isNPM = includes(data, 'strapi');
|
2018-07-02 14:59:50 +02:00
|
|
|
|
2018-07-10 18:02:37 +02:00
|
|
|
if (isNPM) {
|
|
|
|
return isNPM;
|
|
|
|
}
|
|
|
|
|
2018-06-22 16:46:48 +02:00
|
|
|
try {
|
2018-07-11 22:44:04 -05:00
|
|
|
const yarnData = watcher('yarn global list');
|
|
|
|
isNPM = !includes(yarnData, 'strapi');
|
2018-06-22 16:46:48 +02:00
|
|
|
} catch(err) {
|
|
|
|
isNPM = true;
|
|
|
|
}
|
|
|
|
} catch(err) {
|
|
|
|
// If NPM is not installed strapi is installed with Yarn
|
|
|
|
isNPM = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isNPM;
|
|
|
|
},
|
2018-06-21 15:07:50 +02:00
|
|
|
|
2018-07-02 14:59:50 +02:00
|
|
|
commands: function (cmdType, path = '') {
|
|
|
|
const isNPM = this.isStrapiInstalledWithNPM();
|
2018-07-03 16:43:08 +02:00
|
|
|
|
2018-06-21 15:07:50 +02:00
|
|
|
switch(cmdType) {
|
|
|
|
case 'install --prefix':
|
2018-06-22 16:46:48 +02:00
|
|
|
return isNPM ? `npm install --prefix ${path}` : `yarn --cwd ${path} add`;
|
2018-06-21 15:07:50 +02:00
|
|
|
case 'root -g':
|
2018-06-22 16:46:48 +02:00
|
|
|
return isNPM ? 'npm root -g' : 'yarn global dir';
|
2018-06-21 15:07:50 +02:00
|
|
|
case 'install global':
|
2018-06-22 16:46:48 +02:00
|
|
|
return isNPM ? 'npm install' : 'yarn install';
|
2018-06-21 15:07:50 +02:00
|
|
|
case 'install package':
|
2018-06-22 16:46:48 +02:00
|
|
|
return isNPM ? 'npm install' : 'yarn add';
|
2018-06-21 15:07:50 +02:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 16:43:08 +02:00
|
|
|
};
|