-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
112 lines (95 loc) · 2.66 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { exec } from "child_process";
import fs from "fs";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import inquirer from "inquirer";
import autocompletePrompt from "inquirer-autocomplete-prompt";
import logger from "./logger.js";
inquirer.registerPrompt("autocomplete", autocompletePrompt);
const { __dirname } = fileDirName(import.meta);
export const choicesFilePath = path.join(__dirname, "choices.json");
let choices = JSON.parse(fs.readFileSync(choicesFilePath));
export function fileDirName(meta) {
const __filename = fileURLToPath(meta.url);
const __dirname = dirname(__filename);
return { __dirname, __filename };
}
export function openFileInEditor(filePath) {
return new Promise((resolve, reject) => {
let command;
if (process.platform === "darwin") {
command = `open -a "Visual Studio Code" ${filePath}`;
} else {
reject(new Error(`Unsupported platform: ${process.platform}`));
return;
}
exec(command, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
export function askChoices() {
inquirer
.prompt([
{
type: "autocomplete",
name: "_1",
message: "Where to?",
source: (_, input) => {
input = input || "";
return Promise.resolve(
Object.keys(choices).filter((project) =>
project.toLowerCase().startsWith(input.toLowerCase())
)
);
},
},
{
type: "autocomplete",
name: "_2",
message: "And then?",
source: (answersSoFar, input) => {
if (typeof choices[answersSoFar["_1"]] === "edit") {
}
input = input || "";
const places = Object.keys(choices[answersSoFar["_1"]]);
return Promise.resolve(
places.filter((type) =>
type.toLowerCase().startsWith(input.toLowerCase())
)
);
},
},
])
.then((answers) => {
runScript(answers);
});
}
export function runScript(answers) {
let command = choices[answers._1][answers._2];
const isUrl = command.match("https");
if (isUrl) {
command = `open -na "Google Chrome" --args --new-window "${command}"`;
}
const childProcess = exec(command, (error, stdout, stderr) => {
if (error) {
logger.error(`exec error: ${error}`);
return;
}
if (stderr) {
logger.error(`stderr: ${stderr}`);
return;
}
logger.branded("yeehaw");
if (stdout) {
logger.info(`stdout: ${stdout}`);
}
});
childProcess.stdout.on("data", function (chunk) {
logger.info(`${chunk}`);
});
}