1 | const inquirer = require("inquirer");
|
2 | const chalk = require("chalk");
|
3 | const fs = require("fs");
|
4 |
|
5 | const MappedChoices = {
|
6 | "Run project locally": "yarn start",
|
7 | "Choose a Firebase project": () => {
|
8 | execute("firebase list | sed 's/\x1b\\[[0-9;]*m//g'", function(table) {
|
9 |
|
10 | let lines = table.split("\n");
|
11 |
|
12 | let projects = lines.filter(e => e.indexOf("────") === -1);
|
13 | console.warn(projects);
|
14 | projects = projects.map(e =>
|
15 | e
|
16 | .split("│")
|
17 | .map(e => e.trim())
|
18 | .filter(z => z.length > 1)
|
19 | );
|
20 | let res = [];
|
21 | projects.forEach(line => {
|
22 | if (line.length) {
|
23 | if (line[0] !== "Name") {
|
24 | res.push(line[1]);
|
25 | }
|
26 | }
|
27 |
|
28 | });
|
29 | console.log("LIUST", res);
|
30 | inquirer
|
31 | .prompt([
|
32 | {
|
33 | type: "list",
|
34 | choices: res,
|
35 | name: "projectChoice",
|
36 | message: "Which project"
|
37 | }
|
38 | ])
|
39 | .then(({ projectChoice }) => {
|
40 | console.warn("CHOSEN", projectChoice);
|
41 |
|
42 | runCommand("firebase use " + projectChoice);
|
43 | });
|
44 | });
|
45 | },
|
46 | "SUGRGE.SH: Set your sub domain": () => {
|
47 | inquirer
|
48 | .prompt([
|
49 | {
|
50 | type: "input",
|
51 | name: "name",
|
52 | message: "Choose domain name {DOMAIN}.surge.sh: (do not type surge.sh)"
|
53 | }
|
54 | ])
|
55 | .then(({ name }) => {
|
56 | fs.writeFile("CNAME", name + ".surge.sh", err => {
|
57 |
|
58 | if (err) throw err;
|
59 |
|
60 |
|
61 | console.log("Domain saved!");
|
62 | });
|
63 | });
|
64 |
|
65 | },
|
66 | "SURGE.SH: Deploy to internet": "surge dist " + fs.readFileSync("CNAME"),
|
67 | "Login to Firebase": "firebase login",
|
68 | "Create Firebase project": "firebase init",
|
69 | "Deploy Firebase project to web": "yarn deploy"
|
70 | };
|
71 |
|
72 | inquirer
|
73 | .prompt([
|
74 | {
|
75 | type: "list",
|
76 | choices: Object.keys(MappedChoices),
|
77 | name: "choice",
|
78 | message: "Whatcha wanna do:"
|
79 | }
|
80 | ])
|
81 | .then(({ choice }) => {
|
82 | if (typeof MappedChoices[choice] === "string") {
|
83 | runCommand(MappedChoices[choice]);
|
84 | } else {
|
85 | MappedChoices[choice]();
|
86 | }
|
87 |
|
88 |
|
89 | });
|
90 |
|
91 | var exec = require("child_process").exec;
|
92 | function execute(command, callback) {
|
93 | exec(command, function(error, stdout, stderr) {
|
94 | callback(stdout);
|
95 | });
|
96 | }
|
97 |
|
98 | function runCommand(cmdChoice) {
|
99 | const { spawn } = require("child_process");
|
100 | let pars = cmdChoice.split(" ");
|
101 | const shell = spawn(pars[0], pars.slice(1, pars.length), { stdio: "inherit" });
|
102 |
|
103 | shell.on("close", code => {
|
104 | console.log(`${chalk.bold.green("Finished")}`);
|
105 | });
|
106 | }
|
107 |
|
108 | function execCommand(cmdChoice) {
|
109 | const { spawn } = require("child_process");
|
110 | let pars = cmdChoice.split(" ");
|
111 | const shell = spawn(pars[0], pars.slice(1, pars.length), { stdio: "inherit" });
|
112 | return new Promise(resolve => {
|
113 | shell.on("close", code => {
|
114 | console.log(`${chalk.bold.green("Finished")}`);
|
115 | resolve();
|
116 | });
|
117 | });
|
118 | }
|