import { Promise as BPromise } from 'bluebird' import * as path from 'path' import * as fs from 'fs' import * as md5 from 'md5' import { resolve , isFolder } from '@plastichub/osr-cli-commons/fs' import { isArray } from '@plastichub/core/primitives' import { sync as read } from '@plastichub/fs/read' import { sync as exists } from '@plastichub/fs/exists' import { sync } from '@plastichub/osr-sync' import { OSR_CACHE } from '@plastichub/osr-cli-commons' import { MainAssembly, file_path_with_ext_ex, forward_slash, unique_by } from './library' import { logger } from '../src' import { option } from './' const parts_with_laser = (model) => { let parts = [...model.filter((f) => f.IsLaser === "1"), ...model] return parts.filter((f) => { const _path = path.resolve(resolve(f['File Path'])) if (_path) { const dir = path.parse(_path).dir const laser = path.resolve(path.join(dir, 'laser')) if (exists(laser)) { return f } } }) } const createLaserFiles = async (root, product, options, grunt) => { const osr_cache = OSR_CACHE() if (!exists(root)) { options.debug && logger.info("Create Laser Files : Root doesnt exists" + root) } const PRODUCT_ROOT = path.resolve(path.join(root, product)) if (!exists(PRODUCT_ROOT)) { logger.error(`Invalid product root : ${PRODUCT_ROOT}`) return false } const main = MainAssembly(PRODUCT_ROOT, options.filePattern) if (!main) { logger.error("Have no main assembly at " + PRODUCT_ROOT); return false } const modelFilePath = file_path_with_ext_ex(main, 'json') if (!modelFilePath || !exists(modelFilePath)) { logger.error("Have no model at " + modelFilePath) return false } const model = read(modelFilePath, 'json') const laserParts = parts_with_laser(model) let laserDirectories: any[] = [ ...new Set(laserParts.map((f) => { const _path = path.resolve(resolve(f['File Path'])) const dir = path.parse(_path).dir const ret = { target: forward_slash(path.relative(PRODUCT_ROOT, dir)). replace('../components/', ''). replace('components', ''). replace('cad', ''). replace('../', ''), path: dir + '/laser' } return ret })) ] const config: any = read(path.join(PRODUCT_ROOT, 'config.json'), 'json') if(!config){ return [] } laserDirectories = unique_by(laserDirectories, 'path') options.debug && logger.debug('Create laser files for ', laserDirectories) const tasks = await BPromise.resolve(laserDirectories).map((item) => { return BPromise.resolve(['en', 'es']).map((lCode) => { const renamer = (from, to) => { if (isFolder(from)){ return } const fromParts = path.parse('' + from) const toParts = path.parse('' + to) const pCode = config.code || '' const toFileParts = ('' + toParts.name).split('_') const fromFileParts = ('' + fromParts.name).split('_') const thickness = fromFileParts[3] let qty = fromFileParts[2] let material = fromFileParts[4] if (!material) { logger.error('logger.error : invalid material ', from + ' : ' + ' to : ' + to, toFileParts) return to } if (lCode === 'en') { material = material.toLowerCase().replace("ferro", "Steel").replace("inox", "Stainless") } const newName = path.resolve( toParts.dir + '/' + material + '/' + thickness + '/' + pCode + '-' + toFileParts[0] + '_' + toFileParts[1] + '_' + qty + toParts.ext) return newName } return sync({ renamer: renamer, clear: options.clear, source: path.resolve(item.path), target: `${PRODUCT_ROOT}/laser/${lCode}/${item.target}`, debug: options.debug, verbose: options.verbose, profile: options.profile || { "debug": true, "matching": [ "**/*.dxf" ] } }) }, { concurrency: 1 }); }, { concurrency: 1 }) return tasks } const create = async (item, options, grunt) => { return createLaserFiles(options.cwd || '.', item, options, grunt); } export const register = (grunt) => { grunt.registerMultiTask('laser', 'Create public laser files', function () { const done = this.async() const task_options = this.data.options || {}; let src: string[] = isArray(this.data.src) ? this.data.src : [this.data.src] src.forEach((item) => { create(item, { verbose: option('verbose', task_options, grunt, false), clear: option('clear', task_options, grunt, true), debug: option('debug', task_options, grunt, true), filePattern: option('filePattern', task_options, grunt, true), profile: option('profile', task_options, grunt, true), module: option('module', task_options, grunt, true), cwd: option('cwd', task_options, grunt, process.cwd()) }, grunt).then(done) }) }) }