diff --git a/tools/deploy/import.ts b/tools/deploy/import.ts new file mode 100644 index 00000000..f3428084 --- /dev/null +++ b/tools/deploy/import.ts @@ -0,0 +1,25 @@ + +import fs from 'fs'; +import vm from 'vm'; +import pathlib from 'path'; + +export function imp(files: string[], code : string) { + const script = new vm.Script(code); + + const results = []; + for (const file of files) { + const parsed = pathlib.parse(file); + const obj = {dir: parsed.dir, + base: parsed.name, + ext: parsed.ext === "" ? null : parsed.ext.slice(1) + }; + const output = script.runInNewContext({path: obj, ...obj}); + if (typeof output === 'string') { + const dest = pathlib.format({name: output, ext: parsed.ext}); + results.push({src: file, dst: dest}); + } else { + throw new Error(`result of eval on ${file} not string`); + } + } + return results; +} diff --git a/tools/deploy/index.ts b/tools/deploy/index.ts index 79fa6d2c..bd55ee47 100644 --- a/tools/deploy/index.ts +++ b/tools/deploy/index.ts @@ -3,6 +3,33 @@ import program from 'commander'; import {run} from './lang.js'; import {find} from './find.js'; import {link} from './deploy.js'; +import {imp} from './import.js'; +import fs from 'fs'; + +function collect(value : string, previous : string[]) { + return previous.concat([value]); +} + +program + .command('import [files...]') + .option('-o, --output ', 'Output directory') + // TODO: default toID + .option('-e, --eval ', 'Expr') + .option('-m, --module ', 'Module') + .option('-t, --tag ', 'Tag', collect, []) + .action(async (files : string[], {eval: expr, module: mod, output: outputDir, tag: tags}) => { + let code : string; + if (expr !== undefined) { + code = expr; + } else if (mod !== undefined) { + code = fs.readFileSync(mod, 'utf8'); + } else { + throw new Error(`one of -e or -m must be provided`); + } + + const result = imp(files, code); + link(result, outputDir, 'copy'); + }); program .command('deploy ')