Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented npm package for installing state tool #2040

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions installers/npm/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require('fs');
const os = require('os');
const package = require('./package.json');
const path = require('path');
const requestp = require('request-promise');
const decompress = require('decompress');
const sha256 = require('js-sha256');
const child_process = require('child_process');
const process = require('process');


module.exports = async function (callback) {
let version = package.version.split('-RC')[0];

let platform = {
darwin_x64: 'darwin-amd64',
darwin_arm64: 'darwin-amd64',
win32_x64: 'windows-amd64',
linux_x64: 'linux-amd64'
}[process.platform + '_' + process.arch];

if (!platform) {
throw new Error('Unsupported OS: ' + process.platform + '-' + process.arch);
}

console.log(`Preparing Installer for State Tool Package Manager version ${version}`);

let ext = "tar.gz";
if (process.platform === 'win32') {
ext = "zip";
}

let payloadURL = `https://state-tool.s3.amazonaws.com/update/state/release/${version}/${platform}/state-${platform}-${version}.${ext}`;
let payload = await request({method: 'GET', url: payloadURL, encoding: null});

// Verify Hash
let infoURL = `https://state-tool.s3.amazonaws.com/update/state/release/${version}/${platform}/info.json`;
let info = await request(infoURL);
info = JSON.parse(info);
let hash = sha256(payload);
if (hash !== info.sha256) {
throw new Error(`Hash mismatch: ${hash} !== ${info.sha256}`);
}

let tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "activestate-cli"));
await decompress(payload, tmpDir, {strip: 1});

let exeExt = "";
if (process.platform === 'win32') {
exeExt = ".exe";
}
let env = Object.create(process.env);
//env.VERBOSE = true;
let proc = child_process
.spawnSync('"' + path.join(tmpDir, "state-installer" + exeExt) + '"', ["--source-installer", "install.js", "--source-path", tmpDir], {
stdio: 'inherit',
shell: true,
env: env,
});

fs.rmSync(tmpDir, {recursive: true});
process.exit(proc.status);
};


async function request() {
let result;
await requestp.apply(null, arguments)
.then(res => {
result = res;
})
.catch(err => {
throw new Error(`Error downloading ${JSON.stringify(arguments)}: ${err}`);
});
return result;
}
26 changes: 26 additions & 0 deletions installers/npm/license-elm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright 2012-present Evan Czaplicki

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 changes: 29 additions & 0 deletions installers/npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "activestate-cli-preview",
"version": "0.34.0-SHA4436f67-RC7",
"description": "State Tool is the Command Line Interface for accessing [ActiveState Platform](https://www.activestate.com/products/platform/) functionality. It is a cross-platform tool written in Go supporting Windows, Linux and Mac. We're working on extending support to more versions and distributions in tandem with the Platform (feel free to let us know if you have issues in your favorite OS).",
"repository": {
"type": "git",
"url": "https://github.com/ActiveState/cli.git",
"directory": "installers/npm"
},
"preferGlobal": true,
"author": "ActiveState",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/ActiveState/cli/issues"
},
"homepage": "https://github.com/ActiveState/cli#readme",
"bin": {
"state": "./bin/state"
},
"dependencies": {
"decompress": "^4.2.1",
"js-sha256": "^0.9.0",
"request-promise": "^4.2.6",
"which": "^2.0.2"
},
"scripts": {
"postinstall": "node scripts/install.js"
}
}
31 changes: 31 additions & 0 deletions installers/npm/paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const os = require('os');
const path = require('path');
const process = require('process');

// WARNING! THIS FILE MUST BE KEPT IN SYNC WITH `internal/installation/paths_*.go`

const installPath = () => {
switch (process.platform) {
case 'win32':
return path.join(process.env.USERPROFILE, "AppData", "Local", "ActiveState", "StateTool", "release");
case 'darwin':
return path.join(os.homedir(), ".local", "ActiveState", "StateTool", "release");
case 'linux':
return path.join(os.homedir(), ".local", "ActiveState", "StateTool", "release");
default:
throw new Error(`Unsupported platform: ${process.platform}`);
}
};

const binPath = () => {
let ext = "";
if (process.platform === 'win32') {
ext = ".exe";
}
return path.join(installPath(), "bin", "state" + ext);
};

module.exports = {
installPath: installPath,
binPath: binPath,
};
17 changes: 17 additions & 0 deletions installers/npm/scripts/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const process = require('process');
const fs = require('fs');
const paths = require("../paths.js");

if (!process.env.npm_config_global) {
console.error('Must be installed globally (use -g flag).');
process.exit(1);
}

const binaryPath = paths.binPath();

if (fs.existsSync(binaryPath)) {
console.error(`State Tool is already installed at: ${binaryPath}.`);
process.exit(1);
}

require('../install.js')();