Debian Codegraph @

cli/tools-cli/src/scripts/dev.ts

raw symbols SCIP
1 import os from "os";
2 import fs from "fs";
3 import path from "path";
4 import chalk from "chalk";
5 import { Command } from "commander";
6 import { runCompiler } from "./compile";
7 import { isQInstalled, setSetting } from "./settings";
8
9 const AUTOCOMPLETE_LOG_FILE = path.join(os.homedir(), ".fig", "logs", "specs.log");
10
11 function disableDevMode() {
12 console.log("\n\nFig dev mode disabled\n");
13 setSetting("autocomplete.developerMode", false);
14 setSetting("autocomplete.developerModeNPM", false);
15 setSetting("autocomplete.developerModeNPMInvalidateCache", true);
16 process.exit(0);
17 }
18
19 function cleanup() {
20 disableDevMode();
21 fs.unwatchFile(AUTOCOMPLETE_LOG_FILE);
22 }
23
24 async function runProgram({ outdir }: { outdir?: string }) {
25 console.clear();
26
27 if (os.type() === "Darwin") {
28 const globalFigAppPath = "/Applications/Fig.app";
29 const localFigAppPath = path.join(os.homedir(), "Applications/Fig.app");
30 const globalQAppPath = "/Applications/Amazon Q.app";
31 const localQAppPath = path.join(os.homedir(), "Applications/Amazon Q.app");
32
33 if (
34 !fs.existsSync(globalFigAppPath) &&
35 !fs.existsSync(localFigAppPath) &&
36 !fs.existsSync(globalQAppPath) &&
37 !fs.existsSync(localQAppPath)
38 ) {
39 console.log(
40 "\n******\n\n",
41 chalk.bold(chalk.yellow(" WARNING: Amazon Q for command line app is not installed")),
42 "\n\n",
43 chalk.bold(chalk.cyan(" Download Amazon Q for command line at:")),
44 "\n https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-getting-started-installing.html",
45 "\n\n******\n"
46 );
47 }
48 } else if (os.type() === "Linux") {
49 console.log(
50 "\n******\n\n",
51 chalk.bold(chalk.yellow(" WARNING: Amazon Q for command line on Linux is not supported yet")),
52 "\n\n"
53 );
54 } else if (!isQInstalled()) {
55 console.log(
56 "\n******\n\n",
57 chalk.bold(chalk.yellow(" WARNING: Amazon Q for command line is not installed")),
58 "\n\n",
59 chalk.bold(chalk.cyan(" Download Amazon Q for command line at:")),
60 "\n https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-getting-started-installing.html",
61 "\n\n******\n"
62 );
63 }
64
65 console.log(
66 `Welcome to ${chalk.magenta("Fig Dev Mode")}!\n\n`,
67 `All completions will be loaded from ${chalk.bold(
68 `${process.cwd()}/${outdir ?? "build"}`
69 )}. (Note: other completions won't work while in dev mode).\n\n`,
70 `1. Edit your spec(s) in the ${chalk.bold("src/")} directory.\n`,
71 `2. Test changes ${chalk.bold("instantly")} on save in your terminal.\n`,
72 `3. Exit developer mode with ${chalk.bold("ctrl + c")}.\n\n`,
73 `${chalk.bold("Other Notes:")}\n`,
74 `- Generators run on every keystroke\n`
75 );
76 // We are on macos and the fig script exists
77 process.addListener("SIGTERM", cleanup);
78 process.addListener("SIGINT", cleanup);
79 process.addListener("SIGQUIT", cleanup);
80
81 setSetting("autocomplete.developerModeNPM", true);
82 setSetting(
83 "autocomplete.devCompletionsFolder",
84 path.join(process.cwd(), outdir ?? "build").replace(/(\s)/g, "\\$1")
85 );
86 if (!fs.existsSync(path.dirname(AUTOCOMPLETE_LOG_FILE))) {
87 fs.mkdirSync(path.dirname(AUTOCOMPLETE_LOG_FILE), { recursive: true });
88 }
89 fs.writeFileSync(AUTOCOMPLETE_LOG_FILE, "", { encoding: "utf8" });
90 let previousLogContent = "";
91 fs.watch(AUTOCOMPLETE_LOG_FILE, (event) => {
92 if (event === "change") {
93 const currentContent = fs.readFileSync(AUTOCOMPLETE_LOG_FILE, { encoding: "utf8" }).trim();
94 const message = previousLogContent
95 ? currentContent.split("\n").slice(previousLogContent.split("\n").length).join("\n")
96 : currentContent;
97 console.log(chalk.yellow(message));
98 previousLogContent = currentContent;
99 }
100 });
101 await runCompiler({ watch: true, outdir });
102 }
103
104 const program = new Command("dev")
105 .description("watch for changes and compile specs")
106 .option("-o, --outdir <dir>", "Output directory")
107 .action(runProgram);
108
109 export default program;