UNPKG

@plastichub/osr-cad

Version:

This is a CLI(CommandLineInterface) toolset to convert 3D files, using Solidworks and other software.

189 lines 6.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitize = exports.sanitizeSingle = exports.defaultOptions = void 0; const path = require("path"); const _1 = require("./"); const osr_commons_1 = require("@plastichub/osr-commons"); const read_1 = require("@plastichub/fs/read"); const exists_1 = require("@plastichub/fs/exists"); const _2 = require("./"); const defaultOptions = (yargs) => { return yargs.option('src', { default: './', describe: 'The source directory or source file. Glob patters are supported!', demandOption: true }).option('dst', { describe: 'Destination folder or file' }).option('Report', { describe: 'Optional conversion report. Can be JSON, HTML, CSV or Markdown' }).option('debug', { default: false, describe: 'Enable internal debug messages', type: 'boolean' }).option('alt', { default: false, describe: 'Use alternate tokenizer, & instead of $', type: 'boolean' }).option('skip', { default: true, describe: 'Skip existing files', type: 'boolean' }).option('dry', { default: false, describe: 'Run without conversion', type: 'boolean' }).option('verbose', { default: true, describe: 'Show internal messages', type: 'boolean' }).option('saveArgs', { describe: 'Save command line options to a file', type: 'string' }).option('saveAsProfile', { describe: 'Save command line options to a json file. To be loaded via --profile=file.json', type: 'string' }).option('profile', { describe: 'Load options from a json file', type: 'string' }).option('log', { describe: 'Save Slic3r output to a file', type: 'string' }); }; exports.defaultOptions = defaultOptions; // Sanitizes faulty user argv options for all commands. const sanitizeSingle = (argv) => { const src = path.resolve('' + argv.src); const config = argv.config ? (0, read_1.sync)(path.resolve('' + argv.config), 'json') : {}; const extraVariables = {}; for (const key in config) { if (Object.prototype.hasOwnProperty.call(config, key)) { const element = config[key]; if (typeof element === 'string') { extraVariables[key] = element; } } } const args = { src: src, dst: '' + argv.dst, report: argv.report ? path.resolve(argv.report) : null, debug: argv.debug, verbose: argv.verbose, dry: argv.dry, cache: argv.skip, alt: argv.alt, // glob: argv.glob as string, variables: { ...extraVariables }, args: argv.args || '' }; if (!args.src) { _1.logger.error('Invalid source, abort'); return process.exit(); } args.srcInfo = (0, osr_commons_1.pathInfo)(argv.src); if (!args.srcInfo.FILES) { _1.logger.error(`Invalid source files, abort`); return process.exit(); } for (const key in args.srcInfo) { if (Object.prototype.hasOwnProperty.call(args.srcInfo, key)) { args.variables['SRC_' + key] = args.srcInfo[key]; } } if (argv.dst) { args.dst = path.resolve(args.dst); args.dstInfo = (0, osr_commons_1.pathInfo)(args.dst); args.dstInfo.PATH = path.resolve(argv.dst); for (const key in args.dstInfo) { if (Object.prototype.hasOwnProperty.call(args.dstInfo, key)) { args.variables['DST_' + key] = args.dstInfo[key]; } } } return args; }; exports.sanitizeSingle = sanitizeSingle; const sanitize = (argv) => { const src = path.resolve('' + argv.src); const config = argv.config ? (0, read_1.sync)(path.resolve('' + argv.config), 'json') : {}; const extraVariables = {}; for (const key in config) { if (Object.prototype.hasOwnProperty.call(config, key)) { const element = config[key]; if (typeof element === 'string') { extraVariables[key] = element; } } } let args = { src: src, dst: '' + argv.dst, report: argv.report, debug: argv.debug, verbose: argv.verbose, dry: argv.dry, onNode: argv.onNode, cache: argv.skip, alt: argv.alt, variables: { ...extraVariables }, ...argv }; args.extruderOffset = args.extruderOffset ? args.extruderOffset : { x: 0, y: 0 }; if (!args.src) { _1.logger.error('Invalid source, abort'); return; } args.srcInfo = (0, osr_commons_1.pathInfo)(argv.src); if (!args.srcInfo.FILES) { _1.logger.error(`Invalid source files, abort`, args.srcInfo); return process.exit(); } for (const key in args.srcInfo) { if (Object.prototype.hasOwnProperty.call(args.srcInfo, key)) { args.variables['SRC_' + key] = args.srcInfo[key]; } } if (argv.saveAsProfile) { args.saveAsProfile = path.resolve((0, osr_commons_1.resolve)(args.saveAsProfile, args.alt, args.variables)); } if (argv.saveArgs) { args.saveArgs = path.resolve((0, osr_commons_1.resolve)(args.saveArgs, args.alt, args.variables)); } if (argv.report) { args.report = path.resolve((0, osr_commons_1.resolve)(args.report, args.alt, args.variables)); } if (argv.profile) { args.profile = path.resolve((0, osr_commons_1.resolve)(args.profile, args.alt, args.variables)); } if (argv.dst) { args.dst = path.resolve((0, osr_commons_1.resolve)(args.dst, args.alt, args.variables)); args.dstInfo = (0, osr_commons_1.pathInfo)(args.dst); args.dstInfo.PATH = argv.dst; for (const key in args.dstInfo) { if (Object.prototype.hasOwnProperty.call(args.dstInfo, key)) { args.variables['DST_' + key] = args.dstInfo[key]; } } } if (argv.profile) { args.profile = path.resolve((0, osr_commons_1.resolve)(args.profile, args.alt, args.variables)); if ((0, exists_1.sync)(args.profile)) { const profile = (0, read_1.sync)(args.profile, 'json'); if (profile) { args = { ...args, ...profile }; } } } if (argv.log) { args.log = path.resolve((0, _2.substitute)(args.alt, args.log, args.variables)); } return args; }; exports.sanitize = sanitize; //# sourceMappingURL=slic3r_argv.js.map