Skip to content

Commit

Permalink
init check for dev tools offer install
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikey committed Sep 2, 2022
1 parent 7b33870 commit 70fc4f3
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion src/commands/function/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,96 @@ import { store } from "../../store";
import { execSync } from "child_process";
import replace from "replace-in-file";
import { getNpmConfigInitVersion } from "../../lib/utils";
import prompt from "prompt";

prompt.start();
prompt.message = "";
prompt.delimiter = ":";

const { resolve } = require("path");

const sanitizer = /[^a-zA-Z0-9\-]/;

const sanitize = (input: string) => input.replace(sanitizer, "");

const checkForNpm = () => {
try {
execSync("npm --version", { stdio: "ignore" });
return true;
} catch (e) {
return false;
}
};

export const run = (options: any) => {
const { name, path = process.cwd(), private: isPrivate } = options;
const {
name,
path = process.cwd(),
private: isPrivate,
yes = false,
} = options;

if (!checkForNpm()) {
console.log(
Chalk.red(`[error]`),
`npm is required to run this command, please install it and try again\n`,
`npm is a development dependency of the function you are trying to create\n`,
"install npm: https://github.com/nvm-sh/nvm#install--update-script"
);

console.log(" ");
if (!yes) {
prompt.get(
{
properties: {
install: {
description: Chalk.yellow(`install nodejs and npm? (Y/n)`),
required: false,
default: "y",
},
},
},
function (err: any, result: any) {
if (err) {
console.log(err);
}
if (result.install === "Y" || result.install === "y") {
execSync(
`curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash`,
{ stdio: "ignore" }
);
execSync(
`export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. $NVM_DIR/nvm.sh && nvm install 18`
);
try {
execSync(
`export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. $NVM_DIR/nvm.sh && npm --version`,
{ stdio: "ignore" }
);
} catch (e) {
console.log(
Chalk.red(
`[error] unable to install npm/node please try manually`
)
);
}
console.log(
"developmenmt tools installed, please restart this terminal session, or run the following command before trying again"
);
console.log("");
console.log(
`export NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" `
);
}
}
);
}
console.log("");
return;
}

const installationPath = resolve(
process.cwd(),
`${path}`,
Expand Down

0 comments on commit 70fc4f3

Please sign in to comment.