-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (122 loc) · 4.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const ArgumentParser = require('argparse').ArgumentParser;
const locateFiles = require('./src/utils/locate_files');
const OBJ_INPUT_EXT = '.obj';
const DATA_PARAM_EXT = '.json';
const OBJ_GEN_EXT = '.gen.obj';
const DEFAULT_OBJ_MODEL_DIR = 'models';
const DEFAULT_DATA_PARAM_DIR = 'output';
const DEFAULT_OBJ_GEN_DIR = 'output';
main();
function main() {
const parser = new ArgumentParser({
version: '0.0.1',
addHelp: true,
description: 'tbd (shape-gen tools)'
});
const subparsers = parser.addSubparsers({title: 'command', dest: 'command'});
const commands = {};
function addSubcommand(name, addArgs, callback) {
const parser = subparsers.addParser(name, {addHelp: true});
addArgs(parser);
commands[name] = callback;
}
function enforceFileExists(file) {
if (!fs.existsSync(file)) {
console.warn(`file does not exist: '${file}'`);
process.exit();
}
}
// Get info on model / etc data
// Currently being used to test glob()
addSubcommand('info', (parser) => {
parser.addArgument('input');
parser.addArgument('--iext', {defaultValue: '.obj'});
parser.addArgument('--oext', {defaultValue: '.json'});
}, (args) => {
const files = locateFiles({
input: args.input, output: "output",
inputExt: args.iext, outputExt: args.oext,
});
console.dir(files);
});
const getAllFileArgumentsAndRunParallel = (baseArgs, each) => (args) => {
// console.dir(args);
args.__proto__ = baseArgs; // provide defaults for optional args
let tasks = locateFiles(args);
if (!args.rebuild) {
// console.dir(tasks);
tasks = tasks.filter((file) => {
return !fs.existsSync(file.output)
});
// console.dir(tasks);
}
if (args.limit) {
console.log(`reducing ${tasks.length} to ${args.limit}`);
args.limit = Math.min(args.limit, tasks.length);
tasks = tasks.slice(0, args.limit);
}
args.workers = Math.min(args.workers, tasks.length);
if (args.workers > 1) {
console.log(`Launching ${args.workers} cluster workers...`);
require('./src/utils/processing_worker')({
numWorkers: args.workers,
job: baseArgs.job,
tasks: tasks
});
} else {
tasks.forEach((argInstance, i) => {
console.log(`${i} / ${tasks.length}`);
argInstance.__proto__ = args; // forward parent args
each(argInstance);
});
}
};
// view a model, json parameterization, or directory
addSubcommand('view', (parser) => {
parser.addArgument('input');
}, (args) => {
enforceFileExists(args.input);
console.warn("TBD: view");
});
// process an obj model => json parameterization
addSubcommand('process', (parser) => {
parser.addArgument('input');
parser.addArgument('output', {defaultValue: DEFAULT_DATA_PARAM_DIR});
parser.addArgument(['-l', '--levels'], {type: Number, defaultValue: 5});
parser.addArgument(['-J', '--workers'], {type: Number, defaultValue: 1});
parser.addArgument(['--limit'], {type: Number, defaultValue: 0});
parser.addArgument(['-r', '--rebuild'], {action: 'storeTrue'});
}, getAllFileArgumentsAndRunParallel({
job: 'process',
inputExt: OBJ_INPUT_EXT,
outputExt: DATA_PARAM_EXT,
}, (args) => {
enforceFileExists(args.input);
console.log(`processing ${args.input} => ${args.output}, levels = ${args.levels}`);
require('./src/process_file')(args);
}));
// reconstruct a json parameterization => obj model
addSubcommand('reconstruct', (parser) => {
parser.addArgument('input');
parser.addArgument('output', {defaultValue: DEFAULT_OBJ_GEN_DIR});
parser.addArgument(['-l', '--levels'], {type: Number, defaultValue: 5});
parser.addArgument(['-J', '--workers'], {type: Number, defaultValue: 1});
parser.addArgument(['--limit'], {type: Number, defaultValue: 0});
parser.addArgument(['-r', '--rebuild'], {action: 'storeTrue'});
}, getAllFileArgumentsAndRunParallel({
job: 'reconstruct',
inputExt: DATA_PARAM_EXT,
outputExt: OBJ_GEN_EXT,
}, (args) => {
enforceFileExists(args.input);
console.log(`processing ${args.input} => ${args.output}, levels = ${args.levels}`);
require('./src/reconstruct_file')(args);
}));
const args = parser.parseArgs();
// console.dir(args);
commands[args.command](args);
process.exit();
}