-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (74 loc) · 3 KB
/
main.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
'use strict'
const Bounce = require('@hapi/bounce')
const Execa = require('execa')
const Fs = require('fs').promises
const Joi = require('joi')
const Path = require('path')
exports.OpError = class extends Error {
constructor (err, message) {
super(`🦐📦 shrimport — Failed to install your local package!\n${message || err.message}`)
this.name = 'OpError'
this.original = err
}
}
const internals = {}
internals.localTarballPath = (localPackagePath) => {
const { name, version } = require(`${localPackagePath}/package.json`)
return `${localPackagePath}/${name}-${version}.tgz`
}
exports.run = async (localPackage, dest) => {
let localPackagePath
let destPath
const orcwd = process.cwd() // Save original process.cwd, revert back on success
try {
Joi.assert(localPackage, Joi.string().required().empty(['', null]), {
messages: {
'any.required': 'local package path is required!',
'string.base': 'local package path must be a string'
}
})
Joi.assert(dest, Joi.string().empty(['', null]), {
messages: { 'string.base': 'destination path must be a string' }
})
localPackagePath = Path.resolve(localPackage)
destPath = dest ? Path.resolve(dest) : process.cwd()
await Promise.all([
// We confirm the relevant package files exist to guarantee that npm
// always runs relative to the user-specified package vs. traversing upwards
// to a different package.json
Fs.access(Path.join(localPackagePath, 'package.json')),
Fs.access(Path.join(destPath, 'package.json'))
])
// Due to the above access checks, the below commands _shouldn't_ fail
// due to missing files or insufficient perms, leaving only errors
// that, while I can't anticipate, probably/hopefully stem from the
// state of the user's filesystem, not some silliness in this code
process.chdir(localPackagePath)
await Execa.command('npm pack --silent')
process.chdir(destPath)
await Execa.command(`npm install ${internals.localTarballPath(localPackagePath)} --no-save`)
await Fs.unlink(internals.localTarballPath(localPackagePath))
} catch (err) {
Bounce.rethrow(err, 'system')
// Wrap anticipated errors so later detectable for cleaner display
if (err.code === 'ENOENT' && err.syscall === 'access') {
const nonPkgSuffix = 'isn\'t a package (no package.json found)'
let msg
if (err.path === Path.join(localPackagePath, 'package.json')) {
msg = `Local package at ${localPackagePath} ${nonPkgSuffix}`
}
if (err.path === Path.join(destPath, 'package.json')) {
msg = `Destination package at ${destPath} ${nonPkgSuffix}`
}
throw new exports.OpError(err, msg)
}
if (err instanceof Joi.ValidationError) {
throw new exports.OpError(err)
}
throw err // rethrow unanticipated errors
} finally {
// Likely unnecessary, I don't imagine this tool will be run in any
// pipeline or sequence with other tooling, but just in case
process.chdir(orcwd)
}
}