-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(imports): added import extraction and statements
- Loading branch information
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Model from "../model/index.js" | ||
import string from '@adonisjs/core/helpers/string' | ||
|
||
class ModelImport { | ||
declare name: string | ||
declare namespace: string | ||
|
||
isDefault = false | ||
isType = false | ||
|
||
constructor(name: string, namespace: string, isDefault: boolean | null = null, isType: boolean | null = null) { | ||
this.name = name | ||
this.namespace = namespace | ||
this.isDefault = isDefault ?? this.isDefault | ||
this.isType = isType ?? this.isType | ||
} | ||
|
||
static getStatements(imports: ModelImport[]) { | ||
const groups = this.#getNamespaceGroups(imports) | ||
|
||
return Object.values(groups).map((items) => { | ||
const defaultImport = items.find((item) => item.isDefault)?.name | ||
const namedImports = items | ||
.filter((item) => !item.isDefault) | ||
.map((item) => item.name) | ||
.join(', ') | ||
|
||
const names = [defaultImport, namedImports && `{ ${namedImports} }`] | ||
.filter(Boolean) | ||
.join(', ') | ||
|
||
return `import${items[0].isType ? ' type' : ''} ${names} from '${items[0].namespace}'` | ||
}) | ||
} | ||
|
||
static #getNamespaceGroups(imports: ModelImport[]) { | ||
return imports.reduce<Record<string, ModelImport[]>>((groups, imp) => { | ||
const group = groups[imp.namespace] || [] | ||
|
||
group.push(imp) | ||
|
||
groups[imp.namespace] = group | ||
|
||
return groups | ||
}, {}) | ||
} | ||
} | ||
|
||
export default class ModelImportManager { | ||
#imports = new Map<string, ModelImport>() | ||
|
||
add(value: ModelImport) { | ||
const name = this.#getName(value) | ||
const existing = this.#imports.get(name) | ||
|
||
if (existing && existing.isType && !value.isType) { | ||
existing.isType = false | ||
} | ||
|
||
this.#imports.set(name, existing ?? value) | ||
} | ||
|
||
#getName(value: ModelImport) { | ||
return `${value.name}@${value.namespace}` | ||
} | ||
|
||
extract(model: Model) { | ||
this.add(new ModelImport('BaseModel', '@adonisjs/lucid/orm')) | ||
this.add(new ModelImport('column', '@adonisjs/lucid/orm')) | ||
|
||
model.columns.map((column) => { | ||
if (column.type === 'DateTime') { | ||
this.add(new ModelImport('DateTime', 'luxon')) | ||
} | ||
}) | ||
|
||
model.relationships.map((definition) => { | ||
this.add(new ModelImport(definition.relatedModelName, `./${string.snakeCase(definition.relatedModelName)}.js`, true)) | ||
this.add(new ModelImport(definition.type, '@adonisjs/lucid/orm')) | ||
this.add(new ModelImport(string.pascalCase(definition.type), '@adonisjs/lucid/types/relations', false, true)) | ||
}) | ||
|
||
return ModelImport.getStatements([...this.#imports.values()]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters