Automated command to generate composer.json? - composer-php

Is there an automated command to generate the composer.json file? I was hoping for something interactive that just asks me the package names and dumps them into a composer.json file interactively. I somehow remember using something like this in the past... I may be mistaken.

composer init, see docs for more.

Related

How to add a package to "replace" section of "composer.json" using CLI?

How can I add a package to the replace section of composer.json using cli?
For example composer require vendor/package adds a package to the require section.
In composer docs, I did not find anything like composer replace vendor/package command.
Editing the replace section of composer.json is not currently supported via the CLI.
Neither editing conflict, for that matter.
The complete list of CLI options is listed here.
You could use something like jq to do it if for some reason you need to do it programmatically via the CLI (e.g.), but more often than not is the kind of thing a developer adds manually to their composer.json when needed.

Activate/deactivate conda virtualenvs on entering/leaving directories

pyenv-virtualenv offers a nice way of activating the environment on the very instant of entering or leaving the directory which contains a .python-version text file which specifies the environment to activate. It works for the directory it is in and all directories contained in it.
The environment is deactivated once we change the directory to something above it. This allows to easily switch between projects or analyses using different python versions (just by changing the directories).
Is there a way of achieving the same behaviour with (ana)conda?
Edit: added bash tag, because - as far as I understand - pyenv achieves this by hooking a custom script into .bashrc (which allows it to monitor the directory changes). If there is no build-in way in conda, how to create a script which would make it possible?
As mentioned in my comment, this is currently not supported. There is however an open issue on conda's GitHub asking for this feature.
In the meantime you could use autoenv, a small tool that'll automatically run the code in a .env file when entering a directory and that in a .env.leave when leaving the directory (supports bash/zsh and a couple others).
A simple example taken from their readme which illustrates the feature quite nicely:
$ echo "echo 'whoa'" > project/.env
$ cd project
whoa
To load a conda environment your .env would simply look like this:
conda activate <my_env>
Note 1: Check out the Configuration section of their GitHub readme before you start using it.
Note 2: The author of autoenv actually suggests trying direnv instead. However I've never used it, so I can't comment on it.
From autoenv's readme:
you should probably use direnv instead. Simply put, it is higher quality software. But, autoenv is still great, too. Maybe try both? :)

Is it possible to go back and change the last input you entered into a command prompt questionnaire?

If I accidentally entered a typo into a command prompt questionnaire, is there a way to go backwards and edit that input?
For example, when I run npm init, I'm given a bunch questions. If I accidentally write a typo in the package name field and press enter, the questionaire moves on to version:. Is there a way to quickly go back to package name and fix my typo?
My experience with the terminal and npm is limited, but I found out that you cannot undo a command line. However, I'm wonder if you CAN undo an answer to a command prompt, since this is a little different- The command hasn't really run yet until all the prompts are done.
Sat Apr 27 npm Dashie$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npm) oops-typo
version: (1.0.0)
An undo functionality like you are describing would have to be implemented by the developers of the cli tool you are using (in this case, npm).
I think the best I can offer as a means to fix your mistake on the command line, without having to open an editor and modify the package.json file from there, is to use the json npm package.
So in the particular case displayed in your question, you would keep going with the install and afterwards you could simply do:
$ npm i -g json
$ json -I -f package.json -e "this.name='your package name'"
You can edit the package name in the package.json file created when you use init.

How to run a shell script from my published npm package

I realize this is kind of a silly question, but I'm deeply curious about how I accomplish the following.
I created a shell script for my cohort at school which builds out large project templates. I've successfully published the package to npm, but after downloading, I can't get the script to run. I'd like for users to simply download the package and run something like $ npm project-template start(as an example) to trigger the script.
Is this possible?
If anyone has advice, suggestions, pointers, I'd really appreciate it. Thanks in advance!
Its always after I ask the internet for answers that I find the answers on my own.
Add the program to the binaries object
Basically, what I needed to do was to add the command I wanted to run to the bin section of the package.json:
"bin": {
"nameOfCommand": "path/to/script.sh"
}
The left side, nameOfCommand, is the name of the command that is used in your terminal
$ nameOfCommand args --options
The right side is the path to your CLI program, in this case, a bash/shell script.
The information can be found here in this article by NPM.

Where should I install Composer?

Just wondering where I should install Composer. It kind of wrecked my environment last time. I'm running XAMPP and i'm looking to use it within some Framework sites. So to me the XAMPP folder itself seems appropriate. Would that be correct? The main thing for me is that it doesn't alter any environment paths or the such.
Any advice would be great.
The main aspect is that you'd probably want to run Composer easily in the command shell. This implies that Composer has to be in any directory mentioned in the path variable.
Have a look at your current path, pick a convenient directory, and put the composer.phar file there.
If you don't like that, you could also create a batch file that does run PHP with that phar file in a different location, and passes all the other command line arguments to it.

Resources