-
Notifications
You must be signed in to change notification settings - Fork 955
/
Copy pathpackage.validate.mjs
215 lines (182 loc) · 7.28 KB
/
package.validate.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env zx
// @ts-check
import "zx/globals"
import { getWorkspaceList } from "./tools/workspace.mjs"
$.verbose = false
console.log(
["Validating each workspace has the correct package.json fields for publishing."].join("\n")
)
// #region Find lib workspaces
const ROOT_DIR = path.join(__dirname, "..")
const workspaceList = await getWorkspaceList()
const workspacePaths = workspaceList
// create absolute paths
.map((workspace) => path.join(ROOT_DIR, workspace.location))
// filter out workspaces without /lib/ in the path
.filter((workspacePath) => workspacePath.includes("/lib/"))
console.log(`Found ${workspacePaths.length} library workspaces`)
// #endregion
for (const workspacePath of workspacePaths) {
console.log(`Validating "${workspacePath}"`)
// #region Parse package.json
const packageJsonPath = path.join(workspacePath, "package.json")
const packageJsonFile = fs.readFileSync(packageJsonPath, "utf-8")
if (!packageJsonFile || typeof packageJsonFile !== "string") {
console.error(`Failed to read ${packageJsonPath}`)
process.exit(1)
}
const packageJson = JSON.parse(packageJsonFile)
if (!packageJson || typeof packageJson !== "object" || Array.isArray(packageJson)) {
console.error(`Failed to parse ${packageJsonPath}`)
process.exit(1)
}
// #endregion
// #region Validate package.json
// assert "author" field is Infinite Red
if (packageJson.author !== "Infinite Red") {
console.error(`Invalid ${packageJsonPath} author field: ${packageJson.author}`)
process.exit(1)
}
// assert "license" field is MIT
if (packageJson.license !== "MIT") {
console.error(`Invalid ${packageJsonPath} license field: ${packageJson.license}`)
process.exit(1)
}
// assert "bugs.url" field is "https://github.com/infinitered/reactotron/issues"
if (packageJson.bugs.url !== "https://github.com/infinitered/reactotron/issues") {
console.error(`Invalid ${packageJsonPath} bugs.url field: ${packageJson.bugs.url}`)
process.exit(1)
}
// assert "homepage" field is `https://github.com/infinitered/reactotron/tree/master/lib/${workspaceName}`
const workspaceName = path.basename(workspacePath)
const expectedHomepage = `https://github.com/infinitered/reactotron/tree/master/lib/${workspaceName}`
if (packageJson.homepage !== expectedHomepage) {
console.error(`Invalid ${packageJsonPath} homepage field: ${packageJson.homepage}`)
process.exit(1)
}
// assert "repository" field is `https://github.com/infinitered/reactotron/tree/master/lib/${workspaceName}`
if (
packageJson.repository !==
`https://github.com/infinitered/reactotron/tree/master/lib/${workspaceName}`
) {
console.error(`Invalid ${packageJsonPath} repository field: ${packageJson.repository}`)
process.exit(1)
}
// assert "files" field should be ["dist", "src"]
// LICENSE, README, and package.json are implicitly included https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files
if (
!Array.isArray(packageJson.files) ||
packageJson.files.length !== 2 ||
packageJson.files[0] !== "dist" ||
packageJson.files[1] !== "src"
) {
console.error(`Invalid ${packageJsonPath} files field: ${packageJson.files}`)
process.exit(1)
}
// assert "main" field is "dist/index.js"
if (packageJson.main !== "dist/index.js") {
console.error(`Invalid ${packageJsonPath} main field: ${packageJson.main}`)
process.exit(1)
}
// assert "main" field points to a real file
const mainPath = path.join(workspacePath, packageJson.main)
if (!fs.existsSync(mainPath)) {
console.error(`Missing ${mainPath}`)
process.exit(1)
}
// assert "module" field is "dist/index.esm.js"
if (packageJson.module !== "dist/index.esm.js") {
console.error(`Invalid ${packageJsonPath} module field: ${packageJson.module}`)
process.exit(1)
}
// assert "module" field points to a real file
const modulePath = path.join(workspacePath, packageJson.module)
if (!fs.existsSync(modulePath)) {
console.error(`Missing ${modulePath}`)
process.exit(1)
}
// assert "types" field is "dist/types/src/index.d.ts"
if (packageJson.types !== "dist/types/src/index.d.ts") {
console.error(`Invalid ${packageJsonPath} types field: ${packageJson.types}`)
process.exit(1)
}
// assert "types" field points to a real file
const typesPath = path.join(workspacePath, packageJson.types)
if (!fs.existsSync(typesPath)) {
console.error(`Missing ${typesPath}`)
process.exit(1)
}
// assert "react-native" field is "src/index.ts"
if (packageJson["react-native"] !== "src/index.ts") {
console.error(`Invalid ${packageJsonPath} react-native field: ${packageJson["react-native"]}`)
process.exit(1)
}
// assert "react-native" field points to a real file
const reactNativePath = path.join(workspacePath, packageJson["react-native"])
if (!fs.existsSync(reactNativePath)) {
console.error(`Missing ${reactNativePath}`)
process.exit(1)
}
// assert "exports" field is an object with "default", "import", and "types" fields
if (
!packageJson.exports ||
typeof packageJson.exports !== "object" ||
Array.isArray(packageJson.exports) ||
!packageJson.exports.default ||
!packageJson.exports.import ||
!packageJson.exports.types
) {
console.error(
`Invalid ${packageJsonPath} exports field: ${JSON.stringify(packageJson.exports, null, 2)}`
)
process.exit(1)
}
// assert "exports.react-native" field does not exist
if (packageJson.exports["react-native"]) {
console.error(
[
`Invalid ${packageJsonPath} exports.react-native field should not exist`,
`Remove this check once typescript types are more stable`,
`See https://github.com/infinitered/reactotron/issues/1430`,
].join("\n")
)
process.exit(1)
}
// assert "exports.default" field is "./dist/index.js"
if (packageJson.exports.default !== "./dist/index.js") {
console.error(
`Invalid ${packageJsonPath} exports.default field: ${packageJson.exports.default}`
)
process.exit(1)
}
// assert "exports.default" field points to a real file
const exportsDefaultPath = path.join(workspacePath, packageJson.exports.default)
if (!fs.existsSync(exportsDefaultPath)) {
console.error(`Missing ${exportsDefaultPath}`)
process.exit(1)
}
// assert "exports.import" field is "./dist/index.esm.js"
if (packageJson.exports.import !== "./dist/index.esm.js") {
console.error(`Invalid ${packageJsonPath} exports.import field: ${packageJson.exports.import}`)
process.exit(1)
}
// assert "exports.import" field points to a real file
const exportsImportPath = path.join(workspacePath, packageJson.exports.import)
if (!fs.existsSync(exportsImportPath)) {
console.error(`Missing ${exportsImportPath}`)
process.exit(1)
}
// assert "exports.types" field is "./dist/types/src/index.d.ts"
if (packageJson.exports.types !== "./dist/types/src/index.d.ts") {
console.error(`Invalid ${packageJsonPath} exports.types field: ${packageJson.exports.types}`)
process.exit(1)
}
// assert "exports.types" field is a real file
const exportsTypesPath = path.join(workspacePath, packageJson.exports.types)
if (!fs.existsSync(exportsTypesPath)) {
console.error(`Missing ${exportsTypesPath}`)
process.exit(1)
}
// #endregion
}
console.log("All workspaces are valid!")