diff --git a/installers/npm/install.js b/installers/npm/install.js new file mode 100644 index 0000000000..a401c45f65 --- /dev/null +++ b/installers/npm/install.js @@ -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; +} \ No newline at end of file diff --git a/installers/npm/license-elm.md b/installers/npm/license-elm.md new file mode 100644 index 0000000000..7f4fd2c3db --- /dev/null +++ b/installers/npm/license-elm.md @@ -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. diff --git a/installers/npm/package.json b/installers/npm/package.json new file mode 100644 index 0000000000..6309c3c10c --- /dev/null +++ b/installers/npm/package.json @@ -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" + } +} diff --git a/installers/npm/paths.js b/installers/npm/paths.js new file mode 100644 index 0000000000..e352360263 --- /dev/null +++ b/installers/npm/paths.js @@ -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, +}; \ No newline at end of file diff --git a/installers/npm/scripts/install.js b/installers/npm/scripts/install.js new file mode 100644 index 0000000000..5fc9de33cc --- /dev/null +++ b/installers/npm/scripts/install.js @@ -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')(); \ No newline at end of file