forked from rkalis/truffle-plugin-verify
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.js
More file actions
45 lines (37 loc) · 1.59 KB
/
util.js
File metadata and controls
45 lines (37 loc) · 1.59 KB
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
const path = require('path')
const abort = (message, logger = console, code = 1) => {
logger.error(message)
process.exit(code)
}
const enforce = (condition, message, logger, code) => {
if (!condition) abort(message, logger, code)
}
const enforceOrThrow = (condition, message) => {
if (!condition) throw new Error(message)
}
/**
* The metadata in the Truffle artifact file changes source paths on Windows. Instead of
* D:\Hello\World.sol, it looks like /D/Hello/World.sol. When trying to read this path,
* Windows cannot find it, since it is not a valid path. This function changes
* /D/Hello/World.sol to D:\Hello\World.sol. This way, Windows is able to read these source
* files. It does not change regular Unix paths, only Unixified Windows paths. It also does
* not make any changes on platforms that aren't Windows.
*
* @param {string} contractPath path to a contract file in any format.
* @returns {string} path to the contract in Windows format when on Windows, or Unix format otherwise.
*/
const normaliseContractPath = (contractPath) => {
// If the current platform is not Windows, the path does not need to be changed
if (process.platform !== 'win32') return contractPath
// If the contract path doesn't start with '/[A-Z]/' it is not a Unixified Windows path
if (!contractPath.match(/^\/[A-Z]\//i)) return contractPath
const driveLetter = contractPath.substring(1, 2)
const normalisedContractPath = path.resolve(`${driveLetter}:/${contractPath.substring(3)}`)
return normalisedContractPath
}
module.exports = {
abort,
enforce,
enforceOrThrow,
normaliseContractPath
}