chore(nx): move all monorepo-style in subfolder for processing

This commit is contained in:
Elian Doran
2025-04-22 10:06:06 +03:00
parent 2e200eab39
commit 62dbcc0a2e
1469 changed files with 16 additions and 16 deletions

View File

@@ -1,72 +0,0 @@
#!/usr/bin/env bash
set -e # Fail on any command error
# Debug output
echo "Matrix Arch: $MATRIX_ARCH"
# Detect architecture from matrix input, fallback to system architecture
if [ -n "$MATRIX_ARCH" ]; then
ARCH=$MATRIX_ARCH
else
ARCH=$(uname -m)
# Convert system architecture to our naming convention
case $ARCH in
x86_64) ARCH="x64" ;;
aarch64) ARCH="arm64" ;;
esac
fi
# Debug output
echo "Selected Arch: $ARCH"
# Set Node.js version and architecture-specific filename
NODE_VERSION=22.14.0
BUILD_DIR="./build"
DIST_DIR="./dist"
CLEANUP_SCRIPT="./scripts/cleanupNodeModules.ts"
# Trigger the build
echo "Build start"
npm run build:prepare-dist
echo "Build finished"
# pruning of unnecessary files and devDeps in node_modules
node --experimental-strip-types $CLEANUP_SCRIPT $BUILD_DIR
NODE_FILENAME=node-v${NODE_VERSION}-linux-${ARCH}
echo "Downloading Node.js runtime $NODE_FILENAME..."
cd $BUILD_DIR
wget -qO- https://nodejs.org/dist/v${NODE_VERSION}/${NODE_FILENAME}.tar.xz | tar xfJ -
mv $NODE_FILENAME node
cd ..
rm -rf $BUILD_DIR/node/lib/node_modules/{npm,corepack} \
$BUILD_DIR/node/bin/{npm,npx,corepack} \
$BUILD_DIR/node/CHANGELOG.md \
$BUILD_DIR/node/include/node \
$BUILD_DIR/node_modules/electron* \
$BUILD_DIR/electron*.{js,map}
printf "#!/bin/sh\n./node/bin/node src/main\n" > $BUILD_DIR/trilium.sh
chmod 755 $BUILD_DIR/trilium.sh
# TriliumNextTODO: is this still required? If yes → move to copy-dist/copy-trilium
cp tpl/anonymize-database.sql $BUILD_DIR/
VERSION=`jq -r ".version" package.json`
ARCHIVE_NAME="TriliumNextNotes-Server-${VERSION}-linux-${ARCH}"
echo "Creating Archive $ARCHIVE_NAME..."
mkdir $DIST_DIR
cp -r "$BUILD_DIR" "$DIST_DIR/$ARCHIVE_NAME"
cd $DIST_DIR
tar cJf "$ARCHIVE_NAME.tar.xz" "$ARCHIVE_NAME"
rm -rf "$ARCHIVE_NAME"
echo "Server Build Completed!"

View File

@@ -1,102 +0,0 @@
import fs from "fs-extra";
import path from "path";
import type { Dirent } from "fs-extra";
import { execSync } from "node:child_process";
/**
* Example usage with node >= v22:
* node --experimental-strip-types bin/cleanupNodeModules.ts /path/to/build/folder [--skip-prune-dev-deps]
* Example usage with tsx:
* tsx bin/cleanupNodeModules.ts /path/to/build/folder [--skip-prune-dev-deps]
*/
function main() {
if (process.argv.length > 4 || process.argv.length < 3) {
console.error("Usage: cleanupNodeModules.ts [path-to-build-folder] [--skip-prune-dev-deps]");
process.exit(1);
}
const basePath = process.argv[2];
const pruneDevDeps = process.argv[3] !== "--skip-prune-dev-deps";
if (!fs.existsSync(basePath)) {
console.error(`Supplied path '${basePath}' does not exist. Aborting.`);
process.exit(1);
}
console.log(`Starting pruning of node_modules ${!pruneDevDeps ? '(skipping npm pruning)' : ''} in '${basePath}'...`);
cleanupNodeModules(basePath, pruneDevDeps);
console.log("Successfully pruned node_modules.");
}
function cleanupNodeModules(basePath: string, pruneDevDeps: boolean = true) {
const nodeModulesDirPath = path.join(basePath, "node_modules");
const nodeModulesContent = fs.readdirSync(nodeModulesDirPath, { recursive: true, withFileTypes: true });
//const libDir = fs.readdirSync(path.join(basePath, "./libraries"), { recursive: true, withFileTypes: true });
/**
* Delete unnecessary folders
*/
const filterableDirs = new Set([
"demo",
"demos",
"doc",
"docs",
"example",
"examples",
"test",
"tests"
]);
nodeModulesContent
.filter(el => el.isDirectory() && filterableDirs.has(el.name))
.forEach(dir => removeDirent(dir));
/**
* Delete unnecessary files based on file extension
* TODO filter out useless (README).md files
*/
const filterableFileExt = new Set([
"ts",
"map"
]);
nodeModulesContent
// TriliumNextTODO: check if we can improve this naive file ext matching, without introducing any additional dependency
.filter(el => el.isFile() && filterableFileExt.has(el.name.split(".").at(-1) || ""))
.forEach(dir => removeDirent(dir));
/**
* Delete specific unnecessary folders
* TODO: check if we want removeSync to throw an error, if path does not exist anymore -> currently it will silently fail
*/
const extraFoldersDelete = new Set([
path.join(nodeModulesDirPath, ".bin"),
path.join(nodeModulesDirPath, "@excalidraw", "excalidraw", "dist", "dev"),
path.join(nodeModulesDirPath, "boxicons", "svg"),
path.join(nodeModulesDirPath, "boxicons", "node_modules"),
path.join(nodeModulesDirPath, "boxicons", "src"),
path.join(nodeModulesDirPath, "boxicons", "iconjar"),
path.join(nodeModulesDirPath, "@jimp", "plugin-print", "fonts"),
path.join(nodeModulesDirPath, "jimp", "dist", "browser") // missing "@" in front of jimp is not a typo here
]);
nodeModulesContent
.filter(el => el.isDirectory() && extraFoldersDelete.has(path.join(el.parentPath, el.name)))
.forEach(dir => removeDirent(dir))
}
function removeDirent(el: Dirent) {
const elementToDelete = path.join(el.parentPath, el.name);
fs.removeSync(elementToDelete);
if (process.env.VERBOSE) {
console.log(`Deleted ${elementToDelete}`);
}
}
main()

View File

@@ -1,115 +0,0 @@
import fs from "fs-extra";
import path from "path";
const DEST_DIR = "./build";
const VERBOSE = process.env.VERBOSE;
function log(...args: any[]) {
if (VERBOSE) {
console.log(...args);
}
}
import { fileURLToPath } from "url";
import { dirname } from "path";
import { execSync } from "child_process";
const scriptDir = dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(scriptDir, "..", "..", "..");
const clientDir = path.join(rootDir, "apps", "client");
const serverDir = path.join(rootDir, "apps", "server");
function copyAssets(baseDir: string, destDir: string, files: string[]) {
for (const file of files) {
const src = path.join(baseDir, file);
const dest = path.join(destDir, file);
log(`${src} -> ${dest}`);
fs.copySync(src, dest);
}
}
/**
* We cannot copy the node_modules directory directly because we are in a monorepo and all the packages are gathered at root level.
* We cannot copy the files manually because we'd have to implement all the npm lookup logic, especially since there are issues with the same library having multiple versions across dependencies.
*
* @param packageJsonPath
*/
function copyNodeModules(packageJsonPath: string) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
// Skip monorepo packages
packageJson.dependencies = Object.fromEntries(
Object.entries(packageJson.dependencies).filter(([key]) => {
return (key === "@triliumnext/express-partial-content" || !key.startsWith("@triliumnext"));
}));
// Trigger an npm install to obtain the dependencies.
fs.writeFileSync(path.join(DEST_DIR, "package.json"), JSON.stringify(packageJson));
execSync(`npm install --omit=dev`, {
cwd: DEST_DIR,
stdio: "inherit",
});
}
try {
const clientAssets = [
"./libraries",
`./stylesheets`
];
const serverAssets = [
// copy node_module, to avoid downloading packages a 2nd time during pruning
"./node_modules",
"./assets",
"./translations",
"./db",
"./config-sample.ini",
"./package.json",
"./src/public/icon.png",
"./src/public/manifest.webmanifest",
"./src/public/robots.txt",
"./src/public/fonts",
"./src/public/translations",
`./tpl/`,
"./scripts/cleanupNodeModules.ts",
"./src/views/",
"./src/etapi/etapi.openapi.yaml",
"./src/routes/api/openapi.json",
];
const rootAssets = [
"LICENSE",
"README.md"
];
fs.mkdirpSync(DEST_DIR);
copyNodeModules(path.join(serverDir, "package.json"));
// Copy monorepo assets.
fs.copySync("../../packages/commons/build", path.join(DEST_DIR, "node_modules", "@triliumnext/commons"));
fs.copySync("../../packages/turndown-plugin-gfm", path.join(DEST_DIR, "node_modules", "@triliumnext/turndown-plugin-gfm"));
copyAssets(clientDir, path.join(DEST_DIR, "src", "public"), clientAssets);
copyAssets(serverDir, path.join(DEST_DIR), serverAssets);
copyAssets(rootDir, path.join(DEST_DIR), rootAssets);
/**
* Directories to be copied relative to the project root into <resource_dir>/src/public/app-dist.
*/
const publicDirsToCopy = ["./src/public/app/doc_notes"];
const PUBLIC_DIR = path.join(DEST_DIR, "src", "public", "app-dist");
for (const dir of publicDirsToCopy) {
fs.copySync(dir, path.normalize(path.join(PUBLIC_DIR, path.basename(dir))));
}
fs.copySync(path.join(clientDir, "build"), path.join(DEST_DIR, "src", "public", "app-dist"));
fs.copySync(path.join(rootDir, "packages", "turndown-plugin-gfm", "src"), path.join(DEST_DIR, "src", "public", "app-dist", "turndown-plugin-gfm"));
console.log("Copying complete!")
} catch(err) {
console.error("Error during copy:", err.message)
process.exit(1)
}