131 lines
3.8 KiB
TypeScript
131 lines
3.8 KiB
TypeScript
import * as path from 'path'
|
|
import * as fs from 'fs'
|
|
import { z } from "zod";
|
|
|
|
// Author schema
|
|
export const AuthorSchema = z.object({
|
|
name: z.string(),
|
|
url: z.string(),
|
|
});
|
|
|
|
// Content schema
|
|
export const ContentSchema = z.object({
|
|
body: z.string().optional(),
|
|
features: z.string().optional(),
|
|
highlights: z.string().optional(),
|
|
specs: z.string().optional(),
|
|
license: z.string().optional()
|
|
});
|
|
|
|
// Assets schema
|
|
export const AssetsSchema = z.object({
|
|
gallery: z.array(z.string()).optional(),
|
|
renderings: z.array(z.string()).optional(),
|
|
components: z.array(z.string()).optional()
|
|
});
|
|
|
|
// Production schema
|
|
export const ProductionSchema = z.object({
|
|
"fusion-folder": z.string(),
|
|
"nc-folder": z.string(),
|
|
cam: z.array(AuthorSchema),
|
|
});
|
|
|
|
// Component config schema
|
|
export const ComponentConfigSchema = z.object({
|
|
|
|
// shop
|
|
cart_id: z.string().optional(),
|
|
code: z.string(),
|
|
price: z.number().optional(),
|
|
cscartCats: z.array(z.number()).optional(),
|
|
cscartId: z.number().optional(),
|
|
vendorId: z.number().optional(),
|
|
|
|
//internal
|
|
version: z.string().optional(),
|
|
status: z.string().optional(),
|
|
authors: z.array(AuthorSchema).optional(),
|
|
replaced_by: z.string().optional(),
|
|
alternatives: z.array(z.string()).optional(),
|
|
flags: z.number().optional(),
|
|
|
|
|
|
// public
|
|
download: z.boolean().optional(),
|
|
name: z.string(),
|
|
|
|
edrawings: z.string().optional(),
|
|
showDimensions: z.boolean().optional(),
|
|
showParts: z.boolean().optional(),
|
|
slug: z.string(),
|
|
score: z.number().optional(),
|
|
|
|
Preview3d: z.boolean().optional(),
|
|
keywords: z.string().optional(),
|
|
meta_keywords: z.string().optional(),
|
|
|
|
content: ContentSchema.optional(),
|
|
assets: AssetsSchema.optional(),
|
|
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
howto_categories: z.union([z.string(), z.array(z.string())]).optional(),
|
|
steps: z.any().optional(),
|
|
sourceLanguage: z.string().optional(),
|
|
category: z.string(),
|
|
product_dimensions: z.string().optional(),
|
|
production: ProductionSchema.optional(),
|
|
}).passthrough();
|
|
|
|
export type IComponentConfig = z.infer<typeof ComponentConfigSchema>;
|
|
|
|
import { isInvalidMarketplaceComponent, isValidLibraryComponent, isValidMarketplaceComponent, PFilterInvalid, PFilterValid } from './filter.js'
|
|
import { forward_slash, pathInfoEx } from './fs/_glob.js'
|
|
import { resolve } from './variables.js'
|
|
import { readOSRConfig } from './config.js'
|
|
|
|
const find_items = (nodes: string[], options) => {
|
|
nodes = nodes.filter(options.filter)
|
|
return nodes.map((c) => {
|
|
const root = resolve(options.root, false, {})
|
|
return {
|
|
rel: forward_slash(`${path.relative(root, path.parse(c).dir)}`),
|
|
path: forward_slash(`${options.root}/${path.relative(root, c)}`),
|
|
config: readOSRConfig(c)
|
|
}
|
|
})
|
|
}
|
|
|
|
export const get = (src, root, type) => {
|
|
const srcInfo = pathInfoEx(src, false, {
|
|
absolute: true
|
|
})
|
|
switch (type) {
|
|
case PFilterValid.marketplace_component: {
|
|
const options = {
|
|
filter: isValidMarketplaceComponent,
|
|
root
|
|
}
|
|
return find_items(srcInfo.FILES, options)
|
|
}
|
|
|
|
case PFilterValid.library_component: {
|
|
const options = {
|
|
filter: isValidLibraryComponent,
|
|
root
|
|
}
|
|
return find_items(srcInfo.FILES, options)
|
|
}
|
|
|
|
case PFilterInvalid.marketplace_component: {
|
|
const options = {
|
|
filter: isInvalidMarketplaceComponent,
|
|
root
|
|
}
|
|
return find_items(srcInfo.FILES, options)
|
|
}
|
|
}
|
|
}
|