mongoose-tsgen
Advanced tools
Comparing version 9.5.0 to 9.5.1
@@ -6,7 +6,11 @@ "use strict"; | ||
const helpFlag = (opts = {}) => { | ||
return core_1.Flags.boolean(Object.assign(Object.assign({ description: "Show CLI help." }, opts), { parse: async (_, cmd) => { | ||
return core_1.Flags.boolean({ | ||
description: "Show CLI help.", | ||
...opts, | ||
parse: async (_, cmd) => { | ||
new core_1.Help(cmd.config).showCommandHelp(cmd.constructor); | ||
return cmd.exit(0); | ||
} })); | ||
} | ||
}); | ||
}; | ||
exports.helpFlag = helpFlag; |
@@ -17,3 +17,6 @@ "use strict"; | ||
const ogContent = fs_1.default.readFileSync(filePath); | ||
const formattedContent = prettier_1.default.format(ogContent.toString(), Object.assign(Object.assign({}, config), { parser: "typescript" })); | ||
const formattedContent = prettier_1.default.format(ogContent.toString(), { | ||
...config, | ||
parser: "typescript" | ||
}); | ||
fs_1.default.writeFileSync(filePath, formattedContent); | ||
@@ -20,0 +23,0 @@ }); |
import { SourceFile } from "ts-morph"; | ||
import { TsReaderModelTypes } from "../types"; | ||
import { MongooseModel } from "../parser/types"; | ||
export declare const cleanComment: (comment: string) => string; | ||
export declare const sanitizeModelName: (name: string) => string; | ||
export declare const convertFuncSignatureToType: (funcSignature: string, funcType: "query" | "methods" | "statics", modelName: string) => string; | ||
export declare const replaceModelTypes: (sourceFile: SourceFile, modelTypes: TsReaderModelTypes, models: MongooseModel[]) => void; | ||
@@ -5,0 +8,0 @@ export declare const addPopulateHelpers: (sourceFile: SourceFile) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.saveFile = exports.generateTypes = exports.getSchemaTypes = exports.parseFunctions = exports.createSourceFile = exports.overloadQueryPopulate = exports.addPopulateHelpers = exports.replaceModelTypes = void 0; | ||
exports.saveFile = exports.generateTypes = exports.getSchemaTypes = exports.parseFunctions = exports.createSourceFile = exports.overloadQueryPopulate = exports.addPopulateHelpers = exports.replaceModelTypes = exports.convertFuncSignatureToType = exports.sanitizeModelName = exports.cleanComment = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -10,35 +10,63 @@ const ts_morph_1 = require("ts-morph"); | ||
const stringBuilder_1 = require("../writer/stringBuilder"); | ||
const typeSanitization_1 = require("./typeSanitization"); | ||
// TODO next: Pull this file apart. Create a new "file writer" file, move all the ts stuff somewhere else, | ||
// this strips comments of special tokens since ts-morph generates jsdoc tokens automatically | ||
const cleanComment = (comment) => { | ||
if (!comment) | ||
return ""; | ||
if (comment.trim() === "/** */") | ||
return ""; | ||
return comment | ||
.replace(/^\/\*\*[^\S\r\n]?/, "") | ||
.replace(/[^\S\r\n]+\*\s/g, "") | ||
.replace(/(\n)?[^\S\r\n]+\*\/$/, ""); | ||
.replace(/^\/\*\*[^\S\r\n]?/, "") // Remove opening /** | ||
.replace(/[^\S\r\n]+\*\s/g, "") // Remove * at start of lines | ||
.replace(/(\n)?[^\S\r\n]+\*\/$/, ""); // Remove closing */ | ||
}; | ||
const convertFuncSignatureToType = (funcSignature, funcType, modelName) => { | ||
var _a; | ||
const [, params, returnType] = (_a = funcSignature.match(/\((?:this: \w*(?:, )?)?(.*)\) => (.*)/)) !== null && _a !== void 0 ? _a : []; | ||
let type; | ||
if (funcType === "query") { | ||
type = `(this: ${modelName}Query${(params === null || params === void 0 ? void 0 : params.length) > 0 ? ", " + params : ""}) => ${modelName}Query`; | ||
exports.cleanComment = cleanComment; | ||
// Needs to be exported by generator Module | ||
const sanitizeModelName = (name) => (0, typeSanitization_1.sanitizeTypeIdentifier)(name); | ||
exports.sanitizeModelName = sanitizeModelName; | ||
const funcTypeToThisSuffix = { | ||
query: "Query", | ||
methods: "Document", | ||
statics: "Model" | ||
}; | ||
const parseSignature = (signature, modelName, funcType) => { | ||
const thisSuffix = funcTypeToThisSuffix[funcType]; | ||
const thisType = `${modelName}${thisSuffix}`; | ||
const queryReturnType = `${modelName}Query`; | ||
const match = signature === null || signature === void 0 ? void 0 : signature.match(/\((?:this: \w*(?:, )?)?(?<params>.*)\) => (?<returnType>.*)/); | ||
if (!(match === null || match === void 0 ? void 0 : match.groups)) { | ||
console.warn(`Failed to extract types from function signature: ${signature}, falling back to defaults`); | ||
const defaultReturnType = funcType === "query" ? queryReturnType : "any"; | ||
const defaultParams = "...args: any[]"; | ||
return { | ||
params: defaultParams, | ||
returnType: defaultReturnType, | ||
thisType | ||
}; | ||
} | ||
else if (funcType === "methods") { | ||
type = `(this: ${modelName}Document${(params === null || params === void 0 ? void 0 : params.length) > 0 ? ", " + params : ""}) => ${returnType !== null && returnType !== void 0 ? returnType : "any"}`; | ||
} | ||
else { | ||
type = `(this: ${modelName}Model${(params === null || params === void 0 ? void 0 : params.length) > 0 ? ", " + params : ""}) => ${returnType !== null && returnType !== void 0 ? returnType : "any"}`; | ||
} | ||
return type; | ||
const finalReturnType = funcType === "query" ? queryReturnType : match.groups.returnType; | ||
return { | ||
params: match.groups.params, | ||
returnType: finalReturnType, | ||
thisType | ||
}; | ||
}; | ||
const convertFuncSignatureToType = (funcSignature, funcType, modelName) => { | ||
const sanitizedModelName = (0, exports.sanitizeModelName)(modelName); | ||
const { params, returnType, thisType } = parseSignature(funcSignature, sanitizedModelName, funcType); | ||
const paramsString = (params === null || params === void 0 ? void 0 : params.length) > 0 ? `, ${params}` : ""; | ||
return `(this: ${thisType}${paramsString}) => ${returnType}`; | ||
}; | ||
exports.convertFuncSignatureToType = convertFuncSignatureToType; | ||
const replaceModelTypes = (sourceFile, modelTypes, models) => { | ||
Object.entries(modelTypes).forEach(([modelName, types]) => { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r; | ||
const sanitizedModelName = (0, exports.sanitizeModelName)(modelName); | ||
const { methods, statics, query, virtuals, comments } = types; | ||
// methods | ||
if (Object.keys(methods).length > 0) { | ||
(_b = (_a = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}Methods`)) === null || _a === void 0 ? void 0 : _a.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _b === void 0 ? void 0 : _b.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
(_b = (_a = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}Methods`)) === null || _a === void 0 ? void 0 : _a.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _b === void 0 ? void 0 : _b.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
const signature = methods[prop.getName()]; | ||
if (signature) { | ||
const funcType = convertFuncSignatureToType(signature, "methods", modelName); | ||
const funcType = (0, exports.convertFuncSignatureToType)(signature, "methods", modelName); | ||
prop.setType(funcType); | ||
@@ -50,6 +78,6 @@ } | ||
if (Object.keys(statics).length > 0) { | ||
(_d = (_c = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}Statics`)) === null || _c === void 0 ? void 0 : _c.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _d === void 0 ? void 0 : _d.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
(_d = (_c = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}Statics`)) === null || _c === void 0 ? void 0 : _c.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _d === void 0 ? void 0 : _d.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
const signature = statics[prop.getName()]; | ||
if (signature) { | ||
const funcType = convertFuncSignatureToType(signature, "statics", modelName); | ||
const funcType = (0, exports.convertFuncSignatureToType)(signature, "statics", modelName); | ||
prop.setType(funcType); | ||
@@ -61,6 +89,6 @@ } | ||
if (Object.keys(query).length > 0) { | ||
(_f = (_e = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}Queries`)) === null || _e === void 0 ? void 0 : _e.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _f === void 0 ? void 0 : _f.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
(_f = (_e = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}Queries`)) === null || _e === void 0 ? void 0 : _e.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _f === void 0 ? void 0 : _f.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature).forEach((prop) => { | ||
const signature = query[prop.getName()]; | ||
if (signature) { | ||
const funcType = convertFuncSignatureToType(signature, "query", modelName); | ||
const funcType = (0, exports.convertFuncSignatureToType)(signature, "query", modelName); | ||
prop.setType(funcType); | ||
@@ -73,6 +101,6 @@ } | ||
if (virtualNames.length > 0) { | ||
const documentProperties = (_j = (_h = (_g = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}Document`)) === null || _g === void 0 ? void 0 : _g.getFirstChildByKind(ts_morph_1.SyntaxKind.IntersectionType)) === null || _h === void 0 ? void 0 : _h.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _j === void 0 ? void 0 : _j.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
const documentProperties = (_j = (_h = (_g = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}Document`)) === null || _g === void 0 ? void 0 : _g.getFirstChildByKind(ts_morph_1.SyntaxKind.IntersectionType)) === null || _h === void 0 ? void 0 : _h.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _j === void 0 ? void 0 : _j.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
const { schema } = models.find((model) => model.modelName === modelName); | ||
const leanProperties = (0, utils_1.getShouldLeanIncludeVirtuals)(schema) && | ||
((_l = (_k = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}`)) === null || _k === void 0 ? void 0 : _k.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _l === void 0 ? void 0 : _l.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature)); | ||
((_l = (_k = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}`)) === null || _k === void 0 ? void 0 : _k.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _l === void 0 ? void 0 : _l.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature)); | ||
if (documentProperties || leanProperties) { | ||
@@ -110,4 +138,4 @@ virtualNames.forEach((virtualName) => { | ||
if (comments.length > 0) { | ||
const documentProperties = (_p = (_o = (_m = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}Document`)) === null || _m === void 0 ? void 0 : _m.getFirstChildByKind(ts_morph_1.SyntaxKind.IntersectionType)) === null || _o === void 0 ? void 0 : _o.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _p === void 0 ? void 0 : _p.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
const leanProperties = (_r = (_q = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${modelName}`)) === null || _q === void 0 ? void 0 : _q.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _r === void 0 ? void 0 : _r.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
const documentProperties = (_p = (_o = (_m = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}Document`)) === null || _m === void 0 ? void 0 : _m.getFirstChildByKind(ts_morph_1.SyntaxKind.IntersectionType)) === null || _o === void 0 ? void 0 : _o.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _p === void 0 ? void 0 : _p.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
const leanProperties = (_r = (_q = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getTypeAlias(`${sanitizedModelName}`)) === null || _q === void 0 ? void 0 : _q.getFirstChildByKind(ts_morph_1.SyntaxKind.TypeLiteral)) === null || _r === void 0 ? void 0 : _r.getChildrenOfKind(ts_morph_1.SyntaxKind.PropertySignature); | ||
comments.forEach(({ path, comment }) => { | ||
@@ -122,7 +150,7 @@ const pathComponents = path.split("."); | ||
const docPropMatch = (nestedDocProps !== null && nestedDocProps !== void 0 ? nestedDocProps : documentProperties).find((prop) => prop.getName() === nameComponent); | ||
docPropMatch === null || docPropMatch === void 0 ? void 0 : docPropMatch.addJsDoc(cleanComment(comment)); | ||
docPropMatch === null || docPropMatch === void 0 ? void 0 : docPropMatch.addJsDoc((0, exports.cleanComment)(comment)); | ||
} | ||
if (leanProperties) { | ||
const leanPropMatch = (nestedLeanProps !== null && nestedLeanProps !== void 0 ? nestedLeanProps : leanProperties).find((prop) => prop.getName() === nameComponent); | ||
leanPropMatch === null || leanPropMatch === void 0 ? void 0 : leanPropMatch.addJsDoc(cleanComment(comment)); | ||
leanPropMatch === null || leanPropMatch === void 0 ? void 0 : leanPropMatch.addJsDoc((0, exports.cleanComment)(comment)); | ||
} | ||
@@ -166,3 +194,3 @@ return; | ||
const funcSignature = "(...args: any[]) => any"; | ||
const type = convertFuncSignatureToType(funcSignature, funcType, modelName); | ||
const type = (0, exports.convertFuncSignatureToType)(funcSignature, funcType, modelName); | ||
interfaceString += (0, stringBuilder_1.convertKeyValueToLine)({ key, valueType: type }); | ||
@@ -176,27 +204,28 @@ }); | ||
const { modelName, schema } = model; | ||
const sanitizedModelName = (0, exports.sanitizeModelName)(modelName); | ||
let schemaTypes = ""; | ||
// add type alias to modelName so that it can be imported without clashing with the mongoose model | ||
schemaTypes += templates.getObjectDocs(modelName); | ||
schemaTypes += `\nexport type ${modelName}Object = ${modelName}\n\n`; | ||
schemaTypes += templates.getObjectDocs(sanitizedModelName); | ||
schemaTypes += `\nexport type ${sanitizedModelName}Object = ${sanitizedModelName}\n\n`; | ||
schemaTypes += templates.getQueryDocs(); | ||
schemaTypes += `\nexport type ${modelName}Query = mongoose.Query<any, ${modelName}Document, ${modelName}Queries> & ${modelName}Queries\n\n`; | ||
schemaTypes += templates.getQueryHelpersDocs(modelName); | ||
schemaTypes += `\nexport type ${modelName}Queries = {\n`; | ||
schemaTypes += `\nexport type ${sanitizedModelName}Query = mongoose.Query<any, ${sanitizedModelName}Document, ${sanitizedModelName}Queries> & ${sanitizedModelName}Queries\n\n`; | ||
schemaTypes += templates.getQueryHelpersDocs(sanitizedModelName); | ||
schemaTypes += `\nexport type ${sanitizedModelName}Queries = {\n`; | ||
schemaTypes += (0, exports.parseFunctions)((_a = schema.query) !== null && _a !== void 0 ? _a : {}, modelName, "query"); | ||
schemaTypes += "}\n"; | ||
schemaTypes += `\nexport type ${modelName}Methods = {\n`; | ||
schemaTypes += `\nexport type ${sanitizedModelName}Methods = {\n`; | ||
schemaTypes += (0, exports.parseFunctions)(schema.methods, modelName, "methods"); | ||
schemaTypes += "}\n"; | ||
schemaTypes += `\nexport type ${modelName}Statics = {\n`; | ||
schemaTypes += `\nexport type ${sanitizedModelName}Statics = {\n`; | ||
schemaTypes += (0, exports.parseFunctions)(schema.statics, modelName, "statics"); | ||
schemaTypes += "}\n\n"; | ||
const modelExtend = `mongoose.Model<${modelName}Document, ${modelName}Queries>`; | ||
schemaTypes += templates.getModelDocs(modelName); | ||
schemaTypes += `\nexport type ${modelName}Model = ${modelExtend} & ${modelName}Statics\n\n`; | ||
schemaTypes += templates.getSchemaDocs(modelName); | ||
schemaTypes += `\nexport type ${modelName}Schema = mongoose.Schema<${modelName}Document, ${modelName}Model, ${modelName}Methods, ${modelName}Queries>\n\n`; | ||
const modelExtend = `mongoose.Model<${sanitizedModelName}Document, ${sanitizedModelName}Queries>`; | ||
schemaTypes += templates.getModelDocs(sanitizedModelName); | ||
schemaTypes += `\nexport type ${sanitizedModelName}Model = ${modelExtend} & ${sanitizedModelName}Statics\n\n`; | ||
schemaTypes += templates.getSchemaDocs(sanitizedModelName); | ||
schemaTypes += `\nexport type ${sanitizedModelName}Schema = mongoose.Schema<${sanitizedModelName}Document, ${sanitizedModelName}Model, ${sanitizedModelName}Methods, ${sanitizedModelName}Queries>\n\n`; | ||
return schemaTypes; | ||
}; | ||
exports.getSchemaTypes = getSchemaTypes; | ||
// TODO: This should be split up, shouldnt be writing to file and parsing schema simultaneously. Instead parse shema first then write later. | ||
// TODO: This should be split up, shouldn't be writing to file and parsing schema simultaneously. Instead parse schema first then write later. | ||
const generateTypes = ({ sourceFile, imports = [], modelsPaths, noMongoose, datesAsStrings }) => { | ||
@@ -213,13 +242,10 @@ const models = (0, utils_1.loadModels)(modelsPaths); | ||
writer.blankLine(); | ||
// writer.write("if (true)").block(() => { | ||
// writer.write("something;"); | ||
// }); | ||
models.forEach((model) => { | ||
const { modelName, schema } = model; | ||
// passing modelName causes childSchemas to be processed | ||
const leanHeader = templates.getLeanDocs(modelName) + `\nexport type ${modelName} = {\n`; | ||
const sanitizedModelName = (0, exports.sanitizeModelName)(modelName); | ||
const leanHeader = templates.getLeanDocs(sanitizedModelName) + `\nexport type ${sanitizedModelName} = {\n`; | ||
const leanFooter = "}"; | ||
const parserSchema = new schema_1.ParserSchema({ | ||
mongooseSchema: schema, | ||
modelName, | ||
modelName: sanitizedModelName, | ||
model | ||
@@ -249,7 +275,7 @@ }); | ||
: "any"; | ||
const mongooseDocExtend = `mongoose.Document<${_idType}, ${modelName}Queries>`; | ||
const mongooseDocExtend = `mongoose.Document<${_idType}, ${sanitizedModelName}Queries>`; | ||
let documentInterfaceStr = ""; | ||
documentInterfaceStr += (0, exports.getSchemaTypes)(model); | ||
const documentHeader = templates.getDocumentDocs(modelName) + | ||
`\nexport type ${modelName}Document = ${mongooseDocExtend} & ${modelName}Methods & {\n`; | ||
const documentHeader = templates.getDocumentDocs(sanitizedModelName) + | ||
`\nexport type ${sanitizedModelName}Document = ${mongooseDocExtend} & ${sanitizedModelName}Methods & {\n`; | ||
const documentFooter = "}"; | ||
@@ -256,0 +282,0 @@ documentInterfaceStr += parserSchema.generateTemplate({ |
@@ -288,3 +288,6 @@ "use strict"; | ||
modelTypes = findCommentsInFile(sourceFile, modelTypes, maxCommentDepth); | ||
allModelTypes = Object.assign(Object.assign({}, allModelTypes), modelTypes); | ||
allModelTypes = { | ||
...allModelTypes, | ||
...modelTypes | ||
}; | ||
}); | ||
@@ -365,3 +368,6 @@ return allModelTypes; | ||
// Merge paths from extendedConfig into tsConfig | ||
tsConfig.compilerOptions.paths = Object.assign(Object.assign({}, extendedConfig.compilerOptions.paths), tsConfig.compilerOptions.paths); | ||
tsConfig.compilerOptions.paths = { | ||
...extendedConfig.compilerOptions.paths, | ||
...tsConfig.compilerOptions.paths | ||
}; | ||
// We only want to set the base URL if its not already set, since the child tsconfig should always overwrite extended tsconfigs. | ||
@@ -368,0 +374,0 @@ // So the first child we find with a base URL be the final base URL |
@@ -18,9 +18,16 @@ "use strict"; | ||
return { | ||
flags: Object.assign(Object.assign(Object.assign({}, configFileFlags), customConfig.flags), { | ||
flags: { | ||
...configFileFlags, | ||
...customConfig.flags, | ||
// We dont need the config field anymore now that we've merged the config file here | ||
config: undefined, | ||
config: undefined, | ||
// we cant set flags as `default` using the official oclif method since the defaults would overwrite flags provided in the config file. | ||
// instead, well just set "output" and "project" as default manually if theyre still missing after merge with configFile. | ||
output: (_b = (_a = configFileFlags === null || configFileFlags === void 0 ? void 0 : configFileFlags.output) !== null && _a !== void 0 ? _a : customConfig.flags.output) !== null && _b !== void 0 ? _b : "./src/interfaces", project: (_d = (_c = configFileFlags === null || configFileFlags === void 0 ? void 0 : configFileFlags.project) !== null && _c !== void 0 ? _c : customConfig.flags.project) !== null && _d !== void 0 ? _d : "./" }), | ||
args: Object.assign(Object.assign({}, configFileFlags), customConfig.args) | ||
output: (_b = (_a = configFileFlags === null || configFileFlags === void 0 ? void 0 : configFileFlags.output) !== null && _a !== void 0 ? _a : customConfig.flags.output) !== null && _b !== void 0 ? _b : "./src/interfaces", | ||
project: (_d = (_c = configFileFlags === null || configFileFlags === void 0 ? void 0 : configFileFlags.project) !== null && _c !== void 0 ? _c : customConfig.flags.project) !== null && _d !== void 0 ? _d : "./" | ||
}, | ||
args: { | ||
...configFileFlags, | ||
...customConfig.args | ||
} | ||
}; | ||
@@ -27,0 +34,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertKeyValueToLine = void 0; | ||
const constants_1 = require("../helpers/constants"); | ||
const convertKeyValueToLine = ({ key, valueType, isOptional = false, newline = true }) => { | ||
let line = ""; | ||
if (key) { | ||
// If the key contains any special characters, we need to wrap it in quotes | ||
line += /^\w*$/.test(key) ? key : JSON.stringify(key); | ||
// Check if the key is a valid TypeScript identifier: | ||
// 1. Must start with a letter, underscore, or dollar sign | ||
// 2. Can contain letters, numbers, underscores, or dollar signs | ||
// 3. Cannot be a reserved keyword | ||
const isValidTsIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) && !constants_1.tsReservedKeywords.includes(key); | ||
line += isValidTsIdentifier ? key : JSON.stringify(key); | ||
if (isOptional) | ||
@@ -10,0 +15,0 @@ line += "?"; |
{ | ||
"name": "mongoose-tsgen", | ||
"description": "A Typescript interface generator for Mongoose that works out of the box.", | ||
"version": "9.5.0", | ||
"version": "9.5.1", | ||
"author": "Francesco Virga @francescov1", | ||
@@ -37,2 +37,3 @@ "bin": { | ||
"@types/flat": "^5.0.1", | ||
"@types/glob": "^8.1.0", | ||
"@types/jest": "^26.0.14", | ||
@@ -39,0 +40,0 @@ "@types/lodash": "^4.14.167", |
132674
32
2314
23