Skip to content

Commit 33318f6

Browse files
👷 check package.json files for each PR (#3970)
1 parent 5de28a8 commit 33318f6

File tree

5 files changed

+80
-87
lines changed

5 files changed

+80
-87
lines changed

‎scripts/check-licenses.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import * as fs from 'node:fs'
22
import * as path from 'node:path'
33
import * as readline from 'node:readline'
44
import { printLog, printError, runMain } from './lib/executionUtils.ts'
5-
import { findBrowserSdkPackageJsonFiles } from './lib/filesUtils.ts'
5+
import { findPackageJsonFiles } from './lib/filesUtils.ts'
66

77
const LICENSE_FILE = 'LICENSE-3rdparty.csv'
88

99
runMain(async () => {
10-
const packageJsonFiles = findBrowserSdkPackageJsonFiles()
10+
const packageJsonFiles = findPackageJsonFiles()
1111

1212
printLog(
1313
'Looking for dependencies in:\n',

‎scripts/check-packages.ts‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as fs from 'node:fs'
22
import * as path from 'node:path'
33
import { globSync } from 'node:fs'
44
import { minimatch } from 'minimatch'
5-
import { printLog, printError, runMain, printWarning } from './lib/executionUtils.ts'
5+
import { printLog, runMain, printWarning } from './lib/executionUtils.ts'
66
import { command } from './lib/command.ts'
7+
import { checkPackageJsonFiles } from './lib/checkBrowserSdkPackageJsonFiles.ts'
78

89
interface PackageFile {
910
path: string
@@ -14,20 +15,16 @@ interface NpmPackOutput {
1415
}
1516

1617
runMain(() => {
17-
let success = true
18+
checkPackageJsonFiles()
19+
1820
for (const packagePath of globSync('packages/*')) {
19-
success = checkPackage(packagePath) && success
21+
checkBrowserSdkPackage(packagePath)
2022
}
2123

22-
if (success) {
23-
printLog('Packages check done.')
24-
} else {
25-
printError('Packages check failed.')
26-
process.exitCode = 1
27-
}
24+
printLog('Packages check done.')
2825
})
2926

30-
function checkPackage(packagePath: string): boolean {
27+
function checkBrowserSdkPackage(packagePath: string) {
3128
const packageJson = getPackageJson(packagePath)
3229

3330
if (packageJson?.private) {
@@ -39,7 +36,8 @@ function checkPackage(packagePath: string): boolean {
3936

4037
const packageFiles = getPackageFiles(packagePath)
4138

42-
return checkPackageJsonEntryPoints(packageJson, packageFiles) && checkNpmIgnore(packagePath, packageFiles)
39+
checkPackageJsonEntryPoints(packageJson, packageFiles)
40+
checkNpmIgnore(packagePath, packageFiles)
4341
}
4442

4543
function getPackageFiles(packagePath: string): string[] {
@@ -53,23 +51,21 @@ function getPackageFiles(packagePath: string): string[] {
5351
return parsed[0].files.map((file) => file.path)
5452
}
5553

56-
function checkPackageJsonEntryPoints(packageJson: PackageJson, packageFiles: string[]): boolean {
54+
function checkPackageJsonEntryPoints(packageJson: PackageJson, packageFiles: string[]) {
5755
const filesFromPackageJsonEntryPoints = [packageJson.main, packageJson.module, packageJson.types].filter(Boolean)
5856

5957
for (const file of filesFromPackageJsonEntryPoints) {
6058
if (!packageFiles.includes(file)) {
61-
printError(`File ${file} used as an entry point in ${packageJson.name} is missing from the package`)
62-
return false
59+
throw new Error(`File ${file} used as an entry point in ${packageJson.name} is missing from the package`)
6360
}
6461
}
65-
return true
6662
}
6763

6864
// NPM will [always include some files][1] like `package.json` and `README.md`.
6965
// [1]: https://docs.npmjs.com/cli/v9/using-npm/developers#keeping-files-out-of-your-package
7066
const FILES_ALWAYS_INCLUDED_BY_NPM = ['package.json', 'README.md']
7167

72-
function checkNpmIgnore(packagePath: string, packageFiles: string[]): boolean {
68+
function checkNpmIgnore(packagePath: string, packageFiles: string[]) {
7369
const npmIgnorePath = path.join(packagePath, '.npmignore')
7470
const npmNegatedIgnoreRules = fs
7571
.readFileSync(npmIgnorePath, { encoding: 'utf8' })
@@ -81,20 +77,16 @@ function checkNpmIgnore(packagePath: string, packageFiles: string[]): boolean {
8177
// Ensure that each file is explicitly included by checking if at least a negated rule matches it
8278
for (const file of packageFiles) {
8379
if (!FILES_ALWAYS_INCLUDED_BY_NPM.includes(file) && !npmNegatedIgnoreRules.some((rule) => rule.match(`/${file}`))) {
84-
printError(`File ${file} is not explicitly included in ${npmIgnorePath}`)
85-
return false
80+
throw new Error(`File ${file} is not explicitly included in ${npmIgnorePath}`)
8681
}
8782
}
8883

8984
// Ensure that expected files are correctly included by checking if each negated rule matches at least one file
9085
for (const rule of npmNegatedIgnoreRules) {
9186
if (!packageFiles.some((file) => rule.match(`/${file}`))) {
92-
printError(`Rule ${rule.pattern} does not match any file ${npmIgnorePath}`)
93-
return false
87+
throw new Error(`Rule ${rule.pattern} does not match any file ${npmIgnorePath}`)
9488
}
9589
}
96-
97-
return true
9890
}
9991

10092
function getPackageJson(packagePath: string) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { PackageJsonFile } from '../release/check-release.ts'
2+
import { browserSdkVersion as releaseVersion } from './browserSdkVersion.ts'
3+
import { printLog } from './executionUtils.ts'
4+
import { findPackageJsonFiles } from './filesUtils.ts'
5+
6+
export function checkPackageJsonFiles(): void {
7+
const packageJsonFiles = findPackageJsonFiles()
8+
9+
printLog(
10+
`Checking package.json files:
11+
- ${packageJsonFiles.map((packageJsonFile) => packageJsonFile.relativePath).join('\n - ')}
12+
`
13+
)
14+
15+
for (const packageJsonFile of packageJsonFiles) {
16+
checkPackageJsonVersion(packageJsonFile)
17+
checkPackageDependencyVersions(packageJsonFile)
18+
}
19+
}
20+
function checkPackageJsonVersion(packageJsonFile: PackageJsonFile): void {
21+
if (packageJsonFile.content?.private) {
22+
// The developer extension is a private package, but it should still have a version
23+
if (
24+
packageJsonFile.content.version &&
25+
packageJsonFile.content.name !== '@datadog/browser-sdk-developer-extension'
26+
) {
27+
throw new Error(`Private package ${packageJsonFile.relativePath} should not have a version`)
28+
}
29+
} else if (packageJsonFile.content.version !== releaseVersion) {
30+
throw new Error(
31+
`Invalid version for ${packageJsonFile.relativePath}: expected ${releaseVersion}, got ${packageJsonFile.content.version}`
32+
)
33+
}
34+
}
35+
function checkPackageDependencyVersions(packageJsonFile: PackageJsonFile): void {
36+
if (packageJsonFile.content.private) {
37+
return
38+
}
39+
40+
for (const dependencies of [
41+
packageJsonFile.content.dependencies,
42+
packageJsonFile.content.devDependencies,
43+
packageJsonFile.content.peerDependencies,
44+
]) {
45+
if (!dependencies) {
46+
continue
47+
}
48+
49+
for (const [dependencyName, dependencyVersion] of Object.entries(dependencies)) {
50+
if (isBrowserSdkPackageName(dependencyName) && dependencyVersion !== releaseVersion) {
51+
throw new Error(
52+
`Invalid dependency version for ${dependencyName} in ${packageJsonFile.relativePath}: expected ${releaseVersion}, got ${dependencyVersion}`
53+
)
54+
}
55+
}
56+
}
57+
}
58+
function isBrowserSdkPackageName(name: string): boolean {
59+
return name?.startsWith('@datadog/')
60+
}

‎scripts/lib/filesUtils.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ interface PackageJsonInfo {
5656
content: any
5757
}
5858

59-
export function findBrowserSdkPackageJsonFiles(): PackageJsonInfo[] {
59+
export function findPackageJsonFiles(): PackageJsonInfo[] {
6060
const manifestPaths = command`git ls-files -- package.json */package.json`.run()
6161
return manifestPaths
6262
.trim()
Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { browserSdkVersion as releaseVersion } from '../lib/browserSdkVersion.ts'
22
import { printLog, runMain } from '../lib/executionUtils.ts'
33
import { command } from '../lib/command.ts'
4-
import { findBrowserSdkPackageJsonFiles } from '../lib/filesUtils.ts'
4+
import { checkPackageJsonFiles } from '../lib/checkBrowserSdkPackageJsonFiles.ts'
55

6-
interface PackageJsonFile {
6+
export interface PackageJsonFile {
77
relativePath: string
88
content: {
99
name: string
@@ -17,7 +17,7 @@ interface PackageJsonFile {
1717

1818
runMain(() => {
1919
checkGitTag()
20-
checkBrowserSdkPackageJsonFiles()
20+
checkPackageJsonFiles()
2121

2222
printLog('Release check done.')
2323
})
@@ -35,62 +35,3 @@ function checkGitTag(): void {
3535
throw new Error('Git tag not on HEAD')
3636
}
3737
}
38-
39-
function checkBrowserSdkPackageJsonFiles(): void {
40-
const packageJsonFiles = findBrowserSdkPackageJsonFiles()
41-
42-
printLog(
43-
`Checking package.json files:
44-
- ${packageJsonFiles.map((packageJsonFile) => packageJsonFile.relativePath).join('\n - ')}
45-
`
46-
)
47-
48-
for (const packageJsonFile of packageJsonFiles) {
49-
checkPackageJsonVersion(packageJsonFile)
50-
checkPackageDependencyVersions(packageJsonFile)
51-
}
52-
}
53-
54-
function checkPackageJsonVersion(packageJsonFile: PackageJsonFile): void {
55-
if (packageJsonFile.content?.private) {
56-
// The developer extension is a private package, but it should still have a version
57-
if (
58-
packageJsonFile.content.version &&
59-
packageJsonFile.content.name !== '@datadog/browser-sdk-developer-extension'
60-
) {
61-
throw new Error(`Private package ${packageJsonFile.relativePath} should not have a version`)
62-
}
63-
} else if (packageJsonFile.content.version !== releaseVersion) {
64-
throw new Error(
65-
`Invalid version for ${packageJsonFile.relativePath}: expected ${releaseVersion}, got ${packageJsonFile.content.version}`
66-
)
67-
}
68-
}
69-
70-
function checkPackageDependencyVersions(packageJsonFile: PackageJsonFile): void {
71-
if (packageJsonFile.content.private) {
72-
return
73-
}
74-
75-
for (const dependencies of [
76-
packageJsonFile.content.dependencies,
77-
packageJsonFile.content.devDependencies,
78-
packageJsonFile.content.peerDependencies,
79-
]) {
80-
if (!dependencies) {
81-
continue
82-
}
83-
84-
for (const [dependencyName, dependencyVersion] of Object.entries(dependencies)) {
85-
if (isBrowserSdkPackageName(dependencyName) && dependencyVersion !== releaseVersion) {
86-
throw new Error(
87-
`Invalid dependency version for ${dependencyName} in ${packageJsonFile.relativePath}: expected ${releaseVersion}, got ${dependencyVersion}`
88-
)
89-
}
90-
}
91-
}
92-
}
93-
94-
function isBrowserSdkPackageName(name: string): boolean {
95-
return name?.startsWith('@datadog/')
96-
}

0 commit comments

Comments
 (0)