@tanagram/cli 0.1.20 → 0.1.21
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/install.js +78 -33
- package/package.json +1 -1
package/install.js
CHANGED
|
@@ -6,48 +6,87 @@ const path = require('path');
|
|
|
6
6
|
const os = require('os');
|
|
7
7
|
|
|
8
8
|
const GO_VERSION = '1.21.0';
|
|
9
|
+
const GO_CACHE_DIR = path.join(os.homedir(), '.tanagram', `go-${GO_VERSION}`);
|
|
9
10
|
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
// Check if Go is already installed locally
|
|
12
|
+
function checkLocalGo() {
|
|
13
|
+
try {
|
|
14
|
+
const version = execSync('go version', { encoding: 'utf8', stdio: ['pipe', 'pipe', 'ignore'] });
|
|
15
|
+
console.log(`✓ Found local Go: ${version.trim()}`);
|
|
16
|
+
return 'go';
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
// Check if go-bin was previously cached
|
|
23
|
+
function checkCachedGo() {
|
|
24
|
+
const cachedGoPath = path.join(GO_CACHE_DIR, 'bin', 'go');
|
|
25
|
+
if (fs.existsSync(cachedGoPath)) {
|
|
26
|
+
console.log(`✓ Using cached Go from ${GO_CACHE_DIR}`);
|
|
27
|
+
return cachedGoPath;
|
|
22
28
|
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
23
31
|
|
|
24
|
-
|
|
32
|
+
// Download and cache Go using go-bin
|
|
33
|
+
async function downloadGo() {
|
|
34
|
+
console.log('📦 Downloading Go compiler via npm...');
|
|
25
35
|
|
|
26
36
|
try {
|
|
27
|
-
//
|
|
28
|
-
// Use require.resolve to find go-bin in node_modules
|
|
37
|
+
// Load go-bin
|
|
29
38
|
let goBin;
|
|
30
39
|
try {
|
|
31
40
|
goBin = require('go-bin');
|
|
32
41
|
} catch (err) {
|
|
33
|
-
// Fallback: try to require from parent node_modules
|
|
34
42
|
const parentPath = path.join(__dirname, '..', '..', 'go-bin');
|
|
35
43
|
goBin = require(parentPath);
|
|
36
44
|
}
|
|
37
|
-
const goDir = await goBin({ version: GO_VERSION, dir: __dirname });
|
|
38
|
-
const goPath = path.join(goDir, 'bin', 'go');
|
|
39
45
|
|
|
40
|
-
|
|
46
|
+
// Create cache directory
|
|
47
|
+
const cacheParent = path.dirname(GO_CACHE_DIR);
|
|
48
|
+
if (!fs.existsSync(cacheParent)) {
|
|
49
|
+
fs.mkdirSync(cacheParent, { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Download Go to cache
|
|
53
|
+
const goDir = await goBin({
|
|
54
|
+
version: GO_VERSION,
|
|
55
|
+
dir: cacheParent,
|
|
56
|
+
includeTag: true
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
console.log(`✓ Go cached at ${goDir}`);
|
|
60
|
+
return path.join(goDir, 'bin', 'go');
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw new Error(`Failed to download Go: ${error.message}`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
41
65
|
|
|
42
|
-
|
|
43
|
-
|
|
66
|
+
// Build the binary
|
|
67
|
+
function buildBinary(goCommand) {
|
|
68
|
+
const platform = os.platform();
|
|
69
|
+
const arch = os.arch();
|
|
70
|
+
const goos = platform === 'win32' ? 'windows' : platform;
|
|
71
|
+
const goarch = arch === 'x64' ? 'amd64' : arch === 'arm64' ? 'arm64' : arch;
|
|
72
|
+
const binaryName = platform === 'win32' ? 'tanagram.exe' : 'tanagram';
|
|
73
|
+
const binaryPath = path.join(__dirname, 'bin', binaryName);
|
|
74
|
+
|
|
75
|
+
const binDir = path.join(__dirname, 'bin');
|
|
76
|
+
if (!fs.existsSync(binDir)) {
|
|
77
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
console.log('🔧 Building Tanagram CLI...');
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
execSync(`"${goCommand}" build -o "${binaryPath}" .`, {
|
|
44
84
|
cwd: __dirname,
|
|
45
85
|
stdio: 'inherit',
|
|
46
86
|
env: {
|
|
47
87
|
...process.env,
|
|
48
88
|
GOOS: goos,
|
|
49
|
-
GOARCH: goarch
|
|
50
|
-
GOROOT: goDir
|
|
89
|
+
GOARCH: goarch
|
|
51
90
|
}
|
|
52
91
|
});
|
|
53
92
|
|
|
@@ -55,29 +94,35 @@ async function buildWithGoBin() {
|
|
|
55
94
|
fs.chmodSync(binaryPath, '755');
|
|
56
95
|
}
|
|
57
96
|
|
|
58
|
-
// Clean up Go installation
|
|
59
|
-
console.log('🧹 Cleaning up...');
|
|
60
|
-
fs.rmSync(goDir, { recursive: true, force: true });
|
|
61
|
-
|
|
62
97
|
console.log('✓ Tanagram CLI installed successfully');
|
|
63
98
|
return true;
|
|
64
99
|
} catch (error) {
|
|
65
|
-
|
|
66
|
-
return false;
|
|
100
|
+
throw new Error(`Build failed: ${error.message}`);
|
|
67
101
|
}
|
|
68
102
|
}
|
|
69
103
|
|
|
70
104
|
// Main installation flow
|
|
71
105
|
(async () => {
|
|
72
106
|
try {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
107
|
+
// 1. Check if Go is already installed locally
|
|
108
|
+
let goCommand = checkLocalGo();
|
|
109
|
+
|
|
110
|
+
// 2. If not, check if we have a cached go-bin download
|
|
111
|
+
if (!goCommand) {
|
|
112
|
+
goCommand = checkCachedGo();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 3. If still no Go, download via go-bin and cache it
|
|
116
|
+
if (!goCommand) {
|
|
117
|
+
goCommand = await downloadGo();
|
|
77
118
|
}
|
|
119
|
+
|
|
120
|
+
// 4. Build the binary
|
|
121
|
+
buildBinary(goCommand);
|
|
122
|
+
|
|
78
123
|
process.exit(0);
|
|
79
124
|
} catch (error) {
|
|
80
|
-
console.error('Installation
|
|
125
|
+
console.error('\n❌ Installation failed:', error.message);
|
|
81
126
|
process.exit(1);
|
|
82
127
|
}
|
|
83
128
|
})();
|