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

Introducing y-npm (aka: Why NPM?) #162

Closed
wants to merge 0 commits into from
Closed

Conversation

RomainMuller
Copy link
Contributor

@RomainMuller RomainMuller commented Jun 21, 2018

A drop-in replacement for the npm command that uses a local repository as a source for packages, and falls back to the standard npm registry for packages that are not locally available.

This can be used to make private packages available to consumers without the need to set up a full blown repository and managing user authentication and authorization; and also to overlay customized versions of third party packages in a simple way.

The y-npm command has an additional sub-command y-npm y-ls that will output the name and version of all packages that are locally available.

This commit also replaces cdk-beta-npm in favor of using y-npm. Also, the release .zip file no longer includes a full installation of the cdk, instead only makes y-npm, so people can use y-npm instead of npm and work as they normally would besides that (this reduces the file size of the release zip by about 50%).

@RomainMuller RomainMuller changed the title Indtroducing y-npm (aka: Why NPM?) Introducing y-npm (aka: Why NPM?) Jun 21, 2018
Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

Didn't go through everything yet, see my note about bundling...

bundle-beta.sh Outdated
${Y_NPM} publish ${tarball}
done

echo "Installing y-npm" # using my-npm!
Copy link
Contributor

Choose a reason for hiding this comment

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

"my-npm" => "y-npm"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦🏻‍♂️

bundle-beta.sh Outdated
# Symlink 'bin' to the root
echo "Installing cdk-beta-npm"
mkdir -p bin
cat > bin/cdk-beta-npm <<-'HERE'
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's get rid of cdk-beta-npm altogether!

  1. I think the name is wrong in 4 different ways (too long, confusing, ugly, non-ergonomic).
  2. We are going to have y-mvn and y-nuget and eventually we'll publish y-repo as a separate project and can point people to it's docs in case they have any questions or issues. Adding another abstraction layer on top of it will make it hard for people to understand what's going on.

How do we get rid of it? Include most of the bundling logic into y-npm. Think about other users of y-npm - they will effectively have to re-write the code in bundle-beta.sh.

Here's a proposal for an API:

$ y-npm y-stage <tarballs>
bin/y-npm
node_modules/y-npm
node_modules/...
y/npm/<verdaccio-tree>

This will allow you to define the default for behavior in case Y_NPM_REPO is not defined to auto-locate it's repo y/npm/ based on where the y-npm module is installed path.join(__dirname, '../../y.npm').

This will also apply exactly in the same way to y-mvn.

Eventually, all one will need to do is zip the staging directory and be happy!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It took me reading your suggestion thrice, but now that I understand it, I like it 👍
I'll use y/npm repository resolution akin to what node does:

  1. ${Y_NPM_REPOSITORY}
  2. ${cwd}/y/npm
  3. Walk up the tree from ${cwd} until:
    1. A y/npm directory is found
    2. The root of the filesystem is reached
  4. Walk up the tree from path.join(require.resolve('y-npm/package.json'), '..', '..') until:
    1. A y/npm directory is found
    2. The root of the filesystem is reached
  5. Bail out in error.

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now done 👌🏻

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

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

You should write a couple of tests to get at least minimal coverage of the behavior. I would write a few bash scripts that exercise the tool in a few paths.

I am still not in ❤️ with our eventual setup story but we can punt that for a subsequent iteration.

CHANGELOG.md Outdated

* Stopped bundling all dependencies in the release ZIP ([@RomainMuller] in [#161] and [awslabs/jsii#43])
* Replaced `cdk-beta-npm` with more generic `y-npm` tool ([@RomainMuller] in [#162])
* Improved speed of `pack.sh`
Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed in changelog

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of them? Or some of them?

bundle-beta.sh Outdated
# aws-cdk-${version}.zip
# ├─ bin
# ├─ docs
# ├─ repo
Copy link
Contributor

Choose a reason for hiding this comment

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

I will change this to y/mvn shortly

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 already changed y/npm :)

if (await execCdkBetaNpm('install') !== 0) {
throw new Error('cdk-beta-npm install failed!');
const cdkHome = process.env.CDK_HOME ? path.resolve(process.env.CDK_HOME) : path.join(os.homedir(), '.cdk');
const cdkBetaNpm = path.join(cdkHome, 'bin', 'y-npm');
Copy link
Contributor

Choose a reason for hiding this comment

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

rename variable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah! Yes.

```
* For a package from your `npm` registry of choice:
```shell
TGZ=npm pack package[@version] # Download the package locally
Copy link
Contributor

Choose a reason for hiding this comment

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

missing $()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦🏻‍♂️

```shell
TGZ=npm pack package[@version] # Download the package locally
y-npm publish ${TGZ} # Publish the downloaded package
rm ${TGZ} # Clean-up after yourself
Copy link
Contributor

Choose a reason for hiding this comment

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

😄

const storage = await determineStorageDirectory();
if (argv.length === 1) {
switch (argv[0]) {
case 'my-packages':
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't that be y-packagesor better: y-ls

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like y-ls.

import { debug } from './logging';

/** The parent directory of this module's location */
const installRoot = path.resolve(path.join(__dirname, '..', '..'));
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just __dirname?

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 could start by testing two directories that I know upfront will not contain a y/npm directory, but I chose to save the effort.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough

return await callback(file);
} finally {
await fs.remove(file);
await fs.remove(dir);
Copy link
Contributor

Choose a reason for hiding this comment

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

cant you rm -fr the dir?

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 couldn't find a fs API that would achieve this. I can implement a short recursive remove if you want?

Copy link
Contributor

Choose a reason for hiding this comment

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

fs-extra has one and it's a good package to depend on

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacing with fs.emptyDir(dir) then fs.remove(dir). Or is there a call that does both?

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe fs.remove is idempotent

Copy link
Contributor

Choose a reason for hiding this comment

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

remove should be sufficient

@RomainMuller
Copy link
Contributor Author

RomainMuller commented Jun 22, 2018

I've changed the set-up process a bit -- the bundle_beta.sh script now installs three packages so they are available from ~/.cdk/bin after install: y-npm, aws-cdk and aws-cdk-docs. This way, one should be able to unzip, change PATH, and start using CDK without further due.

Updated the PR description accordingly.

@eladb
Copy link
Contributor

eladb commented Jun 22, 2018

Pre-installing aws-cdk and aws-cdk-docs won't work on Windows (because npm won't create the batch files). See #138, which was the initial motivation for this PR...

I think the instructions of "unzip and run y-npm aws-cdk aws-cdk-docs" are perfectly fine for the beta and in a sense are exactly how the CDK will be installed post-beta (just without the unzip portion). Aligning the beta experience with the post-beta experience (and mechanism) has a lot of value.

bundle-beta.sh Outdated
@@ -37,8 +37,8 @@ for tarball in $(find ${root}/.local-npm -iname '*.tgz') $(find ${root}/pack -in
${Y_NPM} publish ${tarball}
done

echo "Installing y-npm" # using y-npm!
${Y_NPM} install --global-style --no-save y-npm
echo "Installing y-npm, aws-cdk and aws-cdk-docs" # using y-npm!
Copy link
Contributor

Choose a reason for hiding this comment

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

This is one step to far... We are losing the idiomatic experience y-npm bought us and won't work on Windows (#138).

}
trap cleanup EXIT

# Now we'll create a hierarchy:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a few words on the motivation for this directory structure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comments on the structure itself attempt to explain the motivation. Can you elaborate on what is not clear enough? I've updated this a little in the upcoming revision.

if [ "${expected}" == "${actual}" ]; then
echo "✅ ${title}"
else
echo "❌ ${title}"
Copy link
Contributor

Choose a reason for hiding this comment

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

I've used bats in the past and loved it.

case 'my-packages':
debug(`Invoking special command: my-packages`);
case 'y-ls':
debug(`Invoking special command: y-ls`);
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of "special" maybe "invoking y-npm command: y-ls" (gives people a reference to where this is coming from)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yah!

return await callback(file);
} finally {
await fs.remove(file);
await fs.remove(dir);
Copy link
Contributor

Choose a reason for hiding this comment

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

remove should be sufficient

RomainMuller added a commit that referenced this pull request Jun 25, 2018
A drop-in replacement for the `npm` command that uses a local repository
as a source for packages, and falls back to the standard `npm` registry
for packages that are not locally available.

This can be used to make private packages available to consumers without
the need to set up a full blown repository and managing user
authentication and authorization; and also to overlay customized
versions of third party packages in a simple way.

The `y-npm` command has an additional sub-command `y-npm y-ls`
that will output the name and version of all packages that are locally
available.

This commit also replaces cdk-beta-npm in favor of using y-npm. Also,
the release .zip file no longer includes a full installation of the cdk,
instead only makes y-npm, so people can use y-npm instead of npm and
work as they normally would besides that (this reduces the file size of
the release zip by about 50%).
@RomainMuller RomainMuller deleted the rmuller/y-npm branch June 25, 2018 17:09
@NGL321 NGL321 added the contribution/core This is a PR that came from AWS. label Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants