@cyberskill/shared 3.6.0 → 3.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/vitest/vitest.e2e.js +1 -1
- package/dist/config/vitest/vitest.e2e.js.map +1 -1
- package/dist/config/vitest/vitest.unit.js +1 -1
- package/dist/config/vitest/vitest.unit.js.map +1 -1
- package/dist/constant/common.d.ts +3 -2
- package/dist/constant/common.js +1 -1
- package/dist/constant/common.js.map +1 -1
- package/dist/node/apollo-server/apollo-server.util.js +15 -13
- package/dist/node/apollo-server/apollo-server.util.js.map +1 -1
- package/dist/node/command/command.util.js +29 -29
- package/dist/node/command/command.util.js.map +1 -1
- package/dist/node/express/express.util.d.ts +4 -0
- package/dist/node/express/express.util.js +7 -6
- package/dist/node/express/express.util.js.map +1 -1
- package/dist/node/mongo/mongo.controller.mongoose.d.ts +6 -1
- package/dist/node/mongo/mongo.controller.mongoose.js +10 -11
- package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
- package/dist/node/mongo/mongo.controller.native.d.ts +1 -1
- package/dist/node/mongo/mongo.controller.native.js +5 -4
- package/dist/node/mongo/mongo.controller.native.js.map +1 -1
- package/dist/node/mongo/mongo.controller.type.d.ts +15 -0
- package/dist/node/mongo/mongo.dynamic-populate.js +12 -12
- package/dist/node/mongo/mongo.dynamic-populate.js.map +1 -1
- package/dist/node/mongo/mongo.util.d.ts +3 -3
- package/dist/node/mongo/mongo.util.js.map +1 -1
- package/dist/node/path/index.js +2 -2
- package/dist/node/path/path.constant.d.ts +21 -7
- package/dist/node/path/path.constant.js +48 -29
- package/dist/node/path/path.constant.js.map +1 -1
- package/dist/node/storage/storage.util.js +2 -6
- package/dist/node/storage/storage.util.js.map +1 -1
- package/dist/node/ws/ws.util.js +2 -0
- package/dist/node/ws/ws.util.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.util.js +42 -41
- package/dist/react/apollo-client/apollo-client.util.js.map +1 -1
- package/dist/react/log/log.util.d.ts +5 -0
- package/dist/react/log/log.util.js.map +1 -1
- package/dist/typescript/common.type.d.ts +18 -0
- package/dist/typescript/common.type.js +9 -2
- package/dist/typescript/common.type.js.map +1 -1
- package/dist/typescript/index.js +2 -2
- package/dist/util/object/object.util.js +43 -37
- package/dist/util/object/object.util.js.map +1 -1
- package/dist/util/serializer/serializer.util.d.ts +13 -0
- package/dist/util/serializer/serializer.util.js.map +1 -1
- package/dist/util/string/string.util.js +45 -13
- package/dist/util/string/string.util.js.map +1 -1
- package/package.json +7 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.util.js","names":[],"sources":["../../../src/node/mongo/mongo.util.ts"],"sourcesContent":["import type migrate from 'migrate-mongo';\nimport type mongooseRaw from 'mongoose';\n\nimport aggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\nimport { randomUUID } from 'node:crypto';\n\nimport { getNestedValue, regexSearchMapper, setNestedValue } from '#util/index.js';\nimport { validate } from '#util/validate/index.js';\n\nimport type { MongoController } from './mongo.controller.js';\nimport type { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualConfig, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_MongoosePlugin, T_MongooseSchema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';\n\nimport { addGitIgnoreEntry, writeFileSync } from '../fs/index.js';\nimport { MIGRATE_MONGO_CONFIG, PATH } from '../path/index.js';\n\n/**\n * Converts enum values to proper model names.\n * Handles common naming conventions like converting 'USER' to 'User'.\n *\n * @param enumValue - The enum value to convert\n * @returns The converted model name\n */\nexport function convertEnumToModelName(enumValue: string): string {\n if (enumValue === enumValue.toUpperCase()) {\n return enumValue.charAt(0).toUpperCase() + enumValue.slice(1).toLowerCase();\n }\n\n return enumValue;\n}\n\n/**\n * Interface for the MongoDB utility object to enable explicit type annotation.\n * Required to avoid TS7056 (inferred type exceeds maximum serialization length).\n */\ninterface I_MongoUtils {\n createGenericFields: () => I_GenericDocument;\n applyPlugins: <T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) => void;\n applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseSchema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;\n createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseSchema<I_GenericDocument>;\n createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseSchema<T>;\n createModel: <T extends Partial<C_Document>, R extends string = string>(options: I_CreateModelOptions<T, R>) => I_ExtendedModel<T>;\n validator: {\n isRequired: <T>() => (this: T, value: unknown) => Promise<boolean>;\n isUnique: <T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) => (this: T, value: unknown) => Promise<boolean>;\n matchesRegex: (regexArray: RegExp[]) => (value: string) => Promise<boolean>;\n };\n migrate: {\n getModule: () => Promise<typeof migrate>;\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => void;\n };\n regexify: <T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]) => T_QueryFilter<T>;\n isDynamicVirtual: <T, R extends string>(options?: T_VirtualOptions<T, R>) => options is I_DynamicVirtualOptions<T, R>;\n getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: Record<string, unknown>) => Promise<T[]>;\n getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: Record<string, unknown>) => Promise<T_WithId<T>[]>;\n}\n\n/**\n * MongoDB utility object providing comprehensive database operations and utilities.\n * This object contains methods for creating generic fields, applying plugins and middlewares,\n * creating schemas and models, validation functions, migration utilities, and regex filtering.\n */\nexport const mongo: I_MongoUtils = {\n /**\n * Creates generic fields that are commonly used across MongoDB documents.\n * This function generates standard fields including a UUID, deletion flag, and timestamps\n * that can be applied to any document schema.\n *\n * @returns An object containing generic document fields (id, isDel, createdAt, updatedAt).\n */\n createGenericFields(): I_GenericDocument {\n return {\n id: randomUUID(),\n isDel: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n },\n /**\n * Applies plugins to a Mongoose schema.\n * This function filters out falsy plugins and applies the remaining valid plugins\n * to the provided schema.\n *\n * @param schema - The Mongoose schema to apply plugins to.\n * @param plugins - An array of plugin functions or false values to filter and apply.\n */\n applyPlugins<T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) {\n plugins\n .filter((plugin): plugin is T_MongoosePlugin => typeof plugin === 'function')\n .forEach(plugin => schema.plugin(plugin));\n },\n /**\n * Applies middleware functions to a Mongoose schema.\n * This function configures pre and post middleware for specified methods on the schema.\n *\n * @param schema - The Mongoose schema to apply middleware to.\n * @param middlewares - An array of middleware configurations with method, pre, and post functions.\n */\n applyMiddlewares<T extends Partial<C_Document>>(\n schema: T_MongooseSchema<T>,\n middlewares: I_MongooseModelMiddleware<T>[],\n ) {\n middlewares.forEach(({ method, pre, post }) => {\n if (method && pre) {\n schema.pre(method as RegExp, pre);\n }\n\n if (method && post) {\n schema.post(method as RegExp, post);\n }\n });\n },\n /**\n * Creates a generic Mongoose schema with common fields.\n * This function creates a base schema with UUID field and deletion flag,\n * configured with automatic timestamps.\n *\n * @param mongoose - The Mongoose instance to create the schema with.\n * @returns A Mongoose schema with generic document fields.\n */\n createGenericSchema(mongoose: typeof mongooseRaw) {\n return new mongoose.Schema<I_GenericDocument>(\n {\n id: { type: String, default: () => randomUUID(), unique: true },\n isDel: { type: Boolean, default: false },\n },\n { timestamps: true },\n );\n },\n /**\n * Creates a Mongoose schema with optional virtual fields and generic fields.\n * This function creates a new Mongoose schema from the provided schema definition,\n * optionally adds virtual fields, and includes generic fields (unless standalone is true).\n *\n * @param options - Configuration options including mongoose instance, schema definition, virtuals, and standalone flag.\n * @param options.mongoose - The Mongoose instance to use for schema creation.\n * @param options.schema - The schema definition object.\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.standalone - Whether to exclude generic fields (default: false).\n * @returns A configured Mongoose schema.\n */\n createSchema<T, R extends string = string>({\n mongoose,\n schema,\n virtuals = [],\n standalone = false,\n }: I_CreateSchemaOptions<T, R>): T_MongooseSchema<T> {\n const createdSchema = new mongoose.Schema<T>(schema, {\n toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals\n toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals\n });\n\n virtuals.forEach(({ name, options, get }) => {\n if (mongo.isDynamicVirtual<T, R>(options)) {\n const schemaStatics = createdSchema.statics as Record<string, unknown>;\n\n if (!schemaStatics['_dynamicVirtuals']) {\n schemaStatics['_dynamicVirtuals'] = [];\n }\n\n (schemaStatics['_dynamicVirtuals'] as I_DynamicVirtualConfig<T>[]).push({\n name: name as string,\n options,\n });\n\n const virtualInstance = createdSchema.virtual(name as string);\n\n if (get) {\n virtualInstance.get(get);\n }\n else {\n virtualInstance.get(function (this: T & { _populated?: { [key: string]: unknown } }) {\n return this._populated?.[name as string] || (options?.count ? 0 : (options?.justOne ? null : []));\n });\n }\n }\n else {\n const virtualInstance = createdSchema.virtual(name as string, options);\n\n if (get) {\n virtualInstance.get(get);\n }\n }\n });\n\n if (!standalone) {\n createdSchema.add(mongo.createGenericSchema(mongoose));\n }\n\n return createdSchema;\n },\n /**\n * Creates a Mongoose model with plugins, middleware, and pagination support.\n * This function creates a model from a schema with optional pagination and aggregation plugins,\n * and applies any specified middleware. If a model with the same name already exists, it returns the existing model.\n *\n * @param options - Configuration options including mongoose instance, model name, schema, and feature flags.\n * @param options.mongoose - The Mongoose instance to use for model creation.\n * @param options.name - The name of the model to create.\n * @param options.schema - The schema definition for the model.\n * @param options.pagination - Whether to enable pagination plugin (default: false).\n * @param options.aggregate - Whether to enable aggregation pagination plugin (default: false).\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.middlewares - Optional array of middleware configurations.\n * @returns A configured Mongoose model with extended functionality.\n * @throws {Error} When the model name is not provided.\n */\n createModel<T extends Partial<C_Document>, R extends string = string>({\n mongoose: currentMongooseInstance,\n name,\n schema,\n virtuals = [],\n pagination = true,\n aggregate = true,\n middlewares = [],\n }: I_CreateModelOptions<T, R>): I_ExtendedModel<T> {\n if (!name) {\n throw new Error('Model name is required.');\n }\n\n if (currentMongooseInstance.models[name]) {\n return currentMongooseInstance.models[name] as I_ExtendedModel<T>;\n }\n\n const createdSchema = mongo.createSchema({ mongoose: currentMongooseInstance, schema, virtuals });\n\n if (pagination || aggregate) {\n mongo.applyPlugins<T>(createdSchema, [\n pagination && mongoosePaginate,\n aggregate && aggregatePaginate,\n ]);\n }\n\n mongo.applyMiddlewares<T>(createdSchema, middlewares);\n\n const model = currentMongooseInstance.model<T>(name, createdSchema) as I_ExtendedModel<T>;\n\n if (virtuals.length > 0) {\n (model as any)._virtualConfigs = virtuals;\n }\n\n return model;\n },\n /**\n * Validation utilities for Mongoose schemas.\n * This object provides common validation functions that can be used in Mongoose schema definitions.\n */\n validator: {\n /**\n * Creates a required field validator.\n * This function returns a validator that checks if a field value is not empty\n * using the validate.isEmpty utility.\n *\n * @returns A validation function that returns true if the field is not empty.\n */\n isRequired<T>(): (this: T, value: unknown) => Promise<boolean> {\n return async function (this: T, value: unknown): Promise<boolean> {\n return !validate.isEmpty(value);\n };\n },\n /**\n * Creates a unique field validator.\n * This function returns a validator that checks if a field value is unique\n * across the specified fields in the collection.\n *\n * @param fields - An array of field names to check for uniqueness.\n * @returns A validation function that returns true if the value is unique across the specified fields.\n * @throws {Error} When fields is not a non-empty array of strings.\n */\n isUnique<T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) {\n return async function (this: T, value: unknown): Promise<boolean> {\n if (!Array.isArray(fields) || fields.length === 0) {\n throw new Error('Fields must be a non-empty array of strings.');\n }\n\n const query = { $or: fields.map(field => ({ [field]: value })) };\n const existingDocument = await this.constructor.exists(query);\n\n return !existingDocument;\n };\n },\n /**\n * Creates a regex pattern validator.\n * This function returns a validator that checks if a string value matches\n * all provided regular expressions.\n *\n * @param regexArray - An array of regular expressions to test against the value.\n * @returns A validation function that returns true if the value matches all regex patterns.\n * @throws {Error} When regexArray is not an array of valid RegExp objects.\n */\n matchesRegex(regexArray: RegExp[]): (value: string) => Promise<boolean> {\n return async (value: string): Promise<boolean> => {\n if (!Array.isArray(regexArray) || regexArray.some(r => !(r instanceof RegExp))) {\n throw new Error('regexArray must be an array of valid RegExp objects.');\n }\n\n return regexArray.every(regex => regex.test(value));\n };\n },\n },\n /**\n * Migration utilities for MongoDB.\n * This object extends the migrate-mongo library with additional configuration utilities.\n */\n migrate: {\n /**\n * Lazily loads the migrate-mongo module to avoid eager import overhead.\n * Use this to access migrate-mongo methods (up, down, status, create) programmatically.\n *\n * @returns A promise resolving to the migrate-mongo module.\n */\n async getModule(): Promise<typeof migrate> {\n return (await import('migrate-mongo')).default;\n },\n /**\n * Sets the migration configuration and updates .gitignore.\n * This function creates a migration configuration file and ensures it's properly\n * excluded from version control.\n *\n * @param options - Migration configuration options to write to the config file.\n */\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => {\n const optionsJS = `// This file is automatically generated by the Cyberskill CLI.\\nmodule.exports = ${JSON.stringify(options, null, 4)}`;\n\n writeFileSync(PATH.MIGRATE_MONGO_CONFIG, optionsJS);\n\n addGitIgnoreEntry(PATH.GIT_IGNORE, MIGRATE_MONGO_CONFIG);\n },\n },\n /**\n * Converts string values in a filter to regex patterns for case-insensitive search.\n * This function recursively processes a filter object and converts string values in specified fields\n * to MongoDB regex patterns that support accented character matching.\n *\n * @param filter - The filter object to process.\n * @param fields - An array of field names to convert to regex patterns.\n * @returns A new filter object with string values converted to regex patterns.\n */\n regexify<T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]): T_QueryFilter<T> {\n if (!filter) {\n return {} as T_QueryFilter<T>;\n }\n\n let newFilter = { ...filter };\n\n if (!fields || fields.length === 0) {\n return newFilter;\n }\n\n const MAX_REGEX_INPUT_LENGTH = 200;\n\n for (const field of fields) {\n const path = field.toString().split('.');\n const value = getNestedValue(newFilter, path);\n\n if (typeof value === 'string' && value.length > 0 && value.length <= MAX_REGEX_INPUT_LENGTH) {\n const regexValue = {\n $regex: `.*${regexSearchMapper(value)}.*`,\n $options: 'i',\n };\n\n newFilter = setNestedValue(newFilter, path, regexValue);\n }\n }\n\n return newFilter;\n },\n /**\n * Checks if a virtual options object has a dynamic ref function.\n *\n * @param options - The virtual options to check.\n * @returns True if the options contain a dynamic ref function.\n */\n isDynamicVirtual<T, R extends string = string>(options?: T_VirtualOptions<T, R>): options is I_DynamicVirtualOptions<T, R> {\n return Boolean(options && typeof options.ref === 'function');\n },\n\n /**\n * Generic utility function to get new records from the database\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record already exists\n * @returns Array of records that don't exist in the database\n */\n async getNewRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: Record<string, unknown> = {},\n ): Promise<T[]> {\n const existingRecords = await controller.findAll(filter as any);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const filteredRecords = recordsToCheck.filter(newRecord =>\n !existingRecords.result.some((existingRecord: T_WithId<T>) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return filteredRecords;\n },\n\n /**\n * Generic utility function to get existing records that match the filter criteria\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record exists\n * @returns Array of existing records that match the filter criteria\n * @throws {Error} When the database query fails — prevents silent data inconsistency.\n */\n async getExistingRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: Record<string, unknown> = {},\n ): Promise<T_WithId<T>[]> {\n const existingRecords = await controller.findAll(filter as any);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const foundRecords = existingRecords.result.filter((existingRecord: T_WithId<T>) =>\n recordsToCheck.some((newRecord: T) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return foundRecords;\n },\n};\n\nexport { applyNestedPopulate } from './mongo.populate.js';\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,EAAuB,GAA2B;AAK9D,QAJI,MAAc,EAAU,aAAa,GAC9B,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,CAAC,aAAa,GAGxE;;AAkCX,IAAa,IAAsB;CAQ/B,sBAAyC;AACrC,SAAO;GACH,IAAI,GAAY;GAChB,OAAO;GACP,2BAAW,IAAI,MAAM;GACrB,2BAAW,IAAI,MAAM;GACxB;;CAUL,aAAgB,GAA6B,GAA0C;AACnF,IACK,QAAQ,MAAuC,OAAO,KAAW,WAAW,CAC5E,SAAQ,MAAU,EAAO,OAAO,EAAO,CAAC;;CASjD,iBACI,GACA,GACF;AACE,IAAY,SAAS,EAAE,WAAQ,QAAK,cAAW;AAK3C,GAJI,KAAU,KACV,EAAO,IAAI,GAAkB,EAAI,EAGjC,KAAU,KACV,EAAO,KAAK,GAAkB,EAAK;IAEzC;;CAUN,oBAAoB,GAA8B;AAC9C,SAAO,IAAI,EAAS,OAChB;GACI,IAAI;IAAE,MAAM;IAAQ,eAAe,GAAY;IAAE,QAAQ;IAAM;GAC/D,OAAO;IAAE,MAAM;IAAS,SAAS;IAAO;GAC3C,EACD,EAAE,YAAY,IAAM,CACvB;;CAcL,aAA2C,EACvC,aACA,WACA,cAAW,EAAE,EACb,gBAAa,MACoC;EACjD,IAAM,IAAgB,IAAI,EAAS,OAAU,GAAQ;GACjD,QAAQ,EAAE,UAAU,IAAM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B,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AASnF,SANwB,EAAe,QAAO,MAC1C,CAAC,EAAgB,OAAO,MAAM,MAC1B,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAaL,MAAM,mBACF,GACA,GACA,GACA,IAAkC,EAAE,EACd;EACtB,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAc;AAE/D,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AASnF,SANqB,EAAgB,OAAO,QAAQ,MAChD,EAAe,MAAM,MACjB,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAIR"}
|
|
1
|
+
{"version":3,"file":"mongo.util.js","names":[],"sources":["../../../src/node/mongo/mongo.util.ts"],"sourcesContent":["import type migrate from 'migrate-mongo';\nimport type mongooseRaw from 'mongoose';\n\nimport aggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\nimport { randomUUID } from 'node:crypto';\n\nimport { getNestedValue, regexSearchMapper, setNestedValue } from '#util/index.js';\nimport { validate } from '#util/validate/index.js';\n\nimport type { MongoController } from './mongo.controller.js';\nimport type { C_Document, I_CreateModelOptions, I_CreateSchemaOptions, I_DynamicVirtualConfig, I_DynamicVirtualOptions, I_ExtendedModel, I_GenericDocument, I_MongooseModelMiddleware, T_Filter, T_MongoosePlugin, T_MongooseSchema, T_QueryFilter, T_VirtualOptions, T_WithId } from './mongo.type.js';\n\nimport { addGitIgnoreEntry, writeFileSync } from '../fs/index.js';\nimport { MIGRATE_MONGO_CONFIG, PATH } from '../path/index.js';\n\n/**\n * Converts enum values to proper model names.\n * Handles common naming conventions like converting 'USER' to 'User'.\n *\n * @param enumValue - The enum value to convert\n * @returns The converted model name\n */\nexport function convertEnumToModelName(enumValue: string): string {\n if (enumValue === enumValue.toUpperCase()) {\n return enumValue.charAt(0).toUpperCase() + enumValue.slice(1).toLowerCase();\n }\n\n return enumValue;\n}\n\n/**\n * Interface for the MongoDB utility object to enable explicit type annotation.\n * Required to avoid TS7056 (inferred type exceeds maximum serialization length).\n */\ninterface I_MongoUtils {\n createGenericFields: () => I_GenericDocument;\n applyPlugins: <T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) => void;\n applyMiddlewares: <T extends Partial<C_Document>>(schema: T_MongooseSchema<T>, middlewares: I_MongooseModelMiddleware<T>[]) => void;\n createGenericSchema: (mongoose: typeof mongooseRaw) => T_MongooseSchema<I_GenericDocument>;\n createSchema: <T, R extends string = string>(options: I_CreateSchemaOptions<T, R>) => T_MongooseSchema<T>;\n createModel: <T extends Partial<C_Document>, R extends string = string>(options: I_CreateModelOptions<T, R>) => I_ExtendedModel<T>;\n validator: {\n isRequired: <T>() => (this: T, value: unknown) => Promise<boolean>;\n isUnique: <T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) => (this: T, value: unknown) => Promise<boolean>;\n matchesRegex: (regexArray: RegExp[]) => (value: string) => Promise<boolean>;\n };\n migrate: {\n getModule: () => Promise<typeof migrate>;\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => void;\n };\n regexify: <T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]) => T_QueryFilter<T>;\n isDynamicVirtual: <T, R extends string>(options?: T_VirtualOptions<T, R>) => options is I_DynamicVirtualOptions<T, R>;\n getNewRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T[]>;\n getExistingRecords: <T extends I_GenericDocument>(controller: MongoController<T>, recordsToCheck: T[], filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean, filter?: T_Filter<T>) => Promise<T_WithId<T>[]>;\n}\n\n/**\n * MongoDB utility object providing comprehensive database operations and utilities.\n * This object contains methods for creating generic fields, applying plugins and middlewares,\n * creating schemas and models, validation functions, migration utilities, and regex filtering.\n */\nexport const mongo: I_MongoUtils = {\n /**\n * Creates generic fields that are commonly used across MongoDB documents.\n * This function generates standard fields including a UUID, deletion flag, and timestamps\n * that can be applied to any document schema.\n *\n * @returns An object containing generic document fields (id, isDel, createdAt, updatedAt).\n */\n createGenericFields(): I_GenericDocument {\n return {\n id: randomUUID(),\n isDel: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n },\n /**\n * Applies plugins to a Mongoose schema.\n * This function filters out falsy plugins and applies the remaining valid plugins\n * to the provided schema.\n *\n * @param schema - The Mongoose schema to apply plugins to.\n * @param plugins - An array of plugin functions or false values to filter and apply.\n */\n applyPlugins<T>(schema: T_MongooseSchema<T>, plugins: Array<T_MongoosePlugin | false>) {\n plugins\n .filter((plugin): plugin is T_MongoosePlugin => typeof plugin === 'function')\n .forEach(plugin => schema.plugin(plugin));\n },\n /**\n * Applies middleware functions to a Mongoose schema.\n * This function configures pre and post middleware for specified methods on the schema.\n *\n * @param schema - The Mongoose schema to apply middleware to.\n * @param middlewares - An array of middleware configurations with method, pre, and post functions.\n */\n applyMiddlewares<T extends Partial<C_Document>>(\n schema: T_MongooseSchema<T>,\n middlewares: I_MongooseModelMiddleware<T>[],\n ) {\n middlewares.forEach(({ method, pre, post }) => {\n if (method && pre) {\n schema.pre(method as RegExp, pre);\n }\n\n if (method && post) {\n schema.post(method as RegExp, post);\n }\n });\n },\n /**\n * Creates a generic Mongoose schema with common fields.\n * This function creates a base schema with UUID field and deletion flag,\n * configured with automatic timestamps.\n *\n * @param mongoose - The Mongoose instance to create the schema with.\n * @returns A Mongoose schema with generic document fields.\n */\n createGenericSchema(mongoose: typeof mongooseRaw) {\n return new mongoose.Schema<I_GenericDocument>(\n {\n id: { type: String, default: () => randomUUID(), unique: true },\n isDel: { type: Boolean, default: false },\n },\n { timestamps: true },\n );\n },\n /**\n * Creates a Mongoose schema with optional virtual fields and generic fields.\n * This function creates a new Mongoose schema from the provided schema definition,\n * optionally adds virtual fields, and includes generic fields (unless standalone is true).\n *\n * @param options - Configuration options including mongoose instance, schema definition, virtuals, and standalone flag.\n * @param options.mongoose - The Mongoose instance to use for schema creation.\n * @param options.schema - The schema definition object.\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.standalone - Whether to exclude generic fields (default: false).\n * @returns A configured Mongoose schema.\n */\n createSchema<T, R extends string = string>({\n mongoose,\n schema,\n virtuals = [],\n standalone = false,\n }: I_CreateSchemaOptions<T, R>): T_MongooseSchema<T> {\n const createdSchema = new mongoose.Schema<T>(schema, {\n toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals\n toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals\n });\n\n virtuals.forEach(({ name, options, get }) => {\n if (mongo.isDynamicVirtual<T, R>(options)) {\n const schemaStatics = createdSchema.statics as Record<string, unknown>;\n\n if (!schemaStatics['_dynamicVirtuals']) {\n schemaStatics['_dynamicVirtuals'] = [];\n }\n\n (schemaStatics['_dynamicVirtuals'] as I_DynamicVirtualConfig<T>[]).push({\n name: name as string,\n options,\n });\n\n const virtualInstance = createdSchema.virtual(name as string);\n\n if (get) {\n virtualInstance.get(get);\n }\n else {\n virtualInstance.get(function (this: T & { _populated?: { [key: string]: unknown } }) {\n return this._populated?.[name as string] || (options?.count ? 0 : (options?.justOne ? null : []));\n });\n }\n }\n else {\n const virtualInstance = createdSchema.virtual(name as string, options);\n\n if (get) {\n virtualInstance.get(get);\n }\n }\n });\n\n if (!standalone) {\n createdSchema.add(mongo.createGenericSchema(mongoose));\n }\n\n return createdSchema;\n },\n /**\n * Creates a Mongoose model with plugins, middleware, and pagination support.\n * This function creates a model from a schema with optional pagination and aggregation plugins,\n * and applies any specified middleware. If a model with the same name already exists, it returns the existing model.\n *\n * @param options - Configuration options including mongoose instance, model name, schema, and feature flags.\n * @param options.mongoose - The Mongoose instance to use for model creation.\n * @param options.name - The name of the model to create.\n * @param options.schema - The schema definition for the model.\n * @param options.pagination - Whether to enable pagination plugin (default: false).\n * @param options.aggregate - Whether to enable aggregation pagination plugin (default: false).\n * @param options.virtuals - Optional array of virtual field configurations.\n * @param options.middlewares - Optional array of middleware configurations.\n * @returns A configured Mongoose model with extended functionality.\n * @throws {Error} When the model name is not provided.\n */\n createModel<T extends Partial<C_Document>, R extends string = string>({\n mongoose: currentMongooseInstance,\n name,\n schema,\n virtuals = [],\n pagination = true,\n aggregate = true,\n middlewares = [],\n }: I_CreateModelOptions<T, R>): I_ExtendedModel<T> {\n if (!name) {\n throw new Error('Model name is required.');\n }\n\n if (currentMongooseInstance.models[name]) {\n return currentMongooseInstance.models[name] as I_ExtendedModel<T>;\n }\n\n const createdSchema = mongo.createSchema({ mongoose: currentMongooseInstance, schema, virtuals });\n\n if (pagination || aggregate) {\n mongo.applyPlugins<T>(createdSchema, [\n pagination && mongoosePaginate,\n aggregate && aggregatePaginate,\n ]);\n }\n\n mongo.applyMiddlewares<T>(createdSchema, middlewares);\n\n const model = currentMongooseInstance.model<T>(name, createdSchema) as I_ExtendedModel<T>;\n\n if (virtuals.length > 0) {\n // Mongoose Model has no typed _virtualConfigs; used by MongooseController.getDynamicVirtuals()\n (model as any)._virtualConfigs = virtuals;\n }\n\n return model;\n },\n /**\n * Validation utilities for Mongoose schemas.\n * This object provides common validation functions that can be used in Mongoose schema definitions.\n */\n validator: {\n /**\n * Creates a required field validator.\n * This function returns a validator that checks if a field value is not empty\n * using the validate.isEmpty utility.\n *\n * @returns A validation function that returns true if the field is not empty.\n */\n isRequired<T>(): (this: T, value: unknown) => Promise<boolean> {\n return async function (this: T, value: unknown): Promise<boolean> {\n return !validate.isEmpty(value);\n };\n },\n /**\n * Creates a unique field validator.\n * This function returns a validator that checks if a field value is unique\n * across the specified fields in the collection.\n *\n * @param fields - An array of field names to check for uniqueness.\n * @returns A validation function that returns true if the value is unique across the specified fields.\n * @throws {Error} When fields is not a non-empty array of strings.\n */\n isUnique<T extends { constructor: { exists: (query: { [key: string]: unknown }) => Promise<unknown> } }>(fields: string[]) {\n return async function (this: T, value: unknown): Promise<boolean> {\n if (!Array.isArray(fields) || fields.length === 0) {\n throw new Error('Fields must be a non-empty array of strings.');\n }\n\n const query = { $or: fields.map(field => ({ [field]: value })) };\n const existingDocument = await this.constructor.exists(query);\n\n return !existingDocument;\n };\n },\n /**\n * Creates a regex pattern validator.\n * This function returns a validator that checks if a string value matches\n * all provided regular expressions.\n *\n * @param regexArray - An array of regular expressions to test against the value.\n * @returns A validation function that returns true if the value matches all regex patterns.\n * @throws {Error} When regexArray is not an array of valid RegExp objects.\n */\n matchesRegex(regexArray: RegExp[]): (value: string) => Promise<boolean> {\n return async (value: string): Promise<boolean> => {\n if (!Array.isArray(regexArray) || regexArray.some(r => !(r instanceof RegExp))) {\n throw new Error('regexArray must be an array of valid RegExp objects.');\n }\n\n return regexArray.every(regex => regex.test(value));\n };\n },\n },\n /**\n * Migration utilities for MongoDB.\n * This object extends the migrate-mongo library with additional configuration utilities.\n */\n migrate: {\n /**\n * Lazily loads the migrate-mongo module to avoid eager import overhead.\n * Use this to access migrate-mongo methods (up, down, status, create) programmatically.\n *\n * @returns A promise resolving to the migrate-mongo module.\n */\n async getModule(): Promise<typeof migrate> {\n return (await import('migrate-mongo')).default;\n },\n /**\n * Sets the migration configuration and updates .gitignore.\n * This function creates a migration configuration file and ensures it's properly\n * excluded from version control.\n *\n * @param options - Migration configuration options to write to the config file.\n */\n setConfig: (options: Partial<migrate.config.Config> & { moduleSystem?: 'commonjs' | 'esm' }) => {\n const optionsJS = `// This file is automatically generated by the Cyberskill CLI.\\nmodule.exports = ${JSON.stringify(options, null, 4)}`;\n\n writeFileSync(PATH.MIGRATE_MONGO_CONFIG, optionsJS);\n\n addGitIgnoreEntry(PATH.GIT_IGNORE, MIGRATE_MONGO_CONFIG);\n },\n },\n /**\n * Converts string values in a filter to regex patterns for case-insensitive search.\n * This function recursively processes a filter object and converts string values in specified fields\n * to MongoDB regex patterns that support accented character matching.\n *\n * @remarks\n * **Performance guard:** Input strings are capped at 200 characters to mitigate ReDoS risk.\n * The generated regex patterns include accented character alternation groups (e.g., `(a|à|á|...)`)\n * which can be polynomially complex for very long inputs. For production search on large collections,\n * consider using MongoDB `$text` search indexes instead of `$regex` for better performance.\n *\n * @param filter - The filter object to process.\n * @param fields - An array of field names to convert to regex patterns.\n * @returns A new filter object with string values converted to regex patterns.\n */\n regexify<T>(filter?: T_QueryFilter<T>, fields?: (keyof T | string)[]): T_QueryFilter<T> {\n if (!filter) {\n return {} as T_QueryFilter<T>;\n }\n\n let newFilter = { ...filter };\n\n if (!fields || fields.length === 0) {\n return newFilter;\n }\n\n const MAX_REGEX_INPUT_LENGTH = 200;\n\n for (const field of fields) {\n const path = field.toString().split('.');\n const value = getNestedValue(newFilter, path);\n\n if (typeof value === 'string' && value.length > 0 && value.length <= MAX_REGEX_INPUT_LENGTH) {\n const regexValue = {\n $regex: `.*${regexSearchMapper(value)}.*`,\n $options: 'i',\n };\n\n newFilter = setNestedValue(newFilter, path, regexValue);\n }\n }\n\n return newFilter;\n },\n /**\n * Checks if a virtual options object has a dynamic ref function.\n *\n * @param options - The virtual options to check.\n * @returns True if the options contain a dynamic ref function.\n */\n isDynamicVirtual<T, R extends string = string>(options?: T_VirtualOptions<T, R>): options is I_DynamicVirtualOptions<T, R> {\n return Boolean(options && typeof options.ref === 'function');\n },\n\n /**\n * Generic utility function to get new records from the database\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record already exists\n * @returns Array of records that don't exist in the database\n */\n async getNewRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const filteredRecords = recordsToCheck.filter(newRecord =>\n !existingRecords.result.some((existingRecord: T_WithId<T>) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return filteredRecords;\n },\n\n /**\n * Generic utility function to get existing records that match the filter criteria\n * @param controller - MongoController instance\n * @param recordsToCheck - Array of records to check\n * @param filterFn - Function to determine if a record exists\n * @returns Array of existing records that match the filter criteria\n * @throws {Error} When the database query fails — prevents silent data inconsistency.\n */\n async getExistingRecords<T extends I_GenericDocument>(\n controller: MongoController<T>,\n recordsToCheck: T[],\n filterFn: (existingRecord: T_WithId<T>, newRecord: T) => boolean,\n filter: T_Filter<T> = {} as T_Filter<T>,\n ): Promise<T_WithId<T>[]> {\n const existingRecords = await controller.findAll(filter);\n\n if (!existingRecords.success) {\n throw new Error(`Failed to query existing records: ${existingRecords.message}`);\n }\n\n const foundRecords = existingRecords.result.filter((existingRecord: T_WithId<T>) =>\n recordsToCheck.some((newRecord: T) =>\n filterFn(existingRecord, newRecord),\n ),\n );\n\n return foundRecords;\n },\n};\n\nexport { applyNestedPopulate } from './mongo.populate.js';\n"],"mappings":";;;;;;;;;;AAuBA,SAAgB,EAAuB,GAA2B;AAK9D,QAJI,MAAc,EAAU,aAAa,GAC9B,EAAU,OAAO,EAAE,CAAC,aAAa,GAAG,EAAU,MAAM,EAAE,CAAC,aAAa,GAGxE;;AAkCX,IAAa,IAAsB;CAQ/B,sBAAyC;AACrC,SAAO;GACH,IAAI,GAAY;GAChB,OAAO;GACP,2BAAW,IAAI,MAAM;GACrB,2BAAW,IAAI,MAAM;GACxB;;CAUL,aAAgB,GAA6B,GAA0C;AACnF,IACK,QAAQ,MAAuC,OAAO,KAAW,WAAW,CAC5E,SAAQ,MAAU,EAAO,OAAO,EAAO,CAAC;;CASjD,iBACI,GACA,GACF;AACE,IAAY,SAAS,EAAE,WAAQ,QAAK,cAAW;AAK3C,GAJI,KAAU,KACV,EAAO,IAAI,GAAkB,EAAI,EAGjC,KAAU,KACV,EAAO,KAAK,GAAkB,EAAK;IAEzC;;CAUN,oBAAoB,GAA8B;AAC9C,SAAO,IAAI,EAAS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,KAAK,EAAkB,EAAM,CAAC;KACtC,UAAU;KACb;AAED,QAAY,EAAe,GAAW,GAAM,EAAW;;;AAI/D,SAAO;;CAQX,iBAA+C,GAA4E;AACvH,SAAO,GAAQ,KAAW,OAAO,EAAQ,OAAQ;;CAUrD,MAAM,cACF,GACA,GACA,GACA,IAAsB,EAAE,EACZ;EACZ,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAO;AAExD,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AASnF,SANwB,EAAe,QAAO,MAC1C,CAAC,EAAgB,OAAO,MAAM,MAC1B,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAaL,MAAM,mBACF,GACA,GACA,GACA,IAAsB,EAAE,EACF;EACtB,IAAM,IAAkB,MAAM,EAAW,QAAQ,EAAO;AAExD,MAAI,CAAC,EAAgB,QACjB,OAAU,MAAM,qCAAqC,EAAgB,UAAU;AASnF,SANqB,EAAgB,OAAO,QAAQ,MAChD,EAAe,MAAM,MACjB,EAAS,GAAgB,EAAU,CACtC,CACJ;;CAIR"}
|
package/dist/node/path/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { dirname as e, join as t, path as n, resolve as r, resolveWorkingPath as i } from "./path.util.js";
|
|
2
|
-
import { AG_KIT_PACKAGE_NAME as a, BUILD_DIRECTORY as o, COMMIT_LINT_CLI as s, COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME as c, COMMIT_LINT_PACKAGE_NAME as l, CYBERSKILL_CLI as u, CYBERSKILL_CLI_PATH as d, CYBERSKILL_DIRECTORY as f, CYBERSKILL_PACKAGE_NAME as p, DOT_AGENT as m, ESLINT_CLI as h, ESLINT_INSPECT_CLI as g, ESLINT_INSPECT_PACKAGE_NAME as _, ESLINT_PACKAGE_NAME as v, GIT_CLI as y, GIT_COMMIT_EDITMSG as b, GIT_EXCLUDE as x, GIT_HOOK as S, GIT_IGNORE as C, LINT_STAGED_CLI as w, LINT_STAGED_PACKAGE_NAME as T, MIGRATE_MONGO_CLI as E, MIGRATE_MONGO_CONFIG as D, MIGRATE_MONGO_PACKAGE_NAME as O, NODE_MODULES as k, NODE_MODULES_INSPECT_CLI as A, NODE_MODULES_INSPECT_PACKAGE_NAME as j, PACKAGE_JSON as M, PACKAGE_LOCK_JSON as N, PATH as P, PNPM_CLI as F, PNPM_EXEC_CLI as I, PNPM_LOCK_YAML as L, PUBLIC_DIRECTORY as R, SIMPLE_GIT_HOOKS_PACKAGE_NAME as z, SIMPLE_GIT_HOOK_CLI as B, SIMPLE_GIT_HOOK_JSON as V, STORYBOOK_CLI as H, STORYBOOK_PACKAGE_NAME as U, TSCONFIG_JSON as W, TSC_CLI as G, TSC_PACKAGE_NAME as K, TSX_CLI as q, VITEST_CLI as J, VITEST_PACKAGE_NAME as Y, WORKING_DIRECTORY as X, command as Z, createGitHooksConfig as Q } from "./path.constant.js";
|
|
3
|
-
export { a as AG_KIT_PACKAGE_NAME, o as BUILD_DIRECTORY, s as COMMIT_LINT_CLI, c as COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME, l as COMMIT_LINT_PACKAGE_NAME, u as CYBERSKILL_CLI, d as CYBERSKILL_CLI_PATH, f as CYBERSKILL_DIRECTORY, p as CYBERSKILL_PACKAGE_NAME, m as DOT_AGENT, h as ESLINT_CLI, g as ESLINT_INSPECT_CLI, _ as ESLINT_INSPECT_PACKAGE_NAME, v as ESLINT_PACKAGE_NAME, y as GIT_CLI, b as GIT_COMMIT_EDITMSG, x as GIT_EXCLUDE, S as GIT_HOOK, C as GIT_IGNORE, w as LINT_STAGED_CLI, T as LINT_STAGED_PACKAGE_NAME, E as MIGRATE_MONGO_CLI, D as MIGRATE_MONGO_CONFIG, O as MIGRATE_MONGO_PACKAGE_NAME, k as NODE_MODULES, A as NODE_MODULES_INSPECT_CLI, j as NODE_MODULES_INSPECT_PACKAGE_NAME, M as PACKAGE_JSON, N as PACKAGE_LOCK_JSON, P as PATH, F as PNPM_CLI, I as PNPM_EXEC_CLI, L as PNPM_LOCK_YAML, R as PUBLIC_DIRECTORY, z as SIMPLE_GIT_HOOKS_PACKAGE_NAME, B as SIMPLE_GIT_HOOK_CLI, V as SIMPLE_GIT_HOOK_JSON, H as STORYBOOK_CLI, U as STORYBOOK_PACKAGE_NAME, W as TSCONFIG_JSON, G as TSC_CLI, K as TSC_PACKAGE_NAME, q as TSX_CLI, J as VITEST_CLI, Y as VITEST_PACKAGE_NAME, X as WORKING_DIRECTORY, Z as command, Q as createGitHooksConfig, e as dirname, t as join, n as path, r as resolve, i as resolveWorkingPath };
|
|
2
|
+
import { AG_KIT_PACKAGE_NAME as a, BUILD_DIRECTORY as o, COMMIT_LINT_CLI as s, COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME as c, COMMIT_LINT_PACKAGE_NAME as l, CYBERSKILL_CLI as u, CYBERSKILL_CLI_PATH as d, CYBERSKILL_DIRECTORY as f, CYBERSKILL_PACKAGE_NAME as p, DOT_AGENT as m, ESLINT_CLI as h, ESLINT_INSPECT_CLI as g, ESLINT_INSPECT_PACKAGE_NAME as _, ESLINT_PACKAGE_NAME as v, GIT_CLI as y, GIT_COMMIT_EDITMSG as b, GIT_EXCLUDE as x, GIT_HOOK as S, GIT_IGNORE as C, LINT_STAGED_CLI as w, LINT_STAGED_PACKAGE_NAME as T, MIGRATE_MONGO_CLI as E, MIGRATE_MONGO_CONFIG as D, MIGRATE_MONGO_PACKAGE_NAME as O, NODE_MODULES as k, NODE_MODULES_INSPECT_CLI as A, NODE_MODULES_INSPECT_PACKAGE_NAME as j, PACKAGE_JSON as M, PACKAGE_LOCK_JSON as N, PATH as P, PNPM_CLI as F, PNPM_EXEC_CLI as I, PNPM_LOCK_YAML as L, PUBLIC_DIRECTORY as R, SIMPLE_GIT_HOOKS_PACKAGE_NAME as z, SIMPLE_GIT_HOOK_CLI as B, SIMPLE_GIT_HOOK_JSON as V, STORYBOOK_CLI as H, STORYBOOK_PACKAGE_NAME as U, TSCONFIG_JSON as W, TSC_CLI as G, TSC_PACKAGE_NAME as K, TSX_CLI as q, VITEST_CLI as J, VITEST_PACKAGE_NAME as Y, WORKING_DIRECTORY as X, command as Z, createGitHooksConfig as Q, getCyberskillDirectory as $ } from "./path.constant.js";
|
|
3
|
+
export { a as AG_KIT_PACKAGE_NAME, o as BUILD_DIRECTORY, s as COMMIT_LINT_CLI, c as COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME, l as COMMIT_LINT_PACKAGE_NAME, u as CYBERSKILL_CLI, d as CYBERSKILL_CLI_PATH, f as CYBERSKILL_DIRECTORY, p as CYBERSKILL_PACKAGE_NAME, m as DOT_AGENT, h as ESLINT_CLI, g as ESLINT_INSPECT_CLI, _ as ESLINT_INSPECT_PACKAGE_NAME, v as ESLINT_PACKAGE_NAME, y as GIT_CLI, b as GIT_COMMIT_EDITMSG, x as GIT_EXCLUDE, S as GIT_HOOK, C as GIT_IGNORE, w as LINT_STAGED_CLI, T as LINT_STAGED_PACKAGE_NAME, E as MIGRATE_MONGO_CLI, D as MIGRATE_MONGO_CONFIG, O as MIGRATE_MONGO_PACKAGE_NAME, k as NODE_MODULES, A as NODE_MODULES_INSPECT_CLI, j as NODE_MODULES_INSPECT_PACKAGE_NAME, M as PACKAGE_JSON, N as PACKAGE_LOCK_JSON, P as PATH, F as PNPM_CLI, I as PNPM_EXEC_CLI, L as PNPM_LOCK_YAML, R as PUBLIC_DIRECTORY, z as SIMPLE_GIT_HOOKS_PACKAGE_NAME, B as SIMPLE_GIT_HOOK_CLI, V as SIMPLE_GIT_HOOK_JSON, H as STORYBOOK_CLI, U as STORYBOOK_PACKAGE_NAME, W as TSCONFIG_JSON, G as TSC_CLI, K as TSC_PACKAGE_NAME, q as TSX_CLI, J as VITEST_CLI, Y as VITEST_PACKAGE_NAME, X as WORKING_DIRECTORY, Z as command, Q as createGitHooksConfig, e as dirname, $ as getCyberskillDirectory, t as join, n as path, r as resolve, i as resolveWorkingPath };
|
|
@@ -13,6 +13,13 @@ export declare const GIT_HOOK = ".git/hooks/";
|
|
|
13
13
|
export declare const GIT_COMMIT_EDITMSG = ".git/COMMIT_EDITMSG";
|
|
14
14
|
export declare const GIT_EXCLUDE = ".git/info/exclude";
|
|
15
15
|
export declare const MIGRATE_MONGO_CONFIG = ".migrate-mongo.config.js";
|
|
16
|
+
/**
|
|
17
|
+
* Lazily computes the CyberSkill directory path.
|
|
18
|
+
* Reads package.json on first access to determine whether this is the shared package itself
|
|
19
|
+
* or a consumer project, avoiding filesystem reads at module load time.
|
|
20
|
+
*/
|
|
21
|
+
export declare function getCyberskillDirectory(): string;
|
|
22
|
+
/** @deprecated Use `getCyberskillDirectory()` instead. Kept for backward compatibility. */
|
|
16
23
|
export declare const CYBERSKILL_DIRECTORY: string;
|
|
17
24
|
export declare const CYBERSKILL_CLI = "cyberskill";
|
|
18
25
|
export declare const CYBERSKILL_CLI_PATH = "src/node/cli/index.ts";
|
|
@@ -44,7 +51,8 @@ export declare const STORYBOOK_CLI = "storybook";
|
|
|
44
51
|
export declare const AG_KIT_PACKAGE_NAME = "@vudovn/ag-kit";
|
|
45
52
|
export declare const DOT_AGENT = ".agent";
|
|
46
53
|
export declare const PATH: {
|
|
47
|
-
|
|
54
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
55
|
+
readonly CYBERSKILL_DIRECTORY: string;
|
|
48
56
|
WORKING_DIRECTORY: string;
|
|
49
57
|
PUBLIC_DIRECTORY: string;
|
|
50
58
|
TS_CONFIG: string;
|
|
@@ -58,12 +66,18 @@ export declare const PATH: {
|
|
|
58
66
|
PNPM_LOCK_YAML: string;
|
|
59
67
|
NODE_MODULES: string;
|
|
60
68
|
MIGRATE_MONGO_CONFIG: string;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
69
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
70
|
+
readonly LINT_STAGED_CONFIG: string;
|
|
71
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
72
|
+
readonly COMMITLINT_CONFIG: string;
|
|
73
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
74
|
+
readonly VITEST_UNIT_CONFIG: string;
|
|
75
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
76
|
+
readonly VITEST_E2E_CONFIG: string;
|
|
77
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
78
|
+
readonly STORYBOOK_MAIN_CONFIG: string;
|
|
79
|
+
/** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */
|
|
80
|
+
readonly STORYBOOK_PREVIEW_CONFIG: string;
|
|
67
81
|
DOT_AGENT: string;
|
|
68
82
|
};
|
|
69
83
|
/**
|
|
@@ -4,37 +4,56 @@ import { E_PackageType as n } from "../package/package.type.js";
|
|
|
4
4
|
import { setupPackages as ee } from "../package/package.util.js";
|
|
5
5
|
import { formatCommand as r, rawCommand as i } from "../command/command.util.js";
|
|
6
6
|
import { join as a, resolveWorkingPath as o } from "./path.util.js";
|
|
7
|
-
import
|
|
8
|
-
|
|
7
|
+
import te from "fs-extra";
|
|
8
|
+
//#region src/node/path/path.constant.ts
|
|
9
|
+
var s = e().CWD, c = "@cyberskill/shared", l = "node_modules", u = "dist", d = "public", f = "package.json", p = "package-lock.json", m = "tsconfig.json", h = ".gitignore", g = ".simple-git-hooks.json", _ = "pnpm-lock.yaml", v = ".git/hooks/", y = ".git/COMMIT_EDITMSG", b = ".git/info/exclude", x = ".migrate-mongo.config.js", S = null;
|
|
10
|
+
function C() {
|
|
11
|
+
if (S !== null) return S;
|
|
9
12
|
try {
|
|
10
|
-
|
|
13
|
+
S = te.readJsonSync(o("package.json")).name === "@cyberskill/shared" ? a(s, u) : a(s, l, c, u);
|
|
11
14
|
} catch {
|
|
12
|
-
|
|
15
|
+
S = a(s, l, c, u);
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
17
|
+
return S;
|
|
18
|
+
}
|
|
19
|
+
var ne = C(), w = "cyberskill", re = "src/node/cli/index.ts", T = "eslint", E = "eslint", D = "vitest", O = "vitest", k = "@commitlint/cli", A = "@commitlint/config-conventional", j = "commitlint", M = "lint-staged", N = "lint-staged", P = "typescript", F = "tsc", I = "tsx", L = "git", R = "pnpm", z = "pnpm exec", B = "simple-git-hooks", V = "simple-git-hooks", H = "@eslint/config-inspector", U = "eslint-config-inspector", W = "node-modules-inspector", G = "node-modules-inspector", K = "migrate-mongo", q = "./node_modules/migrate-mongo/bin/migrate-mongo", J = "storybook", Y = "storybook", X = "@vudovn/ag-kit", Z = ".agent", Q = {
|
|
20
|
+
get CYBERSKILL_DIRECTORY() {
|
|
21
|
+
return C();
|
|
22
|
+
},
|
|
23
|
+
WORKING_DIRECTORY: s,
|
|
24
|
+
PUBLIC_DIRECTORY: o(d),
|
|
25
|
+
TS_CONFIG: o(m),
|
|
26
|
+
GIT_IGNORE: o(h),
|
|
27
|
+
GIT_HOOK: o(v),
|
|
28
|
+
GIT_COMMIT_MSG: o(y),
|
|
29
|
+
GIT_EXCLUDE: o(b),
|
|
30
|
+
SIMPLE_GIT_HOOKS_JSON: o(g),
|
|
31
|
+
PACKAGE_JSON: o(f),
|
|
32
|
+
PACKAGE_LOCK_JSON: o(p),
|
|
33
|
+
PNPM_LOCK_YAML: o(_),
|
|
34
|
+
NODE_MODULES: o(l),
|
|
35
|
+
MIGRATE_MONGO_CONFIG: o(x),
|
|
36
|
+
get LINT_STAGED_CONFIG() {
|
|
37
|
+
return o(`${C()}/config/lint-staged/index.js`);
|
|
38
|
+
},
|
|
39
|
+
get COMMITLINT_CONFIG() {
|
|
40
|
+
return o(`${C()}/config/commitlint/index.js`);
|
|
41
|
+
},
|
|
42
|
+
get VITEST_UNIT_CONFIG() {
|
|
43
|
+
return o(`${C()}/config/vitest/vitest.unit.js`);
|
|
44
|
+
},
|
|
45
|
+
get VITEST_E2E_CONFIG() {
|
|
46
|
+
return o(`${C()}/config/vitest/vitest.e2e.js`);
|
|
47
|
+
},
|
|
48
|
+
get STORYBOOK_MAIN_CONFIG() {
|
|
49
|
+
return o(`${C()}/config/storybook/storybook.main.js`);
|
|
50
|
+
},
|
|
51
|
+
get STORYBOOK_PREVIEW_CONFIG() {
|
|
52
|
+
return o(`${C()}/config/storybook/storybook.preview.js`);
|
|
53
|
+
},
|
|
35
54
|
DOT_AGENT: o(Z)
|
|
36
55
|
};
|
|
37
|
-
function
|
|
56
|
+
function ie() {
|
|
38
57
|
return {
|
|
39
58
|
"pre-commit": N,
|
|
40
59
|
"commit-msg": j,
|
|
@@ -51,7 +70,7 @@ function $({ type: e, packages: n, command: a }) {
|
|
|
51
70
|
}
|
|
52
71
|
};
|
|
53
72
|
}
|
|
54
|
-
var
|
|
73
|
+
var ae = /^[\w-]+$/, oe = {
|
|
55
74
|
simpleGitHooks: $({
|
|
56
75
|
type: t.CLI,
|
|
57
76
|
packages: [{
|
|
@@ -117,7 +136,7 @@ var re = /^[\w-]+$/, ie = {
|
|
|
117
136
|
command: `${O} --config ${Q.VITEST_E2E_CONFIG}`
|
|
118
137
|
}),
|
|
119
138
|
mongoMigrateCreate: (e) => {
|
|
120
|
-
if (!
|
|
139
|
+
if (!ae.test(e)) throw Error("Migration name must only contain alphanumeric characters, underscores, and hyphens.");
|
|
121
140
|
return $({
|
|
122
141
|
type: t.CLI,
|
|
123
142
|
packages: [{
|
|
@@ -217,6 +236,6 @@ var re = /^[\w-]+$/, ie = {
|
|
|
217
236
|
})
|
|
218
237
|
};
|
|
219
238
|
//#endregion
|
|
220
|
-
export { X as AG_KIT_PACKAGE_NAME,
|
|
239
|
+
export { X as AG_KIT_PACKAGE_NAME, u as BUILD_DIRECTORY, j as COMMIT_LINT_CLI, A as COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME, k as COMMIT_LINT_PACKAGE_NAME, w as CYBERSKILL_CLI, re as CYBERSKILL_CLI_PATH, ne as CYBERSKILL_DIRECTORY, c as CYBERSKILL_PACKAGE_NAME, Z as DOT_AGENT, E as ESLINT_CLI, U as ESLINT_INSPECT_CLI, H as ESLINT_INSPECT_PACKAGE_NAME, T as ESLINT_PACKAGE_NAME, L as GIT_CLI, y as GIT_COMMIT_EDITMSG, b as GIT_EXCLUDE, v as GIT_HOOK, h as GIT_IGNORE, N as LINT_STAGED_CLI, M as LINT_STAGED_PACKAGE_NAME, q as MIGRATE_MONGO_CLI, x as MIGRATE_MONGO_CONFIG, K as MIGRATE_MONGO_PACKAGE_NAME, l as NODE_MODULES, G as NODE_MODULES_INSPECT_CLI, W as NODE_MODULES_INSPECT_PACKAGE_NAME, f as PACKAGE_JSON, p as PACKAGE_LOCK_JSON, Q as PATH, R as PNPM_CLI, z as PNPM_EXEC_CLI, _ as PNPM_LOCK_YAML, d as PUBLIC_DIRECTORY, B as SIMPLE_GIT_HOOKS_PACKAGE_NAME, V as SIMPLE_GIT_HOOK_CLI, g as SIMPLE_GIT_HOOK_JSON, Y as STORYBOOK_CLI, J as STORYBOOK_PACKAGE_NAME, m as TSCONFIG_JSON, F as TSC_CLI, P as TSC_PACKAGE_NAME, I as TSX_CLI, O as VITEST_CLI, D as VITEST_PACKAGE_NAME, s as WORKING_DIRECTORY, oe as command, ie as createGitHooksConfig, C as getCyberskillDirectory };
|
|
221
240
|
|
|
222
241
|
//# sourceMappingURL=path.constant.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.constant.js","names":[],"sources":["../../../src/node/path/path.constant.ts"],"sourcesContent":["import fsExtra from 'fs-extra';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { I_PackageInput, I_PackageJson } from '../package/index.js';\n\nimport { E_CommandType, formatCommand, rawCommand } from '../command/index.js';\nimport { E_PackageType, setupPackages } from '../package/index.js';\nimport { join, resolveWorkingPath } from './path.util.js';\n\nconst env = getEnv();\n\nexport const WORKING_DIRECTORY = env.CWD;\nexport const CYBERSKILL_PACKAGE_NAME = '@cyberskill/shared';\nexport const NODE_MODULES = 'node_modules';\nexport const BUILD_DIRECTORY = 'dist';\nexport const PUBLIC_DIRECTORY = 'public';\nexport const PACKAGE_JSON = 'package.json';\nexport const PACKAGE_LOCK_JSON = 'package-lock.json';\nexport const TSCONFIG_JSON = 'tsconfig.json';\nexport const GIT_IGNORE = '.gitignore';\nexport const SIMPLE_GIT_HOOK_JSON = '.simple-git-hooks.json';\nexport const PNPM_LOCK_YAML = 'pnpm-lock.yaml';\nexport const GIT_HOOK = '.git/hooks/';\nexport const GIT_COMMIT_EDITMSG = '.git/COMMIT_EDITMSG';\nexport const GIT_EXCLUDE = '.git/info/exclude';\nexport const MIGRATE_MONGO_CONFIG = '.migrate-mongo.config.js';\nexport const CYBERSKILL_DIRECTORY = (() => {\n try {\n const packageJson = fsExtra.readJsonSync(resolveWorkingPath(PACKAGE_JSON)) as I_PackageJson;\n\n const baseDirectory = packageJson.name === CYBERSKILL_PACKAGE_NAME\n ? join(WORKING_DIRECTORY, BUILD_DIRECTORY)\n : join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n\n return baseDirectory;\n }\n catch {\n return join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n})();\nexport const CYBERSKILL_CLI = 'cyberskill';\nexport const CYBERSKILL_CLI_PATH = 'src/node/cli/index.ts';\nexport const ESLINT_PACKAGE_NAME = 'eslint';\nexport const ESLINT_CLI = 'eslint';\nexport const VITEST_PACKAGE_NAME = 'vitest';\nexport const VITEST_CLI = 'vitest';\nexport const COMMIT_LINT_PACKAGE_NAME = '@commitlint/cli';\nexport const COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME = '@commitlint/config-conventional';\nexport const COMMIT_LINT_CLI = 'commitlint';\nexport const LINT_STAGED_PACKAGE_NAME = 'lint-staged';\nexport const LINT_STAGED_CLI = 'lint-staged';\nexport const TSC_PACKAGE_NAME = 'typescript';\nexport const TSC_CLI = 'tsc';\nexport const TSX_CLI = 'tsx';\nexport const GIT_CLI = 'git';\nexport const PNPM_CLI = 'pnpm';\nexport const PNPM_EXEC_CLI = 'pnpm exec';\nexport const SIMPLE_GIT_HOOKS_PACKAGE_NAME = 'simple-git-hooks';\nexport const SIMPLE_GIT_HOOK_CLI = 'simple-git-hooks';\nexport const ESLINT_INSPECT_PACKAGE_NAME = '@eslint/config-inspector';\nexport const ESLINT_INSPECT_CLI = 'eslint-config-inspector';\nexport const NODE_MODULES_INSPECT_PACKAGE_NAME = 'node-modules-inspector';\nexport const NODE_MODULES_INSPECT_CLI = 'node-modules-inspector';\nexport const MIGRATE_MONGO_PACKAGE_NAME = 'migrate-mongo';\nexport const MIGRATE_MONGO_CLI = './node_modules/migrate-mongo/bin/migrate-mongo';\nexport const STORYBOOK_PACKAGE_NAME = 'storybook';\nexport const STORYBOOK_CLI = 'storybook';\nexport const AG_KIT_PACKAGE_NAME = '@vudovn/ag-kit';\nexport const DOT_AGENT = '.agent';\n\nexport const PATH = {\n CYBERSKILL_DIRECTORY,\n WORKING_DIRECTORY,\n PUBLIC_DIRECTORY: resolveWorkingPath(PUBLIC_DIRECTORY),\n TS_CONFIG: resolveWorkingPath(TSCONFIG_JSON),\n GIT_IGNORE: resolveWorkingPath(GIT_IGNORE),\n GIT_HOOK: resolveWorkingPath(GIT_HOOK),\n GIT_COMMIT_MSG: resolveWorkingPath(GIT_COMMIT_EDITMSG),\n GIT_EXCLUDE: resolveWorkingPath(GIT_EXCLUDE),\n SIMPLE_GIT_HOOKS_JSON: resolveWorkingPath(SIMPLE_GIT_HOOK_JSON),\n PACKAGE_JSON: resolveWorkingPath(PACKAGE_JSON),\n PACKAGE_LOCK_JSON: resolveWorkingPath(PACKAGE_LOCK_JSON),\n PNPM_LOCK_YAML: resolveWorkingPath(PNPM_LOCK_YAML),\n NODE_MODULES: resolveWorkingPath(NODE_MODULES),\n MIGRATE_MONGO_CONFIG: resolveWorkingPath(MIGRATE_MONGO_CONFIG),\n LINT_STAGED_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/lint-staged/index.js`),\n COMMITLINT_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/commitlint/index.js`),\n VITEST_UNIT_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/vitest/vitest.unit.js`),\n VITEST_E2E_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/vitest/vitest.e2e.js`),\n STORYBOOK_MAIN_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/storybook/storybook.main.js`),\n STORYBOOK_PREVIEW_CONFIG: resolveWorkingPath(`${CYBERSKILL_DIRECTORY}/config/storybook/storybook.preview.js`),\n DOT_AGENT: resolveWorkingPath(DOT_AGENT),\n};\n\n/**\n * Creates Git hooks configuration based on whether this is the current project.\n * This function generates a configuration object for Git hooks that includes\n * pre-commit and commit-msg hooks, with an optional pre-push hook for the current project.\n *\n * @returns A Git hooks configuration object with appropriate commands for each hook.\n */\nexport function createGitHooksConfig() {\n return {\n 'pre-commit': LINT_STAGED_CLI,\n 'commit-msg': COMMIT_LINT_CLI,\n 'pre-push': rawCommand(`${GIT_CLI} pull && ${PNPM_CLI} run --if-present test`),\n };\n}\n\n/**\n * Builds a command function based on the specified type and configuration.\n * This function creates a command executor that handles different command types\n * including CLI commands and string commands. It manages package dependencies\n * and formats commands appropriately for execution.\n *\n * The function supports:\n * - CLI commands that require package installation\n * - String commands that are executed directly\n * - Automatic package dependency management\n * - Command formatting and validation\n *\n * @param config - Configuration object containing type, packages, and command properties.\n * @param config.type - The type of command to build (CLI or STRING).\n * @param config.packages - Optional array of packages required for CLI commands.\n * @param config.command - The command string to execute.\n * @returns A function that returns a promise resolving to the formatted command string.\n * @throws {Error} When an unsupported command type is provided.\n */\nfunction buildCommand({ type, packages, command }: { type: E_CommandType; packages?: I_PackageInput[]; command: string }): () => Promise<string> {\n const uniquePackages = packages?.reduce((acc: I_PackageInput[], pkg) => {\n if (!acc.some(existingPkg => existingPkg.name === pkg.name)) {\n acc.push(pkg);\n }\n return acc;\n }, []);\n\n return async () => {\n switch (type) {\n case E_CommandType.CLI: {\n if (uniquePackages?.length) {\n await setupPackages(uniquePackages, {\n install: true,\n });\n }\n\n return formatCommand(rawCommand(`${PNPM_EXEC_CLI} ${command}`)) as string;\n }\n case E_CommandType.STRING: {\n return formatCommand(rawCommand(command)) as string;\n }\n default: {\n throw new Error('Unsupported command type');\n }\n }\n };\n}\n\nconst RE_MIGRATE_NAME = /^[\\w-]+$/;\n\nexport const command = {\n simpleGitHooks: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: SIMPLE_GIT_HOOKS_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: SIMPLE_GIT_HOOK_CLI,\n }),\n eslintInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name:\n ESLINT_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: ESLINT_INSPECT_CLI,\n }),\n nodeModulesInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: NODE_MODULES_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: NODE_MODULES_INSPECT_CLI,\n }),\n eslintCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --no-cache`,\n }),\n eslintFix: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --fix --no-cache`,\n }),\n typescriptCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSC_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${TSC_CLI} -p ${PATH.TS_CONFIG} --noEmit`,\n }),\n testUnit: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_UNIT_CONFIG}`,\n }),\n testE2e: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_E2E_CONFIG}`,\n }),\n mongoMigrateCreate: (migrateName: string) => {\n if (!RE_MIGRATE_NAME.test(migrateName)) {\n throw new Error('Migration name must only contain alphanumeric characters, underscores, and hyphens.');\n }\n\n return buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} create ${migrateName} -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n })();\n },\n mongoMigrateUp: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} up -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n mongoMigrateDown: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} down -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n commitLint: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: COMMIT_LINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n {\n name: COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${COMMIT_LINT_CLI} --edit ${PATH.GIT_COMMIT_MSG} --config ${PATH.COMMITLINT_CONFIG}`,\n }),\n lintStaged: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: LINT_STAGED_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${LINT_STAGED_CLI} --config ${PATH.LINT_STAGED_CONFIG}`,\n }),\n configureGitHook: buildCommand({\n type: E_CommandType.STRING,\n command: `${GIT_CLI} config core.hooksPath ${PATH.GIT_HOOK}`,\n }),\n build: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} run --if-present build`,\n }),\n pnpmInstallStandard: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts`,\n }),\n pnpmInstallLegacy: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --legacy-peer-deps`,\n }),\n pnpmInstallForce: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --force`,\n }),\n pnpmPruneStore: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} store prune`,\n }),\n pnpmCleanCache: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} cache delete`,\n }),\n storybookDev: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} dev`,\n }),\n storybookBuild: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} build`,\n }),\n};\n"],"mappings":";;;;;;;AAYA,IAAa,IAFD,GAAQ,CAEiB,KACxB,IAA0B,sBAC1B,IAAe,gBACf,IAAkB,QAClB,IAAmB,UACnB,IAAe,gBACf,IAAoB,qBACpB,IAAgB,iBAChB,IAAa,cACb,IAAuB,0BACvB,IAAiB,kBACjB,IAAW,eACX,IAAqB,uBACrB,IAAc,qBACd,IAAuB,4BACvB,WAA8B;AACvC,KAAI;AAOA,SANoB,EAAQ,aAAa,EAAA,eAAgC,CAAC,CAExC,SAAA,uBAC5B,EAAK,GAAmB,EAAgB,GACxC,EAAK,GAAmB,GAAc,GAAyB,EAAgB;SAInF;AACF,SAAO,EAAK,GAAmB,GAAc,GAAyB,EAAgB;;IAE1F,EACS,KAAiB,cACjB,IAAsB,yBACtB,IAAsB,UACtB,IAAa,UACb,IAAsB,UACtB,IAAa,UACb,IAA2B,mBAC3B,IAA+C,mCAC/C,IAAkB,cAClB,IAA2B,eAC3B,IAAkB,eAClB,IAAmB,cACnB,IAAU,OACV,IAAU,OACV,IAAU,OACV,IAAW,QACX,IAAgB,aAChB,IAAgC,oBAChC,IAAsB,oBACtB,IAA8B,4BAC9B,IAAqB,2BACrB,IAAoC,0BACpC,IAA2B,0BAC3B,IAA6B,iBAC7B,IAAoB,kDACpB,IAAyB,aACzB,IAAgB,aAChB,IAAsB,kBACtB,IAAY,UAEZ,IAAO;CAChB;CACA;CACA,kBAAkB,EAAmB,EAAiB;CAC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MAAM,EAAc;EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,cAAc,EAAa;EACvB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACL"}
|
|
1
|
+
{"version":3,"file":"path.constant.js","names":[],"sources":["../../../src/node/path/path.constant.ts"],"sourcesContent":["import fsExtra from 'fs-extra';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { I_PackageInput, I_PackageJson } from '../package/index.js';\n\nimport { E_CommandType, formatCommand, rawCommand } from '../command/index.js';\nimport { E_PackageType, setupPackages } from '../package/index.js';\nimport { join, resolveWorkingPath } from './path.util.js';\n\nexport const WORKING_DIRECTORY = getEnv().CWD;\nexport const CYBERSKILL_PACKAGE_NAME = '@cyberskill/shared';\nexport const NODE_MODULES = 'node_modules';\nexport const BUILD_DIRECTORY = 'dist';\nexport const PUBLIC_DIRECTORY = 'public';\nexport const PACKAGE_JSON = 'package.json';\nexport const PACKAGE_LOCK_JSON = 'package-lock.json';\nexport const TSCONFIG_JSON = 'tsconfig.json';\nexport const GIT_IGNORE = '.gitignore';\nexport const SIMPLE_GIT_HOOK_JSON = '.simple-git-hooks.json';\nexport const PNPM_LOCK_YAML = 'pnpm-lock.yaml';\nexport const GIT_HOOK = '.git/hooks/';\nexport const GIT_COMMIT_EDITMSG = '.git/COMMIT_EDITMSG';\nexport const GIT_EXCLUDE = '.git/info/exclude';\nexport const MIGRATE_MONGO_CONFIG = '.migrate-mongo.config.js';\nlet _cyberskillDir: string | null = null;\n\n/**\n * Lazily computes the CyberSkill directory path.\n * Reads package.json on first access to determine whether this is the shared package itself\n * or a consumer project, avoiding filesystem reads at module load time.\n */\nexport function getCyberskillDirectory(): string {\n if (_cyberskillDir !== null) {\n return _cyberskillDir;\n }\n\n try {\n const packageJson = fsExtra.readJsonSync(resolveWorkingPath(PACKAGE_JSON)) as I_PackageJson;\n\n _cyberskillDir = packageJson.name === CYBERSKILL_PACKAGE_NAME\n ? join(WORKING_DIRECTORY, BUILD_DIRECTORY)\n : join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n catch {\n _cyberskillDir = join(WORKING_DIRECTORY, NODE_MODULES, CYBERSKILL_PACKAGE_NAME, BUILD_DIRECTORY);\n }\n\n return _cyberskillDir;\n}\n\n/** @deprecated Use `getCyberskillDirectory()` instead. Kept for backward compatibility. */\nexport const CYBERSKILL_DIRECTORY = getCyberskillDirectory();\nexport const CYBERSKILL_CLI = 'cyberskill';\nexport const CYBERSKILL_CLI_PATH = 'src/node/cli/index.ts';\nexport const ESLINT_PACKAGE_NAME = 'eslint';\nexport const ESLINT_CLI = 'eslint';\nexport const VITEST_PACKAGE_NAME = 'vitest';\nexport const VITEST_CLI = 'vitest';\nexport const COMMIT_LINT_PACKAGE_NAME = '@commitlint/cli';\nexport const COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME = '@commitlint/config-conventional';\nexport const COMMIT_LINT_CLI = 'commitlint';\nexport const LINT_STAGED_PACKAGE_NAME = 'lint-staged';\nexport const LINT_STAGED_CLI = 'lint-staged';\nexport const TSC_PACKAGE_NAME = 'typescript';\nexport const TSC_CLI = 'tsc';\nexport const TSX_CLI = 'tsx';\nexport const GIT_CLI = 'git';\nexport const PNPM_CLI = 'pnpm';\nexport const PNPM_EXEC_CLI = 'pnpm exec';\nexport const SIMPLE_GIT_HOOKS_PACKAGE_NAME = 'simple-git-hooks';\nexport const SIMPLE_GIT_HOOK_CLI = 'simple-git-hooks';\nexport const ESLINT_INSPECT_PACKAGE_NAME = '@eslint/config-inspector';\nexport const ESLINT_INSPECT_CLI = 'eslint-config-inspector';\nexport const NODE_MODULES_INSPECT_PACKAGE_NAME = 'node-modules-inspector';\nexport const NODE_MODULES_INSPECT_CLI = 'node-modules-inspector';\nexport const MIGRATE_MONGO_PACKAGE_NAME = 'migrate-mongo';\nexport const MIGRATE_MONGO_CLI = './node_modules/migrate-mongo/bin/migrate-mongo';\nexport const STORYBOOK_PACKAGE_NAME = 'storybook';\nexport const STORYBOOK_CLI = 'storybook';\nexport const AG_KIT_PACKAGE_NAME = '@vudovn/ag-kit';\nexport const DOT_AGENT = '.agent';\n\nexport const PATH = {\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get CYBERSKILL_DIRECTORY() { return getCyberskillDirectory(); },\n WORKING_DIRECTORY,\n PUBLIC_DIRECTORY: resolveWorkingPath(PUBLIC_DIRECTORY),\n TS_CONFIG: resolveWorkingPath(TSCONFIG_JSON),\n GIT_IGNORE: resolveWorkingPath(GIT_IGNORE),\n GIT_HOOK: resolveWorkingPath(GIT_HOOK),\n GIT_COMMIT_MSG: resolveWorkingPath(GIT_COMMIT_EDITMSG),\n GIT_EXCLUDE: resolveWorkingPath(GIT_EXCLUDE),\n SIMPLE_GIT_HOOKS_JSON: resolveWorkingPath(SIMPLE_GIT_HOOK_JSON),\n PACKAGE_JSON: resolveWorkingPath(PACKAGE_JSON),\n PACKAGE_LOCK_JSON: resolveWorkingPath(PACKAGE_LOCK_JSON),\n PNPM_LOCK_YAML: resolveWorkingPath(PNPM_LOCK_YAML),\n NODE_MODULES: resolveWorkingPath(NODE_MODULES),\n MIGRATE_MONGO_CONFIG: resolveWorkingPath(MIGRATE_MONGO_CONFIG),\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get LINT_STAGED_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/lint-staged/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get COMMITLINT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/commitlint/index.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_UNIT_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.unit.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get VITEST_E2E_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/vitest/vitest.e2e.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_MAIN_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.main.js`); },\n /** Lazily resolved — defers `getCyberskillDirectory()` until first property access. */\n get STORYBOOK_PREVIEW_CONFIG() { return resolveWorkingPath(`${getCyberskillDirectory()}/config/storybook/storybook.preview.js`); },\n DOT_AGENT: resolveWorkingPath(DOT_AGENT),\n};\n\n/**\n * Creates Git hooks configuration based on whether this is the current project.\n * This function generates a configuration object for Git hooks that includes\n * pre-commit and commit-msg hooks, with an optional pre-push hook for the current project.\n *\n * @returns A Git hooks configuration object with appropriate commands for each hook.\n */\nexport function createGitHooksConfig() {\n return {\n 'pre-commit': LINT_STAGED_CLI,\n 'commit-msg': COMMIT_LINT_CLI,\n 'pre-push': rawCommand(`${GIT_CLI} pull && ${PNPM_CLI} run --if-present test`),\n };\n}\n\n/**\n * Builds a command function based on the specified type and configuration.\n * This function creates a command executor that handles different command types\n * including CLI commands and string commands. It manages package dependencies\n * and formats commands appropriately for execution.\n *\n * The function supports:\n * - CLI commands that require package installation\n * - String commands that are executed directly\n * - Automatic package dependency management\n * - Command formatting and validation\n *\n * @param config - Configuration object containing type, packages, and command properties.\n * @param config.type - The type of command to build (CLI or STRING).\n * @param config.packages - Optional array of packages required for CLI commands.\n * @param config.command - The command string to execute.\n * @returns A function that returns a promise resolving to the formatted command string.\n * @throws {Error} When an unsupported command type is provided.\n */\nfunction buildCommand({ type, packages, command }: { type: E_CommandType; packages?: I_PackageInput[]; command: string }): () => Promise<string> {\n const uniquePackages = packages?.reduce((acc: I_PackageInput[], pkg) => {\n if (!acc.some(existingPkg => existingPkg.name === pkg.name)) {\n acc.push(pkg);\n }\n return acc;\n }, []);\n\n return async () => {\n switch (type) {\n case E_CommandType.CLI: {\n if (uniquePackages?.length) {\n await setupPackages(uniquePackages, {\n install: true,\n });\n }\n\n return formatCommand(rawCommand(`${PNPM_EXEC_CLI} ${command}`)) as string;\n }\n case E_CommandType.STRING: {\n return formatCommand(rawCommand(command)) as string;\n }\n default: {\n throw new Error('Unsupported command type');\n }\n }\n };\n}\n\nconst RE_MIGRATE_NAME = /^[\\w-]+$/;\n\nexport const command = {\n simpleGitHooks: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: SIMPLE_GIT_HOOKS_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: SIMPLE_GIT_HOOK_CLI,\n }),\n eslintInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name:\n ESLINT_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: ESLINT_INSPECT_CLI,\n }),\n nodeModulesInspect: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: NODE_MODULES_INSPECT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: NODE_MODULES_INSPECT_CLI,\n }),\n eslintCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --no-cache`,\n }),\n eslintFix: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: ESLINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${ESLINT_CLI} ${PATH.WORKING_DIRECTORY} --fix --no-cache`,\n }),\n typescriptCheck: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSC_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n\n ],\n command: `${TSC_CLI} -p ${PATH.TS_CONFIG} --noEmit`,\n }),\n testUnit: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_UNIT_CONFIG}`,\n }),\n testE2e: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: VITEST_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${VITEST_CLI} --config ${PATH.VITEST_E2E_CONFIG}`,\n }),\n mongoMigrateCreate: (migrateName: string) => {\n if (!RE_MIGRATE_NAME.test(migrateName)) {\n throw new Error('Migration name must only contain alphanumeric characters, underscores, and hyphens.');\n }\n\n return buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} create ${migrateName} -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n })();\n },\n mongoMigrateUp: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} up -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n mongoMigrateDown: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: TSX_CLI,\n type: E_PackageType.DEPENDENCY,\n },\n {\n name: MIGRATE_MONGO_PACKAGE_NAME,\n type: E_PackageType.DEPENDENCY,\n },\n ],\n command: `${TSX_CLI} ${MIGRATE_MONGO_CLI} down -f ${PATH.MIGRATE_MONGO_CONFIG}`,\n }),\n commitLint: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: COMMIT_LINT_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n {\n name: COMMIT_LINT_CONVENTIONAL_CONFIG_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${COMMIT_LINT_CLI} --edit ${PATH.GIT_COMMIT_MSG} --config ${PATH.COMMITLINT_CONFIG}`,\n }),\n lintStaged: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: LINT_STAGED_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${LINT_STAGED_CLI} --config ${PATH.LINT_STAGED_CONFIG}`,\n }),\n configureGitHook: buildCommand({\n type: E_CommandType.STRING,\n command: `${GIT_CLI} config core.hooksPath ${PATH.GIT_HOOK}`,\n }),\n build: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} run --if-present build`,\n }),\n pnpmInstallStandard: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts`,\n }),\n pnpmInstallLegacy: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --legacy-peer-deps`,\n }),\n pnpmInstallForce: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} install --ignore-scripts --force`,\n }),\n pnpmPruneStore: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} store prune`,\n }),\n pnpmCleanCache: buildCommand({\n type: E_CommandType.STRING,\n command: `${PNPM_CLI} cache delete`,\n }),\n storybookDev: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,\n },\n ],\n command: `${STORYBOOK_CLI} dev`,\n }),\n storybookBuild: buildCommand({\n type: E_CommandType.CLI,\n packages: [\n {\n name: STORYBOOK_PACKAGE_NAME,\n type: E_PackageType.DEV_DEPENDENCY,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EACpB,SAAS,GAAG,EAAS;EACxB,CAAC;CACF,cAAc,EAAa;EACvB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACF,gBAAgB,EAAa;EACzB,MAAM,EAAc;EACpB,UAAU,CACN;GACI,MAAM;GACN,MAAM,EAAc;GACvB,CACJ;EACD,SAAS,GAAG,EAAc;EAC7B,CAAC;CACL"}
|
|
@@ -96,12 +96,8 @@ var v = {
|
|
|
96
96
|
return t(e, { returnValue: null });
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
|
-
async set(e,
|
|
100
|
-
|
|
101
|
-
await (await _()).setItem(e, n);
|
|
102
|
-
} catch (e) {
|
|
103
|
-
t(e);
|
|
104
|
-
}
|
|
99
|
+
async set(e, t) {
|
|
100
|
+
await (await _()).setItem(e, t);
|
|
105
101
|
},
|
|
106
102
|
async remove(e) {
|
|
107
103
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import localForage from 'localforage';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { LocalForageDriver, NodeFsDriverState, NodeLocalForageOptions } from './storage.type.js';\n\nimport { catchError, log } from '../log/index.js';\nimport { NODE_FS_DRIVER_NAME, STORAGE_INSTANCE_NAME, STORAGE_KEY_EXTENSION, STORAGE_STORE_NAME } from './storage.constant.js';\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n */\nfunction encodeKey(key: string): string {\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Custom localForage driver that stores JSON-encoded values on the filesystem.\n */\nconst nodeFsDriver: LocalForageDriver = {\n _driver: NODE_FS_DRIVER_NAME,\n _support: true,\n /** Ensures the storage directory exists and respects custom baseDir overrides. */\n async _initStorage(options: unknown) {\n try {\n const baseDirFromOptions = typeof options === 'object' && options !== null && 'baseDir' in options\n ? (options as { baseDir?: unknown }).baseDir\n : undefined;\n\n if (typeof baseDirFromOptions === 'string' && baseDirFromOptions.length > 0) {\n nodeFsDriverState.baseDir = baseDirFromOptions;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries by recreating the directory. Callers must ensure no concurrent operations. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Iterates through all keys, invoking the iterator until it returns a value. */\n async iterate<T, U>(iterator: (value: T, key: string, iterationNumber: number) => U): Promise<U> {\n const keys = await nodeFsDriver.keys();\n let iterationNumber = 1;\n\n for (const key of keys) {\n const value = await nodeFsDriver.getItem<T>(key);\n\n const result = iterator(value as T, key, iterationNumber);\n\n if (result !== undefined) {\n return result;\n }\n iterationNumber += 1;\n }\n\n return undefined as unknown as U;\n },\n /** Returns the key name at the given index or null when out of bounds. */\n async key(keyIndex: number): Promise<string> {\n const keys = await nodeFsDriver.keys();\n\n return keys[keyIndex] ?? null as unknown as string;\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Returns the count of stored keys. */\n async length(): Promise<number> {\n const keys = await nodeFsDriver.keys();\n\n return keys.length;\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\nlet driverInstance: LocalForageDriver | null = null;\n\n/**\n * Prepares and returns the filesystem-backed localForage driver.\n * We bypass localForage's default driver selection and explicitly initialize\n * our custom driver to ensure Node compatibility.\n */\nasync function ensureLocalForageReady(): Promise<LocalForageDriver> {\n if (initPromise) {\n await initPromise;\n return driverInstance as LocalForageDriver;\n }\n\n initPromise = (async () => {\n await localForage.defineDriver(nodeFsDriver);\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n const driver = await localForage.getDriver(NODE_FS_DRIVER_NAME);\n\n const initOptions = {\n baseDir: nodeFsDriverState.baseDir,\n name: STORAGE_INSTANCE_NAME,\n storeName: STORAGE_STORE_NAME,\n } satisfies NodeLocalForageOptions;\n\n await driver._initStorage(initOptions as unknown as Parameters<typeof driver._initStorage>[0]);\n driverInstance = driver;\n })().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return driverInstance as LocalForageDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * This object provides methods for storing, retrieving, and managing data using localForage,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n try {\n const driver = await ensureLocalForageReady();\n const result = await driver.getItem<T>(key);\n\n return result ?? null;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T): Promise<void> {\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.setItem(key, value);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureLocalForageReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n driverInstance = null;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;;;;AAWA,IAAM,IAAuC,EACzC,SAAS,IACZ;AAKD,SAAS,EAAU,GAAqB;AACpC,QAAO,GAAG,mBAAmB,EAAI,GAAG;;AAMxC,SAAS,EAAU,GAA0B;AACzC,QAAO,mBAAmB,EAAS,MAAM,GAAG,CAAC,EAAsB,OAAO,CAAC;;AAM/E,SAAS,EAAY,GAAa,GAAyB;AACvD,QAAO,EAAK,KAAK,GAAS,EAAU,EAAI,CAAC;;AAM7C,IAAM,IAAkC;CACpC,SAAS;CACT,UAAU;CAEV,MAAM,aAAa,GAAkB;AACjC,MAAI;GACA,IAAM,IAAqB,OAAO,KAAY,YAAY,KAAoB,aAAa,IACpF,EAAkC,UACnC,KAAA;AASN,GAPI,OAAO,KAAuB,YAAY,EAAmB,SAAS,IACtE,EAAkB,UAAU,IAG5B,EAAkB,UAAU,GAAQ,CAAC,8BAGzC,MAAM,EAAG,MAAM,EAAkB,SAAS,EAAE,WAAW,IAAM,CAAC;WAE3D,GAAO;AAEV,SADA,EAAI,MAAM,kBAAkB,EAAM,EAC5B;;;CAId,MAAM,QAAQ;EACV,IAAM,EAAE,eAAY;AAEf,QAIL,MAAM,EAAG,GAAG,GAAS;GAAE,WAA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EAAE,aAAa,MAAM,CAAC;;;CAI1D;AAOD,SAAgB,IAA+B;AAG3C,CAFA,IAAc,MACd,IAAiB,MACjB,EAAkB,UAAU"}
|
|
1
|
+
{"version":3,"file":"storage.util.js","names":[],"sources":["../../../src/node/storage/storage.util.ts"],"sourcesContent":["import localForage from 'localforage';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { getEnv } from '#config/env/index.js';\n\nimport type { LocalForageDriver, NodeFsDriverState, NodeLocalForageOptions } from './storage.type.js';\n\nimport { catchError, log } from '../log/index.js';\nimport { NODE_FS_DRIVER_NAME, STORAGE_INSTANCE_NAME, STORAGE_KEY_EXTENSION, STORAGE_STORE_NAME } from './storage.constant.js';\n\nconst nodeFsDriverState: NodeFsDriverState = {\n baseDir: '',\n};\n\n/**\n * Encodes a storage key into a filename-safe string.\n */\nfunction encodeKey(key: string): string {\n return `${encodeURIComponent(key)}${STORAGE_KEY_EXTENSION}`;\n}\n\n/**\n * Decodes a filename-safe key back to the original storage key.\n */\nfunction decodeKey(fileName: string): string {\n return decodeURIComponent(fileName.slice(0, -STORAGE_KEY_EXTENSION.length));\n}\n\n/**\n * Maps a storage key to an absolute file path inside the storage directory.\n */\nfunction getFilePath(key: string, baseDir: string): string {\n return path.join(baseDir, encodeKey(key));\n}\n\n/**\n * Custom localForage driver that stores JSON-encoded values on the filesystem.\n */\nconst nodeFsDriver: LocalForageDriver = {\n _driver: NODE_FS_DRIVER_NAME,\n _support: true,\n /** Ensures the storage directory exists and respects custom baseDir overrides. */\n async _initStorage(options: unknown) {\n try {\n const baseDirFromOptions = typeof options === 'object' && options !== null && 'baseDir' in options\n ? (options as { baseDir?: unknown }).baseDir\n : undefined;\n\n if (typeof baseDirFromOptions === 'string' && baseDirFromOptions.length > 0) {\n nodeFsDriverState.baseDir = baseDirFromOptions;\n }\n else {\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n }\n\n await fs.mkdir(nodeFsDriverState.baseDir, { recursive: true });\n }\n catch (error) {\n log.error('[Storage:init]', error);\n throw error;\n }\n },\n /** Deletes all stored entries by recreating the directory. Callers must ensure no concurrent operations. */\n async clear() {\n const { baseDir } = nodeFsDriverState;\n\n if (!baseDir) {\n return;\n }\n\n await fs.rm(baseDir, { recursive: true, force: true });\n await fs.mkdir(baseDir, { recursive: true });\n },\n /** Reads and parses a stored value; returns null when the file is missing. */\n async getItem<T>(key: string): Promise<T | null> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n try {\n const content = await fs.readFile(filePath, 'utf8');\n\n return JSON.parse(content) as T;\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return null;\n }\n throw error;\n }\n },\n /** Iterates through all keys, invoking the iterator until it returns a value. */\n async iterate<T, U>(iterator: (value: T, key: string, iterationNumber: number) => U): Promise<U> {\n const keys = await nodeFsDriver.keys();\n let iterationNumber = 1;\n\n for (const key of keys) {\n const value = await nodeFsDriver.getItem<T>(key);\n\n const result = iterator(value as T, key, iterationNumber);\n\n if (result !== undefined) {\n return result;\n }\n iterationNumber += 1;\n }\n\n return undefined as unknown as U;\n },\n /** Returns the key name at the given index or null when out of bounds. */\n async key(keyIndex: number): Promise<string> {\n const keys = await nodeFsDriver.keys();\n\n return keys[keyIndex] ?? null as unknown as string;\n },\n /** Lists all stored keys. */\n async keys(): Promise<string[]> {\n const { baseDir } = nodeFsDriverState;\n\n try {\n const files = await fs.readdir(baseDir);\n\n return files\n .filter(file => file.endsWith(STORAGE_KEY_EXTENSION))\n .map(decodeKey);\n }\n catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') {\n return [];\n }\n throw error;\n }\n },\n /** Returns the count of stored keys. */\n async length(): Promise<number> {\n const keys = await nodeFsDriver.keys();\n\n return keys.length;\n },\n /** Removes a stored value for the given key. */\n async removeItem(key: string): Promise<void> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.rm(filePath, { force: true });\n },\n /** Stores a value as JSON on disk. */\n async setItem<T>(key: string, value: T): Promise<T> {\n const { baseDir } = nodeFsDriverState;\n const filePath = getFilePath(key, baseDir);\n\n await fs.mkdir(baseDir, { recursive: true });\n await fs.writeFile(filePath, JSON.stringify(value), 'utf8');\n\n return value;\n },\n};\n\nlet initPromise: Promise<void> | null = null;\nlet driverInstance: LocalForageDriver | null = null;\n\n/**\n * Prepares and returns the filesystem-backed localForage driver.\n * We bypass localForage's default driver selection and explicitly initialize\n * our custom driver to ensure Node compatibility.\n */\nasync function ensureLocalForageReady(): Promise<LocalForageDriver> {\n if (initPromise) {\n await initPromise;\n return driverInstance as LocalForageDriver;\n }\n\n initPromise = (async () => {\n await localForage.defineDriver(nodeFsDriver);\n nodeFsDriverState.baseDir = getEnv().CYBERSKILL_STORAGE_DIRECTORY;\n const driver = await localForage.getDriver(NODE_FS_DRIVER_NAME);\n\n const initOptions = {\n baseDir: nodeFsDriverState.baseDir,\n name: STORAGE_INSTANCE_NAME,\n storeName: STORAGE_STORE_NAME,\n } satisfies NodeLocalForageOptions;\n\n await driver._initStorage(initOptions as unknown as Parameters<typeof driver._initStorage>[0]);\n driverInstance = driver;\n })().catch((error) => {\n initPromise = null;\n throw error;\n });\n\n await initPromise;\n\n return driverInstance as LocalForageDriver;\n}\n\n/**\n * Persistent storage utility object for data persistence across application sessions.\n * This object provides methods for storing, retrieving, and managing data using localForage,\n * with automatic initialization and error handling.\n */\nexport const storage = {\n /**\n * Retrieves a value from persistent storage by key.\n * This method fetches data that was previously stored using the set method.\n * Returns null if the key doesn't exist or if an error occurs.\n *\n * @param key - The unique identifier for the stored value.\n * @returns A promise that resolves to the stored value or null if not found.\n */\n async get<T = unknown>(key: string): Promise<T | null> {\n try {\n const driver = await ensureLocalForageReady();\n const result = await driver.getItem<T>(key);\n\n return result ?? null;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n /**\n * Stores a value in persistent storage with a unique key.\n * This method saves data that can be retrieved later using the get method.\n * The data is automatically serialized and stored in the configured storage directory.\n *\n * @param key - The unique identifier for the value to store.\n * @param value - The data to store (will be automatically serialized).\n * @returns A promise that resolves when the storage operation is complete.\n */\n async set<T = unknown>(key: string, value: T): Promise<void> {\n const driver = await ensureLocalForageReady();\n\n await driver.setItem(key, value);\n },\n /**\n * Removes a value from persistent storage by key.\n * This method permanently deletes the stored data associated with the specified key.\n *\n * @param key - The unique identifier of the value to remove.\n * @returns A promise that resolves when the removal operation is complete.\n */\n async remove(key: string): Promise<void> {\n try {\n const driver = await ensureLocalForageReady();\n\n await driver.removeItem(key);\n }\n catch (error) {\n catchError(error);\n }\n },\n /**\n * Retrieves all storage keys.\n * This method returns an array of all keys that currently have stored values.\n * Returns an empty array if no keys exist or if an error occurs.\n *\n * @returns A promise that resolves to an array of storage keys.\n */\n async keys(): Promise<string[]> {\n try {\n const driver = await ensureLocalForageReady();\n const keys = await driver.keys();\n\n if (!Array.isArray(keys)) {\n log.warn(`[Storage:keys] Invalid keys response:`, keys);\n return [];\n }\n\n return keys;\n }\n catch (error) {\n return catchError(error, { returnValue: [] });\n }\n },\n /**\n * Gets a human-readable log link for a storage key.\n * This method provides a formatted string that shows the storage directory path\n * and the key name for debugging and manual inspection purposes.\n *\n * @param key - The storage key to generate a log link for.\n * @returns A promise that resolves to a formatted log link string or null if an error occurs.\n */\n async getLogLink(key: string): Promise<string | null> {\n try {\n const storagePath = path.join(getEnv().CYBERSKILL_STORAGE_DIRECTORY, encodeKey(key));\n\n return `${storagePath} (key: ${key})`;\n }\n catch (error) {\n return catchError(error, { returnValue: null });\n }\n },\n\n};\n\n/**\n * Resets all module-level singleton state used by the storage module.\n * Intended for use in tests to ensure isolation between test cases.\n * Do NOT call this in production code.\n */\nexport function resetStorageForTesting(): void {\n initPromise = null;\n driverInstance = null;\n nodeFsDriverState.baseDir = '';\n}\n"],"mappings":";;;;;;;AAWA,IAAM,IAAuC,EACzC,SAAS,IACZ;AAKD,SAAS,EAAU,GAAqB;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,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAYvD,MAAM,IAAiB,GAAa,GAAyB;AAGzD,SAFe,MAAM,GAAwB,EAEhC,QAAQ,GAAK,EAAM;;CASpC,MAAM,OAAO,GAA4B;AACrC,MAAI;AAGA,UAFe,MAAM,GAAwB,EAEhC,WAAW,EAAI;WAEzB,GAAO;AACV,KAAW,EAAM;;;CAUzB,MAAM,OAA0B;AAC5B,MAAI;GAEA,IAAM,IAAO,OADE,MAAM,GAAwB,EACnB,MAAM;AAOhC,UALK,MAAM,QAAQ,EAAK,GAKjB,KAJH,EAAI,KAAK,yCAAyC,EAAK,EAChD,EAAE;WAKV,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,EAAE,EAAE,CAAC;;;CAWrD,MAAM,WAAW,GAAqC;AAClD,MAAI;AAGA,UAAO,GAFa,EAAK,KAAK,GAAQ,CAAC,8BAA8B,EAAU,EAAI,CAAC,CAE9D,SAAS,EAAI;WAEhC,GAAO;AACV,UAAO,EAAW,GAAO,EAAE,aAAa,MAAM,CAAC;;;CAI1D;AAOD,SAAgB,IAA+B;AAG3C,CAFA,IAAc,MACd,IAAiB,MACjB,EAAkB,UAAU"}
|
package/dist/node/ws/ws.util.js
CHANGED
|
@@ -3,6 +3,8 @@ import { WebSocketServer as t } from "ws";
|
|
|
3
3
|
//#region src/node/ws/ws.util.ts
|
|
4
4
|
function n(e) {
|
|
5
5
|
let { server: n, path: r, sessionParser: i } = e;
|
|
6
|
+
if (!n) throw Error("[WS] HTTP server instance is required to create a WebSocket server.");
|
|
7
|
+
if (!r || !r.startsWith("/")) throw Error("[WS] WebSocket path must be a non-empty string starting with \"/\".");
|
|
6
8
|
if (i) {
|
|
7
9
|
let e = new t({ noServer: !0 });
|
|
8
10
|
return n.on("upgrade", (t, n, a) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws.util.js","names":[],"sources":["../../../src/node/ws/ws.util.ts"],"sourcesContent":["import type { Request, Response } from 'express';\nimport type { Buffer } from 'node:buffer';\nimport type { IncomingMessage } from 'node:http';\nimport type { Duplex } from 'node:stream';\n\nimport { useServer as createGraphQLWSServer } from 'graphql-ws/use/ws';\nimport { WebSocketServer } from 'ws';\n\nimport type { I_AuthenticatedRequest, I_GraphqlWSOptions, I_WSOptions } from './ws.type.js';\n\n/**\n * Creates a WebSocket server with the specified configuration.\n * This function creates a WebSocket server instance that can be attached to an HTTP server\n * and configured with a specific path for WebSocket connections.\n *\n * @param options - Configuration options including the HTTP server instance and WebSocket path.\n * @returns A configured WebSocket server instance ready to handle connections.\n */\nexport function createWSServer(options: I_WSOptions): WebSocketServer {\n const { server, path, sessionParser } = options;\n\n if (sessionParser) {\n const wss = new WebSocketServer({ noServer: true });\n\n server.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer) => {\n try {\n const url = new URL(req.url || '', 'http://localhost');\n if (url.pathname !== path)\n return;\n\n sessionParser(req as Request, {} as Response, () => {\n wss.handleUpgrade(req, socket, head, (ws) => {\n wss.emit('connection', ws, req);\n });\n });\n }\n catch {\n socket.destroy();\n }\n });\n\n return wss;\n }\n\n return new WebSocketServer({ server, path });\n}\n\n/**\n * Initializes GraphQL WebSocket server with schema and WebSocket server.\n * This function sets up GraphQL subscriptions over WebSocket by creating a GraphQL WebSocket server\n * that can handle GraphQL operations including queries, mutations, and subscriptions.\n *\n * @param options - Configuration options including the GraphQL schema and WebSocket server instance.\n * @returns A configured GraphQL WebSocket server ready to handle GraphQL operations over WebSocket.\n */\nexport function initGraphQLWS(options: I_GraphqlWSOptions) {\n const { schema, server, context: makeExtraContext, onConnect } = options;\n\n return createGraphQLWSServer(\n {\n schema,\n context: async (ctx) => {\n const req = ctx.extra.request as I_AuthenticatedRequest;\n\n const extra = makeExtraContext ? await makeExtraContext(req) : {};\n return { req, ...extra };\n },\n onConnect: async (ctx) => {\n if (onConnect) {\n const req = ctx.extra.request as I_AuthenticatedRequest;\n await onConnect(req);\n }\n },\n },\n server,\n );\n}\n"],"mappings":";;;AAkBA,SAAgB,EAAe,GAAuC;CAClE,IAAM,EAAE,WAAQ,SAAM,qBAAkB;AAExC,KAAI,GAAe;EACf,IAAM,IAAM,IAAI,EAAgB,EAAE,UAAU,IAAM,CAAC;AAmBnD,SAjBA,EAAO,GAAG,YAAY,GAAsB,GAAgB,MAAiB;AACzE,OAAI;AAEA,QADY,IAAI,IAAI,EAAI,OAAO,IAAI,mBAAmB,CAC9C,aAAa,EACjB;AAEJ,MAAc,GAAgB,EAAE,QAAoB;AAChD,OAAI,cAAc,GAAK,GAAQ,IAAO,MAAO;AACzC,QAAI,KAAK,cAAc,GAAI,EAAI;OACjC;MACJ;WAEA;AACF,MAAO,SAAS;;IAEtB,EAEK;;AAGX,QAAO,IAAI,EAAgB;EAAE;EAAQ;EAAM,CAAC;;AAWhD,SAAgB,EAAc,GAA6B;CACvD,IAAM,EAAE,WAAQ,WAAQ,SAAS,GAAkB,iBAAc;AAEjE,QAAO,EACH;EACI;EACA,SAAS,OAAO,MAAQ;GACpB,IAAM,IAAM,EAAI,MAAM;AAGtB,UAAO;IAAE;IAAK,GADA,IAAmB,MAAM,EAAiB,EAAI,GAAG,EAAE;IACzC;;EAE5B,WAAW,OAAO,MAAQ;AACtB,OAAI,GAAW;IACX,IAAM,IAAM,EAAI,MAAM;AACtB,UAAM,EAAU,EAAI;;;EAG/B,EACD,EACH"}
|
|
1
|
+
{"version":3,"file":"ws.util.js","names":[],"sources":["../../../src/node/ws/ws.util.ts"],"sourcesContent":["import type { Request, Response } from 'express';\nimport type { Buffer } from 'node:buffer';\nimport type { IncomingMessage } from 'node:http';\nimport type { Duplex } from 'node:stream';\n\nimport { useServer as createGraphQLWSServer } from 'graphql-ws/use/ws';\nimport { WebSocketServer } from 'ws';\n\nimport type { I_AuthenticatedRequest, I_GraphqlWSOptions, I_WSOptions } from './ws.type.js';\n\n/**\n * Creates a WebSocket server with the specified configuration.\n * This function creates a WebSocket server instance that can be attached to an HTTP server\n * and configured with a specific path for WebSocket connections.\n *\n * @param options - Configuration options including the HTTP server instance and WebSocket path.\n * @returns A configured WebSocket server instance ready to handle connections.\n */\nexport function createWSServer(options: I_WSOptions): WebSocketServer {\n const { server, path, sessionParser } = options;\n\n if (!server) {\n throw new Error('[WS] HTTP server instance is required to create a WebSocket server.');\n }\n\n if (!path || !path.startsWith('/')) {\n throw new Error('[WS] WebSocket path must be a non-empty string starting with \"/\".');\n }\n\n if (sessionParser) {\n const wss = new WebSocketServer({ noServer: true });\n\n server.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer) => {\n try {\n const url = new URL(req.url || '', 'http://localhost');\n if (url.pathname !== path)\n return;\n\n sessionParser(req as Request, {} as Response, () => {\n wss.handleUpgrade(req, socket, head, (ws) => {\n wss.emit('connection', ws, req);\n });\n });\n }\n catch {\n socket.destroy();\n }\n });\n\n return wss;\n }\n\n return new WebSocketServer({ server, path });\n}\n\n/**\n * Initializes GraphQL WebSocket server with schema and WebSocket server.\n * This function sets up GraphQL subscriptions over WebSocket by creating a GraphQL WebSocket server\n * that can handle GraphQL operations including queries, mutations, and subscriptions.\n *\n * @param options - Configuration options including the GraphQL schema and WebSocket server instance.\n * @returns A configured GraphQL WebSocket server ready to handle GraphQL operations over WebSocket.\n */\nexport function initGraphQLWS(options: I_GraphqlWSOptions) {\n const { schema, server, context: makeExtraContext, onConnect } = options;\n\n return createGraphQLWSServer(\n {\n schema,\n context: async (ctx) => {\n const req = ctx.extra.request as I_AuthenticatedRequest;\n\n const extra = makeExtraContext ? await makeExtraContext(req) : {};\n return { req, ...extra };\n },\n onConnect: async (ctx) => {\n if (onConnect) {\n const req = ctx.extra.request as I_AuthenticatedRequest;\n await onConnect(req);\n }\n },\n },\n server,\n );\n}\n"],"mappings":";;;AAkBA,SAAgB,EAAe,GAAuC;CAClE,IAAM,EAAE,WAAQ,SAAM,qBAAkB;AAExC,KAAI,CAAC,EACD,OAAU,MAAM,sEAAsE;AAG1F,KAAI,CAAC,KAAQ,CAAC,EAAK,WAAW,IAAI,CAC9B,OAAU,MAAM,sEAAoE;AAGxF,KAAI,GAAe;EACf,IAAM,IAAM,IAAI,EAAgB,EAAE,UAAU,IAAM,CAAC;AAmBnD,SAjBA,EAAO,GAAG,YAAY,GAAsB,GAAgB,MAAiB;AACzE,OAAI;AAEA,QADY,IAAI,IAAI,EAAI,OAAO,IAAI,mBAAmB,CAC9C,aAAa,EACjB;AAEJ,MAAc,GAAgB,EAAE,QAAoB;AAChD,OAAI,cAAc,GAAK,GAAQ,IAAO,MAAO;AACzC,QAAI,KAAK,cAAc,GAAI,EAAI;OACjC;MACJ;WAEA;AACF,MAAO,SAAS;;IAEtB,EAEK;;AAGX,QAAO,IAAI,EAAgB;EAAE;EAAQ;EAAM,CAAC;;AAWhD,SAAgB,EAAc,GAA6B;CACvD,IAAM,EAAE,WAAQ,WAAQ,SAAS,GAAkB,iBAAc;AAEjE,QAAO,EACH;EACI;EACA,SAAS,OAAO,MAAQ;GACpB,IAAM,IAAM,EAAI,MAAM;AAGtB,UAAO;IAAE;IAAK,GADA,IAAmB,MAAM,EAAiB,EAAI,GAAG,EAAE;IACzC;;EAE5B,WAAW,OAAO,MAAQ;AACtB,OAAI,GAAW;IACX,IAAM,IAAM,EAAI,MAAM;AACtB,UAAM,EAAU,EAAI;;;EAG/B,EACD,EACH"}
|