Skip to content

Commit

Permalink
feat: update the build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Aug 22, 2024
1 parent db185e3 commit 2097b53
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
28 changes: 28 additions & 0 deletions etc/build/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { AsyncLocalStorage } from 'async_hooks'

type CustomConsole = {
log: typeof console.log,
info: typeof console.info,
warn: typeof console.warn,
error: typeof console.error,
}

const storage = new AsyncLocalStorage<CustomConsole>();

export function inject<F extends (...args: any[]) => any>(name: string, cb: F) {
return storage.run({
log: (...args: any[]) => console.log(`[${name}]`, ...args),
info: (...args: any[]) => console.info(`[${name}]`, ...args),
warn: (...args: any[]) => console.warn(`[${name}]`, ...args),
error: (...args: any[]) => console.error(`[${name}]`, ...args),
}, cb)
}

export const logger = {
log: (...args: any[]) => (storage.getStore()?.log ?? console.log)(...args),
info: (...args: any[]) => (storage.getStore()?.log ?? console.info)(...args),
warn: (...args: any[]) => (storage.getStore()?.log ?? console.warn)(...args),
error: (...args: any[]) => (storage.getStore()?.log ?? console.error)(...args),
}

export default logger
34 changes: 19 additions & 15 deletions etc/build/monkey.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path'
import fs from 'fs'
import { POLYFILL_PATH, ROOT_PATH } from '../consts'
import { buildScript, exportLatestDeployInfo, isFile, stringifyBanner, parseFilenames, parseBanner, exportInjectFiles } from './monkey.utils'
import logger, { inject as loggerInject } from './logger'
import { ROOT_PATH } from '../consts'
import { buildScript, exportLatestDeployInfo, isFile, stringifyBanner, parseFilenames, exportInjectFiles } from './monkey.utils'

const srcPath = path.resolve(ROOT_PATH, 'src/monkey')
const outPath = path.resolve(ROOT_PATH, 'dist/monkey')
Expand All @@ -17,23 +18,26 @@ const outPath = path.resolve(ROOT_PATH, 'dist/monkey')

for (const filepath of filepaths) {
const {
raw,
meta,
user,
deployJson
} = parseFilenames(filepath)
const sourcePath = path.resolve(filepath, parseFilenames(filepath).name)
const targetPath = path.resolve(outPath, user)
console.log(`Building ${path.relative(ROOT_PATH, sourcePath)} --outfile ${path.relative(ROOT_PATH, targetPath)} ...`)
const deployInfo = await exportLatestDeployInfo(filepath)
const banner = stringifyBanner(sourcePath, { version: deployInfo.version })
await buildScript(sourcePath, {
banner: { js: banner },
outfile: targetPath,
inject: exportInjectFiles(sourcePath),
await loggerInject(raw, async () => {
const sourcePath = path.resolve(filepath, parseFilenames(filepath).name)
const targetPath = path.resolve(outPath, user)
logger.log(`Building ${path.relative(ROOT_PATH, sourcePath)} --outfile ${path.relative(ROOT_PATH, targetPath)} ...`)
const deployInfo = await exportLatestDeployInfo(filepath)
const banner = stringifyBanner(sourcePath, { version: deployInfo.version })
await buildScript(sourcePath, {
banner: { js: banner },
outfile: targetPath,
inject: exportInjectFiles(sourcePath),
})
const metaPath = path.resolve(outPath, meta)
fs.writeFileSync(path.resolve(outPath, deployJson), JSON.stringify(deployInfo, null, 2), 'utf-8')
fs.writeFileSync(metaPath, banner, 'utf-8')
logger.log(`Building ${path.relative(ROOT_PATH, sourcePath)} --outfile=${path.relative(ROOT_PATH, targetPath)} ... Done!`)
})
const metaPath = path.resolve(outPath, meta)
fs.writeFileSync(path.resolve(outPath, deployJson), JSON.stringify(deployInfo, null, 2), 'utf-8')
fs.writeFileSync(metaPath, banner, 'utf-8')
console.log(`Building ${path.relative(ROOT_PATH, sourcePath)} --outfile=${path.relative(ROOT_PATH, targetPath)} ... Done!`)
}
})()
13 changes: 7 additions & 6 deletions etc/build/monkey.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import md5file from 'md5-file'
import md5 from 'md5'
import yn from 'yn'
import download from 'download'
import logger from './logger'
import { bannerOrderMap, githubRawPrefix } from './monkey.const'
import { POLYFILL_PATH, ROOT_PATH } from '../consts'
import CSSMinifyTextPlugin from '../esbuild-plugins/css-minify-text'
Expand Down Expand Up @@ -124,22 +125,22 @@ export const exportLatestDeployInfo = async (filepath: string) => {
const latestDeployUrl = `${githubRawPrefix}/${deployJson}`
if (isCI) {
try {
console.info(`Download the latest deploy info: ${latestDeployUrl} ...`)
logger.info(`Download the latest deploy info: ${latestDeployUrl} ...`)
await download(latestDeployUrl, os.tmpdir(), { filename: deployJson })
console.log(`Download the latest deploy info: ${latestDeployUrl} ... Done`)
logger.log(`Download the latest deploy info: ${latestDeployUrl} ... Done`)
const downloadDeployJson = JSON.parse(fs.readFileSync(path.resolve(os.tmpdir(), deployJson), 'utf-8'))
if (downloadDeployJson.contentHash !== contentHash || downloadDeployJson.bannerHash !== bannerHash) {
version = Number(downloadDeployJson.version) + 1
console.info(`Version increased: ${downloadDeployJson.version} => ${version}`)
logger.info(`Version increased: ${downloadDeployJson.version} => ${version}`)
} else {
version = Number(downloadDeployJson.version)
}
} catch (e) {
console.error(`Download the latest deploy info: ${latestDeployUrl} ... Failed`)
console.info(`Version use: ${version}`)
logger.error(`Download the latest deploy info: ${latestDeployUrl} ... Failed`)
logger.info(`Version use: ${version}`)
}
} else {
console.info('Current Environment is not CI, use default version 1')
logger.info('Current Environment is not CI, use default version 1')
}
return {
contentHash,
Expand Down
19 changes: 11 additions & 8 deletions etc/build/qinglong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs'
import * as esbuild from 'esbuild'
import { ROOT_PATH } from '../consts'
import logger, { inject as loggerInject } from './logger'

const buildinDependencies = [
'fs',
Expand All @@ -23,14 +24,16 @@ const buildinDependencies = [
const dependencies = Object.keys(require(path.resolve(ROOT_PATH, 'package.json')).dependencies)

for (const filepath of filepaths) {
console.log(`esbuild ${path.relative(ROOT_PATH, filepath)} --outdir=${path.relative(ROOT_PATH, outPath)} ...`)
esbuild.buildSync({
entryPoints: [filepath],
bundle: true,
treeShaking: true,
external: [...buildinDependencies, ...dependencies],
outdir: outPath,
loggerInject(path.basename(filepath), () => {
logger.log(`esbuild ${path.relative(ROOT_PATH, filepath)} --outdir ${path.relative(ROOT_PATH, outPath)} ...`)
esbuild.buildSync({
entryPoints: [filepath],
bundle: true,
treeShaking: true,
external: [...buildinDependencies, ...dependencies],
outdir: outPath,
})
logger.log(`esbuild ${path.relative(ROOT_PATH, filepath)} --outdir=${path.relative(ROOT_PATH, outPath)} ... Done!`)
})
console.log(`esbuild ${path.relative(ROOT_PATH, filepath)} --outdir=${path.relative(ROOT_PATH, outPath)} ... Done!`)
}
})()

0 comments on commit 2097b53

Please sign in to comment.