Skip to content
Prev Previous commit
Next Next commit
wip: implementing args exploitation
  • Loading branch information
Florian Adonis committed Jan 10, 2019
commit 7360d64bbac16329a452d1a694f6f18b7d57ec2d
100 changes: 34 additions & 66 deletions lib/mp.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@


function mp(args) {
module.exports = mp

function mp(args, cb) {
const filePath = file ? `${process.cwd()}/${file}` : null
const isDir = fs.lstatSync(filePath).isDirectory()
const packagesInstalled = getPackagesInstalled()
const packagesToInstall = check(filePath)

if (program.install && program.check) {
// eslint-disable-next-line
console.log('\\033[0;31mStop fooling around please !\\033[0m')
// eslint-disable-next-line
console.log('Read the doc ! ---> mp -h')
} else if (program.install && isDir) {
const packagesToInstall = checkDirectory(filePath)
const packagesInstalled = getPackagesInstalled()

installPackages(packagesToInstall, packagesInstalled)
} else if (program.install) {
const packagesToInstall = checkFile(filePath)
const packagesInstalled = getPackagesInstalled()

installPackages(packagesToInstall, packagesInstalled)
} else if (isDir) {
const packagesToInstall = checkDirectory(filePath)
const packagesInstalled = getPackagesInstalled()

displayPackages(packagesToInstall, packagesInstalled)
} else {
const packagesToInstall = checkFile(filePath)
const packagesInstalled = getPackagesInstalled()

displayPackages(packagesToInstall, packagesInstalled)
}
}
const action = args.shift()

module.exports = mp
if (action === 'i' || action === "install") {
installPackages(packagesToInstall, packagesInstalled)
} else if (action === 'c' || action === "check") {
displayPackages(packagesToInstall, packagesInstalled)
} else {
// unknown action // test if file
}
}

////////////// INSTALL FROM QUESTIONS ///////////////////

Expand Down Expand Up @@ -83,10 +67,18 @@ const displayPackages = (packagesToShow, installed) => {
/////////////////// CHECK FILE ///////////////////

// Checks a file before deciding weither to install or display
const checkFile = (filePath) => {
const checkFile = (filePath, installedPackages = []) => {
const file = fs.readFileSync(filePath, 'utf8')
const packages = extractPackagesToInstall(file)

if (packages) {
packages.forEach((pack) => {
if (!installedPackages.includes(pack)) {
installedPackages.push(pack)
}
})
}

return packages
}

Expand All @@ -96,33 +88,28 @@ const checkDirectoryRecusrsive = (path, packages) => {
fs.readdirSync(path).forEach((file) => {
const filePath = `${path}/${file}`

if (file.match(/\.js/gu) && !file.match(/\.json/gu)) {
let newPackages = checkFile(filePath)

if (newPackages) {
newPackages.forEach((pack) => {
if (!packages.includes(pack)) {
packages.push(pack)
}
})
}
} else if (fs.lstatSync(filePath).isDirectory()
if (fs.lstatSync(filePath).isDirectory()
&& file !== 'node_modules') {
return checkDirectoryRecusrsive(`${path}/${file}`, packages)
} else {
if (file.match(/\.js/gu) && !file.match(/\.json/gu)) {
packages = checkFile(filePath, packages)
return packages
}
}

return []
})

return packages
}

// Checks all files in a directory
const checkDirectory = (path) => {
const check = (path, isDir = false) => {
let packages = []

// Passing an empty array to initiate recursivity
const packagesFound = checkDirectoryRecusrsive(path, [])
packages = isDir ? checkDirectoryRecusrsive(path, packages) : checkFile(path, packages)

return packagesFound
return packages
}

/////////////// GET PACKAGE JSON ///////////////
Expand Down Expand Up @@ -150,7 +137,7 @@ const getPackageJson = () => {
return packageJson
}

export const getPackagesInstalled = () => {
const getPackagesInstalled = () => {
const packageJson = getPackageJson()

if (!packageJson) {
Expand Down Expand Up @@ -178,23 +165,4 @@ const extractPackagesToInstall = (fileContent) => {
const mappedPackages = packages.map((pack) => pack.substr(9, pack.length - 11))

return mappedPackages
}

////////////// INSTALL PACKAGES ////////////////////

// Prompts the user before installing missing packages
const installPackages = (packages, installed) => {
const toInstallQuestions = packages
.filter((pack) => !installed.includes(pack))
// eslint-disable-next-line
.map((pack) => {
return {
default: false,
message: `Install package \x1b[32m${pack}\x1b[0m ?`,
name: pack,
type: 'confirm',
}
})

installFromQuestions(toInstallQuestions)
}