recursive imports in CLI

This commit is contained in:
Vyacheslav Matyukhin 2022-06-18 22:17:23 +03:00
parent 930c6ef474
commit e2c9dfaacc
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C

View File

@ -8,8 +8,16 @@ import chalk from "chalk";
import { Command } from "commander"; import { Command } from "commander";
import glob from "glob"; import glob from "glob";
function run(fileName) { const processFile = (fileName, seen = []) => {
const normalizedFileName = path.resolve(fileName);
if (seen.includes(normalizedFileName)) {
throw new Error(`Recursive dependency for file ${fileName}`);
}
const fileContents = fs.readFileSync(fileName, "utf-8"); const fileContents = fs.readFileSync(fileName, "utf-8");
if (!fileName.endsWith(".squiggleU")) {
return fileContents;
}
const regex = /\@import\(\s*([^)]+?)\s*\)/g; const regex = /\@import\(\s*([^)]+?)\s*\)/g;
const matches = Array.from(fileContents.matchAll(regex)).map((r) => const matches = Array.from(fileContents.matchAll(regex)).map((r) =>
@ -23,19 +31,9 @@ function run(fileName) {
const rename = r[1]; const rename = r[1];
const item = fs.statSync(importFileName); const item = fs.statSync(importFileName);
if (item.isFile()) { if (item.isFile()) {
const data = fs.readFileSync( const data = processFile(importFileName, [...seen, normalizedFileName]);
importFileName,
{ encoding: "utf8" },
function (err, _data) {
if (err) {
console.log(`Error importing ${importFileName}: `, err);
return false;
}
return _data;
}
);
if (data) { if (data) {
let importString = `${rename} = {\n${indentString(data, 2)}\n}`; const importString = `${rename} = {\n${indentString(data, 2)}\n}\n`;
appendings.push(importString); appendings.push(importString);
} }
} else { } else {
@ -52,26 +50,31 @@ function run(fileName) {
const imports = appendings.join("\n"); const imports = appendings.join("\n");
const newerContent = imports.concat(newContent); const newerContent = imports.concat(newContent);
const parsedPath = path.parse(fileName) return newerContent;
const newFilename = parsedPath.dir + "/" + parsedPath.name + ".squiggle"; };
fs.writeFileSync(newFilename, newerContent);
console.log(chalk.cyan(`Updated ${fileName} -> ${newFilename}`));
}
function compile() { 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);
console.log(chalk.cyan(`Updated ${fileName} -> ${newFilename}`));
};
const compile = () => {
glob("**/*.squiggleU", (_err, files) => { glob("**/*.squiggleU", (_err, files) => {
files.forEach(run); files.forEach(run);
}); });
} };
function watch() { const watch = () => {
chokidar chokidar
.watch("**.squiggleU") .watch("**.squiggleU")
.on("ready", () => console.log(chalk.green("Ready!"))) .on("ready", () => console.log(chalk.green("Ready!")))
.on("change", (event, _) => { .on("change", (event, _) => {
run(event); run(event);
}); });
} };
const program = new Command(); const program = new Command();
@ -83,15 +86,11 @@ program
program program
.command("watch") .command("watch")
.description("watch files and compile on the fly") .description("watch files and compile on the fly")
.action(() => { .action(watch);
watch();
});
program program
.command("compile") .command("compile")
.description("compile all .squiggleU files into .squiggle files") .description("compile all .squiggleU files into .squiggle files")
.action(() => { .action(compile);
compile();
});
program.parse(); program.parse();