@fink/cli
Version:
CLI for compiling fink to JS
140 lines (131 loc) • 3 kB
JavaScript
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
import { dirname } from "path";
import { parse } from "@fink/larix/parser.js";
import { generate } from "@fink/loxia/generate.js";
import { decode } from "@fink/std-lib/str.js";
import { is_empty, map } from "@fink/std-lib/iter.js";
import { get_files } from "./files.js";
import { logger } from "./logging.js";
const {
warn
} = logger();
export const transform = (source, filename, options) => {
const ast = parse(source, filename);
{
const ˆvalue_1 = ast;
if (ˆvalue_1 != null) {
const {
errors: ˆp_3
} = ˆvalue_1;
if (is_empty(ˆp_3)) {
return generate(ast, filename, source, {
babel: {
babelrc: false,
configFile: false
},
...options
});
}
}
{
return {
errors: ast.errors
};
}
}
};
export const compile = (filename, options) => {
const buff = readFileSync(filename);
const source = decode(buff, `utf-8`);
return transform(source, filename, options);
};
export const output_to_dir = ({
code,
src_path,
out_path
}) => {
const dir = dirname(out_path);
{
ˆmatch_5: {
const ˆvalue_4 = false;
if (ˆvalue_4 === existsSync(dir)) {
{
mkdirSync(dir, {
recursive: true
});
warn(`created ${dir}`);
}
break ˆmatch_5;
}
}
}
writeFileSync(out_path, code);
warn(`compiled ${src_path} -> ${out_path}`);
return out_path;
};
export const output_code = ({
stdout
}, item) => {
{
ˆmatch_7: {
const ˆvalue_6 = item.out_path;
if (ˆvalue_6 === false) {
stdout.write(`${item.code}\n`);
break ˆmatch_7;
}
{
output_to_dir(item);
break ˆmatch_7;
}
}
}
return item;
};
export const compile_all = (proc, src, out_dir = false, ignore = false, options = {}) => {
const files = get_files(src, out_dir, ignore, options.module_ext);
{
let ˆpipe_result_8 = files;
return ˆpipe_result_8 = map(({
src_path,
out_path,
rel_path
}) => {
const compiled = compile(src_path, options);
{
const ˆvalue_9 = compiled;
if (ˆvalue_9 != null) {
const {
errors: ˆp_11
} = ˆvalue_9;
if (is_empty(ˆp_11)) {
{
const {
code
} = compiled;
const item = {
src_path,
out_path,
rel_path,
code
};
return output_code(proc, item);
}
}
}
{
{
const {
errors
} = compiled;
return {
src_path,
out_path,
rel_path,
errors
};
}
}
}
})(ˆpipe_result_8);
}
};