refactor(core): refactor cli

This commit is contained in:
syy11cn 2022-05-29 15:04:50 +08:00
parent b312977932
commit 7b1c2bd355
5 changed files with 93 additions and 4 deletions

14
packages/core/bin/index.js Executable file
View File

@ -0,0 +1,14 @@
#!/usr/bin/env node
'use strict'
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const resolveFrom = require('resolve-from')
let modulePath = '../dist/cli'
try {
// use local cli if exists
modulePath = path.join(path.dirname(resolveFrom(process.cwd(), '@md-report/core')), 'cli.js')
}
catch {}
require(modulePath)

24
packages/core/src/cli.ts Normal file
View File

@ -0,0 +1,24 @@
import { readFileSync, writeFileSync } from 'fs'
import { cwd } from 'process'
import { getBuffer } from '@md-report/parser'
import yargs from 'yargs'
import { defaultConfig } from './config'
const argv = yargs(process.argv.slice(2)).options({
md: { type: 'string' },
theme: { type: 'string' },
}).parseSync()
async function cli(props: {
filename?: string
theme?: string
}): Promise<void> {
console.log('\x1B[36m%s\x1B[0m', '[Markdown Report]: start...')
const { filename = 'index.md' } = props
const file = readFileSync(`${cwd()}/${filename}`)
const buffer = await getBuffer({ markdown: file.toString(), config: defaultConfig })
writeFileSync(`${cwd()}/My document.docx`, buffer)
console.log('\x1B[36m%s\x1B[0m', '[Markdown Report]: finish.')
}
cli({ filename: argv.md, theme: argv.theme })

View File

@ -0,0 +1,52 @@
import type { IMarkdownReportConfig } from '@md-report/types'
import { StyleId, StyleName } from '@md-report/types'
import { AlignmentType } from 'docx'
import { ptToHalfPt } from './utils'
export const defaultConfig: IMarkdownReportConfig = {
styles: {
paragraphStyles: [
{
id: StyleId.normal,
name: StyleName.normal,
run: {
size: ptToHalfPt(12),
font: {
ascii: 'Times New Roman',
eastAsia: 'SimSun',
},
},
paragraph: {
indent: {
firstLine: 2,
},
spacing: {
before: 120,
after: 120,
},
alignment: AlignmentType.JUSTIFIED,
},
},
{
id: StyleId.h1,
name: StyleName.h1,
run: {
size: ptToHalfPt(15),
bold: true,
font: {
ascii: 'Times New Roman',
eastAsia: 'SimHei',
},
},
paragraph: {
spacing: {},
alignment: AlignmentType.CENTER,
},
},
],
},
}
export function defineConfig(config: IMarkdownReportConfig): IMarkdownReportConfig {
return { ...defaultConfig, ...config }
}

View File

@ -1,4 +0,0 @@
/* eslint-disable no-console */
const src = '$111$xxx$222$yyy\n$$\n\frac{1}{2}\n\frac{2}{3}\n$$'
console.log(src.match(/\$\$[^]*\$\$/g).map(item => item.trim().replace(/\$*\n/, '').replace(/\n\$*$/, '').split(/\r?\n/).join('')))

View File

@ -0,0 +1,3 @@
export function ptToHalfPt(pt: number) {
return pt * 2
}