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

feat: add "deno init" subcommand #15469

Merged
merged 11 commits into from
Aug 19, 2022
Merged

Conversation

crowlKats
Copy link
Member

@crowlKats crowlKats commented Aug 12, 2022

This adds an init subcommand to that creates a project starter similar to cargo init.

$ deno init my_project
Project initialized
Run these commands to get started:
  cd my_project
  deno run main.ts
  deno run main_test.ts
$ deno run main.ts
Add 2 + 3 5
$ cat main.ts
export function add(a: number, b: number): number {
  return a + b;
}
if (import.meta.main) {
  console.log("Add 2 + 3", add(2, 3));
}
$ cat main_test.ts
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { add } from "./main.ts";
Deno.test(function addTest() {
    assertEquals(add(2, 3), 5);
});

Related to #12781 but takes entirely different approach.

This is just an MVP that enables developers to get started even quicker (similarly to cargo init); it boils down to 9 keystrokes to get going, without the need to manually create files from the terminal or IDE.

Potential features that could be added should this be landed:

  1. prompt if user wants to use JS or TS
  2. prompt if user wants to add a config file
  3. prompt if user wants to add an import map
  4. prompt if user wants to setup Deno VSCode extension

cc @bartlomieju

@crowlKats crowlKats changed the title add init subcommand feat: add init subcommand Aug 12, 2022
@bartlomieju bartlomieju changed the title feat: add init subcommand feat: add "deno init" subcommand Aug 12, 2022
cli/main.rs Outdated

if (import.meta.main) {
console.log("Add 2 + 3", add(2, 3));
}
Copy link
Member

@dsherret dsherret Aug 14, 2022

Choose a reason for hiding this comment

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

Maybe we should exclude the if statement, but keep the console.log? I think the if statement might confuse some beginners into thinking it's required (or not know what it is).

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be also confusing if you run the test and then you get a console.log :/

Copy link
Member

@dsherret dsherret Aug 14, 2022

Choose a reason for hiding this comment

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

Wouldn’t that be obvious looking at the code? Most people are familiar with console.log, but import.meta.main is not something used often. Anyway, I don’t have strong feelings.

Copy link
Contributor

Choose a reason for hiding this comment

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

And the way the test runner works, it would show as test output, letting users know they get this sort of stuff managed for them.

Copy link
Member

Choose a reason for hiding this comment

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

And the way the test runner works, it would show as test output, letting users know they get this sort of stuff managed for them.

Not really - since it would be a console.log at the top level of the file it would be printed just after "Check ..." like so:

Check file:///Users/ib/dev/deno/mod_test.ts
Add 2 + 3 5
running 1 test from ./mod_test.ts
addTest ... ok (4ms)

ok | 1 passed | 0 failed (24ms)

I'm fine with removing import.meta.main conditional, or we could add a comment that explains its purpose or linking to the manual.

@ije
Copy link

ije commented Aug 15, 2022

can we prompt to add tasks for esm.sh cli script?

deno task npm:add [package...] # deno run -A https://esm.sh/v91
deno task npm:update [package...] # upgrade packages
deno task npm:remove[package...] # remove packages

since i added the init command in https://github.com/ije/esm.sh/releases/tag/v91

@bartlomieju bartlomieju marked this pull request as ready for review August 17, 2022 14:36
Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

A couple of comments but LGTM

Comment on lines +14 to +20
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(dir.join(filename))
.with_context(|| format!("Failed to create {} file", filename))?;
file.write_all(content.as_bytes())?;
Ok(())
Copy link
Member

Choose a reason for hiding this comment

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

std::fs::write(dir.join(filename), content.as_bytes())

Copy link
Member

Choose a reason for hiding this comment

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

This will overwrite existing files and doesn't provide useful error message if something goes wrong

cli/tools/init/mod.rs Outdated Show resolved Hide resolved
cli/tools/init/templates/main.ts Outdated Show resolved Hide resolved
@bartlomieju bartlomieju merged commit 1ffbd56 into denoland:main Aug 19, 2022
@crowlKats crowlKats deleted the init_subcommand branch August 19, 2022 23:57
@crowlKats crowlKats restored the init_subcommand branch October 14, 2022 08:39
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

Successfully merging this pull request may close these issues.

6 participants