machines/tasks/component.ts
2023-11-12 21:43:05 +01:00

140 lines
4.0 KiB
TypeScript

import * as path from 'path'
import { resolve } from '@plastichub/osr-cli-commons/fs'
import { files, forward_slash } from '@plastichub/osr-cli-commons/glob'
import { sync as cp } from '@plastichub/fs/copy'
import { sync as rm } from '@plastichub/fs/remove'
import { sync as exists } from '@plastichub/fs/exists'
import {
componentContentOptions as componentContentOptions
} from './library'
import { logger } from './'
const COMPONENT_TEMPLATE = './osr/bazar/root.html'
const debug = true
const verbose = false
let DEFAULT_CAD_OUTPUTS = '${SRC_DIR}/${SRC_NAME}.+(json|html|step|x_t)'
const HTML_DEFAULT_PATH = 'resources/edrawings.html'
export const productLaserTask = (grunt, product, options: any = {}, laser_tasks) => {
const config = {}
const slug = path.parse(product).base
config[`component-${slug}`] = {
src: [product],
options: {
clear: true,
debug,
verbose
}
}
grunt.extendConfig({
laser: config
})
grunt.registerTask(`component-${slug}`, `laser:component-${slug}`)
laser_tasks.push(`laser:component-${slug}`)
grunt.registerTask(`products-laser`, laser_tasks)
}
export const productCADTask = (grunt, component, options: any = {}, cad_tasks) => {
const config = {}
const slug = path.parse(component).base
component = path.resolve(path.join(process.cwd(), component))
const rel = forward_slash(path.relative(path.resolve('.'), component))
let glob = `${rel}/cad/*Global*.+(SLDASM)`
const filesSolidworks = files(path.resolve('.'), glob)
if (!filesSolidworks.length) {
return
}
config[`component-${slug}`] = {
src: [`${component}/cad/*Global*.+(SLDASM)`],
output: DEFAULT_CAD_OUTPUTS,
options: {
debug,
verbose,
skip:true,
onNode: (data => {
try {
options.debug && logger.info(`Converted ${data.src} to ${data.target}`)
const parts = path.parse(data.target)
if (parts.ext === '.html' && parts.name.toLowerCase().indexOf('global') !== -1) {
const target = path.join(component, HTML_DEFAULT_PATH)
if(exists(target)){
rm(target)
}
cp(data.target, target, {})
}
} catch (error) {
logger.info(`Error copying ${data.src} to ${HTML_DEFAULT_PATH}`, error)
}
})
}
}
grunt.extendConfig({
cad: config
})
grunt.registerTask(`component-${slug}`, `cad:component-${slug}`)
cad_tasks.push(`cad:component-${slug}`)
grunt.registerTask(`components-cad`, cad_tasks)
}
export const productBazarTask = (grunt, component, options: any = {}, cscart_tasks) => {
if (!component) {
logger.error('Invalid component')
}
const config = {}
const slug = path.parse(component).base
config[`component-${slug}`] = {
src: [COMPONENT_TEMPLATE],
options: componentContentOptions(component)
}
grunt.extendConfig({
compile: config
})
grunt.registerTask(`component-${slug}`, `compile:component-${slug}`)
cscart_tasks.push(`compile:component-${slug}`)
grunt.registerTask(`products-bazar`, cscart_tasks)
}
export const productGitSyncTask = (grunt, component, options: any = {}, git_tasks) => {
const config = {}
const opts = {
clean: true,
debug,
verbose,
cwd: '${OSR_ROOT}/osr-machines/',
rel: component.replace('products/', '')
}
const slug = path.parse(component).base
config[`component-${slug}`] = opts
grunt.extendConfig({
git: config
})
grunt.registerTask(`git-component-${slug}`, `git:component-${slug}`)
git_tasks.push(`git:component-${slug}`)
grunt.registerTask(`components-git`, git_tasks)
}