Skip to content
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

Merged
merged 83 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
4e587ba
feat(gatsby-dev-cli): add verdaccio support [wip]
pieh Feb 3, 2019
3e2d0d5
yuck - don't look
pieh Feb 3, 2019
e5c179c
just to verify verdaccio
pieh Feb 3, 2019
641a91c
fix gitignore and add htpasswd with dummy gatsby-dev user
pieh Feb 3, 2019
6f555ab
some logging to understand why it tries to handle all the packages
pieh Feb 3, 2019
41d9583
just temporarily so it hopefully at least works
pieh Feb 3, 2019
c69e1f7
moar data
pieh Feb 3, 2019
571a8ae
don't copy all the packages right now
pieh Feb 3, 2019
408aeca
don't rely on package name starting with "gatsby" heuristic
pieh Feb 3, 2019
d1ec15b
tmp commit
pieh Feb 4, 2019
af89ec5
tmp
pieh Feb 4, 2019
4de0f3a
fs change
pieh Feb 4, 2019
d51ac61
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 20, 2019
be2e6c1
tmp
pieh Mar 20, 2019
eca2644
a
pieh Mar 20, 2019
229f02c
updates
pieh Mar 21, 2019
f9edc11
remove random files that shouldn't be here
pieh Mar 21, 2019
9018f6d
tmp
pieh Mar 21, 2019
071bc16
revert dep changes
pieh Mar 21, 2019
7fb7c74
ignore changes to package.json created during publishing
pieh Mar 22, 2019
e1287a7
testing of dep change in gatsby-cli is picked up by gatsby-dev-cli
pieh Mar 22, 2019
09a2441
let's see if gatsby-dev will handle case of no node_modules
pieh Mar 22, 2019
9f51113
sanity check
pieh Mar 22, 2019
29f63a6
adjust console message
pieh Mar 22, 2019
87d426c
add missing devDeps
pieh Mar 22, 2019
4071a24
more missing devDeps
pieh Mar 22, 2019
6ab2a24
grab package.json from unpkg if package is not yet installed locally
pieh Mar 22, 2019
e70a1e0
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 22, 2019
c044394
correctly ignore package.json changes during publishing
pieh Mar 22, 2019
a679496
cleaning things up
pieh Mar 22, 2019
35748c6
htpasswd loc
pieh Mar 22, 2019
1b27f5a
restore gatsby package.json
pieh Mar 22, 2019
d354d5b
adjust ignore files
pieh Mar 22, 2019
634c406
gatsby-dev-cli: ignore tests
pieh Mar 22, 2019
0706879
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 22, 2019
45838c2
remove extra ln
pieh Mar 22, 2019
685213c
less console output
pieh Mar 22, 2019
bc4bdc1
turns out you don't need htpasswd after all
pieh Mar 22, 2019
2d948d3
use exact when installing local packages - timestamp is used in version
pieh Mar 22, 2019
734e51e
why it failed?
pieh Mar 22, 2019
e534677
use yarn for publishing - see https://github.com/verdaccio/verdaccio/…
pieh Mar 22, 2019
7b56ebf
v0.1.1
pieh Mar 22, 2019
bf4d5eb
add temporary .npmrc file to allow anonymous publishes
pieh Mar 22, 2019
42c9b78
add some jsdocs
pieh Mar 22, 2019
5ea67a9
update yarn.lock
pieh Mar 22, 2019
dcdaf02
oops
pieh Mar 22, 2019
fc1045a
debug why still can't publish anon
pieh Mar 23, 2019
302ea47
adjust npmrc content
pieh Mar 23, 2019
0b7a7b0
doh
pieh Mar 23, 2019
42acc31
Revert "debug why still can't publish anon"
pieh Mar 23, 2019
5364ccf
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 25, 2019
61679d1
handle package installation of deps didn't change
pieh Mar 25, 2019
1b3193b
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 26, 2019
04c59c5
skip version changes for packages in gatsby mono-repo, queue copying …
pieh Mar 26, 2019
ba221b7
fix tests
pieh Mar 26, 2019
85a27ee
packages stored in verdaccio storage seems to be picked up by jest an…
pieh Mar 27, 2019
a145185
bye "wat", you will be missed
pieh Mar 27, 2019
99ae4b8
bail early to avoid conditional bracket hell
pieh Mar 27, 2019
a9b5aa9
more bailing early
pieh Mar 27, 2019
3270d94
more concise
pieh Mar 27, 2019
33fff32
new line
pieh Mar 27, 2019
af0a0ae
use exaca to simplify running yarn
pieh Mar 27, 2019
c04bd7e
clean up www/.gitignore
pieh Mar 27, 2019
ab28b27
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Mar 27, 2019
5e0373b
handle not existing packages
pieh Mar 28, 2019
48890c0
remove unused (was needed for basic auth)
pieh Mar 28, 2019
cc8faca
cleanup temp file changes when process exit in middle of publishing
pieh Mar 28, 2019
e33a383
Apply suggestions from code review
wardpeet Mar 28, 2019
09c1ae7
ops
pieh Mar 28, 2019
0d148dc
cleanup unpkg response handler
pieh Mar 28, 2019
7a37d5d
move stuff around
pieh Mar 28, 2019
0d0895f
remove tmp comment
pieh Mar 28, 2019
bbce98b
document traversePackageDocs fn
pieh Mar 28, 2019
7e66caf
restrcuture files
pieh Mar 28, 2019
a962399
add basic tests for traversingPackageDeps
pieh Mar 28, 2019
5002629
add basic tests for generating packages to publish (based on dep graph)
pieh Mar 28, 2019
c8578cd
fix tests on windows
pieh Mar 29, 2019
64c7870
Merge remote-tracking branch 'upstream/master' into dev-verdaccio
pieh Mar 29, 2019
cde13fb
handle circular dependencies when constructing list of packages to pu…
pieh Mar 29, 2019
3960fa9
Merge remote-tracking branch 'origin/master' into dev-verdaccio
pieh Apr 2, 2019
65e040e
remove cat ;(
pieh Apr 2, 2019
14ea6c8
restore original e2e-test script
pieh Apr 2, 2019
d11552a
update gitignore
pieh Apr 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/gatsby-dev-cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ node_modules

decls
dist

# verdaccio local storage
verdaccio/storage
# autogenerated by verdaccio (?)
/.htpasswd

5 changes: 3 additions & 2 deletions packages/gatsby-dev-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"fs-extra": "^4.0.1",
"is-absolute": "^0.2.6",
"lodash": "^4.17.10",
"npm-cli-login": "^0.1.1",
"verdaccio": "^3.11.1",
"yargs": "^8.0.2"
},
"devDependencies": {
Expand All @@ -36,6 +38,5 @@
"prepare": "cross-env NODE_ENV=production npm run build",
"test": "echo \"Error: no test specified\" && exit 1",
"watch": "babel -w src --out-dir dist"
},
"gitHead": "5bd5aebe066b9875354a81a4b9ed98722731c465"
}
}
20 changes: 20 additions & 0 deletions packages/gatsby-dev-cli/src/utils.js
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()
}
})
})
137 changes: 137 additions & 0 deletions packages/gatsby-dev-cli/src/verdaccio.js
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
Copy link
Contributor

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.

Copy link
Contributor Author

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

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to log the error here? Is there an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an error because I made promisifiedSpawn to reject with exit code if it's non-zero and unpublish command will exit with non-zero if package doesn't exist. But as mentioned above - I will actually need to scratch this code because it doesn't handle deps of deps (i.e. if I change dep in gatsby-cli and re-install gatsby package - it won't reinstall gatsby-cli - because version didn't change) - I will use lerna to publish canary release to local registry and later on install that canary - yet to fully verify, but it "should work" (TM)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you publish canary 2 times in a row (with same code) it will result in same "hash" or not - TBD

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`)
}
76 changes: 75 additions & 1 deletion packages/gatsby-dev-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const _ = require(`lodash`)
const fs = require(`fs-extra`)
const path = require(`path`)

const { publishPackageLocally, registryUrl } = require(`./verdaccio`)
const { promisifiedSpawn } = require(`./utils`)

let numCopied = 0

const quit = () => {
Expand All @@ -26,6 +29,21 @@ const copyPath = (oldPath, newPath, quiet) =>
})
})

const installFromVerdaccio = async args => {
await publishPackageLocally(args)

const installCmd = [
`yarn`,
[`add`, args.packageName, `--registry=${registryUrl}`],
]

console.log(`Installing '${args.packageName}' locally`)

await promisifiedSpawn(installCmd)

console.log(`Installed '${args.packageName}' locally`)
}

/*
* non-existant packages break on('ready')
* See: https://github.com/paulmillr/chokidar/issues/449
Expand Down Expand Up @@ -61,11 +79,67 @@ function watch(root, packages, { scanOnce, quiet }) {
return
}

const relativePackageFile = path.relative(prefix, filePath)

const newPath = path.join(
`./node_modules/${packageName}`,
path.relative(prefix, filePath)
relativePackageFile
)

if (relativePackageFile === `package.json`) {
// compare dependencies with local version

try {
// Delete require cache so we can reload the module.
delete require.cache[require.resolve(filePath)]
// eslint-disable-next-line
} catch {}
try {
// Delete require cache so we can reload the module.
delete require.cache[
require.resolve(path.join(process.cwd(), newPath))
]
// eslint-disable-next-line
} catch {}

const monorepoPKGjson = require(filePath)

let localPKGjson
try {
localPKGjson = require(path.join(process.cwd(), newPath))
} catch {
localPKGjson = {
dependencies: {},
}
}

const areDepsEqual = _.isEqual(
monorepoPKGjson.dependencies,
localPKGjson.dependencies
)

console.log({
packageName,
filePath,
localPackage: path.join(process.cwd(), newPath),
areDepsEqual,
mDeps: monorepoPKGjson.dependencies,
lDeps: localPKGjson.dependencies,
})

if (!areDepsEqual) {
allCopies.push(
installFromVerdaccio({
pathToPackage: prefix,
packageName,
version: monorepoPKGjson.version,
})
)
}

return
}

let localCopies = [copyPath(filePath, newPath, quiet)]

// If this is from "cache-dir" also copy it into the site's .cache
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-dev-cli/verdaccio/.htpasswd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gatsby-dev:$apr1$vh8SxK9U$4yf.LQ1.u5TftWGfAXQ7q.
5 changes: 3 additions & 2 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"graphql-playground-middleware-express": "^1.7.10",
"graphql-relay": "^0.6.0",
"graphql-skip-limit": "^2.0.5",
"graphql-tools": "^3.0.4",
"graphql-tools": "^4.0.3",
"graphql-type-json": "^0.2.1",
"hash-mod": "^0.0.5",
"invariant": "^2.2.4",
Expand Down Expand Up @@ -165,7 +165,8 @@
"url": "git+https://github.com/gatsbyjs/gatsby.git"
},
"resolutions": {
"graphql": "^14.1.1"
"graphql": "^14.1.1",
"terser": "3.14.1"
},
"scripts": {
"build": "npm run build:src && npm run build:internal-plugins && npm run build:rawfiles && npm run build:cjs",
Expand Down
8 changes: 5 additions & 3 deletions scripts/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ SRC_PATH=$1
CUSTOM_COMMAND="${2:-yarn test}"
GATSBY_PATH="${CIRCLE_WORKING_DIRECTORY:-../../}"

npm install -g gatsby-dev-cli &&
# npm install -g gatsby-dev-cli &&

# setting up child integration test link to gatsby packages
cd $SRC_PATH &&
yarn &&
gatsby-dev --set-path-to-repo $GATSBY_PATH &&
gatsby-dev --scan-once --copy-all --quiet && # copies _all_ files in gatsby/packages
#gatsby-dev --set-path-to-repo $GATSBY_PATH &&
#gatsby-dev --scan-once --copy-all --quiet && # copies _all_ files in gatsby/packages
node ../../packages/gatsby-dev-cli/dist/index.js --set-path-to-repo $GATSBY_PATH &&
node ../../packages/gatsby-dev-cli/dist/index.js --scan-once --quiet && # copies _all_ files in gatsby/packages
chmod +x ./node_modules/.bin/gatsby && # this is sometimes necessary to ensure executable
sh -c "$CUSTOM_COMMAND" &&
echo "e2e test run succeeded"
Loading