第一基础版
This commit is contained in:
53
scripts/clean.mjs
Normal file
53
scripts/clean.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
import { promises as fs } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const rootDir = process.cwd();
|
||||
|
||||
/**
|
||||
* 递归查找并删除目标目录
|
||||
* @param {string} currentDir - 当前遍历的目录路径
|
||||
*/
|
||||
async function cleanTargetsRecursively(currentDir, targets) {
|
||||
const items = await fs.readdir(currentDir);
|
||||
|
||||
for (const item of items) {
|
||||
try {
|
||||
const itemPath = join(currentDir, item);
|
||||
if (targets.includes(item)) {
|
||||
// 匹配到目标目录或文件时直接删除
|
||||
await fs.rm(itemPath, { force: true, recursive: true });
|
||||
console.log(`Deleted: ${itemPath}`);
|
||||
}
|
||||
const stat = await fs.lstat(itemPath);
|
||||
if (stat.isDirectory()) {
|
||||
await cleanTargetsRecursively(itemPath, targets);
|
||||
}
|
||||
} catch {
|
||||
// console.error(
|
||||
// `Error handling item ${item} in ${currentDir}: ${error.message}`,
|
||||
// );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(async function startCleanup() {
|
||||
// 要删除的目录及文件名称
|
||||
const targets = ['node_modules', 'dist', '.turbo', 'dist.zip'];
|
||||
|
||||
const deleteLockFile = process.argv.includes('--del-lock');
|
||||
const cleanupTargets = [...targets];
|
||||
if (deleteLockFile) {
|
||||
cleanupTargets.push('pnpm-lock.yaml');
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Starting cleanup of targets: ${cleanupTargets.join(', ')} from root: ${rootDir}`,
|
||||
);
|
||||
|
||||
try {
|
||||
await cleanTargetsRecursively(rootDir, cleanupTargets);
|
||||
console.log('Cleanup process completed.');
|
||||
} catch (error) {
|
||||
console.error(`Unexpected error during cleanup: ${error.message}`);
|
||||
}
|
||||
})();
|
||||
31
scripts/deploy/Dockerfile
Normal file
31
scripts/deploy/Dockerfile
Normal file
@@ -0,0 +1,31 @@
|
||||
FROM node:20-slim AS builder
|
||||
|
||||
# --max-old-space-size
|
||||
ENV PNPM_HOME="/pnpm"
|
||||
ENV PATH="$PNPM_HOME:$PATH"
|
||||
ENV NODE_OPTIONS=--max-old-space-size=8192
|
||||
ENV TZ=Asia/Shanghai
|
||||
|
||||
RUN corepack enable
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# copy package.json and pnpm-lock.yaml to workspace
|
||||
COPY . /app
|
||||
|
||||
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
|
||||
RUN pnpm run build --filter=\!./docs
|
||||
|
||||
RUN echo "Builder Success 🎉"
|
||||
|
||||
FROM nginx:stable-alpine AS production
|
||||
|
||||
RUN echo "types { application/javascript js mjs; }" > /etc/nginx/conf.d/mjs.conf
|
||||
COPY --from=builder /app/playground/dist /usr/share/nginx/html
|
||||
|
||||
COPY --from=builder /app/scripts/deploy/nginx.conf /etc/nginx/nginx.conf
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
# start nginx
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
55
scripts/deploy/build-local-docker-image.sh
Normal file
55
scripts/deploy/build-local-docker-image.sh
Normal file
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
LOG_FILE=${SCRIPT_DIR}/build-local-docker-image.log
|
||||
ERROR=""
|
||||
IMAGE_NAME="vben-admin-local"
|
||||
|
||||
function stop_and_remove_container() {
|
||||
# Stop and remove the existing container
|
||||
docker stop ${IMAGE_NAME} >/dev/null 2>&1
|
||||
docker rm ${IMAGE_NAME} >/dev/null 2>&1
|
||||
}
|
||||
|
||||
function remove_image() {
|
||||
# Remove the existing image
|
||||
docker rmi vben-admin-pro >/dev/null 2>&1
|
||||
}
|
||||
|
||||
function install_dependencies() {
|
||||
# Install all dependencies
|
||||
cd ${SCRIPT_DIR}
|
||||
pnpm install || ERROR="install_dependencies failed"
|
||||
}
|
||||
|
||||
function build_image() {
|
||||
# build docker
|
||||
docker build ../../ -f Dockerfile -t ${IMAGE_NAME} || ERROR="build_image failed"
|
||||
}
|
||||
|
||||
function log_message() {
|
||||
if [[ ${ERROR} != "" ]];
|
||||
then
|
||||
>&2 echo "build failed, Please check build-local-docker-image.log for more details"
|
||||
>&2 echo "ERROR: ${ERROR}"
|
||||
exit 1
|
||||
else
|
||||
echo "docker image with tag '${IMAGE_NAME}' built sussessfully. Use below sample command to run the container"
|
||||
echo ""
|
||||
echo "docker run -d -p 8010:8080 --name ${IMAGE_NAME} ${IMAGE_NAME}"
|
||||
fi
|
||||
}
|
||||
|
||||
echo "Info: Stopping and removing existing container and image" | tee ${LOG_FILE}
|
||||
stop_and_remove_container
|
||||
remove_image
|
||||
|
||||
echo "Info: Installing dependencies" | tee -a ${LOG_FILE}
|
||||
install_dependencies 1>> ${LOG_FILE} 2>> ${LOG_FILE}
|
||||
|
||||
if [[ ${ERROR} == "" ]]; then
|
||||
echo "Info: Building docker image" | tee -a ${LOG_FILE}
|
||||
build_image 1>> ${LOG_FILE} 2>> ${LOG_FILE}
|
||||
fi
|
||||
|
||||
log_message | tee -a ${LOG_FILE}
|
||||
75
scripts/deploy/nginx.conf
Normal file
75
scripts/deploy/nginx.conf
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
#user nobody;
|
||||
worker_processes 1;
|
||||
|
||||
#error_log logs/error.log;
|
||||
#error_log logs/error.log notice;
|
||||
#error_log logs/error.log info;
|
||||
|
||||
#pid logs/nginx.pid;
|
||||
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
|
||||
http {
|
||||
include mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
types {
|
||||
application/javascript js mjs;
|
||||
text/css css;
|
||||
text/html html;
|
||||
}
|
||||
|
||||
sendfile on;
|
||||
# tcp_nopush on;
|
||||
|
||||
#keepalive_timeout 0;
|
||||
# keepalive_timeout 65;
|
||||
|
||||
# gzip on;
|
||||
# gzip_buffers 32 16k;
|
||||
# gzip_comp_level 6;
|
||||
# gzip_min_length 1k;
|
||||
# gzip_static on;
|
||||
# gzip_types text/plain
|
||||
# text/css
|
||||
# application/javascript
|
||||
# application/json
|
||||
# application/x-javascript
|
||||
# text/xml
|
||||
# application/xml
|
||||
# application/xml+rss
|
||||
# text/javascript; #设置压缩的文件类型
|
||||
# gzip_vary on;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
server_name localhost;
|
||||
|
||||
location / {
|
||||
root /usr/share/nginx/html;
|
||||
try_files $uri $uri/ /index.html;
|
||||
index index.html;
|
||||
# Enable CORS
|
||||
add_header 'Access-Control-Allow-Origin' '*';
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
|
||||
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
|
||||
if ($request_method = 'OPTIONS') {
|
||||
add_header 'Access-Control-Max-Age' 1728000;
|
||||
add_header 'Content-Type' 'text/plain charset=UTF-8';
|
||||
add_header 'Content-Length' 0;
|
||||
return 204;
|
||||
}
|
||||
}
|
||||
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
|
||||
location = /50x.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
scripts/turbo-run/README.md
Normal file
3
scripts/turbo-run/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# @vben/turbo-run
|
||||
|
||||
turbo-run is a command line tool that allows you to run multiple commands in parallel.
|
||||
3
scripts/turbo-run/bin/turbo-run.mjs
Normal file
3
scripts/turbo-run/bin/turbo-run.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import('../dist/index.mjs');
|
||||
7
scripts/turbo-run/build.config.ts
Normal file
7
scripts/turbo-run/build.config.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { defineBuildConfig } from 'unbuild';
|
||||
|
||||
export default defineBuildConfig({
|
||||
clean: true,
|
||||
declaration: true,
|
||||
entries: ['src/index'],
|
||||
});
|
||||
29
scripts/turbo-run/package.json
Normal file
29
scripts/turbo-run/package.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@vben/turbo-run",
|
||||
"version": "5.5.4",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"stub": "pnpm unbuild --stub"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"bin": {
|
||||
"turbo-run": "./bin/turbo-run.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"default": "./dist/index.mjs"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@clack/prompts": "catalog:",
|
||||
"@vben/node-utils": "workspace:*",
|
||||
"cac": "catalog:"
|
||||
}
|
||||
}
|
||||
29
scripts/turbo-run/src/index.ts
Normal file
29
scripts/turbo-run/src/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { colors, consola } from '@vben/node-utils';
|
||||
|
||||
import { cac } from 'cac';
|
||||
|
||||
import { run } from './run';
|
||||
|
||||
try {
|
||||
const turboRun = cac('turbo-run');
|
||||
|
||||
turboRun
|
||||
.command('[script]')
|
||||
.usage(`Run turbo interactively.`)
|
||||
.action(async (command: string) => {
|
||||
run({ command });
|
||||
});
|
||||
|
||||
// Invalid command
|
||||
turboRun.on('command:*', () => {
|
||||
consola.error(colors.red('Invalid command!'));
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
turboRun.usage('turbo-run');
|
||||
turboRun.help();
|
||||
turboRun.parse();
|
||||
} catch (error) {
|
||||
consola.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
67
scripts/turbo-run/src/run.ts
Normal file
67
scripts/turbo-run/src/run.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { execaCommand, getPackages } from '@vben/node-utils';
|
||||
|
||||
import { cancel, isCancel, select } from '@clack/prompts';
|
||||
|
||||
interface RunOptions {
|
||||
command?: string;
|
||||
}
|
||||
|
||||
export async function run(options: RunOptions) {
|
||||
const { command } = options;
|
||||
if (!command) {
|
||||
console.error('Please enter the command to run');
|
||||
process.exit(1);
|
||||
}
|
||||
const { packages } = await getPackages();
|
||||
// const appPkgs = await findApps(process.cwd(), packages);
|
||||
// const websitePkg = packages.find(
|
||||
// (item) => item.packageJson.name === '@vben/website',
|
||||
// );
|
||||
|
||||
// 只显示有对应命令的包
|
||||
const selectPkgs = packages.filter((pkg) => {
|
||||
return (pkg?.packageJson as Record<string, any>)?.scripts?.[command];
|
||||
});
|
||||
|
||||
let selectPkg: string | symbol;
|
||||
if (selectPkgs.length > 1) {
|
||||
selectPkg = await select<any, string>({
|
||||
message: `Select the app you need to run [${command}]:`,
|
||||
options: selectPkgs.map((item) => ({
|
||||
label: item?.packageJson.name,
|
||||
value: item?.packageJson.name,
|
||||
})),
|
||||
});
|
||||
|
||||
if (isCancel(selectPkg) || !selectPkg) {
|
||||
cancel('👋 Has cancelled');
|
||||
process.exit(0);
|
||||
}
|
||||
} else {
|
||||
selectPkg = selectPkgs[0]?.packageJson?.name ?? '';
|
||||
}
|
||||
|
||||
if (!selectPkg) {
|
||||
console.error('No app found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
execaCommand(`pnpm --filter=${selectPkg} run ${command}`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤app包
|
||||
* @param root
|
||||
* @param packages
|
||||
*/
|
||||
// async function findApps(root: string, packages: Package[]) {
|
||||
// // apps内的
|
||||
// const appPackages = packages.filter((pkg) => {
|
||||
// const viteConfigExists = fs.existsSync(join(pkg.dir, 'vite.config.mts'));
|
||||
// return pkg.dir.startsWith(join(root, 'apps')) && viteConfigExists;
|
||||
// });
|
||||
|
||||
// return appPackages;
|
||||
// }
|
||||
6
scripts/turbo-run/tsconfig.json
Normal file
6
scripts/turbo-run/tsconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@vben/tsconfig/node.json",
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
3
scripts/vsh/README.md
Normal file
3
scripts/vsh/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# @vben/vsh
|
||||
|
||||
shell 脚本工具集合
|
||||
3
scripts/vsh/bin/vsh.mjs
Normal file
3
scripts/vsh/bin/vsh.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import('../dist/index.mjs');
|
||||
7
scripts/vsh/build.config.ts
Normal file
7
scripts/vsh/build.config.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { defineBuildConfig } from 'unbuild';
|
||||
|
||||
export default defineBuildConfig({
|
||||
clean: true,
|
||||
declaration: true,
|
||||
entries: ['src/index'],
|
||||
});
|
||||
31
scripts/vsh/package.json
Normal file
31
scripts/vsh/package.json
Normal file
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@vben/vsh",
|
||||
"version": "5.5.4",
|
||||
"private": true,
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"stub": "pnpm unbuild --stub"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"bin": {
|
||||
"vsh": "./bin/vsh.mjs"
|
||||
},
|
||||
"main": "./dist/index.mjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"default": "./dist/index.mjs"
|
||||
},
|
||||
"./package.json": "./package.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vben/node-utils": "workspace:*",
|
||||
"cac": "catalog:",
|
||||
"circular-dependency-scanner": "catalog:",
|
||||
"depcheck": "catalog:",
|
||||
"publint": "catalog:"
|
||||
}
|
||||
}
|
||||
80
scripts/vsh/src/check-circular/index.ts
Normal file
80
scripts/vsh/src/check-circular/index.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { extname } from 'node:path';
|
||||
|
||||
import { getStagedFiles } from '@vben/node-utils';
|
||||
|
||||
import { circularDepsDetect, printCircles } from 'circular-dependency-scanner';
|
||||
|
||||
const IGNORE_DIR = [
|
||||
'dist',
|
||||
'.turbo',
|
||||
'output',
|
||||
'.cache',
|
||||
'scripts',
|
||||
'internal',
|
||||
'packages/effects/request/src/',
|
||||
'packages/@core/ui-kit/menu-ui/src/',
|
||||
'packages/@core/ui-kit/popup-ui/src/',
|
||||
].join(',');
|
||||
|
||||
const IGNORE = [`**/{${IGNORE_DIR}}/**`];
|
||||
|
||||
interface CommandOptions {
|
||||
staged: boolean;
|
||||
verbose: boolean;
|
||||
}
|
||||
|
||||
async function checkCircular({ staged, verbose }: CommandOptions) {
|
||||
const results = await circularDepsDetect({
|
||||
absolute: staged,
|
||||
cwd: process.cwd(),
|
||||
ignore: IGNORE,
|
||||
});
|
||||
|
||||
if (staged) {
|
||||
let files = await getStagedFiles();
|
||||
|
||||
const allowedExtensions = new Set([
|
||||
'.cjs',
|
||||
'.js',
|
||||
'.jsx',
|
||||
'.mjs',
|
||||
'.ts',
|
||||
'.tsx',
|
||||
'.vue',
|
||||
]);
|
||||
|
||||
// 过滤文件列表
|
||||
files = files.filter((file) => allowedExtensions.has(extname(file)));
|
||||
|
||||
const circularFiles: string[][] = [];
|
||||
|
||||
for (const file of files) {
|
||||
for (const result of results) {
|
||||
const resultFiles = result.flat();
|
||||
if (resultFiles.includes(file)) {
|
||||
circularFiles.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
verbose && printCircles(circularFiles);
|
||||
} else {
|
||||
verbose && printCircles(results);
|
||||
}
|
||||
}
|
||||
|
||||
function defineCheckCircularCommand(cac: CAC) {
|
||||
cac
|
||||
.command('check-circular')
|
||||
.option(
|
||||
'--staged',
|
||||
'Whether it is the staged commit mode, in which mode, if there is a circular dependency, an alarm will be given.',
|
||||
)
|
||||
.usage(`Analysis of project circular dependencies.`)
|
||||
.action(async ({ staged }) => {
|
||||
await checkCircular({ staged, verbose: true });
|
||||
});
|
||||
}
|
||||
|
||||
export { defineCheckCircularCommand };
|
||||
85
scripts/vsh/src/check-dep/index.ts
Normal file
85
scripts/vsh/src/check-dep/index.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { getPackages } from '@vben/node-utils';
|
||||
|
||||
import depcheck from 'depcheck';
|
||||
|
||||
async function runDepcheck() {
|
||||
const { packages } = await getPackages();
|
||||
await Promise.all(
|
||||
packages.map(async (pkg) => {
|
||||
if (
|
||||
[
|
||||
'@vben/backend-mock',
|
||||
'@vben/commitlint-config',
|
||||
'@vben/eslint-config',
|
||||
'@vben/lint-staged-config',
|
||||
'@vben/node-utils',
|
||||
'@vben/prettier-config',
|
||||
'@vben/stylelint-config',
|
||||
'@vben/tailwind-config',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/vite-config',
|
||||
'@vben/vsh',
|
||||
].includes(pkg.packageJson.name)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const unused = await depcheck(pkg.dir, {
|
||||
ignoreMatches: [
|
||||
'vite',
|
||||
'vitest',
|
||||
'unbuild',
|
||||
'@vben/tsconfig',
|
||||
'@vben/vite-config',
|
||||
'@vben/tailwind-config',
|
||||
'@types/*',
|
||||
'@vben-core/design',
|
||||
],
|
||||
ignorePatterns: ['dist', 'node_modules', 'public'],
|
||||
});
|
||||
|
||||
// 删除file:前缀的依赖提示,该依赖是本地依赖
|
||||
Reflect.deleteProperty(unused.missing, 'file:');
|
||||
Object.keys(unused.missing).forEach((key) => {
|
||||
unused.missing[key] = (unused.missing[key] || []).filter(
|
||||
(item: string) => !item.startsWith('/'),
|
||||
);
|
||||
if (unused.missing[key].length === 0) {
|
||||
Reflect.deleteProperty(unused.missing, key);
|
||||
}
|
||||
});
|
||||
|
||||
if (
|
||||
Object.keys(unused.missing).length === 0 &&
|
||||
unused.dependencies.length === 0 &&
|
||||
unused.devDependencies.length === 0
|
||||
) {
|
||||
return;
|
||||
}
|
||||
console.error(
|
||||
'\n',
|
||||
pkg.packageJson.name,
|
||||
'\n missing:',
|
||||
unused.missing,
|
||||
'\n dependencies:',
|
||||
unused.dependencies,
|
||||
'\n devDependencies:',
|
||||
unused.devDependencies,
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function defineDepcheckCommand(cac: CAC) {
|
||||
cac
|
||||
.command('check-dep')
|
||||
.usage(`Analysis of project circular dependencies.`)
|
||||
.action(async () => {
|
||||
await runDepcheck();
|
||||
});
|
||||
}
|
||||
|
||||
export { defineDepcheckCommand };
|
||||
78
scripts/vsh/src/code-workspace/index.ts
Normal file
78
scripts/vsh/src/code-workspace/index.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { join, relative } from 'node:path';
|
||||
|
||||
import {
|
||||
colors,
|
||||
consola,
|
||||
findMonorepoRoot,
|
||||
getPackages,
|
||||
gitAdd,
|
||||
outputJSON,
|
||||
prettierFormat,
|
||||
toPosixPath,
|
||||
} from '@vben/node-utils';
|
||||
|
||||
const CODE_WORKSPACE_FILE = join('vben-admin.code-workspace');
|
||||
|
||||
interface CodeWorkspaceCommandOptions {
|
||||
autoCommit?: boolean;
|
||||
spaces?: number;
|
||||
}
|
||||
|
||||
async function createCodeWorkspace({
|
||||
autoCommit = false,
|
||||
spaces = 2,
|
||||
}: CodeWorkspaceCommandOptions) {
|
||||
const { packages, rootDir } = await getPackages();
|
||||
|
||||
let folders = packages.map((pkg) => {
|
||||
const { dir, packageJson } = pkg;
|
||||
return {
|
||||
name: packageJson.name,
|
||||
path: toPosixPath(relative(rootDir, dir)),
|
||||
};
|
||||
});
|
||||
|
||||
folders = folders.filter(Boolean);
|
||||
|
||||
const monorepoRoot = findMonorepoRoot();
|
||||
const outputPath = join(monorepoRoot, CODE_WORKSPACE_FILE);
|
||||
await outputJSON(outputPath, { folders }, spaces);
|
||||
|
||||
await prettierFormat(outputPath);
|
||||
if (autoCommit) {
|
||||
await gitAdd(CODE_WORKSPACE_FILE, monorepoRoot);
|
||||
}
|
||||
}
|
||||
|
||||
async function runCodeWorkspace({
|
||||
autoCommit,
|
||||
spaces,
|
||||
}: CodeWorkspaceCommandOptions) {
|
||||
await createCodeWorkspace({
|
||||
autoCommit,
|
||||
spaces,
|
||||
});
|
||||
if (autoCommit) {
|
||||
return;
|
||||
}
|
||||
consola.log('');
|
||||
consola.success(colors.green(`${CODE_WORKSPACE_FILE} is updated!`));
|
||||
consola.log('');
|
||||
}
|
||||
|
||||
function defineCodeWorkspaceCommand(cac: CAC) {
|
||||
cac
|
||||
.command('code-workspace')
|
||||
.usage('Update the `.code-workspace` file')
|
||||
.option('--spaces [number]', '.code-workspace JSON file spaces.', {
|
||||
default: 2,
|
||||
})
|
||||
.option('--auto-commit', 'auto commit .code-workspace JSON file.', {
|
||||
default: false,
|
||||
})
|
||||
.action(runCodeWorkspace);
|
||||
}
|
||||
|
||||
export { defineCodeWorkspaceCommand };
|
||||
41
scripts/vsh/src/index.ts
Normal file
41
scripts/vsh/src/index.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { colors, consola } from '@vben/node-utils';
|
||||
|
||||
import { cac } from 'cac';
|
||||
|
||||
import { defineCheckCircularCommand } from './check-circular';
|
||||
import { defineDepcheckCommand } from './check-dep';
|
||||
import { defineCodeWorkspaceCommand } from './code-workspace';
|
||||
import { defineLintCommand } from './lint';
|
||||
import { definePubLintCommand } from './publint';
|
||||
|
||||
try {
|
||||
const vsh = cac('vsh');
|
||||
|
||||
// vsh lint
|
||||
defineLintCommand(vsh);
|
||||
|
||||
// vsh publint
|
||||
definePubLintCommand(vsh);
|
||||
|
||||
// vsh code-workspace
|
||||
defineCodeWorkspaceCommand(vsh);
|
||||
|
||||
// vsh check-circular
|
||||
defineCheckCircularCommand(vsh);
|
||||
|
||||
// vsh check-dep
|
||||
defineDepcheckCommand(vsh);
|
||||
|
||||
// Invalid command
|
||||
vsh.on('command:*', () => {
|
||||
consola.error(colors.red('Invalid command!'));
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
vsh.usage('vsh');
|
||||
vsh.help();
|
||||
vsh.parse();
|
||||
} catch (error) {
|
||||
consola.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
48
scripts/vsh/src/lint/index.ts
Normal file
48
scripts/vsh/src/lint/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { CAC } from 'cac';
|
||||
|
||||
import { execaCommand } from '@vben/node-utils';
|
||||
|
||||
interface LintCommandOptions {
|
||||
/**
|
||||
* Format lint problem.
|
||||
*/
|
||||
format?: boolean;
|
||||
}
|
||||
|
||||
async function runLint({ format }: LintCommandOptions) {
|
||||
// process.env.FORCE_COLOR = '3';
|
||||
|
||||
if (format) {
|
||||
await execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache --fix`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
await execaCommand(`eslint . --cache --fix`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
await execaCommand(`prettier . --write --cache --log-level warn`, {
|
||||
stdio: 'inherit',
|
||||
});
|
||||
return;
|
||||
}
|
||||
await Promise.all([
|
||||
execaCommand(`eslint . --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
execaCommand(`prettier . --ignore-unknown --check --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
execaCommand(`stylelint "**/*.{vue,css,less,scss}" --cache`, {
|
||||
stdio: 'inherit',
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
function defineLintCommand(cac: CAC) {
|
||||
cac
|
||||
.command('lint')
|
||||
.usage('Batch execute project lint check.')
|
||||
.option('--format', 'Format lint problem.')
|
||||
.action(runLint);
|
||||
}
|
||||
|
||||
export { defineLintCommand };
|
||||
185
scripts/vsh/src/publint/index.ts
Normal file
185
scripts/vsh/src/publint/index.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import type { CAC } from 'cac';
|
||||
import type { Result } from 'publint';
|
||||
|
||||
import { basename, dirname, join } from 'node:path';
|
||||
|
||||
import {
|
||||
colors,
|
||||
consola,
|
||||
ensureFile,
|
||||
findMonorepoRoot,
|
||||
generatorContentHash,
|
||||
getPackages,
|
||||
outputJSON,
|
||||
readJSON,
|
||||
UNICODE,
|
||||
} from '@vben/node-utils';
|
||||
|
||||
import { publint } from 'publint';
|
||||
import { formatMessage } from 'publint/utils';
|
||||
|
||||
const CACHE_FILE = join(
|
||||
'node_modules',
|
||||
'.cache',
|
||||
'publint',
|
||||
'.pkglintcache.json',
|
||||
);
|
||||
|
||||
interface PubLintCommandOptions {
|
||||
/**
|
||||
* Only errors are checked, no program exit is performed
|
||||
*/
|
||||
check?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get files that require lint
|
||||
* @param files
|
||||
*/
|
||||
async function getLintFiles(files: string[] = []) {
|
||||
const lintFiles: string[] = [];
|
||||
|
||||
if (files?.length > 0) {
|
||||
return files.filter((file) => basename(file) === 'package.json');
|
||||
}
|
||||
|
||||
const { packages } = await getPackages();
|
||||
|
||||
for (const { dir } of packages) {
|
||||
lintFiles.push(join(dir, 'package.json'));
|
||||
}
|
||||
return lintFiles;
|
||||
}
|
||||
|
||||
function getCacheFile() {
|
||||
const root = findMonorepoRoot();
|
||||
return join(root, CACHE_FILE);
|
||||
}
|
||||
|
||||
async function readCache(cacheFile: string) {
|
||||
try {
|
||||
await ensureFile(cacheFile);
|
||||
return await readJSON(cacheFile);
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
async function runPublint(files: string[], { check }: PubLintCommandOptions) {
|
||||
const lintFiles = await getLintFiles(files);
|
||||
const cacheFile = getCacheFile();
|
||||
|
||||
const cacheData = await readCache(cacheFile);
|
||||
const cache: Record<string, { hash: string; result: Result }> = cacheData;
|
||||
|
||||
const results = await Promise.all(
|
||||
lintFiles.map(async (file) => {
|
||||
try {
|
||||
const pkgJson = await readJSON(file);
|
||||
|
||||
if (pkgJson.private) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reflect.deleteProperty(pkgJson, 'dependencies');
|
||||
Reflect.deleteProperty(pkgJson, 'devDependencies');
|
||||
Reflect.deleteProperty(pkgJson, 'peerDependencies');
|
||||
const content = JSON.stringify(pkgJson);
|
||||
const hash = generatorContentHash(content);
|
||||
|
||||
const publintResult: Result =
|
||||
cache?.[file]?.hash === hash
|
||||
? (cache?.[file]?.result ?? [])
|
||||
: await publint({
|
||||
level: 'suggestion',
|
||||
pkgDir: dirname(file),
|
||||
strict: true,
|
||||
});
|
||||
|
||||
cache[file] = {
|
||||
hash,
|
||||
result: publintResult,
|
||||
};
|
||||
|
||||
return { pkgJson, pkgPath: file, publintResult };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
await outputJSON(cacheFile, cache);
|
||||
printResult(results, check);
|
||||
}
|
||||
|
||||
function printResult(
|
||||
results: Array<null | {
|
||||
pkgJson: Record<string, number | string>;
|
||||
pkgPath: string;
|
||||
publintResult: Result;
|
||||
}>,
|
||||
check?: boolean,
|
||||
) {
|
||||
let errorCount = 0;
|
||||
let warningCount = 0;
|
||||
let suggestionsCount = 0;
|
||||
|
||||
for (const result of results) {
|
||||
if (!result) {
|
||||
continue;
|
||||
}
|
||||
const { pkgJson, pkgPath, publintResult } = result;
|
||||
const messages = publintResult?.messages ?? [];
|
||||
if (messages?.length < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
consola.log('');
|
||||
consola.log(pkgPath);
|
||||
for (const message of messages) {
|
||||
switch (message.type) {
|
||||
case 'error': {
|
||||
errorCount++;
|
||||
|
||||
break;
|
||||
}
|
||||
case 'suggestion': {
|
||||
suggestionsCount++;
|
||||
break;
|
||||
}
|
||||
case 'warning': {
|
||||
warningCount++;
|
||||
|
||||
break;
|
||||
}
|
||||
// No default
|
||||
}
|
||||
const ruleUrl = `https://publint.dev/rules#${message.code.toLocaleLowerCase()}`;
|
||||
consola.log(
|
||||
` ${formatMessage(message, pkgJson)}${colors.dim(` ${ruleUrl}`)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const totalCount = warningCount + errorCount + suggestionsCount;
|
||||
if (totalCount > 0) {
|
||||
consola.error(
|
||||
colors.red(
|
||||
`${UNICODE.FAILURE} ${totalCount} problem (${errorCount} errors, ${warningCount} warnings, ${suggestionsCount} suggestions)`,
|
||||
),
|
||||
);
|
||||
!check && process.exit(1);
|
||||
} else {
|
||||
consola.log(colors.green(`${UNICODE.SUCCESS} No problem`));
|
||||
}
|
||||
}
|
||||
|
||||
function definePubLintCommand(cac: CAC) {
|
||||
cac
|
||||
.command('publint [...files]')
|
||||
.usage('Check if the monorepo package conforms to the publint standard.')
|
||||
.option('--check', 'Only errors are checked, no program exit is performed.')
|
||||
.action(runPublint);
|
||||
}
|
||||
|
||||
export { definePubLintCommand };
|
||||
6
scripts/vsh/tsconfig.json
Normal file
6
scripts/vsh/tsconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@vben/tsconfig/node.json",
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user