-
Notifications
You must be signed in to change notification settings - Fork 402
feat: publish netlify
package
#7293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6d757f1
a56363a
70dacdb
0bbc22d
9e5e4a1
fa878fc
01b4055
5822c84
92e0755
7b3d3d9
8c1e8bb
bc1f081
0490402
c9d4f80
7a9405d
384e146
fda2b6c
0623be6
06be0e1
548eeb9
5cdda7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// @ts-check | ||
import assert from 'node:assert' | ||
import { dirname, resolve } from 'node:path' | ||
import { readFile, stat, writeFile } from 'node:fs/promises' | ||
|
||
import execa from 'execa' | ||
|
||
/** | ||
* @import {Package} from "normalize-package-data" | ||
*/ | ||
|
||
const packageJSON = await getPackageJSON() | ||
|
||
async function getPackageJSON() { | ||
const packageJSONPath = resolve('package.json') | ||
|
||
/** | ||
* @type {Package} | ||
*/ | ||
const contents = JSON.parse(await readFile(packageJSONPath, 'utf8')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We already have a dep on There's even a util There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This use case is a bit different. We're reading I have used the |
||
|
||
return { | ||
contents, | ||
path: packageJSONPath, | ||
} | ||
} | ||
|
||
async function preparePackageJSON() { | ||
const binPath = Object.values(packageJSON.contents.bin ?? {})[0] | ||
if (!binPath) { | ||
throw new Error('Did not find a non-empty binary entry in `package.json`, so the `npx` flow will not work.') | ||
} | ||
|
||
const newPackageJSON = { | ||
...packageJSON.contents, | ||
main: './dist/index.js', | ||
name: 'netlify', | ||
scripts: { | ||
...packageJSON.contents.scripts, | ||
|
||
// We don't need the pre-publish script because we expect the work in | ||
// there to be done when publishing the `netlify-cli` package. We'll | ||
// ensure this is the case by throwing if a shrinkwrap file isn't found. | ||
prepublishOnly: undefined, | ||
}, | ||
bin: { | ||
npxnetlify: binPath, | ||
}, | ||
} | ||
|
||
try { | ||
const shrinkwrap = await stat(resolve(packageJSON.path, '../npm-shrinkwrap.json')) | ||
|
||
assert.ok(shrinkwrap.isFile()) | ||
} catch { | ||
throw new Error('Failed to find npm-shrinkwrap.json file. Did you run the pre-publish script?') | ||
} | ||
|
||
console.log(`Writing updated package.json to ${packageJSON.path}...`) | ||
await writeFile(packageJSON.path, `${JSON.stringify(newPackageJSON, null, 2)}\n`) | ||
|
||
console.log('Regenerating shrinkwrap file with updated package name...') | ||
await execa('npm', ['shrinkwrap'], { | ||
cwd: dirname(packageJSON.path), | ||
}) | ||
} | ||
|
||
await preparePackageJSON() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This is an entrypoint that mirrors the interface that the `netlify` package | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: Should we log a warning here that the package has been renamed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That feels a bit extreme? We might be logging something that is not actionable to the user, since this might be a transitive dependency. |
||
// used to have, before it was renamed to `@netlify/api`. We keep it for | ||
// backwards-compatibility. | ||
export { NetlifyAPI, methods } from '@netlify/api' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I was going to comment that this codebase does not follow this naming convention, but I see that
scripts/prepublishOnly
is the one exception, which you probably saw right next to this one!🤷🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did name
scripts/prepublishOnly
as it is (despite not matching other file naming conventions) specifically so it would match up exactly with the npm script name.