cli-changescribe 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/init.js ADDED
@@ -0,0 +1,61 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const SCRIPT_MAP = {
5
+ commit: 'changescribe commit',
6
+ 'pr:summary': 'changescribe pr:summary',
7
+ 'feature:pr': 'changescribe feature:pr',
8
+ 'staging:pr': 'changescribe staging:pr',
9
+ };
10
+
11
+ function readPackageJson(packagePath) {
12
+ try {
13
+ const raw = fs.readFileSync(packagePath, 'utf8');
14
+ return JSON.parse(raw);
15
+ } catch (error) {
16
+ throw new Error(`Failed to read ${packagePath}: ${error.message}`);
17
+ }
18
+ }
19
+
20
+ function writePackageJson(packagePath, data) {
21
+ const contents = `${JSON.stringify(data, null, 2)}\n`;
22
+ fs.writeFileSync(packagePath, contents, 'utf8');
23
+ }
24
+
25
+ function ensureScripts(pkg) {
26
+ const scripts = pkg.scripts ?? {};
27
+ const added = [];
28
+ for (const [name, command] of Object.entries(SCRIPT_MAP)) {
29
+ if (!scripts[name]) {
30
+ scripts[name] = command;
31
+ added.push(name);
32
+ }
33
+ }
34
+ pkg.scripts = scripts;
35
+ return added;
36
+ }
37
+
38
+ function runInit(cwd = process.cwd()) {
39
+ const packagePath = path.join(cwd, 'package.json');
40
+ if (!fs.existsSync(packagePath)) {
41
+ console.error('❌ No package.json found in the current directory.');
42
+ process.exit(1);
43
+ }
44
+
45
+ const pkg = readPackageJson(packagePath);
46
+ const added = ensureScripts(pkg);
47
+ writePackageJson(packagePath, pkg);
48
+
49
+ if (added.length === 0) {
50
+ console.log('✅ Scripts already present; no changes made.');
51
+ return;
52
+ }
53
+
54
+ console.log(`✅ Added npm scripts: ${added.join(', ')}`);
55
+ }
56
+
57
+ if (require.main === module) {
58
+ runInit();
59
+ }
60
+
61
+ module.exports = { runInit };