deploy: import first pass

This commit is contained in:
Christopher Monsanto
2020-07-25 15:21:56 -04:00
parent ca17fcd6e1
commit dab36f0771
2 changed files with 52 additions and 0 deletions

25
tools/deploy/import.ts Normal file
View File

@@ -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;
}

View File

@@ -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 <dir>', 'Output directory')
// TODO: default toID
.option('-e, --eval <expr>', 'Expr')
.option('-m, --module <mod>', 'Module')
.option('-t, --tag <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 <tag> <outputDir>')