Skip to content

Commit

Permalink
feat: update the build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Jun 14, 2024
1 parent 05f7cf9 commit 361c125
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_size = 2
indent_style = space
trim_trailing_whitespace = true
92 changes: 64 additions & 28 deletions etc/build/monkey.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
import path from 'path'
import fs from 'fs'
import * as esbuild from 'esbuild'
import { ROOT_PATH } from '../consts'
import { ROOT_PATH, githubInfo } from '../consts'

const domain = 'https://raw.githubusercontent.com'
const repo = 'Arylo/scripts/'
const branch = 'monkey'
const srcPath = path.resolve(ROOT_PATH, 'src/monkey')
const outPath = path.resolve(ROOT_PATH, 'dist/monkey')
const cachePath = path.resolve(ROOT_PATH, '.github/cache/monkey')

const parseJsonPath = (filepath: string) => filepath.replace(/\.ts$/, '.json');
const paresBanner = (filepath: string) => {
const pkg = require(path.resolve(ROOT_PATH, 'package.json'))
const pkgInfo = require(path.resolve(ROOT_PATH, 'package.json'))
const { domain, repo, branch } = githubInfo
const githubPrefix = `${domain}/${repo}/${branch}`

const parseFilenames = (filepath: string) => {
const filename = path.basename(filepath, path.extname(filepath))
return {
raw: path.basename(filename),
name: filename,
meta: `${filename}.meta.js`,
user: `${filename}.user.js`,
min: `${filename}.min.js`,
json: `${filename}.json`,
}
}
const parseJsonPath = (filepath: string) => path.resolve(path.dirname(filepath), parseFilenames(filepath).json)
const paresBanner = (filepath: string, appendInfo = {}) => {
const jsonPath = parseJsonPath(filepath)
const jsonContent = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'))
const filename = path.basename(jsonPath, '.json')
console.log(filename)
const metaData = Object
.entries({
...jsonContent,
author: pkg.author,
downloadURL: `${domain}/${repo}${branch}/${filename}.js`,
updateURL: `${domain}/${repo}${branch}/${filename}.meta.js`,
const { user, meta } = parseFilenames(filepath)

const metaData = {
...jsonContent,
...appendInfo,
author: pkgInfo.author,
downloadURL: `${githubPrefix}/${user}`,
updateURL: `${githubPrefix}/${meta}`,
}
const content = Object
.entries(metaData)
.sort(([key1], [key2]) => {
const orderHead = ['name', 'description', 'version', 'author', 'namespace']
const orderTail = ['downloadURL', 'updateURL', 'run-at', 'grant']
const key1Order = [...orderHead, ...orderTail].indexOf(key1)
const key2Order = [...orderHead, ...orderTail].indexOf(key2)

let result = -1
if (key1Order !== -1 && key2Order !== -1) {
result = key1Order - key2Order
} else if (key1Order !== -1) {
result = key1Order >= orderHead.length ? 1 : -1
} else if (key2Order !== -1) {
result = key2Order >= orderHead.length ? -1 : 1
} else {
result = key1.localeCompare(key2)
}
return result
})
.reduce<string[]>((list, [key, value]) => {
if (typeof value === 'string') {
Expand All @@ -29,30 +63,32 @@ const paresBanner = (filepath: string) => {
}
return list
}, [])
const monkeyMetaData = ['==UserScript==', ...metaData, '==/UserScript==']
return monkeyMetaData.map((metaData) => `// ${metaData}`).join('\n')
const bannerContent = ['==UserScript==', ...content, '==/UserScript==']
return bannerContent.map((content) => `// ${content}`).join('\n')
}

(() => {
const srcPath = path.resolve(ROOT_PATH, 'src/monkey')
const outPath = path.resolve(ROOT_PATH, 'dist/monkey')

const filepaths = fs.readdirSync(srcPath)
.map((filename) => path.resolve(srcPath, filename))
.filter((filepath) => fs.statSync(filepath).isFile() && filepath.endsWith('.ts') && fs.statSync(parseJsonPath(filepath)).isFile())

for (const filepath of filepaths) {
console.log(`esbuild ${path.relative(srcPath, filepath)} --outdir=${outPath} ...`)
const filename = path.basename(filepath, '.ts')
const banner = paresBanner(filepath)
const {
meta,
user,
} = parseFilenames(filepath)
const sourcePath = filepath
const targetPath = path.resolve(outPath, user)
console.log(`Building ${path.relative(srcPath, sourcePath)} --outfile=${targetPath} ...`)
const banner = paresBanner(sourcePath)
esbuild.buildSync({
entryPoints: [filepath],
entryPoints: [sourcePath],
bundle: true,
banner: { js: banner },
outdir: outPath,
outfile: targetPath,
})
const metaPath = path.resolve(outPath, `${filename}.meta.js`)
const metaPath = path.resolve(outPath, meta)
fs.writeFileSync(metaPath, banner, 'utf-8')
console.log(`esbuild ${path.relative(srcPath, filepath)} --outdir=${outPath} ... Done!`)
console.log(`Building ${path.relative(srcPath, sourcePath)} --outfile=${targetPath} ... Done!`)
}
})()
})()
8 changes: 7 additions & 1 deletion etc/consts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import path from 'path'

export const ROOT_PATH = path.resolve(__dirname, '..')
export const ROOT_PATH = path.resolve(__dirname, '..')

export const githubInfo = {
domain: 'https://raw.githubusercontent.com',
repo: 'Arylo/scripts/',
branch: 'monkey',
}

0 comments on commit 361c125

Please sign in to comment.