Skip to content

Commit

Permalink
Merge pull request #64 from codecrafters-io/CC-1122-ts-support
Browse files Browse the repository at this point in the history
feat: add typescript support, using bun runtime
  • Loading branch information
ryan-gang authored Apr 26, 2024
2 parents 139f321 + 276adbe commit 06fda55
Show file tree
Hide file tree
Showing 27 changed files with 338 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiled_starters/typescript/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions compiled_starters/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
39 changes: 39 additions & 0 deletions compiled_starters/typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for TypeScript solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `app/main.ts`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `bun (1.1)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`app/main.ts`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
27 changes: 27 additions & 0 deletions compiled_starters/typescript/app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const args = process.argv;
const pattern = args[3];

const inputLine: string = await Bun.stdin.text();

function matchPattern(inputLine: string, pattern: string): boolean {
if (pattern.length === 1) {
return inputLine.includes(pattern);
} else {
throw new Error(`Unhandled pattern: ${pattern}`);
}
}

if (args[2] !== "-E") {
console.log("Expected first argument to be '-E'");
process.exit(1);
}

// You can use print statements as follows for debugging, they'll be visible when running tests.
console.log("Logs from your program will appear here!");

// Uncomment this block to pass the first stage
// if (matchPattern(inputLine, pattern)) {
// process.exit(0);
// } else {
// process.exit(1);
// }
Binary file added compiled_starters/typescript/bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions compiled_starters/typescript/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the TypeScript version used to run your code
# on Codecrafters.
#
# Available versions: bun-1.1
language_pack: bun-1.1
11 changes: 11 additions & 0 deletions compiled_starters/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@codecrafters/build-your-own-grep",
"description": "Build your own Grep challenge, from CodeCrafters",
"type": "module",
"scripts": {
"dev": "bun run app/main.ts"
},
"dependencies": {
"fs-extra": "^11.2.0"
}
}
8 changes: 8 additions & 0 deletions compiled_starters/typescript/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec bun run app/main.ts "$@"
2 changes: 2 additions & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ languages:
- slug: "rust"
- slug: "csharp"
release_status: "beta"
- slug: "typescript"
release_status: "beta"

marketing:
difficulty: medium
Expand Down
17 changes: 17 additions & 0 deletions dockerfiles/bun-1.1.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM oven/bun:1.1.4-alpine

ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="package.json,bun.lockb"

WORKDIR /app

COPY package.json ./
COPY bun.lockb ./

# For reproducible builds.
# This will install the exact versions of each package specified in the lockfile.
# If package.json disagrees with bun.lockb, Bun will exit with an error. The lockfile will not be updated.
RUN bun install --frozen-lockfile

RUN mkdir -p /app-cached
# If the node_modules directory exists, move it to /app-cached
RUN if [ -d "/app/node_modules" ]; then mv /app/node_modules /app-cached; fi
1 change: 1 addition & 0 deletions solutions/typescript/01-init/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions solutions/typescript/01-init/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
39 changes: 39 additions & 0 deletions solutions/typescript/01-init/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for TypeScript solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `app/main.ts`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `bun (1.1)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`app/main.ts`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
23 changes: 23 additions & 0 deletions solutions/typescript/01-init/code/app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const args = process.argv;
const pattern = args[3];

const inputLine: string = await Bun.stdin.text();

function matchPattern(inputLine: string, pattern: string): boolean {
if (pattern.length === 1) {
return inputLine.includes(pattern);
} else {
throw new Error(`Unhandled pattern: ${pattern}`);
}
}

if (args[2] !== "-E") {
console.log("Expected first argument to be '-E'");
process.exit(1);
}

if (matchPattern(inputLine, pattern)) {
process.exit(0);
} else {
process.exit(1);
}
Binary file added solutions/typescript/01-init/code/bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions solutions/typescript/01-init/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the TypeScript version used to run your code
# on Codecrafters.
#
# Available versions: bun-1.1
language_pack: bun-1.1
11 changes: 11 additions & 0 deletions solutions/typescript/01-init/code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@codecrafters/build-your-own-grep",
"description": "Build your own Grep challenge, from CodeCrafters",
"type": "module",
"scripts": {
"dev": "bun run app/main.ts"
},
"dependencies": {
"fs-extra": "^11.2.0"
}
}
8 changes: 8 additions & 0 deletions solutions/typescript/01-init/code/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec bun run app/main.ts "$@"
33 changes: 33 additions & 0 deletions solutions/typescript/01-init/diff/app/main.ts.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@@ -1,27 +1,23 @@
const args = process.argv;
const pattern = args[3];

const inputLine: string = await Bun.stdin.text();

function matchPattern(inputLine: string, pattern: string): boolean {
if (pattern.length === 1) {
return inputLine.includes(pattern);
} else {
throw new Error(`Unhandled pattern: ${pattern}`);
}
}

if (args[2] !== "-E") {
console.log("Expected first argument to be '-E'");
process.exit(1);
}

-// You can use print statements as follows for debugging, they'll be visible when running tests.
-console.log("Logs from your program will appear here!");
-
-// Uncomment this block to pass the first stage
-// if (matchPattern(inputLine, pattern)) {
-// process.exit(0);
-// } else {
-// process.exit(1);
-// }
+if (matchPattern(inputLine, pattern)) {
+ process.exit(0);
+} else {
+ process.exit(1);
+}
20 changes: 20 additions & 0 deletions solutions/typescript/01-init/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The entry point for your grep implementation is in `app/main.ts`.

Study and uncomment the relevant code:

```typescript
// Uncomment this block to pass the first stage
if (matchPattern(inputLine, pattern)) {
process.exit(0);
} else {
process.exit(1);
}
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
23 changes: 23 additions & 0 deletions starter-repository-definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,26 @@
template_attributes:
required_executable: "dotnet (8.0)"
user_editable_file: "src/Program.cs"

- language: typescript
file_mappings:
- source: starter_templates/README.md
target: README.md
- source: starter_templates/codecrafters.yml
target: codecrafters.yml
- source: starter_templates/typescript/app/main.ts
target: app/main.ts
- source: starter_templates/typescript/package.json
target: package.json
- source: starter_templates/typescript/bun.lockb
target: bun.lockb
should_skip_template_evaluation: true
- source: starter_templates/typescript/your_grep.sh
target: your_grep.sh
- source: starter_templates/.gitattributes
target: .gitattributes
- source: starter_templates/typescript/.gitignore
target: .gitignore
template_attributes:
required_executable: "bun (1.1)"
user_editable_file: "app/main.ts"
4 changes: 4 additions & 0 deletions starter_templates/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ language_pack: dotnet-8.0
# Available versions: cpp-20
language_pack: cpp-20
{{/language_is_cpp}}
{{#language_is_typescript}}
# Available versions: bun-1.1
language_pack: bun-1.1
{{/language_is_typescript}}
1 change: 1 addition & 0 deletions starter_templates/typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
27 changes: 27 additions & 0 deletions starter_templates/typescript/app/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const args = process.argv;
const pattern = args[3];

const inputLine: string = await Bun.stdin.text();

function matchPattern(inputLine: string, pattern: string): boolean {
if (pattern.length === 1) {
return inputLine.includes(pattern);
} else {
throw new Error(`Unhandled pattern: ${pattern}`);
}
}

if (args[2] !== "-E") {
console.log("Expected first argument to be '-E'");
process.exit(1);
}

// You can use print statements as follows for debugging, they'll be visible when running tests.
console.log("Logs from your program will appear here!");

// Uncomment this block to pass the first stage
// if (matchPattern(inputLine, pattern)) {
// process.exit(0);
// } else {
// process.exit(1);
// }
Binary file added starter_templates/typescript/bun.lockb
Binary file not shown.
11 changes: 11 additions & 0 deletions starter_templates/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@codecrafters/build-your-own-grep",
"description": "Build your own Grep challenge, from CodeCrafters",
"type": "module",
"scripts": {
"dev": "bun run app/main.ts"
},
"dependencies": {
"fs-extra": "^11.2.0"
}
}
8 changes: 8 additions & 0 deletions starter_templates/typescript/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec bun run app/main.ts "$@"

0 comments on commit 06fda55

Please sign in to comment.