From 361c125121675c0c05335ae60c7f257b26e5e18b Mon Sep 17 00:00:00 2001 From: Doubley Yang Date: Fri, 14 Jun 2024 20:59:44 +0800 Subject: [PATCH] feat: update the build script --- .editorconfig | 9 +++++ etc/build/monkey.ts | 92 +++++++++++++++++++++++++++++++-------------- etc/consts.ts | 8 +++- 3 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b368786 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/etc/build/monkey.ts b/etc/build/monkey.ts index 2b5006a..143a119 100644 --- a/etc/build/monkey.ts +++ b/etc/build/monkey.ts @@ -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((list, [key, value]) => { if (typeof value === 'string') { @@ -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!`) } -})() \ No newline at end of file +})() diff --git a/etc/consts.ts b/etc/consts.ts index 98bac36..9b47697 100644 --- a/etc/consts.ts +++ b/etc/consts.ts @@ -1,3 +1,9 @@ import path from 'path' -export const ROOT_PATH = path.resolve(__dirname, '..') \ No newline at end of file +export const ROOT_PATH = path.resolve(__dirname, '..') + +export const githubInfo = { + domain: 'https://raw.githubusercontent.com', + repo: 'Arylo/scripts/', + branch: 'monkey', +}