Skip to content

Commit 7ab1347

Browse files
committed
Add: Bash script for automatically populating the scripts key in package.json files
1 parent 181849c commit 7ab1347

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

README.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ $ tree 283-move-zeroes
1414
## `package.json` scripts
1515
You can use the following scripts on each exercise:
1616
```json
17+
// leetcode-node-scripts.json
1718
{
18-
// ...
19-
"scripts": {
20-
"start": "node index.js",
21-
"debug": "node --inspect-brk index.js",
22-
"test": "node test.js"
23-
},
24-
// ...
19+
"start": "node index.js",
20+
"debug": "node --inspect-brk index.js",
21+
"test": "node test.js"
2522
}
2623

2724
```
@@ -30,3 +27,12 @@ Where:
3027
- `start`: Starts the program
3128
- `debug`: Starts the program in debug mode
3229
- `test`: Run the test suite for this exercise
30+
31+
### Populating Automatically the `scripts` Key of `package.json` files
32+
Execute the following script, in this example the `package.json` file has been created beforehand
33+
inside the directory `./1004-max-consecutive-ones-iii` using `npm init -y`:
34+
```sh
35+
# Execute from the repository root
36+
$ ./add-scripts.sh ./1004-max-consecutive-ones-iii/package.json
37+
38+
```

add-scripts.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Check if jq is installed
4+
if ! command -v jq &> /dev/null
5+
then
6+
echo "jq could not be found. Please install it."
7+
exit 1
8+
fi
9+
10+
# Check if the correct number of arguments is given
11+
if [ "$#" -ne 1 ]; then
12+
echo "Usage: $0 path/to/package.json"
13+
exit 1
14+
fi
15+
16+
# File paths
17+
PACKAGE_JSON="$1"
18+
SCRIPTS_JSON="leetcode-node-scripts.json"
19+
20+
# Check if the package.json file exists
21+
if [ ! -f "$PACKAGE_JSON" ]; then
22+
echo "package.json not found at $PACKAGE_JSON"
23+
exit 1
24+
fi
25+
26+
# Check if the leetcode-node-scripts.json file exists
27+
if [ ! -f "$SCRIPTS_JSON" ]; then
28+
echo "$SCRIPTS_JSON not found"
29+
exit 1
30+
fi
31+
32+
# Replace the scripts section in package.json
33+
jq --argfile scripts "$SCRIPTS_JSON" '.scripts = $scripts' "$PACKAGE_JSON" > temp.json && mv temp.json "$PACKAGE_JSON"
34+
35+
echo "Scripts section has been updated in $PACKAGE_JSON"

leetcode-node-scripts.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"start": "node index.js",
3+
"debug": "node --inspect-brk index.js",
4+
"test": "node test.js"
5+
}

0 commit comments

Comments
 (0)