Skip to content

Instantly share code, notes, and snippets.

@roderik
Created January 15, 2024 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roderik/c83efef5412b7bbd7d76329c02a28338 to your computer and use it in GitHub Desktop.
Save roderik/c83efef5412b7bbd7d76329c02a28338 to your computer and use it in GitHub Desktop.
#!/bin/bash
## Configure all the nx, jest and tx config files for the libraries.
## The only thing you need to make sure of is to set the name correct in the package.json file
jq '.compilerOptions.paths = {}' tsconfig.base.json > temp.json && mv temp.json tsconfig.base.json
find ./libs -name "package.json" | while read pkg; do
pkg_name=$(jq -r '.name' "$pkg")
pkg_dir=$(dirname "$pkg")
jq --arg pkg_name "$pkg_name" --arg pkg_dir "$pkg_dir" '.compilerOptions.paths[$pkg_name] = [$pkg_dir + "/src/index.ts"]' tsconfig.base.json > temp.json && mv temp.json tsconfig.base.json
done
function relative_path_to_root {
local file_path=$1
local depth=$(echo $file_path | grep -o "/" | wc -l)
local relative_path=""
for ((i=1; i<=depth-1; i++)); do
relative_path+="../"
done
echo "${relative_path}"
}
for dir in ./libs/shared/next ./libs/saas/next ./libs/onprem/next; do
find "$dir" -name "package.json" | while read pkg; do
rel_path=$(relative_path_to_root "$pkg")
project_name=$(jq -r '.name' "$pkg" | sed 's/@bpaas\///')
echo "Project name: $project_name"
project_dir=$(dirname "$pkg" | sed 's/^\.\///')
project_json_path="$project_dir/project.json"
tags=("type:library")
if [[ $project_name == *"shared"* ]]; then
tags+=("scope:next-shared")
elif [[ $project_name == *"saas"* ]]; then
tags+=("scope:next-saas")
elif [[ $project_name == *"onprem"* ]]; then
tags+=("scope:next-onprem")
fi
cat << EOF > "$project_json_path"
{
"name": "$project_name",
"\$schema": "${rel_path}node_modules/nx/schemas/project-schema.json",
"sourceRoot": "$project_dir/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"defaultConfiguration": "production",
"configurations": {
"production": {
"eslintConfig": "{projectRoot}/.eslintrc.ci.json",
"hasTypeAwareRules": true
},
"development": {
"eslintConfig": "{projectRoot}/.eslintrc.json",
"hasTypeAwareRules": false,
"fix": true,
"quiet": true
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"forceExit": true,
},
"defaultConfiguration": "production",
"configurations": {
"production": {
"runInBand": true
},
"development": {
"maxWorkers": 6,
}
}
},
"check-circular": {
"executor": "nx:run-commands",
"inputs": ["{projectRoot}/**/*.ts"],
"options": {
"command": "pnpm madge --ts-config {projectRoot}/tsconfig.json --circular --extensions ts {projectRoot} --include-npm --no-spinner || true"
}
},
"build": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/{projectRoot}",
"tsConfig": "{projectRoot}/tsconfig.lib.json",
"project": "{projectRoot}/package.json",
"entryFile": "{projectRoot}/src/index.ts",
"external": "all",
"rollupConfig": "@nx/react/plugins/bundle-rollup",
"compiler": "swc",
"assets": [
{
"glob": "{projectRoot}/README.md",
"input": ".",
"output": "."
}
]
},
}
},
"tags": $(printf '%s\n' "${tags[@]}" | jq -R . | jq -s -c .)
}
EOF
cat << EOF > "$project_dir/tsconfig.json"
{
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"extends": "${rel_path}tsconfig.base.json"
}
EOF
cat << EOF > "$project_dir/tsconfig.lib.json"
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "${rel_path}dist/out-tsc",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts",
"next",
"@nx/next/typings/image.d.ts"
]
},
"exclude": [
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"src/**/*.spec.tsx",
"src/**/*.test.tsx",
"src/**/*.spec.js",
"src/**/*.test.js",
"src/**/*.spec.jsx",
"src/**/*.test.jsx"
],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
EOF
cat << EOF > "$project_dir/tsconfig.spec.json"
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "${rel_path}dist/out-tsc",
"module": "commonjs",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts",
"next",
"@nx/next/typings/image.d.ts",
"jest"
]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.test.tsx", "src/**/*.spec.tsx","src/**/*.d.ts"]
}
EOF
rm -Rf "$project_dir/.swcrc"
cat << EOF > "$project_dir/.eslintrc.json"
{
"extends": ["${rel_path}.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"rules": {}
}
]
}
EOF
cat << EOF > "$project_dir/.eslintrc.ci.json"
{
"extends": ["${rel_path}.eslintrc.ci.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"rules": {},
"parserOptions": {
"project": ["$project_dir/tsconfig.*?.json"]
}
}
]
}
EOF
cat << EOF > "$project_dir/jest.config.ts"
/* eslint-disable */
export default {
displayName: '$project_name',
preset: '${rel_path}jest.preset.ts',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
testEnvironment: 'jsdom',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
coverageDirectory: '${rel_path}coverage/$project_dir',
transformIgnorePatterns: ['node_modules/.pnpm/(?!@amcharts|wagmi|crypto-hash|d3-|@wagmi|internmap|@rainbow-me)'],
};
EOF
done
done
find ./libs/shared/nest -name "package.json" | grep -v "docker-image-versions" | while read pkg; do
rel_path=$(relative_path_to_root "$pkg")
project_name=$(jq -r '.name' "$pkg" | sed 's/@bpaas\///')
echo "Project name: $project_name"
project_dir=$(dirname "$pkg" | sed 's/^\.\///')
project_json_path="$project_dir/project.json"
tags=("type:library")
if [[ $project_name == *"api"* ]]; then
tags+=("scope:nest-api")
elif [[ $project_name == *"database"* ]]; then
tags+=("scope:nest-database")
elif [[ $project_name == *"processor"* ]]; then
tags+=("scope:nest-processor")
elif [[ $project_name == *"deploy-worker"* ]]; then
tags+=("scope:nest-deploy-worker")
elif [[ $project_name == *"jobs"* ]]; then
tags+=("scope:nest-jobs")
elif [[ $project_name == *"singleton"* ]]; then
tags+=("scope:nest-singleton")
else
tags+=("scope:nest-common")
fi
cat << EOF > "$project_json_path"
{
"name": "$project_name",
"\$schema": "${rel_path}node_modules/nx/schemas/project-schema.json",
"sourceRoot": "$project_dir/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"defaultConfiguration": "production",
"configurations": {
"production": {
"eslintConfig": "{projectRoot}/.eslintrc.ci.json",
"hasTypeAwareRules": true
},
"development": {
"eslintConfig": "{projectRoot}/.eslintrc.json",
"hasTypeAwareRules": false,
"fix": true,
"quiet": true
}
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"forceExit": true,
},
"defaultConfiguration": "production",
"configurations": {
"production": {
"runInBand": true
},
"development": {
"maxWorkers": 6,
}
}
},
"check-circular": {
"executor": "nx:run-commands",
"inputs": ["{projectRoot}/**/*.ts"],
"options": {
"command": "pnpm madge --ts-config {projectRoot}/tsconfig.json --circular --extensions ts {projectRoot} --include-npm --no-spinner"
}
},
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"clean": false,
"outputPath": "dist/{projectRoot}",
"tsConfig": "{projectRoot}/tsconfig.lib.json",
"packageJson": "{projectRoot}/package.json",
"main": "{projectRoot}/src/index.ts",
"assets": [
"{projectRoot}/*.md",
"{projectRoot}/src/*.json",
"{projectRoot}/src/**/*.json"
]
}
}
},
"tags": $(printf '%s\n' "${tags[@]}" | jq -R . | jq -s -c .)
}
EOF
cat << EOF > "$project_dir/tsconfig.json"
{
"extends": "${rel_path}tsconfig.base.json",
"compilerOptions": {},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
EOF
cat << EOF > "$project_dir/tsconfig.lib.json"
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "${rel_path}dist/out-tsc",
"declaration": true,
"types": ["node"],
"target": "es2021"
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
EOF
cat << EOF > "$project_dir/tsconfig.spec.json"
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "${rel_path}dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}
EOF
cat << EOF > "$project_dir/package.json"
{
"name": "@bpaas/$project_name",
"version": "0.0.1",
"dependencies": {
"tslib": "2.6.2"
},
"type": "module",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
EOF
cat << EOF > "$project_dir/jest.config.ts"
/* eslint-disable */
export default {
displayName: '$project_name',
preset: '${rel_path}jest.preset.ts',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html', 'json'],
coverageDirectory: '${rel_path}coverage/$project_dir',
};
EOF
rm -Rf "$project_dir/.swcrc"
cat << EOF > "$project_dir/.eslintrc.json"
{
"extends": ["${rel_path}.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"rules": {}
}
]
}
EOF
cat << EOF > "$project_dir/.eslintrc.ci.json"
{
"extends": ["${rel_path}.eslintrc.ci.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"extends": ["plugin:@darraghor/nestjs-typed/recommended", "plugin:@darraghor/nestjs-typed/no-swagger"],
"plugins": ["@darraghor/nestjs-typed"],
"rules": {
'@darraghor/nestjs-typed/injectable-should-be-provided': [
'error',
{
src: ['$project_dir/src/**/*.ts'],
filterFromPaths: ['node_modules', '.test.', '.spec.', 'jest.config.ts', 'tsconfig.spec.json', 'tsconfig.scripts.json'],
},
],
},
"parserOptions": {
"project": ["$project_dir/tsconfig.*?.json"]
}
}
]
}
EOF
done
# Directories to start the search from
dirs=("apps" "libs")
# Loop over directories
for dir in ${dirs[@]}; do
# Recursively find all project.json files in the directory
find "$dir" -name "project.json" | while read project; do
# Derive the project directory
project_dir=$(dirname "$project")
# Check if there are any spec files in the directory or its subdirectories
if ! find "$project_dir" -name "*.spec.ts" -o -name "*.spec.tsx" | grep -q .; then
# Rename test to xtest in project.json if test is present
sed -i '' 's/"test"/"xtest"/g' "$project"
else
# If spec files exist and the task is xtest, rename xtest back to test
sed -i '' 's/"xtest"/"test"/g' "$project"
fi
done
done
pnpm nx format:write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment