Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
func.push(initPrivateTmpDir(value));
break;
default:
func.push(
writeStdout(hostMsg(`No handler found for ${key}.`, "warn")),
);
}
}
} else {
func.push(writeStdout(hostMsg(`No handler found for ${msg}.`, "warn")));
}
return Promise.all(func);
};
/* input */
const input = new Input();
/**
* read stdin
* @param {string|Buffer} chunk - chunk
* @returns {?AsyncFunction} - Promise chain
*/
const readStdin = chunk => {
const func = [];
const arr = input.decode(chunk);
if (Array.isArray(arr) && arr.length) {
for (const msg of arr) {
msg && func.push(handleMsg(msg));
}
}
return func.length && Promise.all(func).catch(handleReject) || null;
};
const handleEditorPathInput = async editorFilePath => {
let editorPath;
if (isFile(editorFilePath) && isExecutable(editorFilePath)) {
editorPath = editorFilePath;
} else {
const ans = readline.questionPath("Input editor path: ", {
isFile: true,
});
if (isExecutable(ans)) {
editorPath = ans;
} else {
console.warn(`${ans} is not executable.`);
editorPath = await handleEditorPathInput();
}
}
return editorPath;
};
const handleEditorPathInput = async editorFilePath => {
let editorPath;
if (isFile(editorFilePath) && isExecutable(editorFilePath)) {
editorPath = editorFilePath;
} else {
const ans = readline.question("Input editor path: ");
if (isExecutable(ans)) {
editorPath = ans;
} else {
console.warn(`${ans} is not executable.`);
editorPath = await handleEditorPathInput();
}
}
return editorPath;
};
const exportEditorConfig = async (data, editorConfigPath) => {
if (!isString(data)) {
throw new TypeError(`Expected String but got ${getType(data)}.`);
}
let func;
data = data && JSON.parse(data);
if (isObjectNotEmpty(data)) {
const {editorPath} = data;
const editorName = await getFileNameFromFilePath(editorPath);
const executable = isExecutable(editorPath);
const timestamp = await getFileTimestamp(editorConfigPath);
const reg =
new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`);
const keys = Object.keys(editorConfig);
for (const key of keys) {
const value = data[key];
if (key === "editorPath") {
editorConfig[key] = value;
}
if (key === "cmdArgs") {
editorConfig[key] = new CmdArgs(value).toArray();
editorConfig.hasPlaceholder = reg.test(value);
}
}
const msg = {
[EDITOR_CONFIG_RES]: {
const handleEditorPathInput = async editorFilePath => {
let editorPath;
if (isFile(editorFilePath) && isExecutable(editorFilePath)) {
editorPath = editorFilePath;
} else {
const ans = readline.question("Input editor path: ");
if (isExecutable(ans)) {
editorPath = ans;
} else {
console.warn(`${ans} is not executable.`);
editorPath = await handleEditorPathInput();
}
}
return editorPath;
};
const handleEditorPathInput = async editorFilePath => {
let editorPath;
if (isFile(editorFilePath) && isExecutable(editorFilePath)) {
editorPath = editorFilePath;
} else {
const ans = readline.questionPath("Input editor path: ", {
isFile: true,
});
if (isExecutable(ans)) {
editorPath = ans;
} else {
console.warn(`${ans} is not executable.`);
editorPath = await handleEditorPathInput();
}
}
return editorPath;
};
data = data && JSON.parse(data);
if (isObjectNotEmpty(data)) {
const {editorPath} = data;
const editorName = await getFileNameFromFilePath(editorPath);
const executable = isExecutable(editorPath);
const timestamp = await getFileTimestamp(editorConfigPath);
const reg =
new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`);
const keys = Object.keys(editorConfig);
for (const key of keys) {
const value = data[key];
if (key === "editorPath") {
editorConfig[key] = value;
}
if (key === "cmdArgs") {
editorConfig[key] = new CmdArgs(value).toArray();
editorConfig.hasPlaceholder = reg.test(value);
}
}
const msg = {
[EDITOR_CONFIG_RES]: {
editorName, executable,
[EDITOR_CONFIG_TS]: timestamp,
},
};
func = writeStdout(msg);
}
return func || null;
};
throw new Error("Application is not executable.");
}
const {cmdArgs, hasPlaceholder} = editorConfig;
const opt = {
cwd: null,
encoding: CHAR,
env: process.env,
};
let args, proc;
if (Array.isArray(cmdArgs)) {
args = cmdArgs.slice();
} else {
args = new CmdArgs(cmdArgs).toArray();
}
if (hasPlaceholder) {
const [filePath] = new CmdArgs(quoteArg(file)).toArray();
const reg =
new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`);
const l = args.length;
let i = 0;
while (i < l) {
const arg = args[i];
reg.test(arg) && args.splice(i, 1, arg.replace(reg, filePath));
i++;
}
proc = await new ChildProcess(app, args, opt).spawn();
} else {
proc = await new ChildProcess(app, args, opt).spawn(file);
}
proc.on("error", handleChildProcessErr);
proc.stderr.on("data", handleChildProcessStderr);
proc.stdout.on("data", handleChildProcessStdout);
throw new Error(`No such file: ${file}`);
}
if (!isExecutable(app)) {
throw new Error("Application is not executable.");
}
const {cmdArgs, hasPlaceholder} = editorConfig;
const opt = {
cwd: null,
encoding: CHAR,
env: process.env,
};
let args, proc;
if (Array.isArray(cmdArgs)) {
args = cmdArgs.slice();
} else {
args = new CmdArgs(cmdArgs).toArray();
}
if (hasPlaceholder) {
const [filePath] = new CmdArgs(quoteArg(file)).toArray();
const reg =
new RegExp(`\\$(?:${TMP_FILE_PLACEHOLDER}|{${TMP_FILE_PLACEHOLDER}})`);
const l = args.length;
let i = 0;
while (i < l) {
const arg = args[i];
reg.test(arg) && args.splice(i, 1, arg.replace(reg, filePath));
i++;
}
proc = await new ChildProcess(app, args, opt).spawn();
} else {
proc = await new ChildProcess(app, args, opt).spawn(file);
}
const createEditorConfig = async () => {
const configPath = setupOpts.get("configPath");
if (!isDir(configPath)) {
throw new Error(`No such directory: ${configPath}`);
}
const filePath = path.join(configPath, EDITOR_CONFIG_FILE);
const editorPath =
await handleEditorPathInput(setupOpts.get("editorFilePath"));
const cmdArgs = await handleCmdArgsInput(setupOpts.get("editorCmdArgs"));
const content = `${JSON.stringify({editorPath, cmdArgs}, null, INDENT)}\n`;
await createFile(filePath, content, {
encoding: CHAR,
flag: "w",
mode: PERM_FILE,
});
console.info(`Created: ${filePath}`);
return filePath;
};