-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby-dev-cli): add verdaccio support #11525
Changes from 8 commits
4e587ba
3e2d0d5
e5c179c
641a91c
6f555ab
41d9583
c69e1f7
571a8ae
408aeca
d1ec15b
af89ec5
4de0f3a
d51ac61
be2e6c1
eca2644
229f02c
f9edc11
9018f6d
071bc16
7fb7c74
e1287a7
09a2441
9f51113
29f63a6
87d426c
4071a24
6ab2a24
e70a1e0
c044394
a679496
35748c6
1b27f5a
d354d5b
634c406
0706879
45838c2
685213c
bc4bdc1
2d948d3
734e51e
e534677
7b56ebf
bf4d5eb
42c9b78
5ea67a9
dcdaf02
fc1045a
302ea47
0b7a7b0
42acc31
5364ccf
61679d1
1b3193b
04c59c5
ba221b7
85a27ee
a145185
99ae4b8
a9b5aa9
3270d94
33fff32
af0a0ae
c04bd7e
ab28b27
5e0373b
48890c0
cc8faca
e33a383
09c1ae7
0d148dc
7a37d5d
0d0895f
bbce98b
7e66caf
a962399
5002629
c8578cd
64c7870
cde13fb
3960fa9
65e040e
14ea6c8
d11552a
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 |
---|---|---|
|
@@ -28,3 +28,9 @@ node_modules | |
|
||
decls | ||
dist | ||
|
||
# verdaccio local storage | ||
verdaccio/storage | ||
# autogenerated by verdaccio (?) | ||
/.htpasswd | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const child_process = require(`child_process`) | ||
|
||
exports.promisifiedSpawn = ([cmd, args = [], spawnArgs = {}]) => | ||
new Promise((resolve, reject) => { | ||
const proc = child_process.spawn(cmd, args, spawnArgs) | ||
|
||
proc.stdout.setEncoding(`utf8`) | ||
proc.stdout.on(`data`, console.log) | ||
|
||
proc.stderr.setEncoding(`utf8`) | ||
proc.stderr.on(`data`, console.error) | ||
|
||
proc.on(`close`, code => { | ||
if (code) { | ||
reject(code) | ||
} else { | ||
resolve() | ||
} | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
const startVerdaccio = require(`verdaccio`).default | ||
const path = require(`path`) | ||
const npmLogin = require(`npm-cli-login`) | ||
const fs = require(`fs-extra`) | ||
const { promisifiedSpawn } = require(`./utils`) | ||
|
||
let VerdaccioInitPromise = null | ||
|
||
const verdaccioConfig = { | ||
storage: path.join(__dirname, `..`, `verdaccio`, `storage`), | ||
port: 4873, // default | ||
web: { | ||
enable: true, | ||
title: `gatsby-dev`, | ||
}, | ||
auth: { | ||
htpasswd: { | ||
file: `./.htpasswd`, | ||
max_users: 1000, | ||
}, | ||
}, | ||
packages: { | ||
"**": { | ||
access: `$all`, | ||
publish: `gatsby-dev`, // we create dummy gatsby-dev username | ||
proxy: `npmjs`, | ||
}, | ||
}, | ||
uplinks: { | ||
npmjs: { | ||
url: `https://registry.npmjs.org/`, | ||
}, | ||
}, | ||
self_path: path.join(__dirname, `..`, `verdaccio`), | ||
} | ||
|
||
const registryUrl = `http://localhost:${verdaccioConfig.port}` | ||
|
||
exports.registryUrl = registryUrl | ||
|
||
const startServer = () => { | ||
if (VerdaccioInitPromise) { | ||
return VerdaccioInitPromise | ||
} | ||
|
||
console.log(`Starting local verdaccio server`) | ||
|
||
// clear storage | ||
fs.removeSync(verdaccioConfig.storage) | ||
|
||
VerdaccioInitPromise = new Promise(resolve => { | ||
startVerdaccio( | ||
verdaccioConfig, | ||
verdaccioConfig.port, | ||
verdaccioConfig.storage, | ||
`1.0.0`, | ||
`gatsby-dev`, | ||
(webServer, addr, pkgName, pkgVersion) => { | ||
webServer.listen(addr.port || addr.path, addr.host, () => { | ||
console.log(`Started local verdaccio server`) | ||
|
||
// Login with dummy gatsby-dev user - see ./verdaccio/.htpasswd . | ||
// This is needed to be able to actually publish packages | ||
// to local registry. | ||
console.log(`Logging in with dummy user`) | ||
|
||
npmLogin( | ||
`gatsby-dev`, | ||
`gatsby-dev`, | ||
`gatsby-dev@gatsbyjs.org`, | ||
registryUrl | ||
) | ||
|
||
// This is silly - `npm-cli-login` doesn't return promise or callback | ||
// so let's just wait reasaonable amount before resolving | ||
// There is open pull request to add returning promise: | ||
// https://github.com/postmanlabs/npm-cli-login/pull/19 | ||
setTimeout(() => { | ||
console.log(`Hopefully logged in already`) | ||
resolve() | ||
}, 1500) | ||
}) | ||
} | ||
) | ||
}) | ||
|
||
return VerdaccioInitPromise | ||
} | ||
|
||
exports.publishPackageLocally = async ({ | ||
pathToPackage, | ||
packageName, | ||
version, | ||
}) => { | ||
// <ake sure verdaccio is running | ||
await startServer() | ||
|
||
// npm unpublish | ||
// because version doesn't change when developing locally verdaccio wouldn't | ||
// let republish package with same version - so as quick workaround we | ||
// unpublish package. | ||
// Ideally we could check if we need to unpublish: i.e. compare deps of | ||
// published package to deps of package in monorepo. | ||
// Also for CI we could make special case to skip this, because then | ||
// there is no way to edit source files and we can use fast path. | ||
const unpublishCmd = [ | ||
`npm`, | ||
[`unpublish`, `${packageName}@${version}`, `--registry=${registryUrl}`], | ||
] | ||
|
||
console.log( | ||
`Trying to unpublish ${packageName}@${version} to local registry, in case it was published before` | ||
) | ||
try { | ||
await promisifiedSpawn(unpublishCmd) | ||
console.log(`Unpublished ${packageName}@${version} from local registry`) | ||
} catch { | ||
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. Do we want to log the error here? Is there an error? 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. There is an error because I made 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. I'm not sure if you publish |
||
console.log( | ||
`Didn't unpublish ${packageName}@${version} - probably package wasn't published before` | ||
) | ||
} | ||
|
||
// npm publish | ||
const publishCmd = [ | ||
`npm`, | ||
[`publish`, `--registry=${registryUrl}`], | ||
{ | ||
cwd: pathToPackage, | ||
}, | ||
] | ||
|
||
console.log(`Publishing ${packageName}@${version} to local registry`) | ||
|
||
await promisifiedSpawn(publishCmd) | ||
|
||
console.log(`Published ${packageName}@${version} to local registry`) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gatsby-dev:$apr1$vh8SxK9U$4yf.LQ1.u5TftWGfAXQ7q. |
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.
If you force publish
npm publish --force
the version is replaced. Perhaps that helps.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.
ohhh, cool, thanks for the tip! But there is another issue that will render this a bit irrelevant - let me write up follow up comment to document what I mean