forked from paralleldrive/aidd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.js
More file actions
executable file
Β·184 lines (159 loc) Β· 5.14 KB
/
release.js
File metadata and controls
executable file
Β·184 lines (159 loc) Β· 5.14 KB
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
#!/usr/bin/env node
import { execSync } from "child_process";
import process from "process";
import { errorCauses, createError } from "error-causes";
// Configuration objects (camelCase per javascript.mdc)
const semverTypes = ["major", "minor", "patch"];
const bumpAliases = {
breaking: "major",
feature: "minor",
fix: "patch",
major: "major",
minor: "minor",
patch: "patch",
};
const defaultBump = "minor";
const allowedBranches = ["main", "master"];
const config = {
validBumpTypes: Object.keys(bumpAliases),
};
// Error causes definition using error-causes library
const [releaseErrors, handleReleaseErrors] = errorCauses({
ValidationError: {
code: "VALIDATION_ERROR",
message: "Input validation failed",
},
GitError: {
code: "GIT_ERROR",
message: "Git operation failed",
},
ReleaseItError: {
code: "RELEASE_IT_ERROR",
message: "release-it command failed",
},
});
const { ValidationError, GitError, ReleaseItError } = releaseErrors;
// Pure utility functions (explicit parameter defaults per javascript.mdc)
const parseBumpType = ({
argv = process.argv,
defaultType = defaultBump,
} = {}) => argv.slice(2)[0] || defaultType;
const validateBumpType = (bumpType) => {
if (!bumpAliases[bumpType]) {
throw createError({
...ValidationError,
message: `Invalid bump type: ${bumpType}. Valid options: ${config.validBumpTypes.join(", ")}`,
});
}
return bumpAliases[bumpType];
};
const getCurrentBranch = () => {
try {
const branch = execSync("git branch --show-current", {
encoding: "utf8",
}).trim();
return branch;
} catch (originalError) {
throw createError({
...GitError,
message: "Failed to get current git branch",
cause: originalError,
});
}
};
const validateBranch = (branch) => {
const allowed = allowedBranches.includes(branch);
if (!allowed) {
throw createError({
...ValidationError,
message: `Not on allowed branch. Current: ${branch}, Allowed: ${allowedBranches.join(", ")}`,
});
}
return { branch, valid: true };
};
const runReleaseIt = (semverType) => {
try {
console.log(`π Starting release with release-it (${semverType})...`);
execSync(`npx release-it ${semverType} --ci`, {
stdio: "inherit",
env: { ...process.env },
});
console.log("π Release completed successfully!");
} catch (originalError) {
throw createError({
...ReleaseItError,
message: "release-it command failed",
cause: originalError,
});
}
};
// Use error-causes handleErrors pattern
const handleError = handleReleaseErrors({
ValidationError: ({ name, code, message, cause }) => {
console.error(`β Validation failed: ${message}`);
console.error("π‘ Fix the issue and try again.");
if (cause) console.error(`π Root cause: ${cause.message || cause}`);
process.exit(1);
},
GitError: ({ name, code, message, cause }) => {
console.error(`β Git command failed: ${message}`);
console.error("π‘ Check your git configuration and network connection.");
if (cause?.command) console.error(`π Failed command: ${cause.command}`);
if (cause?.message) console.error(`π Root cause: ${cause.message}`);
process.exit(1);
},
ReleaseItError: ({ name, code, message, cause }) => {
console.error(`β release-it failed: ${message}`);
console.error("π‘ Check the release-it output above for details.");
if (cause?.message) console.error(`π Root cause: ${cause.message}`);
process.exit(1);
},
});
// Simplified release flow using release-it
const createRelease = ({ argv, defaultType }) => {
const bumpType = parseBumpType({ argv, defaultType });
const semverType = validateBumpType(bumpType);
// Basic validation pipeline
const currentBranch = getCurrentBranch();
validateBranch(currentBranch);
console.log(
`π― Preparing ${bumpType} (${semverType}) release on branch ${currentBranch}...`,
);
// Use release-it to handle the complete release workflow
runReleaseIt(semverType);
};
// Main function - now concise with explicit parameter defaults
function main({ argv = process.argv, defaultType = defaultBump } = {}) {
try {
createRelease({ argv, defaultType });
} catch (error) {
// Enhanced error handling with specific error types
handleError(error);
}
}
// Show usage if --help is provided
if (process.argv.includes("--help") || process.argv.includes("-h")) {
console.log(`
π Release Script (powered by release-it)
Usage: npm run release [<bump-type>]
Bump types:
major, breaking - Breaking changes (1.0.0 -> 2.0.0)
minor, feature - New features (1.0.0 -> 1.1.0) [default]
patch, fix - Bug fixes (1.0.0 -> 1.0.1)
Examples:
npm run release # minor bump (default)
npm run release breaking # major bump
npm run release feature # minor bump
npm run release fix # patch bump
This script uses release-it to:
1. Run tests before release
2. Bump the version in package.json
3. Commit the version change
4. Create a git tag (v*.*.*)
5. Push commits and tags to origin
6. Create GitHub release with auto-generated changelog
7. Publish to npm registry
`);
process.exit(0);
}
main();