Skip to content

Commit e619635

Browse files
committed
feat: add init workspaces
Add workspaces support to `npm init` - Refactored `lib/exec.js` into `libnpmexec` - Updates init-package-json@2.0.3 - Added ability to create a new workspace using the -w config
1 parent a4e7f4e commit e619635

28 files changed

+1315
-426
lines changed

docs/content/commands/npm-exec.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,4 @@ project.
291291
* [npm restart](/commands/npm-restart)
292292
* [npm stop](/commands/npm-stop)
293293
* [npm config](/commands/npm-config)
294+
* [npm workspaces](/using-npm/workspaces)

docs/content/commands/npm-init.md

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ description: Create a package.json file
88

99
```bash
1010
npm init [--force|-f|--yes|-y|--scope]
11-
npm init <@scope> (same as `npx <@scope>/create`)
12-
npm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)
11+
npm init <@scope> (same as `npm exec <@scope>/create`)
12+
npm init [<@scope>/]<name> (same as `npm exec [<@scope>/]create-<name>`)
13+
npm init [-w <dir>] [args...]
1314
```
1415

1516
### Description
@@ -18,19 +19,19 @@ npm init [<@scope>/]<name> (same as `npx [<@scope>/]create-<name>`)
1819
package.
1920

2021
`initializer` in this case is an npm package named `create-<initializer>`,
21-
which will be installed by [`npx`](https://npm.im/npx), and then have its
22+
which will be installed by [`npm-exec`](/commands/npm-exec), and then have its
2223
main bin executed -- presumably creating or updating `package.json` and
2324
running any other initialization-related operations.
2425

25-
The init command is transformed to a corresponding `npx` operation as
26+
The init command is transformed to a corresponding `npm exec` operation as
2627
follows:
2728

28-
* `npm init foo` -> `npx create-foo`
29-
* `npm init @usr/foo` -> `npx @usr/create-foo`
30-
* `npm init @usr` -> `npx @usr/create`
29+
* `npm init foo` -> `npm exec create-foo`
30+
* `npm init @usr/foo` -> `npm exec @usr/create-foo`
31+
* `npm init @usr` -> `npm exec @usr/create`
3132

3233
Any additional options will be passed directly to the command, so `npm init
33-
foo -- --hello` will map to `npx create-foo --hello`.
34+
foo -- --hello` will map to `npm exec -- create-foo --hello`.
3435

3536
If the initializer is omitted (by just calling `npm init`), init will fall
3637
back to legacy init behavior. It will ask you a bunch of questions, and
@@ -71,6 +72,68 @@ Generate it without having it ask any questions:
7172
$ npm init -y
7273
```
7374

75+
### Workspaces support
76+
77+
It's possible to create a new workspace within your project by using the
78+
`workspace` config option. When using `npm init -w <dir>` the cli will
79+
create the folders and boilerplate expected while also adding a reference
80+
to your project `package.json` `"workspaces": []` property in order to make
81+
sure that new generated **workspace** is properly set up as such.
82+
83+
Given a project with no workspaces, e.g:
84+
85+
```
86+
.
87+
+-- package.json
88+
```
89+
90+
You may generate a new workspace using the legacy init:
91+
92+
```bash
93+
$ npm init -w packages/a
94+
```
95+
96+
That will generate a new folder and `package.json` file, while also updating
97+
your top-level `package.json` to add the reference to this new workspace:
98+
99+
```
100+
.
101+
+-- package.json
102+
`-- packages
103+
`-- a
104+
`-- package.json
105+
```
106+
107+
The workspaces init also supports the `npm init <initializer> -w <dir>`
108+
syntax, following the same set of rules explained earlier in the initial
109+
**Description** section of this page. Similar to the previous example of
110+
creating a new React-based project using
111+
[`create-react-app`](https://npm.im/create-react-app), the following syntax
112+
will make sure to create the new react app as a nested **workspace** within your
113+
project and configure your `package.json` to recognize it as such:
114+
115+
```bash
116+
npm init -w packages/my-react-app react-app .
117+
```
118+
119+
This will make sure to generate your react app as expected, one important
120+
consideration to have in mind is that `npm exec` is going to be run in the
121+
context of the newly created folder for that workspace, and that's the reason
122+
why in this example the initializer uses the initializer name followed with a
123+
dot to represent the current directory in that context, e.g: `react-app .`:
124+
125+
```
126+
.
127+
+-- package.json
128+
`-- packages
129+
+-- a
130+
| `-- package.json
131+
`-- my-react-app
132+
+-- README
133+
+-- package.json
134+
`-- ...
135+
```
136+
74137
### A note on caching
75138

76139
The npm cli utilizes its internal package cache when using the package
@@ -93,10 +156,38 @@ requested from the server. To force full offline mode, use `offline`.
93156
Forces full offline mode. Any packages not locally cached will result in
94157
an error.
95158

159+
#### workspace
160+
161+
* Alias: `-w`
162+
* Type: Array
163+
* Default: `[]`
164+
165+
Enable running `npm init` in the context of workspaces, creating any missing
166+
folders, generating files and adding/updating the `"workspaces"` property of
167+
the project `package.json`.
168+
169+
the provided names or paths provided.
170+
171+
Valid values for the `workspace` config are either:
172+
- Workspace names
173+
- Path to a workspace directory
174+
- Path to a parent workspace directory (will result to selecting all of the
175+
children workspaces)
176+
177+
#### workspaces
178+
179+
* Alias: `-ws`
180+
* Type: Boolean
181+
* Default: `false`
182+
183+
Run `npm init` in the context of all configured workspaces for the
184+
current project.
185+
96186
### See Also
97187

98188
* [init-package-json module](http://npm.im/init-package-json)
99189
* [package.json](/configuring-npm/package-json)
100190
* [npm version](/commands/npm-version)
101191
* [npm scope](/using-npm/scope)
102192
* [npm exec](/commands/npm-exec)
193+
* [npm workspaces](/using-npm/workspaces)

docs/content/commands/npm-run-script.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,4 @@ project.
204204
* [npm restart](/commands/npm-restart)
205205
* [npm stop](/commands/npm-stop)
206206
* [npm config](/commands/npm-config)
207+
* [npm workspaces](/using-npm/workspaces)

0 commit comments

Comments
 (0)