error-central / error-central

1 stars 1 forks source link

Install bash script upon installation of npm package #15

Open wanderingstan opened 4 years ago

wanderingstan commented 4 years ago

When user does npm install error-central, we should automagically add line to the shell startup script (.bashrc/.bash_profile). This may require escalated privileges.

As template, the tabtab project has a similar installer: Docs: https://github.com/mklabs/tabtab/blob/db75a56c3935bfffb0d528286f6aa23b4035a675/api/installer.js.md Code: https://github.com/mklabs/tabtab/blob/master/lib/installer.js

Test for their installer: (Could be good template for testing too) https://github.com/mklabs/tabtab/blob/db75a56c3935bfffb0d528286f6aa23b4035a675/test/tabtab-install.js

Prompt: https://github.com/mklabs/tabtab/blob/db75a56c3935bfffb0d528286f6aa23b4035a675/lib/prompt.js

wanderingstan commented 4 years ago

Working in branch install-script-npm

wanderingstan commented 4 years ago

Note that it seems nie impossible to determine the user's shell from within node. env vars ZSH_VERSION and FISH_VERSION are null from within node.

I'm thinking the best bet might be to look for the existence of files .bashrc, .bash_profile, zshrc, etc and simply add our loader there if the file is existing. @werg , thoughts?

wanderingstan commented 4 years ago

old code for postinstall.ts used to test:

import { X_OK } from "constants";

/**
 * To test, run `npm run postinstall`
 */
const installer = require("./installer");
const prompt = require("./prompt");
const {
  BASH_LOCATION,
  MACOS_BASH_LOCATION,
  FISH_LOCATION,
  ZSH_LOCATION,
  COMPLETION_DIR,
  TABTAB_SCRIPT_NAME
} = require("./constants");

/**
 * Install error-central
 * @param options
 */
const install = async (options = { name: "" }) => {
  const { name } = options;
  if (!name) throw new TypeError("options.name is required");

  console.log("process.env.FISH_VERSION", process.env.FISH_VERSION);
  console.log("process.env.ZSH_VERSION", process.env.ZSH_VERSION);
  console.log("process.platform", process.platform);

  const location = (
    process.env.FISH_VERSION ? FISH_LOCATION :
      process.env.ZSH_VERSION ? ZSH_LOCATION :
        process.platform == "darwin" ? MACOS_BASH_LOCATION :
          BASH_LOCATION);
  const shell = (
    process.env.FISH_VERSION ? "fish" :
      process.env.ZSH_VERSION ? "zsh" :
        "bash");

  const x = { location, shell };

  // return prompt().then((x: any) => {
  console.log("x:", x);
  installer.install({
    name,
    location: x.location,
    shell: x.shell
  });
  // }
  // );
};

install({
  name: "ec"
});