2023-09-07 09:56:17 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-09-07 09:56:17 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as fs from 'node:fs/promises';
|
|
|
|
import * as path from 'node:path';
|
2024-02-17 05:34:50 +01:00
|
|
|
import { fileURLToPath } from 'node:url';
|
2023-09-07 09:56:17 +02:00
|
|
|
import cssnano from 'cssnano';
|
2024-02-17 05:34:50 +01:00
|
|
|
import * as yaml from 'js-yaml';
|
2023-09-07 09:56:17 +02:00
|
|
|
import postcss from 'postcss';
|
|
|
|
import * as terser from 'terser';
|
|
|
|
|
2023-11-30 06:48:02 +01:00
|
|
|
import { build as buildLocales } from '../locales/index.js';
|
2023-09-10 12:31:01 +02:00
|
|
|
import generateDTS from '../locales/generateDTS.js';
|
2024-06-13 03:18:01 +02:00
|
|
|
import meta from '../package.json' with { type: "json" };
|
2024-02-12 02:02:27 +01:00
|
|
|
import buildTarball from './tarball.mjs';
|
2024-10-15 18:23:18 +02:00
|
|
|
import { localesVersion } from '../locales/version.js';
|
2023-09-07 09:56:17 +02:00
|
|
|
|
2024-02-17 05:34:50 +01:00
|
|
|
const configDir = fileURLToPath(new URL('../.config', import.meta.url));
|
|
|
|
const configPath = process.env.MISSKEY_CONFIG_YML
|
|
|
|
? path.resolve(configDir, process.env.MISSKEY_CONFIG_YML)
|
|
|
|
: process.env.NODE_ENV === 'test'
|
|
|
|
? path.resolve(configDir, 'test.yml')
|
|
|
|
: path.resolve(configDir, 'default.yml');
|
2023-09-07 09:56:17 +02:00
|
|
|
|
2023-11-30 06:48:02 +01:00
|
|
|
let locales = buildLocales();
|
|
|
|
|
2024-02-17 05:34:50 +01:00
|
|
|
async function loadConfig() {
|
|
|
|
return fs.readFile(configPath, 'utf-8').then(data => yaml.load(data)).catch(() => null);
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:56:17 +02:00
|
|
|
async function copyFrontendFonts() {
|
|
|
|
await fs.cp('./packages/frontend/node_modules/three/examples/fonts', './built/_frontend_dist_/fonts', { dereference: true, recursive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function copyFrontendTablerIcons() {
|
2023-09-30 18:52:13 +02:00
|
|
|
await fs.cp('./packages/frontend/node_modules/@phosphor-icons/web/src', './built/_frontend_dist_/phosphor-icons', { dereference: true, recursive: true });
|
2024-06-19 16:46:57 +02:00
|
|
|
|
|
|
|
for (const file of [
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/bold/style.css',
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/duotone/style.css',
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/fill/style.css',
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/light/style.css',
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/regular/style.css',
|
|
|
|
'./built/_frontend_dist_/phosphor-icons/thin/style.css',
|
|
|
|
]) {
|
|
|
|
let source = await fs.readFile(file, { encoding: 'utf-8' });
|
|
|
|
source = source.replaceAll(/(url\(.+?Phosphor.+?\.(?:[a-zA-Z0-9]+))/g, `$1?version=${meta.version}`);
|
|
|
|
await fs.writeFile(file, source);
|
|
|
|
}
|
|
|
|
|
2023-09-07 09:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function copyFrontendLocales() {
|
2023-09-10 12:31:01 +02:00
|
|
|
generateDTS();
|
|
|
|
|
2023-09-07 09:56:17 +02:00
|
|
|
await fs.mkdir('./built/_frontend_dist_/locales', { recursive: true });
|
|
|
|
|
2024-10-15 18:23:18 +02:00
|
|
|
const v = { '_version_': localesVersion };
|
2023-09-07 09:56:17 +02:00
|
|
|
|
|
|
|
for (const [lang, locale] of Object.entries(locales)) {
|
2024-10-15 18:23:18 +02:00
|
|
|
await fs.writeFile(`./built/_frontend_dist_/locales/${lang}.${localesVersion}.json`, JSON.stringify({ ...locale, ...v }), 'utf-8');
|
2023-09-07 09:56:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function copyBackendViews() {
|
|
|
|
await fs.cp('./packages/backend/src/server/web/views', './packages/backend/built/server/web/views', { recursive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildBackendScript() {
|
|
|
|
await fs.mkdir('./packages/backend/built/server/web', { recursive: true });
|
|
|
|
|
|
|
|
for (const file of [
|
|
|
|
'./packages/backend/src/server/web/boot.js',
|
2024-09-09 13:57:36 +02:00
|
|
|
'./packages/backend/src/server/web/boot.embed.js',
|
2023-09-07 09:56:17 +02:00
|
|
|
'./packages/backend/src/server/web/bios.js',
|
|
|
|
'./packages/backend/src/server/web/cli.js'
|
|
|
|
]) {
|
|
|
|
let source = await fs.readFile(file, { encoding: 'utf-8' });
|
2024-10-15 18:23:18 +02:00
|
|
|
source = source.replaceAll(/\bLANGS\b/g, JSON.stringify(Object.keys(locales)));
|
2024-10-15 18:28:22 +02:00
|
|
|
source = source.replaceAll(/\bLANGS_VERSION\b/g, JSON.stringify(localesVersion));
|
2023-09-07 09:56:17 +02:00
|
|
|
const { code } = await terser.minify(source, { toplevel: true });
|
|
|
|
await fs.writeFile(`./packages/backend/built/server/web/${path.basename(file)}`, code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildBackendStyle() {
|
|
|
|
await fs.mkdir('./packages/backend/built/server/web', { recursive: true });
|
|
|
|
|
|
|
|
for (const file of [
|
|
|
|
'./packages/backend/src/server/web/style.css',
|
2024-09-09 13:57:36 +02:00
|
|
|
'./packages/backend/src/server/web/style.embed.css',
|
2023-09-07 09:56:17 +02:00
|
|
|
'./packages/backend/src/server/web/bios.css',
|
|
|
|
'./packages/backend/src/server/web/cli.css',
|
|
|
|
'./packages/backend/src/server/web/error.css'
|
|
|
|
]) {
|
|
|
|
const source = await fs.readFile(file, { encoding: 'utf-8' });
|
|
|
|
const { css } = await postcss([cssnano({ zindex: false })]).process(source, { from: undefined });
|
|
|
|
await fs.writeFile(`./packages/backend/built/server/web/${path.basename(file)}`, css);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function build() {
|
|
|
|
await Promise.all([
|
|
|
|
copyFrontendFonts(),
|
|
|
|
copyFrontendTablerIcons(),
|
|
|
|
copyFrontendLocales(),
|
|
|
|
copyBackendViews(),
|
|
|
|
buildBackendScript(),
|
|
|
|
buildBackendStyle(),
|
2024-02-17 05:34:50 +01:00
|
|
|
loadConfig().then(config => config?.publishTarballInsteadOfProvideRepositoryUrl && buildTarball()),
|
2023-09-07 09:56:17 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
await build();
|
|
|
|
|
2024-02-12 02:02:27 +01:00
|
|
|
if (process.argv.includes('--watch')) {
|
2023-11-30 06:48:02 +01:00
|
|
|
const watcher = fs.watch('./locales');
|
|
|
|
for await (const event of watcher) {
|
|
|
|
const filename = event.filename?.replaceAll('\\', '/');
|
|
|
|
if (/^[a-z]+-[A-Z]+\.yml/.test(filename)) {
|
2023-12-14 12:16:02 +01:00
|
|
|
console.log(`update ${filename} ...`)
|
2023-11-30 06:48:02 +01:00
|
|
|
locales = buildLocales();
|
|
|
|
await copyFrontendLocales()
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 09:56:17 +02:00
|
|
|
}
|