@gorsee/code 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/npm/postinstall.js +49 -0
- package/package.json +1 -1
package/npm/postinstall.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const https = require("https");
|
|
6
|
+
const os = require("os");
|
|
6
7
|
const path = require("path");
|
|
7
8
|
|
|
8
9
|
const version = require("../package.json").version;
|
|
@@ -28,6 +29,7 @@ async function install() {
|
|
|
28
29
|
await download(url, output);
|
|
29
30
|
if (process.platform !== "win32") {
|
|
30
31
|
fs.chmodSync(output, 0o755);
|
|
32
|
+
ensureCommandInPath();
|
|
31
33
|
}
|
|
32
34
|
stop("OK");
|
|
33
35
|
}
|
|
@@ -82,6 +84,53 @@ function isRedirect(statusCode) {
|
|
|
82
84
|
return [301, 302, 303, 307, 308].includes(statusCode);
|
|
83
85
|
}
|
|
84
86
|
|
|
87
|
+
function ensureCommandInPath() {
|
|
88
|
+
if (process.env.npm_config_global !== "true") {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const pathDirs = (process.env.PATH || "")
|
|
93
|
+
.split(path.delimiter)
|
|
94
|
+
.filter(Boolean)
|
|
95
|
+
.map((dir) => path.resolve(dir));
|
|
96
|
+
const npmBin = path.resolve(process.env.npm_config_prefix || "", "bin");
|
|
97
|
+
if (pathDirs.includes(npmBin)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const linkDir = pathDirs.find(isUserWritableDir);
|
|
102
|
+
if (!linkDir) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const link = path.join(linkDir, "gcode");
|
|
107
|
+
if (fs.existsSync(link)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ponytail: npm can use a global bin dir outside PATH; this makes the one-command install work.
|
|
112
|
+
try {
|
|
113
|
+
fs.symlinkSync(path.join(__dirname, "gcode.js"), link);
|
|
114
|
+
} catch {}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function isUserWritableDir(dir) {
|
|
118
|
+
const home = path.resolve(os.homedir());
|
|
119
|
+
if (!dir.startsWith(home + path.sep)) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
if (!fs.statSync(dir).isDirectory()) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
128
|
+
return true;
|
|
129
|
+
} catch {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
85
134
|
function spinner(label) {
|
|
86
135
|
if (!process.stdout.isTTY) {
|
|
87
136
|
console.log(`${label}...`);
|