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

support pkg little better with default setup #177

Closed
twk-b opened this issue Oct 15, 2018 · 24 comments
Closed

support pkg little better with default setup #177

twk-b opened this issue Oct 15, 2018 · 24 comments

Comments

@twk-b
Copy link

twk-b commented Oct 15, 2018

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

pkg . doesnt include multi commands

What is the expected behavior?

pkg . includes multi commands

adding the following seems pretty low bar to get basic support to package.json
"pkg": {
"scripts": "src/commands/**/*.js"
}

and
"scripts: {
"pkg": "pkg ."
}

The docs where missing a links mutli intro introduction page to package.json:pkg.topics as the place to document the subdirectories as they are created.

@jdx
Copy link
Contributor

jdx commented Oct 15, 2018

I don't suggest using pkg at all so I won't be modifying the generated code. I couldn't make sense of your second paragraph though

@twk-b
Copy link
Author

twk-b commented Oct 15, 2018

I think oclif is an awesome tool. I understand there are a lot of CLI apps intended for developers, but I think there are also a lot of apps being provided to teams not running NodeJS locally, without docker and other standard dev tooling. In this case I think pkg is a great solution. For us we have graphics team members and content developers using CLI tools.

For me the biggest advantage over Commander that I see is connecting the dots with ENV variables also, as well as establishing more of a structure to help quick build out simpler commands.

The documentation about setting up Multi commands using the generate, did not include a link to how to setup the "topics" in the help for the directories (instead of showing the description of the first task in the directory). Very minor detail, but it took a search to find how to set them...

@jdx
Copy link
Contributor

jdx commented Oct 15, 2018

oclif already provides a means for including node with the built in oclif-dev pack commands

@quinn
Copy link

quinn commented Oct 22, 2018

@jdxcode that is incorrect. That is for building a bundled command that could be installed via homebrew, this is simply for building the command to be published to NPM. there is no way to do this currently, as far as I can tell.

@jdx
Copy link
Contributor

jdx commented Oct 22, 2018

None of what we've discussed has anything to do with npm distribution

@quinn
Copy link

quinn commented Oct 22, 2018

@jdxcode yeah, but it's about building the thing. there isn't any builtin support for that, you need to do it your self (in order to publish to NPM, or whatever)

@quinn
Copy link

quinn commented Oct 22, 2018

@jdxcode and oclif-dev pack is not that. it serves a different purpose

@jdx
Copy link
Contributor

jdx commented Oct 22, 2018

See my comment on your other post, the prepack step is not working on your local machine. oclif-dev pack is used for tarball distribution not npm distribution.

@twk-b
Copy link
Author

twk-b commented Oct 22, 2018

@jdxcode my problem was not if node was included or not, but if the commands JS files are included in the pkg (ie src/commands/**/*.js). In my case it looked like it was working because the package.json had some information for titles in it (so running pkgname --help showed help), but none of the commands JS was bundled.

The two configs I suggest would make npm run pkg always build a proper single file app (single command and multi command).

we are not trying to distribute a tarball.

intended users are not using Node/NPM locally; we are simply trying to give them some simple CLI tools.

@jdx
Copy link
Contributor

jdx commented Oct 22, 2018

I understand that you want to use pkg. I'm saying the tarball method solves the same problem that pkg would solve via a different means. (Bundling Node with the CLI and so you don't have to install via npm)

I also understand that we could make some simple changes to more easily accommodate pkg, but including such config essentially condones its use which I'm not sure I want to do. I would prefer people use the tarball method instead

@twk-b
Copy link
Author

twk-b commented Oct 22, 2018

ill look into oclif-dev pack, feel free to close

@jdx
Copy link
Contributor

jdx commented Oct 22, 2018

I'll leave it open since I'm willing to be convinced otherwise and others may chime in. Would love to hear what your thoughts are about pack as well

@mnapoli
Copy link

mnapoli commented Aug 28, 2020

Hi! I'm going the pkg route because I don't understand what to do with the tarballs.

I'm not sure what users should do with it: download, unzip, and then what? Create a symlink in /usr/local/bin? If that's the case, that wouldn't work for me.

The homebrew method wouldn't work as well because I would need to support multiple package managers (debian, etc.) that sounds like way too much effort for distributing a little app.

@mnapoli
Copy link

mnapoli commented Dec 6, 2020

Just to confirm that I managed to use pkg to distribute my closed-source binary (https://port7777.com/).

@joe307bad
Copy link

joe307bad commented Mar 2, 2021

I am with @mnapoli. At first I tried to distribute my cli app to some colleagues using the tarbells. This required generating the tarbell, converting to a zip file (because Windows users don't have an easy way to unpack tarbells), sending the zip file, unzipping, and instructing users to execute commands from the bin folder.

I was able to simplify the process using pkg

  1. Add pkg to your dev dependencies
  2. Add the following to package.json
"pkg": {
    "scripts": "./lib/**/*.js"
  },
  "scripts": {
    "pkg": "pkg -o dist/fcli -t node14-win ."
  },

And now I can execute yarn pkg and have a single file (dist/fcli.exe) that I can send to my Windows users. :)

@Chuwiey
Copy link

Chuwiey commented Apr 20, 2021

My use case is similar to @mnapoli where I want to distribute a closed-source binary. In a multi-command project, I am able to run pkg . but the sub-commands are not being picked up. I have added the glob to pkg.scripts. Any help would be appreciated.

@dominics
Copy link

dominics commented May 4, 2021

@Chuwiey oclif uses globby to look for commands (https://github.com/oclif/config/blob/0779d64dedc5a3a0dee28376971c0e42715c4d36/src/plugin.ts#L232)

For versions of pkg that are >= 4.5.0 (including 5.x), globby doesn't work when looking in directories within /snapshot (all the packaged code) - I posted a reproduction at vercel/pkg#1130 (comment)

So I recommend locking down your version of pkg to 4.4.9 until that's fixed if you're packaging oclif.

PS: one other problem you might run into is pkg-fetch starting to fail, because the version range for that dependency in pkg matches a packaging mistake that was made at 2.7.x - I've worked around that with Yarn's resolutions for now.

@christroutner
Copy link

It would be really impractical to expect users to know how to work with tarballs or distribution packages. The whole point of creating a CLI app (at least in my cases), is to lower the barrier of entry for non-technical or semi-technical people.

I was able to get pkg to work with a mutli-command oclif app by making the following changes to package.json:

Use pkg v4.4.9:

"devDependencies": {
    "pkg": "4.4.9"
  },

Add a pkg script:

"scripts": {
    "pkg": "pkg --out-path dist/ ."
  },

Include the commands directory:

"pkg": {
    "scripts": "src/commands/**/*.js"
  }

@ngkimnhatnam
Copy link

@christroutner Hi! I'm bumping into the same need, e.g. I need to package my Node/Express app with pkg, and including oclif as simple CLI for some basic commands. I cloned your repo https://github.com/christroutner/bin-cli-boilerplate and tried out, but unfortunately could not follow the same. I'm using Typescript btw.
Could you explain some fields in package.json e.g. "oclif" and "files", what role they play? :)

@christroutner
Copy link

@ngkimnhatnam I hit a wall as well. The technique I described worked at first, but I ran into issues as I started to add more complex npm packages.

@mnapoli
Copy link

mnapoli commented Oct 23, 2021

Here is how we did it, hopefully that helps but since it was done a while ago, and it was very confusing to pull off, I may not be sharing 100% of the steps needed to achieve it.

package.json:

{
  ...
  "bin": {
    "7777": "bin/run"
  },
  "dependencies": {
    ...
  },
  "files": [
    "/bin",
    "/lib",
    "/npm-shrinkwrap.json",
    "/oclif.manifest.json"
  ],
  "main": "lib/index.js",
  "oclif": {
    "commands": "./lib/commands",
    "bin": "7777",
    "plugins": [
      "@oclif/plugin-help"
    ]
  },
  "scripts": {
    "postpack": "rm -f oclif.manifest.json",
    "prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
    "test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
    "version": "oclif-dev readme && git add README.md",
    "tsc": "tsc"
  }
}

The lib/index.js file is generated from that simple src/index.ts file:

export {run} from '@oclif/command'

Build script:

# Install pkg
npm install -g pkg@4.4.9

	# Delete the previous binaries
	rm -rf dist/ lib/
	# This compiles Typescript to JavaScript in the `lib` directory
	npm pack
	# This archive is also generated but we don't use it, let's remove it
	rm -f 7777-*.tgz
	# Now before creating the binary, we lighten the final binary by removing dev dependencies
	npm ci --only=prod
	# Generate the binaries using the `pkg` tool
	./node_modules/.bin/pkg . --targets=node14-macos --out-path dist

We use pkg 4.4.9 because of vercel/pkg#1130

@mii9000
Copy link

mii9000 commented Jan 10, 2022

I needed to first do npm run build to generate a dist directory and then do pkg --out-path exe/ . where exe is the folder where executables would be generated. Spent hours trying to figure out that this in official docs was not working. Not even sure why ./lib is mentioned.

@phhoef
Copy link

phhoef commented May 13, 2022

I am trying to package my small cli app with pkg.
Somehow it is working, but I am always getting a warning saying that the @oclif/plugin-help could not find the package.json

(node:41304) Error Plugin: my-app: could not find package.json with {
  type: 'core',
  root: '/snapshot/my-app,
  name: '@oclif/plugin-help'
}
module: @oclif/core@1.8.1
task: loadPlugins
plugin: my-app
root: /snapshot/my-app
See more details with DEBUG=*
(Use `my-app --trace-warnings ...` to show where the warning was created)

I've "pkg": "^5.6.0" to my devDependencies
Added the following section to my package.json

  "pkg": {
    "scripts": "./dist/**/*.js",
    "outputPath": "exec"
  },

and running the following command yarn build && pkg . --targets node16-macos-x64
Though, my commands are working, but always getting the shown warning.

Can anyone give me a hint what's going on?

@mdonnalley mdonnalley closed this as not planned Won't fix, can't repro, duplicate, stale Aug 16, 2022
@0xdevalias
Copy link

@mdonnalley It would be nice to include some context/description on why the issue is being closed (if it was completed, then when/where it was completed; if it's 'not planned' then more of an explanation of why it's not going to happen). This current process of 'close a huge pile of issues' with no context is generally considered pretty 'hostile'/'disrespectful' (for lack of a better term) to open source/collaboration/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests