Skip to content

Commit

Permalink
refator: refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Apr 30, 2024
1 parent 88e252d commit 5d0ae13
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
6 changes: 4 additions & 2 deletions lib/plugins/console/list/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { underline } from 'picocolors';
import table from 'text-table';
import { stringLength } from './common';
import type Hexo from '../../../hexo';
import type { CategorySchema } from '../../../types';
import type Model from 'warehouse/dist/model';

function listCategory(this: Hexo): void {
const categories = this.model('Category');
const categories: Model<CategorySchema> = this.model('Category');

const data = categories.sort({name: 1}).map(cate => [cate.name, String(cate.length)]);
const data = categories.sort({name: 1}).map((cate: CategorySchema) => [cate.name, String(cate.length)]);

// Table header
const header = ['Name', 'Posts'].map(str => underline(str));
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/console/list/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { magenta, underline, gray } from 'picocolors';
import table from 'text-table';
import { stringLength } from './common';
import type Hexo from '../../../hexo';
import type { PageSchema } from '../../../types';
import type Model from 'warehouse/dist/model';

function listPage(this: Hexo): void {
const Page = this.model('Page');
const Page: Model<PageSchema> = this.model('Page');

const data = Page.sort({date: 1}).map(page => {
const data = Page.sort({date: 1}).map((page: PageSchema) => {
const date = page.date.format('YYYY-MM-DD');
return [gray(date), page.title, magenta(page.source)];
});
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/console/list/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { gray, magenta, underline } from 'picocolors';
import table from 'text-table';
import { stringLength } from './common';
import type Hexo from '../../../hexo';
import type { PostSchema } from '../../../types';
import type Model from 'warehouse/dist/model';

function mapName(item) {
return item.name;
}

function listPost(this: Hexo): void {
const Post = this.model('Post');
const Post: Model<PostSchema> = this.model('Post');

const data = Post.sort({published: -1, date: 1}).map(post => {
const data = Post.sort({published: -1, date: 1}).map((post: PostSchema) => {
const date = post.published ? post.date.format('YYYY-MM-DD') : 'Draft';
const tags = post.tags.map(mapName);
const categories = post.categories.map(mapName);
Expand Down
8 changes: 4 additions & 4 deletions lib/plugins/console/list/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function listRoute(this: Hexo): void {
console.log(s);
}

function buildTree(routes) {
const obj = {};
let cursor;
function buildTree(routes: string[]) {
const obj: Record<string, any> = {};
let cursor: typeof obj;

for (let i = 0, len = routes.length; i < len; i++) {
const item = routes[i].split('/');
Expand All @@ -32,7 +32,7 @@ function buildTree(routes) {
return obj;
}

function buildNodes(tree) {
function buildNodes(tree: Record<string, any>) {
const nodes = [];

for (const [key, item] of Object.entries(tree)) {
Expand Down
6 changes: 4 additions & 2 deletions lib/plugins/console/list/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { magenta, underline } from 'picocolors';
import table from 'text-table';
import { stringLength } from './common';
import type Hexo from '../../../hexo';
import type { TagSchema } from '../../../types';
import type Model from 'warehouse/dist/model';

function listTag(this: Hexo): void {
const Tag = this.model('Tag');
const Tag: Model<TagSchema> = this.model('Tag');

const data = Tag.sort({name: 1}).map(tag => [tag.name, String(tag.length), magenta(tag.path)]);
const data = Tag.sort({name: 1}).map((tag: TagSchema) => [tag.name, String(tag.length), magenta(tag.path)]);

// Table header
const header = ['Name', 'Posts', 'Path'].map(str => underline(str));
Expand Down
2 changes: 2 additions & 0 deletions test/scripts/hexo/hexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('Hexo', () => {
themeConfig.a.b.should.eql(3);

const Locals = hexo._generateLocals();
// @ts-expect-error
const { theme: themeLocals } = new Locals('', {path: '', layout: [], data: {}});

themeLocals.a.should.have.own.property('c');
Expand All @@ -187,6 +188,7 @@ describe('Hexo', () => {
themeConfig.c.should.eql(3);

const Locals = hexo._generateLocals();
// @ts-expect-error
const { theme: themeLocals } = new Locals('', {path: '', layout: [], data: {}});

themeLocals.should.have.own.property('c');
Expand Down

0 comments on commit 5d0ae13

Please sign in to comment.