Skip to content

Commit 4a0cecc

Browse files
authoredAug 12, 2022
fix: preserve readme definitions when checking project config (#1041)
Also print useful error messages when parsing a file as JSON fails (e.g. print out which file)
1 parent b10c733 commit 4a0cecc

8 files changed

+38
-52
lines changed
 

‎src/check-project/check-licence-files.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/* eslint-disable no-console */
33

4-
import fs from 'fs'
4+
import fs from 'fs-extra'
55
import path from 'path'
66
import chalk from 'chalk'
77
import {
@@ -14,9 +14,7 @@ import {
1414
export async function checkLicenseFiles (projectDir) {
1515
console.info('Check license files')
1616

17-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
18-
encoding: 'utf-8'
19-
}))
17+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
2018

2119
if (pkg.license !== 'Apache-2.0 OR MIT') {
2220
throw new Error(`Incorrect license field - found '${pkg.license}', expected 'Apache-2.0 OR MIT'`)

‎src/check-project/check-monorepo-files.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22

3-
import fs from 'fs'
3+
import fs from 'fs-extra'
44
import path from 'path'
55
import {
66
ensureFileHasContents
@@ -15,9 +15,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
1515
export async function checkMonorepoFiles (projectDir) {
1616
console.info('Check monorepo files')
1717

18-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
19-
encoding: 'utf-8'
20-
}))
18+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
2119

2220
let defaultLernaContent = fs.readFileSync(path.join(__dirname, 'files/lerna.json'), {
2321
encoding: 'utf-8'

‎src/check-project/check-monorepo-readme.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/* eslint-disable no-console */
33

4-
import fs from 'fs'
4+
import fs from 'fs-extra'
55
import path from 'path'
66
import {
77
ensureFileHasContents
@@ -29,9 +29,7 @@ export async function checkMonorepoReadme (projectDir, repoUrl, defaultBranch, p
2929

3030
console.info('Check README files')
3131

32-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
33-
encoding: 'utf-8'
34-
}))
32+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
3533

3634
const readmePath = path.join(projectDir, 'README.md')
3735
let readmeContents = ''

‎src/check-project/check-readme.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
/* eslint-disable no-console */
33

4-
import fs from 'fs'
4+
import fs from 'fs-extra'
55
import path from 'path'
66
import {
77
ensureFileHasContents
@@ -28,9 +28,7 @@ export async function checkReadme (projectDir, repoUrl, defaultBranch) {
2828

2929
console.info('Check README files')
3030

31-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
32-
encoding: 'utf-8'
33-
}))
31+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
3432

3533
const readmePath = path.join(projectDir, 'README.md')
3634
let readmeContents = ''
@@ -61,6 +59,9 @@ export async function checkReadme (projectDir, repoUrl, defaultBranch) {
6159
let installIndex = -1
6260
let licenseFound = false
6361

62+
/** @type {import('mdast').Content[]} */
63+
const footer = []
64+
6465
file.children.forEach((child, index) => {
6566
const rendered = writeMarkdown(child).toLowerCase()
6667

@@ -101,6 +102,11 @@ export async function checkReadme (projectDir, repoUrl, defaultBranch) {
101102
return
102103
}
103104

105+
if (child.type === 'definition') {
106+
footer.push(child)
107+
return
108+
}
109+
104110
if ((child.type === 'heading' && rendered.includes('license')) || licenseFound) {
105111
licenseFound = true
106112
return
@@ -115,7 +121,8 @@ export async function checkReadme (projectDir, repoUrl, defaultBranch) {
115121
parsedReadme.children = [
116122
...installation.children,
117123
...parsedReadme.children,
118-
...license.children
124+
...license.children,
125+
...footer
119126
]
120127

121128
const toc = makeToc(parsedReadme, {

‎src/check-project/files/lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "4.0.0",
2+
"lerna": "5.4.0",
33
"useWorkspaces": true,
44
"version": "independent",
55
"command": {

‎src/check-project/index.js

+8-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console,complexity */
22

3-
import fs from 'fs'
3+
import fs from 'fs-extra'
44
import path from 'path'
55
import { execa } from 'execa'
66
import prompt from 'prompt'
@@ -106,10 +106,7 @@ async function processMonorepo (projectDir, manifest, branchName, repoUrl) {
106106
cwd: projectDir,
107107
absolute: true
108108
})) {
109-
const pkg = JSON.parse(fs.readFileSync(path.join(subProjectDir, 'package.json'), {
110-
encoding: 'utf-8'
111-
}))
112-
109+
const pkg = fs.readJSONSync(path.join(subProjectDir, 'package.json'))
113110
const homePage = `${repoUrl}/tree/master${subProjectDir.substring(projectDir.length)}`
114111

115112
console.info('Found monorepo project', pkg.name)
@@ -152,10 +149,7 @@ async function alignMonorepoProjectDependencies (projectDirs) {
152149

153150
// first loop over every project and choose the most recent version of a given dep
154151
for (const projectDir of projectDirs) {
155-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
156-
encoding: 'utf-8'
157-
}))
158-
152+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
159153
siblingVersions[pkg.name] = calculateSiblingVersion(pkg.version)
160154

161155
chooseVersions(pkg.dependencies || {}, deps)
@@ -166,9 +160,7 @@ async function alignMonorepoProjectDependencies (projectDirs) {
166160

167161
// now propose the most recent version of a dep for all projects
168162
for (const projectDir of projectDirs) {
169-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
170-
encoding: 'utf-8'
171-
}))
163+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
172164

173165
selectVersions(pkg.dependencies || {}, deps, siblingVersions)
174166
selectVersions(pkg.devDependencies || {}, devDeps, siblingVersions)
@@ -233,9 +225,7 @@ async function configureMonorepoProjectReferences (projectDirs) {
233225

234226
// first loop over every project and choose the most recent version of a given dep
235227
for (const projectDir of projectDirs) {
236-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
237-
encoding: 'utf-8'
238-
}))
228+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
239229

240230
references[pkg.name] = projectDir
241231
}
@@ -247,14 +237,8 @@ async function configureMonorepoProjectReferences (projectDirs) {
247237
continue
248238
}
249239

250-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
251-
encoding: 'utf-8'
252-
}))
253-
254-
const tsconfig = JSON.parse(fs.readFileSync(path.join(projectDir, 'tsconfig.json'), {
255-
encoding: 'utf-8'
256-
}))
257-
240+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
241+
const tsconfig = fs.readJSONSync(path.join(projectDir, 'tsconfig.json'))
258242
const refs = new Set()
259243

260244
addReferences(pkg.dependencies || {}, references, refs)
@@ -406,11 +390,7 @@ export default new Listr([
406390
task: async () => {
407391
const projectDir = process.argv[3] || process.cwd()
408392
const { branchName, repoUrl } = await getConfig(projectDir)
409-
410-
const manifest = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
411-
encoding: 'utf-8'
412-
}))
413-
393+
const manifest = fs.readJSONSync(path.join(projectDir, 'package.json'))
414394
const monorepo = manifest.workspaces != null
415395

416396
if (monorepo) {

‎src/check-project/readme/structure.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs'
1+
import fs from 'fs-extra'
22
import path from 'path'
33

44
/**
@@ -10,9 +10,7 @@ export const STRUCTURE = (monorepoDir, projectDirs) => {
1010
const packages = {}
1111

1212
for (const projectDir of projectDirs.sort()) {
13-
const pkg = JSON.parse(fs.readFileSync(path.join(projectDir, 'package.json'), {
14-
encoding: 'utf-8'
15-
}))
13+
const pkg = fs.readJSONSync(path.join(projectDir, 'package.json'))
1614

1715
const key = projectDir.replace(monorepoDir, '')
1816

‎src/check-project/utils.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ export async function ensureFileHasContents (projectDir, filePath, expectedConte
2828
let existingContents = ''
2929

3030
if (fileExists) {
31-
existingContents = fs.readFileSync(path.join(projectDir, filePath), {
31+
const existingFilePath = path.join(projectDir, filePath)
32+
existingContents = fs.readFileSync(existingFilePath, {
3233
encoding: 'utf-8'
3334
})
3435

3536
if (filePath.endsWith('.json')) {
36-
existingContents = JSON.stringify(JSON.parse(existingContents), null, 2) + '\n'
37+
try {
38+
existingContents = JSON.stringify(JSON.parse(existingContents), null, 2) + '\n'
39+
} catch (err) {
40+
console.error('Could not parse', existingFilePath, 'as JSON')
41+
42+
throw err
43+
}
3744
}
3845
} else {
3946
if (process.env.CI) {

0 commit comments

Comments
 (0)
Please sign in to comment.