Skip to content

Commit 65369cd

Browse files
authored
chore: Add check local packages action (#1092)
* chore: Update asset-packs package * fix: Allow dev package * chore: Move check no local packages to a GHAction * chore: Update asset-packs package * fix: Convert TS file to CJS * fix: GHAction comment id * chore: Move local dependencies check to ci pipeline * fix: debug downloaded files * chore: Enhance message * chore: simplify comment dependencies logic * chore: Generate comment.md to properly show in the PR * fix: string comment * fix: error detection * fix: check exit code * fix: catch exit code * chore: Update @dcl/asset-packs
1 parent 059ccbd commit 65369cd

File tree

3 files changed

+200
-5
lines changed

3 files changed

+200
-5
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
// Function to recursively find all package.json files
5+
function findPackageJsonFiles(dir, fileList = []) {
6+
const files = fs.readdirSync(dir)
7+
8+
for (const file of files) {
9+
const filePath = path.join(dir, file)
10+
const stat = fs.statSync(filePath)
11+
12+
if (stat.isDirectory() && !filePath.includes('node_modules')) {
13+
findPackageJsonFiles(filePath, fileList)
14+
} else if (file === 'package.json') {
15+
fileList.push(filePath)
16+
}
17+
}
18+
19+
return fileList
20+
}
21+
22+
// Check for local package dependencies
23+
function checkNoLocalPackages(packageJsonPath) {
24+
try {
25+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
26+
const errors = []
27+
28+
// Check both dependencies and devDependencies
29+
for (const deps of [packageJson.dependencies, packageJson.devDependencies]) {
30+
if (!deps) continue
31+
32+
for (const [key, value] of Object.entries(deps)) {
33+
if (
34+
value.startsWith('file:') ||
35+
value.startsWith('http:') ||
36+
value.startsWith('https:') ||
37+
value.startsWith('git:')
38+
) {
39+
errors.push(`Dependency ${key} is not pointing to a published version: ${value}`)
40+
}
41+
}
42+
}
43+
44+
if (errors.length) {
45+
console.error(`\n\nErrors in ${packageJsonPath}:`)
46+
errors.forEach((error) => console.error(`- ${error}`))
47+
return false
48+
}
49+
50+
return true
51+
} catch (error) {
52+
console.error(`Error processing ${packageJsonPath}: ${error.message}`)
53+
return false
54+
}
55+
}
56+
57+
// Main function
58+
function main() {
59+
// Check if a directory was passed as an argument
60+
const targetDir = process.argv[2] || '.'
61+
62+
console.log(`Checking for local package dependencies in ${targetDir}...`)
63+
const packageJsonFiles = findPackageJsonFiles(targetDir)
64+
console.log(`Found ${packageJsonFiles.length} package.json files to check.`)
65+
66+
let hasErrors = false
67+
68+
for (const packageJsonPath of packageJsonFiles) {
69+
console.log(`Checking ${packageJsonPath}...`)
70+
if (!checkNoLocalPackages(packageJsonPath)) {
71+
hasErrors = true
72+
}
73+
}
74+
75+
if (hasErrors) {
76+
console.error(
77+
`\n\nFound packages with local dependencies in ${targetDir}. Please publish these dependencies instead of using local references.`
78+
)
79+
process.exit(1)
80+
} else {
81+
console.log(`\n\nNo local dependencies found in ${targetDir}. All good!`)
82+
}
83+
}
84+
85+
main()

.github/workflows/ci.yml

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,45 @@ jobs:
3838
cache: 'npm'
3939
- name: docs
4040
run: make docs
41+
4142
test:
4243
runs-on: ubuntu-latest
4344
steps:
4445
- uses: actions/checkout@master
46+
4547
- uses: actions/setup-node@v3
4648
with:
4749
node-version: 20
4850
cache: 'npm'
51+
4952
- name: install
5053
run: make install
54+
5155
- name: build
5256
run: make build
57+
5358
- name: test
5459
env:
55-
NODE_OPTIONS: "--max_old_space_size=4096"
60+
NODE_OPTIONS: '--max_old_space_size=4096'
5661
run: make test-coverage
62+
5763
- name: test-cli
5864
run: make test-cli
65+
5966
- name: test-inspector
6067
env:
61-
NODE_OPTIONS: "--max_old_space_size=4096"
68+
NODE_OPTIONS: '--max_old_space_size=4096'
6269
run: make test-inspector
70+
6371
- uses: codecov/codecov-action@v3
6472
if: always()
6573
with:
6674
fail_ci_if_error: false # optional (default = false)
6775
verbose: true # optional (default = false)
76+
6877
- name: prepare releasable packages
6978
run: make prepare
79+
7080
test-e2e:
7181
needs: [notify_deployment]
7282
runs-on: ubuntu-latest
@@ -90,8 +100,9 @@ jobs:
90100
run: cd packages/@dcl/inspector && npm i && cd ../../../
91101
- name: test-inspector E2E
92102
env:
93-
NODE_OPTIONS: "--max_old_space_size=4096"
103+
NODE_OPTIONS: '--max_old_space_size=4096'
94104
run: E2E_URL=https://${{steps.get-cloudflare-url.outputs.result}}/inspector make test-inspector-e2e
105+
95106
build:
96107
runs-on: ubuntu-latest
97108
permissions:
@@ -110,7 +121,6 @@ jobs:
110121
node-version: 20
111122
cache: 'npm'
112123

113-
114124
- name: install
115125
run: make install
116126

@@ -447,3 +457,104 @@ jobs:
447457
env:
448458
AWS_ACCESS_KEY_ID: ${{ secrets.SDK_TEAM_AWS_ID }}
449459
AWS_SECRET_ACCESS_KEY: ${{ secrets.SDK_TEAM_AWS_SECRET }}
460+
461+
check-local-deps:
462+
name: Check for development dependencies
463+
needs: [build]
464+
runs-on: ubuntu-latest
465+
if: >
466+
(github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'main') ||
467+
github.ref == 'refs/heads/main'
468+
469+
steps:
470+
- uses: actions/checkout@v3
471+
472+
- uses: actions/setup-node@v3
473+
with:
474+
node-version: 20
475+
476+
- name: Download prepared packages
477+
uses: actions/download-artifact@v4
478+
with:
479+
name: built-artifacts
480+
path: downloaded-packages
481+
482+
- name: Set up artifacts for checking
483+
run: |
484+
echo "Setting up built artifacts for checking..."
485+
mkdir -p extracted-packages
486+
487+
# Extract all packages to separate directories
488+
find downloaded-packages -type f -name "*.tgz" | while read pkg; do
489+
if [ -f "$pkg" ]; then
490+
# Get package name from the filename
491+
pkg_name=$(basename "$pkg" .tgz)
492+
493+
# Create directory for this package
494+
mkdir -p "extracted-packages/$pkg_name"
495+
496+
echo "Extracting $pkg to extracted-packages/$pkg_name"
497+
tar -xzf "$pkg" -C "extracted-packages/$pkg_name"
498+
fi
499+
done
500+
501+
# List all extracted packages
502+
echo "Extracted packages:"
503+
find extracted-packages -name "package.json" | sort
504+
505+
- name: Check for local package dependencies
506+
id: dependency-check
507+
continue-on-error: true
508+
run: |
509+
set +e
510+
node .github/scripts/check-no-local-packages.cjs ./extracted-packages > check-output.txt 2>&1
511+
check_exit_code=$?
512+
set -e
513+
514+
echo "Full output from dependency check:"
515+
cat check-output.txt
516+
517+
if [ $check_exit_code -ne 0 ]; then
518+
grep -i "dependency " check-output.txt > dependency-errors.txt || echo "No specific dependency errors found" > dependency-errors.txt
519+
if [ ! -s dependency-errors.txt ]; then
520+
echo "⚠️ No specific dependency errors found" > dependency-errors.txt
521+
fi
522+
exit 1
523+
fi
524+
525+
- name: Generate comment content
526+
if: steps.dependency-check.outcome == 'failure'
527+
run: |
528+
echo "## ⚠️ Local Dependencies Detected" >> dependency-check-comment.md
529+
echo "This PR contains package dependencies that use local paths (\`file:\`) or non-allowed URLs (\`http:\`, \`https:\`, \`git:\`)." >> dependency-check-comment.md
530+
echo "These must be published and referenced properly before merging." >> dependency-check-comment.md
531+
echo "### Not allowed dependencies:" >> dependency-check-comment.md
532+
echo '```' >> dependency-check-comment.md
533+
cat dependency-errors.txt >> dependency-check-comment.md
534+
echo '```' >> dependency-check-comment.md
535+
echo "Review the workflow logs for more details." >> dependency-check-comment.md
536+
echo "> **Note:** This comment is automatically updated when the PR is updated." >> dependency-check-comment.md
537+
538+
- name: Find Local Dependencies Comment if check fails
539+
if: ${{ github.event_name == 'pull_request' && steps.dependency-check.outcome == 'failure' }}
540+
uses: peter-evans/find-comment@v3
541+
id: fc-local-deps
542+
with:
543+
issue-number: ${{ github.event.pull_request.number }}
544+
comment-author: 'github-actions[bot]'
545+
body-includes: Local Dependencies Detected
546+
547+
- name: Create or update Local Dependencies Comment if check fails
548+
if: ${{ github.event_name == 'pull_request' && steps.dependency-check.outcome == 'failure' }}
549+
uses: peter-evans/create-or-update-comment@v4
550+
with:
551+
comment-id: ${{ steps.fc-local-deps.outputs.comment-id }}
552+
issue-number: ${{ github.event.pull_request.number }}
553+
body-path: dependency-check-comment.md
554+
edit-mode: replace
555+
556+
- name: Fail check if local dependencies found
557+
if: steps.dependency-check.outcome == 'failure'
558+
run: |
559+
echo "❌ Local package dependencies detected! These must be fixed before merging."
560+
exit 1

scripts/prepare.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ flow('build-all', () => {
3535
for (const dependency of processWithOptimisticDependencies(graph)) {
3636
const projectDirectory = resolveProjectPath(dependency)
3737
installCrossDependencies(projectDirectory)
38-
checkNoLocalPackages(projectDirectory)
3938
itExecutes('npm pack', projectDirectory)
4039
}
4140
})

0 commit comments

Comments
 (0)