@automattic/jetpack-cli 0.1.0-beta.1

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.
Files changed (2) hide show
  1. package/bin/jp.js +150 -0
  2. package/package.json +19 -0
package/bin/jp.js ADDED
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { spawnSync } from 'child_process';
4
+ import fs from 'fs';
5
+ import { dirname, resolve } from 'path';
6
+ import process from 'process';
7
+ import chalk from 'chalk';
8
+ import prompts from 'prompts';
9
+
10
+ /**
11
+ * Check if a directory is the monorepo root.
12
+ *
13
+ * @param {string} dir - Directory to check
14
+ * @return {boolean} True if this is the monorepo root
15
+ */
16
+ const isMonorepoRoot = dir => {
17
+ try {
18
+ return fs.existsSync( resolve( dir, 'tools/docker/bin/monorepo' ) );
19
+ } catch {
20
+ return false;
21
+ }
22
+ };
23
+
24
+ /**
25
+ * Find monorepo root from a starting directory.
26
+ *
27
+ * @param {string} startDir - Directory to start searching from
28
+ * @return {string|null} Path to monorepo root, or null if not found
29
+ */
30
+ const findMonorepoRoot = startDir => {
31
+ let dir = startDir;
32
+ while ( dir !== '/' ) {
33
+ if ( isMonorepoRoot( dir ) ) {
34
+ return dir;
35
+ }
36
+ dir = dirname( dir );
37
+ }
38
+ return null;
39
+ };
40
+
41
+ /**
42
+ * Clone the monorepo.
43
+ *
44
+ * @param {string} targetDir - Directory to clone into
45
+ * @throws {Error} If clone fails
46
+ */
47
+ const cloneMonorepo = async targetDir => {
48
+ // eslint-disable-next-line no-console
49
+ console.log( chalk.blue( 'Cloning Jetpack monorepo...' ) );
50
+ const result = spawnSync(
51
+ 'git',
52
+ [ 'clone', 'https://github.com/Automattic/jetpack.git', targetDir ],
53
+ { stdio: 'inherit' }
54
+ );
55
+
56
+ if ( result.status !== 0 ) {
57
+ throw new Error( 'Failed to clone repository' );
58
+ }
59
+ };
60
+
61
+ /**
62
+ * Initialize a new Jetpack development environment.
63
+ *
64
+ * @throws {Error} If initialization fails
65
+ */
66
+ const initJetpack = async () => {
67
+ const response = await prompts( {
68
+ type: 'text',
69
+ name: 'directory',
70
+ message: 'Where would you like to clone the Jetpack monorepo?',
71
+ initial: './jetpack',
72
+ } );
73
+
74
+ if ( ! response.directory ) {
75
+ throw new Error( 'Setup cancelled' );
76
+ }
77
+
78
+ const targetDir = resolve( process.cwd(), response.directory );
79
+
80
+ if ( fs.existsSync( targetDir ) ) {
81
+ throw new Error( `Directory ${ targetDir } already exists` );
82
+ }
83
+
84
+ try {
85
+ await cloneMonorepo( targetDir );
86
+ // eslint-disable-next-line no-console
87
+ console.log( chalk.green( '\nJetpack monorepo has been cloned successfully!' ) );
88
+ // eslint-disable-next-line no-console
89
+ console.log( '\nNext steps:' );
90
+ // eslint-disable-next-line no-console
91
+ console.log( '1. cd', response.directory );
92
+ // eslint-disable-next-line no-console
93
+ console.log( '2. jp docker up' );
94
+ // eslint-disable-next-line no-console
95
+ console.log( '3. jp docker install' );
96
+ } catch ( error ) {
97
+ throw new Error( `Failed to initialize Jetpack: ${ error.message }` );
98
+ }
99
+ };
100
+
101
+ // Main execution
102
+ const main = async () => {
103
+ try {
104
+ const args = process.argv.slice( 2 );
105
+
106
+ // Handle 'init' command specially
107
+ if ( args[ 0 ] === 'init' ) {
108
+ await initJetpack();
109
+ return;
110
+ }
111
+
112
+ // Try to find monorepo root from current directory
113
+ const monorepoRoot = findMonorepoRoot( process.cwd() );
114
+
115
+ if ( ! monorepoRoot ) {
116
+ // eslint-disable-next-line no-console
117
+ console.error( chalk.red( 'Could not find Jetpack monorepo.' ) );
118
+ // eslint-disable-next-line no-console
119
+ console.log( '\nTo get started:' );
120
+ // eslint-disable-next-line no-console
121
+ console.log( '1. Run', chalk.blue( 'jp init' ), 'to clone the repository' );
122
+ // eslint-disable-next-line no-console
123
+ console.log( ' OR' );
124
+ // eslint-disable-next-line no-console
125
+ console.log( '2. Navigate to an existing Jetpack monorepo directory' );
126
+ throw new Error( 'Monorepo not found' );
127
+ }
128
+
129
+ // Run the monorepo script with the original arguments
130
+ const result = spawnSync(
131
+ resolve( monorepoRoot, 'tools/docker/bin/monorepo' ),
132
+ [ 'pnpm', 'jetpack', ...args ],
133
+ {
134
+ stdio: 'inherit',
135
+ shell: true,
136
+ cwd: monorepoRoot, // Ensure we're in the monorepo root when running commands
137
+ }
138
+ );
139
+
140
+ if ( result.status !== 0 ) {
141
+ throw new Error( `Command failed with status ${ result.status }` );
142
+ }
143
+ } catch ( error ) {
144
+ // eslint-disable-next-line no-console
145
+ console.error( chalk.red( error.message ) );
146
+ process.exitCode = 1;
147
+ }
148
+ };
149
+
150
+ main();
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@automattic/jetpack-cli",
3
+ "version": "0.1.0-beta.1",
4
+ "description": "Docker-based CLI for Jetpack development",
5
+ "bin": {
6
+ "jp": "bin/jp.js"
7
+ },
8
+ "files": [
9
+ "bin"
10
+ ],
11
+ "type": "module",
12
+ "dependencies": {
13
+ "chalk": "^4.1.2",
14
+ "prompts": "^2.4.2"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ }
19
+ }