mirror of
https://github.com/smogon/sprites.git
synced 2026-08-20 16:55:19 -05:00
deploy: import first pass
This commit is contained in:
25
tools/deploy/import.ts
Normal file
25
tools/deploy/import.ts
Normal 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;
|
||||
}
|
||||
@@ -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>')
|
||||
|
||||
Reference in New Issue
Block a user