Skip to content

Commit

Permalink
Allow modifying the outdir for cli build
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 committed Jan 4, 2024
1 parent 17ee2d5 commit 139888b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
}
}
18 changes: 10 additions & 8 deletions cli/tools-cli/src/scripts/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { setSetting } from "./settings";

// Folder names
const SOURCE_FOLDER_NAME = "src";
const DESTINATION_FOLDER_NAME = "build";
const DEFAULT_DESTINATION_FOLDER_NAME = "build";

function invalidateCache() {
setSetting("autocomplete.developerModeNPMInvalidateCache", true);
Expand All @@ -18,11 +18,11 @@ function invalidateCache() {
* Transpiles all passed files and prints the progress
* @param specs Array of filepaths
*/
async function processFiles(files: string[], isDev?: boolean) {
async function processFiles(files: string[], isDev?: boolean, outdir?: string) {
const fileName = files.length === 1 ? files[0] : `${files.length} specs`;
await build({
entryPoints: files,
outdir: DESTINATION_FOLDER_NAME,
outdir: outdir ?? DEFAULT_DESTINATION_FOLDER_NAME,
bundle: true,
outbase: "src",
format: "esm",
Expand All @@ -34,23 +34,25 @@ async function processFiles(files: string[], isDev?: boolean) {
invalidateCache();
}

export async function runCompiler(options: { watch: boolean }) {
export async function runCompiler({ watch, outdir }: { watch: boolean; outdir?: string }) {
const SOURCE_FILE_GLOB = `${SOURCE_FOLDER_NAME}/**/*.ts`;
const files = await glob(SOURCE_FILE_GLOB);
await processFiles(files);

if (options.watch) {
await processFiles(files, undefined, outdir);

if (watch) {
const watcher = chokidar.watch(SOURCE_FILE_GLOB, { ignoreInitial: true });

// Process the changed file
watcher.on("change", (file) => processFiles([file], true));
watcher.on("add", (file) => processFiles([file], true));
watcher.on("change", (file) => processFiles([file], true, outdir));
watcher.on("add", (file) => processFiles([file], true, outdir));
}
}

const program = new Command("compile")
.description("compile specs in the current directory")
.option("-w, --watch", "Watch files and re-compile on change")
.option("-o, --outdir <dir>", "Output directory")
.action(runCompiler);

export default program;
9 changes: 5 additions & 4 deletions cli/tools-cli/src/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function cleanup() {
fs.unwatchFile(AUTOCOMPLETE_LOG_FILE);
}

async function runProgram() {
async function runProgram({ outdir }: { outdir?: string }) {
console.clear();

if (os.type() === "Darwin") {
Expand Down Expand Up @@ -70,7 +70,7 @@ async function runProgram() {
console.log(
`Welcome to ${chalk.magenta("Fig Dev Mode")}!\n\n`,
`All completions will be loaded from ${chalk.bold(
`${process.cwd()}/build`
`${process.cwd()}/${outdir ?? "build"}`
)}. (Note: other completions won't work while in dev mode).\n\n`,
`1. Edit your spec(s) in the ${chalk.bold("src/")} directory.\n`,
`2. Test changes ${chalk.bold("instantly")} on save in your terminal.\n`,
Expand All @@ -86,7 +86,7 @@ async function runProgram() {
setSetting("autocomplete.developerModeNPM", true);
setSetting(
"autocomplete.devCompletionsFolder",
path.join(process.cwd(), "build").replace(/(\s)/g, "\\$1")
path.join(process.cwd(), outdir ?? "build").replace(/(\s)/g, "\\$1")
);
if (!fs.existsSync(path.dirname(AUTOCOMPLETE_LOG_FILE))) {
fs.mkdirSync(path.dirname(AUTOCOMPLETE_LOG_FILE), { recursive: true });
Expand All @@ -103,11 +103,12 @@ async function runProgram() {
previousLogContent = currentContent;
}
});
await runCompiler({ watch: true });
await runCompiler({ watch: true, outdir });
}

const program = new Command("dev")
.description("watch for changes and compile specs")
.option("-o, --outdir <dir>", "Output directory")
.action(runProgram);

export default program;

0 comments on commit 139888b

Please sign in to comment.