-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-nodejs-package
executable file
·188 lines (169 loc) · 5.43 KB
/
bootstrap-nodejs-package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
#
# Functions
#
# Function to format text as bold
bold() {
echo -e "\033[1m$1\033[0m"
}
#
# Script
#
# Get the package name from the command-line argument or prompt the user
if [ -z "$1" ]; then
read -p "Enter your package name: " package_name
else
package_name=$1
fi
# Check if the folder already exists
if [ -d "$package_name" ]; then
echo "Error: The folder '$package_name' already exists. Please choose a different package name or delete that folder before running this script."
exit 1
fi
# Create a folder and set up an empty git repository
echo "$(bold "Step 1"): Creating a new folder '$package_name'"
mkdir $package_name
cd $package_name
echo "$(bold "Step 2"): Initializing git repository with an empty initial commit"
git init
git commit -m 'initial commit' --allow-empty
# Set up npm
echo "$(bold "Step 3"): Initializing npm"
npm init -y
jq '.type = "module"' package.json > temp.json && mv temp.json package.json
echo "$(bold "Step 4"): Setting up GitHub actions"
mkdir -p .github/workflows
touch .github/workflows/ci.yml
echo '
name: CI build
on: [push]
jobs:
Build-And-Test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [22]
name: Node ${{ matrix.node }} sample
steps:
- uses: actions/checkout@v3
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm test
- run: npm run test:coverage
- run: npm run lint
- run: npm run check-format
- run: npm run build
' > .github/workflows/ci.yml
echo "$(bold "Step 5"): Writing eslint config"
echo '
import pluginJs from "@eslint/js";
import comments from "@eslint-community/eslint-plugin-eslint-comments/configs";
import simpleImportSort from "eslint-plugin-simple-import-sort";
import globals from "globals";
import tseslint from "typescript-eslint";
/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.{ts,tsx}"]},
{
ignores: ["coverage/**"],
},
{files: ["**/*.ts"], languageOptions: {sourceType: "script"}},
{languageOptions: { globals: globals.node }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
comments.recommended,
{
rules: {
"@eslint-community/eslint-comments/no-unused-disable": "error"
}
},
{
plugins: {
"simple-import-sort": simpleImportSort,
},
rules: {
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error",
},
},
{
rules: {
"no-console": "error",
"no-duplicate-imports": "error",
"max-depth": ["error", 2],
"max-nested-callbacks": ["error", 2],
"max-lines-per-function": ["error", 56],
"max-statements": ["error", 22],
"max-params": ["error", 3],
}
}
];
' > eslint.config.mjs
# Install TypeScript, ESLint, and Prettier
echo "$(bold "Step 6"): Installing dev dependencies"
npm install --save-dev typescript eslint prettier jest ts-jest @types/jest typescript-eslint @eslint-community/eslint-plugin-eslint-comments @eslint/js globals eslint-plugin-simple-import-sort
# Initialize TypeScript and Prettier
echo "$(bold "Step 7"): Initializing TypeScript"
npx tsc --init --outDir './dist'
echo '{
"compilerOptions": {
"target": "es2016",
"module": "node16",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"ts-node": {
"experimentalSpecifierResolution": "node",
"transpileOnly": true,
"esm": true
}
}' > tsconfig.json
echo "Step 8: Initializing Prettier"
npx prettier --write .
echo 'coverage
dist' > .prettierignore
echo "$(bold "Step 8"): Adding .gitignore"
echo '.env
node_modules
' > .gitignore
echo "$(bold "Step 9"): Adding Jest config"
echo '/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/dist/"],
};
' > jest.config.cjs
# Add useful commands to package.json
echo "$(bold "Step 10"): Adding scripts to package.json"
if ! command -v jq >/dev/null 2>&1; then
echo "jq is not installed on your system. try installing it to fully automate this bootstrapping"
echo "$(bold "You are almost done!") You can now add the following scripts section to your package.json:"
echo '
"scripts": {
"build": "tsc",
"test": "jest",
"test:coverage": "jest --coverage",
"check-format": "prettier --check .",
"fix-format": "prettier --write .",
"lint": "eslint .",
"fix-lint": "eslint . --fix",
"fix": "npm run fix-format && npm run fix-lint"
},
'
exit 1
fi
jq '.scripts.build = "tsc ."' package.json > temp.json && mv temp.json package.json
jq '.scripts.test = "jest"' package.json > temp.json && mv temp.json package.json
jq '.scripts."test:coverage" = "jest --coverage"' package.json > temp.json && mv temp.json package.json
jq '.scripts."check-format" = "prettier --check ."' package.json > temp.json && mv temp.json package.json
jq '.scripts."fix-format" = "prettier --write ."' package.json > temp.json && mv temp.json package.json
jq '.scripts.lint = "eslint ."' package.json > temp.json && mv temp.json package.json
jq '.scripts."fix-lint" = "eslint . --fix"' package.json > temp.json && mv temp.json package.json
jq '.scripts.fix = "npm run fix-format && npm run fix-lint"' package.json > temp.json && mv temp.json package.json