Skip to content

Commit

Permalink
fix: 🧩 优化构建逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Plumbiu committed Aug 13, 2023
1 parent df2f1e0 commit 9a8dd35
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 35 deletions.
6 changes: 5 additions & 1 deletion packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ program
const depth = +dep
if (Number.isNaN(depth))
throw new TypeError('illegal type of depth')
await genByCommand({ dep: depth, isBoth: both, jsonPath: json })
if (json) {
await genOutputFile(depth, 'json', json)
return
}
await genByCommand({ dep: depth, isBoth: both })
}
catch (err) {
logDepthError()
Expand Down
24 changes: 14 additions & 10 deletions packages/cli/src/genFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { writeFile } from 'node:fs/promises'
import path from 'node:path'
import { genGraph, genPkgTree, genPkgs, genRelations, genTree, genVersions } from '@truth-cli/core'
import type { IOptions } from '@truth-cli/shared'
import { devDistPath, distPath, logFileWirteError, logFileWirteFinished } from '@truth-cli/shared'
import { devDistPath, distPath, logFileWirteError, logFileWirteFinished, logLogo } from '@truth-cli/shared'

const relations = genRelations()
/**
* 方便命令行操作的函数
*/
export async function genWebFile(options: IOptions) {
const begin = Date.now()
let { dep, isBoth, isDev, writePath } = options
let { dep, isBoth, isBuild: isDev, writePath } = options
const graph = genGraph()
const tree = genTree(dep)
const versions = genVersions()
Expand All @@ -32,20 +32,24 @@ export async function genWebFile(options: IOptions) {
*/
export async function genOutputFile(
dep: number,
fileType: 'json' | 'txt',
fileType: 'json' | 'txt' | 'both',
p?: string | boolean,
) {
logLogo()
const begin = Date.now()
if (!p || typeof p === 'boolean')
p = './'
try {
let pkgs = ''
if (fileType === 'json')
pkgs = JSON.stringify(genPkgs(dep))
else
pkgs = genPkgTree(dep)
const writeName = fileType === 'json' ? './pkgs.json' : './treePkgs.txt'
await writeFile(path.resolve(p, writeName), pkgs)
if (fileType === 'json') {
await writeFile(path.resolve(p, './pkgs.json'), JSON.stringify(genPkgs(dep)))
}
else if (fileType === 'txt') {
await writeFile(path.resolve(p, './treePkgs.txt'), genPkgTree(dep))
}
else {
await writeFile(path.resolve(p, './pkgs.json'), JSON.stringify(genPkgs(dep)))
await writeFile(path.resolve(p, './treePkgs.txt'), genPkgTree(dep))
}
logFileWirteFinished(Date.now() - begin, p)
}
catch (err: any) {
Expand Down
23 changes: 8 additions & 15 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { createServer } from 'node:http'
import { readFileSync } from 'node:fs'
import type { IOptions } from '@truth-cli/shared'
import { devDistPath, distPath, logFileWirteError, logLogo, logWebStart } from '@truth-cli/shared'
import { genOutputFile, genWebFile } from './genFile.js'
import { genWebFile } from './genFile.js'

const server = function (webPath: string) {
const startServer = function (webPath: string) {
return createServer((req, res) => {
const html = readFileSync(`${webPath}/index.html`)
const graph = readFileSync(`${webPath}/graph.json`)
Expand All @@ -26,28 +26,21 @@ const server = function (webPath: string) {
/**
* 启动服务器
*/
function startWeb(begin: number, isDev?: boolean) {
isDev || logWebStart(Date.now() - begin)
const webStartPath = isDev ? devDistPath : distPath
server(webStartPath).listen(3002)
}
/**
* 命令行操作函数
*/
export async function genByCommand(options: IOptions) {
logLogo()
const { dep, isBoth, isDev, isDeploy, jsonPath } = options
const { dep, isBoth, isBuild } = options
const begin = Date.now()
try {
// 表示生成 pkgs.json 不打开网页
if (jsonPath) {
await genOutputFile(dep, 'json', jsonPath)
return
await genWebFile({ dep, isBoth, isBuild })
const webStartPath = isBuild ? devDistPath : distPath
if (!isBuild) {
logWebStart(Date.now() - begin)
startServer(webStartPath).listen(3002)
}
await genWebFile({ dep, isBoth, isDev })
// 如果是 deploy 环境下,不启动网页
if (!isDeploy)
startWeb(begin, isDev)
}
catch (err: any) {
logFileWirteError(err.message)
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export const categories = [
export interface IOptions {
dep: number
isBoth?: boolean
isDev?: boolean
isDeploy?: boolean
jsonPath?: boolean
isBuild?: boolean
writePath?: string
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/build-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { buildWeb } from './utils.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))

async function buildForDeploy() {
await genByCommand({ dep: 3, isDev: true, isBoth: false, isDeploy: true })
await genByCommand({ dep: 3, isBuild: true })
await buildWeb({
isDeploy: true,
buildPath: path.resolve(__dirname, '../packages/web/dist'),
Expand Down
3 changes: 1 addition & 2 deletions scripts/build-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ async function createPlaygroundServer() {
dep: 3,
writePath: playgroundAssetsPath,
})
await genOutputFile(3, 'json', playgroundAssetsPath)
await genOutputFile(3, 'txt', playgroundAssetsPath)
await genOutputFile(3, 'both', playgroundAssetsPath)
await buildWeb({
isDeploy: true,
buildPath: path.resolve(__dirname, '../playground/dist'),
Expand Down
2 changes: 1 addition & 1 deletion scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createViteServer } from './utils.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))

async function createCliServer() {
await genByCommand({ dep: 3, isDev: true, isBoth: true })
await genByCommand({ dep: 3, isBuild: true, isBoth: true })
const server = await createViteServer(path.resolve(__dirname, '../packages/web'))
await server.listen()
server.printUrls()
Expand Down
3 changes: 1 addition & 2 deletions scripts/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ async function createPlaygroundServer() {
dep: 3,
writePath: playgroundAssetsPath,
})
await genOutputFile(3, 'json', playgroundAssetsPath)
await genOutputFile(3, 'txt', playgroundAssetsPath)
await genOutputFile(3, 'both', playgroundAssetsPath)
const server = await createViteServer(path.resolve(__dirname, '../playground/'), 1338)
await server.listen()
server.printUrls()
Expand Down

1 comment on commit 9a8dd35

@vercel
Copy link

@vercel vercel bot commented on 9a8dd35 Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.