This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathcli.js
executable file
·130 lines (118 loc) · 4.1 KB
/
cli.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
#!/usr/bin/env node
require("source-map-support/register");
const semver = require("semver"); // to validate Node version
const TruffleError = require("@truffle/error");
const analytics = require("./lib/services/analytics");
const version = require("./lib/version");
const versionInfo = version.info();
const XRegExp = require("xregexp");
// we need to make sure this function exists so ensjs doesn't complain as it requires
// getRandomValues for some functionalities - webpack strips out the crypto lib
// so we shim it here
global.crypto = {
getRandomValues: require("get-random-values")
};
// pre-flight check: Node version compatibility
const minimumNodeVersion = "16.0.0";
if (!semver.gte(process.version, minimumNodeVersion)) {
console.log(
"Error: Node version not supported. You are currently using version " +
process.version.slice(1) +
" of Node. Truffle requires Node v" +
minimumNodeVersion +
" or higher."
);
analytics.send({
exception: "wrong node version",
version: versionInfo.bundle || "(unbundled) " + versionInfo.core
});
process.exit(1);
}
// This should be removed when issue is resolved upstream:
// https://github.com/ethereum/web3.js/issues/1648
const listeners = process.listeners("warning");
listeners.forEach(listener => process.removeListener("warning", listener));
const { handleHelpInput } = require("./lib/cliHelp");
const {
getCommand,
prepareOptions,
runCommand,
displayGeneralHelp
} = require("./lib/command-utils");
const inputArguments = process.argv.slice(2);
// handle cases where input indicates the user wants to access Truffle's help
const { displayHelp, inputStrings } = handleHelpInput({ inputArguments });
if (displayHelp) {
displayGeneralHelp();
process.exit();
}
const command = getCommand({
inputStrings,
options: {},
noAliases: false
});
//getCommand() will return null if a command not recognized by truffle is used.
if (command === null) {
console.log(
`\`truffle ${inputStrings.join(
" "
)}\` is not a valid truffle command. Please see \`truffle help\` for available commands.`
);
process.exit(1);
}
const options = prepareOptions({
command,
inputStrings,
options: {}
});
runCommand(command, options)
.then(returnStatus => {
process.exitCode = returnStatus;
return require("@truffle/promise-tracker").waitForOutstandingPromises();
})
.then(() => {
process.exit();
})
.catch(error => {
if (error instanceof TruffleError) {
analytics.send({
exception: "TruffleError - missing configuration file",
version: versionInfo.bundle
? versionInfo.bundle
: "(unbundled) " + versionInfo.core
});
console.log(error.message);
version.logTruffleAndNode(options.logger);
} else if (typeof error === "number") {
analytics.send({
exception: "Numbered Error - " + error,
version: versionInfo.bundle
? versionInfo.bundle
: "(unbundled) " + versionInfo.core
});
// If a number is returned, exit with that number.
process.exit(error);
} else {
let errorData = error.stack || error.message || error.toString();
//remove identifying information if error stack is passed to analytics
if (errorData === error.stack) {
const directory = __dirname;
//making sure users' identifying information does not get sent to
//analytics by cutting off everything before truffle. Will not properly catch the user's info
//here if the user has truffle in their name.
let identifyingInfo = String.raw`${directory.split("truffle")[0]}`;
let removedInfo = new XRegExp(XRegExp.escape(identifyingInfo), "g");
errorData = errorData.replace(removedInfo, "");
}
analytics.send({
exception: "Other Error - " + errorData,
version: versionInfo.bundle
? versionInfo.bundle
: "(unbundled) " + versionInfo.core
});
// Bubble up all other unexpected errors.
console.log(error.stack || error.message || error.toString());
version.logTruffleAndNode(options.logger);
}
process.exit(1);
});