diff options
author | silverwind <me@silverwind.io> | 2024-03-17 23:12:36 +0100 |
---|---|---|
committer | Earl Warren <contact@earl-warren.org> | 2024-03-26 19:04:25 +0100 |
commit | 6eb576c36d84354c2047232c2682753f333b7794 (patch) | |
tree | a1af28b51e3b9612b0dae2c8edcc37c63c529593 /build | |
parent | Fix missing code in the user profile (#29865) (diff) | |
download | forgejo-6eb576c36d84354c2047232c2682753f333b7794.tar.xz forgejo-6eb576c36d84354c2047232c2682753f333b7794.zip |
move some scripts from 'build' to 'tools' directory, misc refactors (#29844)
- Move some scripts from `build` to new `tools` dir. Eventually i would
like to move all but let's do it step-by-step.
- Add dir to eslint and move the files into vars.
- Update docs accordingly.
- While updating docs I noticed we were incorrectly having `public/img`
path still in a few places. Replace those with the current
`public/assets/img`.
---------
Co-authored-by: Nanguan Lin <nanguanlin6@gmail.com>
Co-authored-by: Giteabot <teabot@gitea.io>
(cherry picked from commit 095fdd691dd1a7d7748372cc73e7708278c80933)
Diffstat (limited to 'build')
-rwxr-xr-x | build/generate-images.js | 79 | ||||
-rwxr-xr-x | build/generate-svg.js | 70 | ||||
-rw-r--r-- | build/watch.sh | 8 |
3 files changed, 0 insertions, 157 deletions
diff --git a/build/generate-images.js b/build/generate-images.js deleted file mode 100755 index db31d19e2a..0000000000 --- a/build/generate-images.js +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env node -import imageminZopfli from 'imagemin-zopfli'; -import {optimize} from 'svgo'; -import {loadSVGFromString, Canvas, Rect, util} from 'fabric/node'; -import {readFile, writeFile} from 'node:fs/promises'; -import {argv, exit} from 'node:process'; - -function doExit(err) { - if (err) console.error(err); - exit(err ? 1 : 0); -} - -async function generate(svg, path, {size, bg}) { - const outputFile = new URL(path, import.meta.url); - - if (String(outputFile).endsWith('.svg')) { - const {data} = optimize(svg, { - plugins: [ - 'preset-default', - 'removeDimensions', - { - name: 'addAttributesToSVGElement', - params: {attributes: [{width: size}, {height: size}]} - }, - ], - }); - await writeFile(outputFile, data); - return; - } - - const {objects, options} = await loadSVGFromString(svg); - const canvas = new Canvas(); - canvas.setDimensions({width: size, height: size}); - const ctx = canvas.getContext('2d'); - ctx.scale(options.width ? (size / options.width) : 1, options.height ? (size / options.height) : 1); - - if (bg) { - canvas.add(new Rect({ - left: 0, - top: 0, - height: size * (1 / (size / options.height)), - width: size * (1 / (size / options.width)), - fill: 'white', - })); - } - - canvas.add(util.groupSVGElements(objects, options)); - canvas.renderAll(); - - let png = Buffer.from([]); - for await (const chunk of canvas.createPNGStream()) { - png = Buffer.concat([png, chunk]); - } - - png = await imageminZopfli({more: true})(png); - await writeFile(outputFile, png); -} - -async function main() { - const gitea = argv.slice(2).includes('gitea'); - const logoSvg = await readFile(new URL('../assets/logo.svg', import.meta.url), 'utf8'); - const faviconSvg = await readFile(new URL('../assets/favicon.svg', import.meta.url), 'utf8'); - - await Promise.all([ - generate(logoSvg, '../public/assets/img/logo.svg', {size: 32}), - generate(logoSvg, '../public/assets/img/logo.png', {size: 512}), - generate(faviconSvg, '../public/assets/img/favicon.svg', {size: 32}), - generate(faviconSvg, '../public/assets/img/favicon.png', {size: 180}), - generate(logoSvg, '../public/assets/img/avatar_default.png', {size: 200}), - generate(logoSvg, '../public/assets/img/apple-touch-icon.png', {size: 180, bg: true}), - gitea && generate(logoSvg, '../public/assets/img/gitea.svg', {size: 32}), - ]); -} - -try { - doExit(await main()); -} catch (err) { - doExit(err); -} diff --git a/build/generate-svg.js b/build/generate-svg.js deleted file mode 100755 index 660ac9157e..0000000000 --- a/build/generate-svg.js +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env node -import fastGlob from 'fast-glob'; -import {optimize} from 'svgo'; -import {parse} from 'node:path'; -import {readFile, writeFile, mkdir} from 'node:fs/promises'; -import {fileURLToPath} from 'node:url'; -import {exit} from 'node:process'; - -const glob = (pattern) => fastGlob.sync(pattern, { - cwd: fileURLToPath(new URL('..', import.meta.url)), - absolute: true, -}); - -function doExit(err) { - if (err) console.error(err); - exit(err ? 1 : 0); -} - -async function processFile(file, {prefix, fullName} = {}) { - let name; - if (fullName) { - name = fullName; - } else { - name = parse(file).name; - if (prefix) name = `${prefix}-${name}`; - if (prefix === 'octicon') name = name.replace(/-[0-9]+$/, ''); // chop of '-16' on octicons - } - - // Set the `xmlns` attribute so that the files are displayable in standalone documents - // The svg backend module will strip the attribute during startup for inline display - const {data} = optimize(await readFile(file, 'utf8'), { - plugins: [ - {name: 'preset-default'}, - {name: 'removeDimensions'}, - {name: 'prefixIds', params: {prefix: () => name}}, - {name: 'addClassesToSVGElement', params: {classNames: ['svg', name]}}, - { - name: 'addAttributesToSVGElement', params: { - attributes: [ - {'xmlns': 'http://www.w3.org/2000/svg'}, - {'width': '16'}, {'height': '16'}, {'aria-hidden': 'true'}, - ] - } - }, - ], - }); - - await writeFile(fileURLToPath(new URL(`../public/assets/img/svg/${name}.svg`, import.meta.url)), data); -} - -function processFiles(pattern, opts) { - return glob(pattern).map((file) => processFile(file, opts)); -} - -async function main() { - try { - await mkdir(fileURLToPath(new URL('../public/assets/img/svg', import.meta.url)), {recursive: true}); - } catch {} - - await Promise.all([ - ...processFiles('node_modules/@primer/octicons/build/svg/*-16.svg', {prefix: 'octicon'}), - ...processFiles('web_src/svg/*.svg'), - ]); -} - -try { - doExit(await main()); -} catch (err) { - doExit(err); -} diff --git a/build/watch.sh b/build/watch.sh deleted file mode 100644 index 5e8defa49c..0000000000 --- a/build/watch.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -euo pipefail - -make --no-print-directory watch-frontend & -make --no-print-directory watch-backend & - -trap 'kill $(jobs -p)' EXIT -wait |