Skip to content

Commit

Permalink
Fix broken CI – confusing path to package.json in the 'dist' director…
Browse files Browse the repository at this point in the history
…y! (#946)

* Reliably look up project and pwa-kit-dev package.json in scripts
  • Loading branch information
olibrook committed Jan 27, 2023
1 parent c033ed0 commit 6dc369c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
27 changes: 13 additions & 14 deletions packages/pwa-kit-dev/bin/pwa-kit-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const WebSocket = require('ws')
const program = require('commander')
const validator = require('validator')
const {execSync: _execSync} = require('child_process')
const pkg = require('../package.json')
const {getConfig} = require('pwa-kit-runtime/utils/ssr-config')

// Scripts in ./bin have never gone through babel, so we
Expand Down Expand Up @@ -51,6 +50,14 @@ const execSync = (cmd, opts) => {
return _execSync(cmd, {...defaults, ...opts})
}

const getProjectName = async () => {
const projectPkg = await scriptUtils.getProjectPkg()
if (!projectPkg.name) {
throw new Error(`Missing "name" field in "package.json"`)
}
return projectPkg.name
}

const main = async () => {
const pkgRoot = p.join(__dirname, '..')
process.env.CONTEXT = process.cwd()
Expand All @@ -71,7 +78,7 @@ const main = async () => {
``,
`Usage inside NPM scripts:`,
``,
` The PWA Kit Developer Tools is used in NPM scripts so you can conveniently`,
` The PWA Kit Developer Tools are used in NPM scripts so you can conveniently`,
` run eg. 'npm run push' to push a bundle from a project.`,
``,
` To pass args to pwa-kit-dev when wrapped in an NPM script, separate them`,
Expand Down Expand Up @@ -286,16 +293,7 @@ const main = async () => {
const mobify = getConfig() || {}

if (!projectSlug) {
try {
const projectPkg = p.join(process.cwd(), 'package.json')
const {name} = fse.readJsonSync(projectPkg)
if (!name) throw new Error(`Missing "name" field in ${projectPkg}`)
projectSlug = name
} catch (err) {
throw new Error(
`Could not detect project slug from "name" field in package.json: ${err.message}`
)
}
projectSlug = await getProjectName()
}

const bundle = await scriptUtils.createBundle({
Expand Down Expand Up @@ -364,7 +362,7 @@ const main = async () => {
.requiredOption('-e, --environment <environmentSlug>', 'the environment slug')
.action(async ({project, environment, cloudOrigin, credentialsFile}) => {
if (!project) {
project = scriptUtils.getPkgJSON()['name']
project = await getProjectName()
}

const credentials = await scriptUtils.readCredentials(credentialsFile)
Expand Down Expand Up @@ -423,8 +421,9 @@ const main = async () => {
})

// Global options
program.option('-v, --version', 'show version number').action(({version}) => {
program.option('-v, --version', 'show version number').action(async ({version}) => {
if (version) {
const pkg = await scriptUtils.getPkgJSON()
console.log(pkg.version)
} else {
program.help({error: true})
Expand Down
37 changes: 35 additions & 2 deletions packages/pwa-kit-dev/src/utils/script-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,41 @@ interface Bundle {
ssr_shared: string[]
}

export const getPkgJSON = async () => {
return readJson(path.join(__dirname, '..', '..', 'package.json'))
interface Pkg {
name: string
version: string
}

/**
* Get the package info for pwa-kit-dev.
*/
export const getPkgJSON = async (): Promise<Pkg> => {
const candidates = [
path.join(__dirname, '..', 'package.json'),
path.join(__dirname, '..', '..', 'package.json')
]
for (const candidate of candidates) {
try {
const data = await readJson(candidate)
return data as Pkg
} catch {
// Keep looking
}
}
return {name: 'pwa-kit-dev', version: 'unknown'}
}

/**
* Get the package info for the current project.
*/
export const getProjectPkg = async (): Promise<Pkg> => {
const p = path.join(process.cwd(), 'package.json')
try {
const data = await readJson(p)
return data as Pkg
} catch {
throw new Error(`Could not read project package at "${p}"`)
}
}

export class CloudAPIClient {
Expand Down

0 comments on commit 6dc369c

Please sign in to comment.