Use gzip for release assets so they can easily be extracted from node
This commit is contained in:
parent
b0fe816441
commit
e7bb57550b
6 changed files with 33 additions and 36 deletions
3
cli/npm/.gitignore
vendored
3
cli/npm/.gitignore
vendored
|
|
@ -1,4 +1,3 @@
|
|||
tree-sitter
|
||||
tree-sitter.exe
|
||||
*.tar.gz
|
||||
*.zip
|
||||
*.gz
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const fs = require('fs');
|
||||
const zlib = require('zlib');
|
||||
const https = require('https');
|
||||
const execFileSync = require('child_process').execFileSync;
|
||||
const packageJSON = require('./package.json');
|
||||
|
||||
// Determine the URL of the file.
|
||||
const isWindows = process.platform === 'win32';
|
||||
const platformName = {
|
||||
'darwin': 'osx',
|
||||
'linux': 'linux',
|
||||
|
|
@ -15,48 +14,41 @@ const platformName = {
|
|||
if (!platformName) {
|
||||
throw new Error(`Cannot install tree-sitter-cli for platform ${process.platform}`);
|
||||
}
|
||||
|
||||
const archName = {
|
||||
'x64': 'x64',
|
||||
'x86': 'x86',
|
||||
'ia32': 'x86'
|
||||
}[process.arch];
|
||||
if (!archName) {
|
||||
throw new Error(`Cannot install tree-sitter-cli for architecture ${process.arch}`);
|
||||
}
|
||||
|
||||
const releaseURL = `https://github.com/tree-sitter/tree-sitter/releases/download/${packageJSON.version}`;
|
||||
const assetExtension = isWindows ? 'zip' : 'tar.gz';
|
||||
const assetName = `tree-sitter-${platformName}-${process.arch}.${assetExtension}`;
|
||||
const assetName = `tree-sitter-${platformName}-${archName}.gz`;
|
||||
const assetURL = `${releaseURL}/${assetName}`;
|
||||
|
||||
// Remove previously-downloaded files.
|
||||
const executableName = isWindows ? 'tree-sitter.exe' : 'tree-sitter';
|
||||
const executableName = process.platform === 'win32' ? 'tree-sitter.exe' : 'tree-sitter';
|
||||
if (fs.existsSync(executableName)) {
|
||||
fs.unlinkSync(executableName);
|
||||
}
|
||||
if (fs.existsSync(assetName)) {
|
||||
fs.unlinkSync(assetName);
|
||||
}
|
||||
|
||||
// Download the compressed file.
|
||||
console.log(`Downloading ${assetURL}`);
|
||||
const file = fs.createWriteStream(assetName);
|
||||
const file = fs.createWriteStream(executableName);
|
||||
get(assetURL, response => {
|
||||
if (response.statusCode > 299) {
|
||||
throw new Error([
|
||||
'Download failed',
|
||||
'',
|
||||
`url: ${url}`,
|
||||
`url: ${assetURL}`,
|
||||
`status: ${response.statusCode}`,
|
||||
`headers: ${JSON.stringify(response.headers, null, 2)}`,
|
||||
'',
|
||||
].join('\n'));
|
||||
}
|
||||
|
||||
response.pipe(file);
|
||||
});
|
||||
|
||||
// Extract the file.
|
||||
file.on('finish', () => {
|
||||
console.log(`Extracting ${assetName}`);
|
||||
if (isWindows) {
|
||||
execFileSync('7z', ['e', assetName]);
|
||||
} else {
|
||||
execFileSync('tar', ['xzf', assetName]);
|
||||
}
|
||||
fs.unlinkSync(assetName);
|
||||
console.log(`Done`);
|
||||
response.pipe(zlib.createGunzip()).pipe(file);
|
||||
});
|
||||
|
||||
// Follow redirects.
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
],
|
||||
"main": "lib/api/index.js",
|
||||
"scripts": {
|
||||
"install": "./install.js"
|
||||
"install": "install.js"
|
||||
},
|
||||
"bin": {
|
||||
"tree-sitter": "tree-sitter"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue