69 lines
2.6 KiB
TypeScript
69 lines
2.6 KiB
TypeScript
import * as pMap from 'p-map'
|
|
import * as path from 'path'
|
|
import { isArray } from '@plastichub/core/primitives'
|
|
import { convert as cadConvert } from '@plastichub/osr-cad/cad/sw-lib'
|
|
import { SolidworkOptions } from '@plastichub/osr-cad'
|
|
import { sanitize } from '@plastichub/osr-cad/sw_argv'
|
|
import { resolve } from '@plastichub/osr-commons'
|
|
|
|
import { logger } from '../src'
|
|
import { option } from '../library'
|
|
|
|
export interface ICadTask {
|
|
src: string | string[]
|
|
output: string
|
|
options: any
|
|
}
|
|
|
|
const create_cad_options = (src: string, dst: string, options: SolidworkOptions) => {
|
|
return { ...options, src, dst }
|
|
}
|
|
const convert = async (src, options, output) => {
|
|
const args = create_cad_options(src, output, options);
|
|
const syncArgs = sanitize({
|
|
...args,
|
|
...options
|
|
})
|
|
return cadConvert(syncArgs)
|
|
}
|
|
const convertFiles = async (items, options, output) => {
|
|
return (pMap as any)(items, async (s) => {
|
|
return convert(s, options, output)
|
|
}, {
|
|
concurrency: 1
|
|
})
|
|
}
|
|
|
|
export const register = (grunt) => {
|
|
grunt.registerMultiTask('cad', 'converts CAD files', function () {
|
|
const done = this.async()
|
|
const task_options = this.data.options || {}
|
|
const options: SolidworkOptions = {
|
|
...this.data.options,
|
|
root: option('root', task_options, grunt, path.resolve('./')),
|
|
debug: option('debug', task_options, grunt),
|
|
verbose: option('verbose', task_options, grunt),
|
|
sw: option('sw', task_options, grunt),
|
|
swv: option('swv', task_options, grunt),
|
|
args: option('args', task_options, grunt),
|
|
dry: option('dry', task_options, grunt),
|
|
onNode: option('onNode', task_options, grunt),
|
|
cache: option('cache', task_options, grunt, true),
|
|
report: option('report', task_options, grunt),
|
|
configuration: option('configuration', task_options, grunt),
|
|
hidden: option('hidden', task_options, grunt),
|
|
quality: option('quality', task_options, grunt),
|
|
renderer: option('renderer', task_options, grunt),
|
|
view: option('view', task_options, grunt),
|
|
close: option('close', task_options, grunt, true),
|
|
logLevel: option('logLevel', task_options, grunt, 'warn')
|
|
}
|
|
let src: string[] = isArray(this.data.src) ? this.data.src : [this.data.src]
|
|
src = src.map(f => (resolve(f)))
|
|
try {
|
|
convertFiles(src, options, this.data.output).then(done)
|
|
} catch (e) {
|
|
logger.error('Error converting files', e)
|
|
}
|
|
})
|
|
} |