Skip to content

Commit 2035475

Browse files
authored
Adds packageManager during init (#286)
<!-- CURSOR_SUMMARY --> > [!NOTE] > **Medium Risk** > Touches project initialization and `package.json` generation, which can affect all newly created projects; risk is moderate because changes are localized but impact default manifest fields. > > **Overview** > `yarn init` now always writes a `packageManager` field to `package.json` using the running CLI version (passed through `InitParams`), instead of relying on `INFRA_VERSION`/omitting it. > > When initializing a new manifest, it also seeds `type: "module"`, and the default empty `package.json` template now includes a trailing newline. Separately bumps `zpm-switch` from `6.0.0-rc.17` to `6.0.0-rc.18` (lockfile + manifest). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 167e9ae. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 3624906 commit 2035475

3 files changed

Lines changed: 43 additions & 28 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/zpm-switch/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "zpm-switch"
3-
version = "6.0.0-rc.17"
3+
version = "6.0.0-rc.18"
44
edition = "2021"
55

66
[[bin]]

packages/zpm/src/commands/init.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl InitWithTemplate {
6363
private: self.private,
6464
workspace: self.workspace,
6565
name: self.name.clone(),
66+
version: self.cli_environment.info.version.clone(),
6667
};
6768

6869
let mut project
@@ -78,9 +79,10 @@ impl InitWithTemplate {
7879
let package_cache
7980
= project.package_cache()?;
8081

81-
let install_context = InstallContext::default()
82-
.with_package_cache(Some(&package_cache))
83-
.with_project(Some(&project));
82+
let install_context
83+
= InstallContext::default()
84+
.with_package_cache(Some(&package_cache))
85+
.with_project(Some(&project));
8486

8587
let template
8688
= self.template.resolve(&install_context, &resolve_options).await?;
@@ -144,6 +146,7 @@ impl Init {
144146
private: self.private,
145147
workspace: self.workspace,
146148
name: self.name.clone(),
149+
version: self.cli_environment.info.version.clone(),
147150
};
148151

149152
let mut project
@@ -161,6 +164,7 @@ pub struct InitParams {
161164
private: Option<bool>,
162165
workspace: bool,
163166
name: Option<String>,
167+
version: String,
164168
}
165169

166170
pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project, Error> {
@@ -172,29 +176,33 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
172176
let manifest_content
173177
= manifest_path.fs_read_prealloc()
174178
.ok_missing()?
175-
.unwrap_or_else(|| b"{}".to_vec());
179+
.unwrap_or_else(|| b"{}\n".to_vec());
176180

177181
let mut document
178182
= JsonDocument::new(manifest_content)?;
179183

180184
if !manifest_path.fs_exists() {
181-
let init_name = params.name.as_ref()
182-
.map(|n| Ident::new(n))
183-
.unwrap_or_else(|| Ident::new(init_cwd.basename().unwrap_or("package")));
185+
let init_name
186+
= params.name.as_ref()
187+
.map(|n| Ident::new(n))
188+
.unwrap_or_else(|| Ident::new(init_cwd.basename().unwrap_or("package")));
184189

185190
document.set_path(
186191
&zpm_parsers::Path::from_segments(vec!["name".to_string()]),
187192
Value::String(init_name.to_file_string()),
188193
)?;
189-
}
190194

191-
if let Some(version) = option_env!("INFRA_VERSION") {
192195
document.set_path(
193-
&zpm_parsers::Path::from_segments(vec!["packageManager".to_string()]),
194-
Value::String(format!("yarn@{version}")),
196+
&zpm_parsers::Path::from_segments(vec!["type".to_string()]),
197+
Value::String("module".to_file_string()),
195198
)?;
196199
}
197200

201+
document.set_path(
202+
&zpm_parsers::Path::from_segments(vec!["packageManager".to_string()]),
203+
Value::String(format!("yarn@{}", params.version)),
204+
)?;
205+
198206
if let Some(private) = params.private {
199207
let private_field = match private {
200208
true => Value::Bool(true),
@@ -211,8 +219,9 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
211219
// define a workspace root (we should have a different flag
212220
// for that).
213221
if params.workspace {
214-
let packages_dir = init_cwd
215-
.with_join_str("packages");
222+
let packages_dir
223+
= init_cwd
224+
.with_join_str("packages");
216225

217226
packages_dir
218227
.fs_create_dir_all()?;
@@ -238,7 +247,8 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
238247
];
239248

240249
let readme_path
241-
= init_cwd.with_join_str("README.md");
250+
= init_cwd
251+
.with_join_str("README.md");
242252

243253
if !readme_path.fs_exists() {
244254
if let Some(name) = manifest.name.as_ref() {
@@ -253,14 +263,16 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
253263
}
254264

255265
// Only create lockfile and other files if we're in the project root
256-
let is_project_root = existing_project
257-
.as_ref()
258-
.map(|(project_cwd, _)| project_cwd == init_cwd)
259-
.unwrap_or(true);
266+
let is_project_root
267+
= existing_project
268+
.as_ref()
269+
.map(|(project_cwd, _)| project_cwd == init_cwd)
270+
.unwrap_or(true);
260271

261272
if is_project_root {
262-
let lockfile_path = init_cwd
263-
.with_join_str("yarn.lock");
273+
let lockfile_path
274+
= init_cwd
275+
.with_join_str("yarn.lock");
264276

265277
if !lockfile_path.fs_exists() {
266278
lockfile_path
@@ -271,8 +283,9 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
271283
);
272284
}
273285

274-
let gitignore_path = init_cwd
275-
.with_join_str(".gitignore");
286+
let gitignore_path
287+
= init_cwd
288+
.with_join_str(".gitignore");
276289

277290
if !gitignore_path.fs_exists() {
278291
let gitignore_content = [
@@ -287,8 +300,9 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
287300
);
288301
}
289302

290-
let gitattributes_path = init_cwd
291-
.with_join_str(".gitattributes");
303+
let gitattributes_path
304+
= init_cwd
305+
.with_join_str(".gitattributes");
292306

293307
if !gitattributes_path.fs_exists() {
294308
let gitattributes_content = [
@@ -304,8 +318,9 @@ pub async fn init_project(init_cwd: &Path, params: InitParams) -> Result<Project
304318
);
305319
}
306320

307-
let editorconfig_path = init_cwd
308-
.with_join_str(".editorconfig");
321+
let editorconfig_path
322+
= init_cwd
323+
.with_join_str(".editorconfig");
309324

310325
if !editorconfig_path.fs_exists() {
311326
let editorconfig_content = [

0 commit comments

Comments
 (0)