squiggle/packages/cli/index.js

97 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2022-06-18 04:45:58 +00:00
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import indentString from "indent-string";
import chokidar from "chokidar";
import chalk from "chalk";
import { Command } from "commander";
import glob from "glob";
2022-06-18 19:17:23 +00:00
const processFile = (fileName, seen = []) => {
const normalizedFileName = path.resolve(fileName);
if (seen.includes(normalizedFileName)) {
throw new Error(`Recursive dependency for file ${fileName}`);
}
2022-06-18 04:45:58 +00:00
const fileContents = fs.readFileSync(fileName, "utf-8");
2022-06-18 19:17:23 +00:00
if (!fileName.endsWith(".squiggleU")) {
return fileContents;
}
2022-06-18 04:45:58 +00:00
const regex = /\@import\(\s*([^)]+?)\s*\)/g;
const matches = Array.from(fileContents.matchAll(regex)).map((r) =>
r[1].split(/\s*,\s*/)
);
const newContent = fileContents.replaceAll(regex, "");
const appendings = [];
matches.forEach((r) => {
const importFileName = r[0];
const rename = r[1];
const item = fs.statSync(importFileName);
if (item.isFile()) {
2022-06-18 19:17:23 +00:00
const data = processFile(importFileName, [...seen, normalizedFileName]);
2022-06-18 04:45:58 +00:00
if (data) {
2022-06-18 19:17:23 +00:00
const importString = `${rename} = {\n${indentString(data, 2)}\n}\n`;
2022-06-18 04:45:58 +00:00
appendings.push(importString);
}
} else {
console.log(
chalk.red(`Import Error`) +
`: ` +
chalk.cyan(importFileName) +
` not found in file ` +
chalk.cyan(fileName) +
`. Make sure the @import file names all exist in this repo.`
);
}
});
const imports = appendings.join("\n");
const newerContent = imports.concat(newContent);
2022-06-18 19:17:23 +00:00
return newerContent;
};
const run = (fileName) => {
const content = processFile(fileName);
const parsedPath = path.parse(path.resolve(fileName));
const newFilename = `${parsedPath.dir}/${parsedPath.name}.squiggle`;
fs.writeFileSync(newFilename, content);
2022-06-18 04:45:58 +00:00
console.log(chalk.cyan(`Updated ${fileName} -> ${newFilename}`));
2022-06-18 19:17:23 +00:00
};
2022-06-18 04:45:58 +00:00
2022-06-18 19:17:23 +00:00
const compile = () => {
2022-06-18 04:45:58 +00:00
glob("**/*.squiggleU", (_err, files) => {
files.forEach(run);
});
2022-06-18 19:17:23 +00:00
};
2022-06-18 04:45:58 +00:00
2022-06-18 19:17:23 +00:00
const watch = () => {
2022-06-18 04:45:58 +00:00
chokidar
.watch("**.squiggleU")
.on("ready", () => console.log(chalk.green("Ready!")))
.on("change", (event, _) => {
2022-06-18 19:17:23 +00:00
run(event);
2022-06-18 04:45:58 +00:00
});
2022-06-18 19:17:23 +00:00
};
2022-06-18 04:45:58 +00:00
const program = new Command();
program
.name("squiggle-utils")
.description("CLI to transform squiggle files with @imports")
.version("0.0.1");
program
.command("watch")
.description("watch files and compile on the fly")
2022-06-18 19:17:23 +00:00
.action(watch);
2022-06-18 04:45:58 +00:00
program
.command("compile")
.description("compile all .squiggleU files into .squiggle files")
2022-06-18 19:17:23 +00:00
.action(compile);
2022-06-18 04:45:58 +00:00
program.parse();