Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
v1.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
k2on committed Apr 6, 2021
1 parent 2fbfc58 commit 723997f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sivrad/matrix-collection-tools",
"version": "1.0.9",
"version": "1.0.10",
"description": "Collection CI/CD.",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
43 changes: 40 additions & 3 deletions src/commands/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path';
import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { SOURCE_DIRECTORY, TYPES_DIRECTORY } from './constants';
import { BUILT_IN_TYPES, SOURCE_DIRECTORY, TYPES_DIRECTORY } from './constants';
import { formatAsLabel, formatTable, getTypeFiles } from '../../util';
import { COLLECTION_FILE_PATH, TYPE_FILES_PATH } from '../../constants';
import { Type, Field, ObjectOf, Collection } from '../../type';
Expand Down Expand Up @@ -110,7 +110,7 @@ class Builder {
for (const schemaPath of Object.keys(this.schemas)) {
const schema = this.schemas[schemaPath];
const classNameFormat = formatAsClassName(schema.name);
indexFileContent += `export { ${classNameFormat}, Serialized${classNameFormat} } from './${schema.name}';`;
indexFileContent += `export { ${classNameFormat}, Serialized${classNameFormat} } from './${schema.name}';\n`;
}
writeFileSync(indexFilePath, indexFileContent);
}
Expand Down Expand Up @@ -351,6 +351,41 @@ ${formatTable(argTable)}
);
}

/**
* Isolate a type into seperate types.
* @param {string} type The type expression.
* @returns {string[]} An array of types.
*/
isolateFieldTypes(type: string): string[] {
type = type.replace(/ /g, '');
return type.split('|').map((t) => t.replace(/\[\]/g, ''));
}

/**
* Import all external types from the fields of a type.
* @param {InternalType} schema The schema.
* @param {Imports} imports The imports instance.
*/
importExternalFieldTypes(schema: InternalType, imports: Imports): void {
for (const [, field] of Object.entries(schema.fields)) {
const types = this.isolateFieldTypes(field.type).filter(
(type) =>
BUILT_IN_TYPES.indexOf(type) == -1 && type != schema.name,
);
for (const typeName of types) {
const typeNameParts = typeName.split('.');
const [pkg, type] =
typeNameParts.length == 2
? [
`@sivrad/matrix-collection-${typeNameParts[0]}`,
typeNameParts[1],
]
: ['.', typeName];
imports.add(pkg, type);
}
}
}

/**
* Generates the type class file content.
* @param {Type} schema The schema of the type.
Expand All @@ -366,6 +401,8 @@ ${formatTable(argTable)}
'Field',
`Serialized${parentName}`,
);
// Import all the external field types.
this.importExternalFieldTypes(schema, imports);
// Get the class name.
const className = formatAsClassName(schema.name);
const serializedClassName = `Serialized${className}`;
Expand Down Expand Up @@ -401,7 +438,7 @@ export interface ${serializedClassName} extends Serialized${parentName} {${this.
)
.join('\n')}\n }`;
} else {
serializedSchemaInterface = `export type ${serializedClassName} = Record<string, never>;`;
serializedSchemaInterface = `export type ${serializedClassName} = SerializedMatrixBaseType;`;
classFields = '{}';
}

Expand Down
1 change: 1 addition & 0 deletions src/commands/build/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const SOURCE_DIRECTORY = 'src/';
export const TYPES_DIRECTORY = `${SOURCE_DIRECTORY}types/`;
export const BUILT_IN_TYPES = ['string', 'number', 'boolean', 'null'];

0 comments on commit 723997f

Please sign in to comment.