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

refactor(installation): update postinstall to always copy cmf-cli to current directory and prioritize a local cmf bin file #446

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
"adm-zip": "^0.5.5",
"axios": "^0.21.1",
"debug": "^4.3.1",
"env-paths": "^2.2.1",
"is-installed-globally": "^0.4.0",
"is-path-inside": "^3.0.3",
"mkdirp": "^1.0.4",
"node_modules-path": "^2.0.5",
"rimraf": "^3.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@

const path = require('path'),
mkdirp = require('mkdirp'),
envPaths = require('env-paths'),
rimraf = require('rimraf'),
fs = require('fs'),
axios = require('axios'),
AdmZip = require("adm-zip"),
tmp = require('tmp'),
dbg= require('debug'),
dbg = require('debug'),
node_modules = require('node_modules-path'),
{ parsePackageJson, PLATFORM_MAPPING, ARCH_MAPPING } = require('./utils');


const debug = dbg("cmf:debug");
const error = dbg("cmf:debug:error");

async function getInstallationPath(opts) {
debug("Getting installation path...");
debug(`So targeting node_modules/.bin/${opts.binBaseName}. Making sure path exists...`);
// install into node_modules/.bin/${opts.binBaseName}
const value = path.join(node_modules(), ".bin");
const dir = path.join(value, opts.binBaseName);
debug(`Targeting node_modules/.bin/${opts.binBaseName}. Making sure path exists...`);
const dir = path.join(node_modules(), ".bin", opts.binBaseName);
await mkdirp(dir);
debug(`Install path exists!`);
return dir;
Expand All @@ -44,7 +40,6 @@ async function verifyAndPlaceBinary(binName, binPath, callback) {
*/
var INVALID_INPUT = "Invalid inputs";
async function install(callback) {

var opts = parsePackageJson(".");
if (!opts) return callback(INVALID_INPUT);
console.info(`Copying the relevant binary for your platform ${process.platform}`);
Expand Down Expand Up @@ -104,6 +99,7 @@ var actions = {
"install": install,
"uninstall": uninstall
};

/**
* Executes a shell command and return it as a Promise.
* @param cmd {string}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#! /usr/bin/env node

"use strict";

const { spawn } = require('child_process');
const path = require('path');
const node_modules = require('node_modules-path');
const dbg = require('debug');
const { parsePackageJson, ARCH_MAPPING, PLATFORM_MAPPING } = require('./utils');
const { parsePackageJson } = require('./utils');

const opts = parsePackageJson(__dirname);
const debug = dbg("cmf:debug");

debug(`Executing ${opts.binBaseName}`);
let exePath = null;
debug(`Determining if ${opts.binBaseName} is installed globally or locally...`);
debug(`Getting binary from node_modules/.bin/${opts.binName}...`);
exePath = path.join(node_modules(), ".bin", opts.binBaseName, opts.binName);
const exePath = path.join(node_modules(), ".bin", opts.binBaseName, opts.binName);
debug("Obtained binary path: " + exePath);

debug(`Spawning ${opts.binName} from ${exePath} with args ${process.argv.slice(2)} and piping.`);
const child = spawn(exePath, process.argv.slice(2), {stdio: "inherit"});
child.on('close', (code) => {
debug("Process exited with code " + code);
process.exitCode = code;
});
});
3 changes: 0 additions & 3 deletions npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
"adm-zip": "^0.5.12",
"axios": "^1.6.8",
"debug": "^4.3.4",
"env-paths": "^2.2.1",
"global-dirs": "3.0.0",
"is-installed-globally": "^0.4.0",
"is-path-inside": "^3.0.3",
"mkdirp": "^2.1.6",
"node_modules-path": "^2.0.7",
"rimraf": "^3.0.2",
Expand Down
39 changes: 10 additions & 29 deletions npm/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,25 @@

const path = require('path'),
mkdirp = require('mkdirp'),
envPaths = require('env-paths'),
rimraf = require('rimraf'),
fs = require('fs'),
axios = require('axios'),
AdmZip = require("adm-zip"),
tmp = require('tmp'),
dbg= require('debug'),
dbg = require('debug'),
node_modules = require('node_modules-path'),
{ parsePackageJson, PLATFORM_MAPPING, ARCH_MAPPING } = require('./utils');


const debug = dbg("cmf:debug");
const error = dbg("cmf:debug:error");

async function getInstallationPath() {
debug("Getting installation path...");
if (!!process.env.npm_config_global) {
debug("Install is global, so targeting home directory for binaries");
// install into home:
// win: /AppData/Local/CMF/cmf-cli
// linux: ~/.local/share/cmf-cli
// osx: ~/Library/Application Support/cmf-cli
const paths = envPaths("cmf-cli", {suffix: ""});
debug(`Install at ${paths.data}. Making sure path exists...`);
await mkdirp(paths.data);
debug(`Install path exists!`);
return paths.data;
} else {
debug("Install is local, so targeting node_modules/.bin/cmf-cli. Making sure path exists...");
// install into node_modules/.bin/cmf-cli
const value = path.join(node_modules(), ".bin");
const dir = path.join(value, "cmf-cli");
await mkdirp(dir);
debug(`Install path exists!`);
return dir;
}
debug("Targeting node_modules/.bin/cmf-cli. Making sure path exists...");
const dir = path.join(node_modules(), ".bin", "cmf-cli");
await mkdirp(dir);
debug(`Install path exists!`);
return dir;
}

async function verifyAndPlaceBinary(binName, binPath, callback) {
Expand All @@ -57,7 +40,6 @@ async function verifyAndPlaceBinary(binName, binPath, callback) {
*/
var INVALID_INPUT = "Invalid inputs";
async function install(callback) {

var opts = parsePackageJson(".");
if (!opts) return callback(INVALID_INPUT);
console.info(`Copying the relevant binary for your platform ${process.platform}`);
Expand Down Expand Up @@ -94,16 +76,14 @@ async function install(callback) {
await execShellCommand(`cp -r ${src}/** "${installPath}"`);
await execShellCommand(`chmod +x "${installPath}/cmf"`);
}

await verifyAndPlaceBinary(opts.binName, installPath, callback);
}

async function uninstall(callback) {
var opts = parsePackageJson(".");
try {
const installationPath = await getInstallationPath();
debug("Deleting binaries from " + installationPath);
rimraf.sync(installationPath);
const installPath = await getInstallationPath();
debug("Deleting binaries from " + installPath);
rimraf.sync(installPath);
} catch (ex) {
console.log(ex);
callback(ex);
Expand All @@ -118,6 +98,7 @@ var actions = {
"install": install,
"uninstall": uninstall
};

/**
* Executes a shell command and return it as a Promise.
* @param cmd {string}
Expand Down
24 changes: 5 additions & 19 deletions npm/run.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#! /usr/bin/env node

"use strict";

const { spawn } = require('child_process');
const envPaths = require('env-paths');
const path = require('path');
const isInstalledGlobally = require('is-installed-globally');
const isPathInside = require('is-path-inside');
const node_modules = require('node_modules-path');
const dbg = require('debug');
const { parsePackageJson } = require('./utils');
Expand All @@ -13,26 +12,13 @@ const opts = parsePackageJson(__dirname);
const debug = dbg("cmf:debug");

debug("Executing cmf-cli");
let exePath = null;
debug("Determining if cli is installed globally or locally...");
/**
* we need to check if we are installed globally.
* However, this does not cover some cases, depending on how Node was installed, that we need to check manually:
* - in windows, Node was installed in Program Files but NPM is installed in the user profile (AppData/Roaming)
*/
if (isInstalledGlobally || isPathInside(__dirname, process.env.APPDATA || "dummy") || isPathInside(__dirname, "/usr/local")) {
debug("cli is installed globally. Getting binary location from user profile...");
const paths = envPaths("cmf-cli", {suffix: ""});
exePath = path.join(paths.data, opts.binName);
} else {
debug("cli is installed locally. Getting binary from node_modules/.bin/cmf-cli...");
exePath = path.join(node_modules(), ".bin", "cmf-cli", opts.binName);
}
debug("Getting binary from node_modules/.bin/cmf-cli...");
const exePath = path.join(node_modules(), ".bin", "cmf-cli", opts.binName);
debug("Obtained binary path: " + exePath);

debug(`Spawning cmf-cli from ${exePath} with args ${process.argv.slice(2)} and piping.`);
const child = spawn(exePath, process.argv.slice(2), {stdio: "inherit"});
child.on('close', (code) => {
debug("Process exited with code " + code);
process.exitCode = code;
});
});
Loading