@caweb/cli 1.0.5 → 1.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.
@@ -1,20 +1,22 @@
1
1
  /**
2
- * Modified from wp-env 8.11.0
2
+ * Modified from wp-env
3
3
  * Few modifications made:
4
4
  * - Downloads CAWebPublishing Resources, excluding WordPress.
5
- * - Target directory for downloadZipSource is expected to be absolute.
6
5
  * - Ensure parent directory of source.path exists before attempting to extract in downloadZipSource, otherwise it will complain that file/directory doesn't exist.
7
6
  * @see @wordpress/env/lib/download-sources.js
8
7
  */
9
- 'use strict';
8
+
10
9
  /**
11
10
  * External dependencies
12
11
  */
13
- const util = require( 'util' );
14
- const path = require( 'path' );
15
- const fs = require( 'fs-extra' );
16
- const SimpleGit = require( 'simple-git' );
17
- const got = require( 'got' );
12
+ import util from 'util';
13
+ import path from 'path';
14
+ import fs from 'fs-extra';
15
+ import SimpleGit from 'simple-git';
16
+ import got from 'got';
17
+ import stream from 'stream';
18
+ import zip from 'extract-zip';
19
+ import rim from 'rimraf';
18
20
 
19
21
  /**
20
22
  * Internal dependencies
@@ -23,9 +25,9 @@ const got = require( 'got' );
23
25
  /**
24
26
  * Promisified dependencies
25
27
  */
26
- const pipeline = util.promisify( require( 'stream' ).pipeline );
27
- const extractZip = util.promisify( require( 'extract-zip' ) );
28
- const rimraf = util.promisify( require( 'rimraf' ) );
28
+ const pipeline = util.promisify( stream.pipeline );
29
+ const extractZip = util.promisify( zip );
30
+ const rimraf = util.promisify( rim );
29
31
 
30
32
  /**
31
33
  * Download CAWeb Resources.
@@ -120,7 +122,6 @@ async function downloadSource(src, { onProgress, spinner, debug } ){
120
122
  }
121
123
  }
122
124
 
123
-
124
125
  /**
125
126
  * Clones the git repository at `source.url` into `source.path`. If the
126
127
  * repository already exists, it is updated instead.
@@ -271,7 +272,6 @@ function dowloadPlugins(pluginDir){
271
272
  ]
272
273
  }
273
274
 
274
- module.exports = {
275
+ export {
275
276
  downloadSources
276
-
277
277
  }
package/lib/env.js CHANGED
@@ -1,12 +1,17 @@
1
- 'use strict';
2
1
  /**
3
2
  * Internal dependencies
4
3
  */
4
+ import start from './start.js';
5
+ import destroy from './destroy.js';
6
+ import test from './test.js';
7
+ import shell from './shell.js';
5
8
 
6
- //const { ValidationError } = require( './config' );
7
- //const { LifecycleScriptError } = require( './execute-lifecycle-script' );
8
- const commands = require( './commands' );
9
+ import updatePlugins from './tasks/update-plugins.js'
9
10
 
10
- module.exports = {
11
- ...commands
12
- };
11
+ export {
12
+ start,
13
+ destroy,
14
+ test,
15
+ shell,
16
+ updatePlugins
17
+ }
package/lib/options.js CHANGED
@@ -253,7 +253,7 @@ const DIVI_OPTIONS = {
253
253
  }
254
254
  }
255
255
 
256
- module.exports = {
256
+ export {
257
257
  CAWEB_OPTIONS,
258
258
  DIVI_OPTIONS
259
259
  }
package/lib/spinner.js ADDED
@@ -0,0 +1,70 @@
1
+ import chalk from 'chalk';
2
+ import ora from 'ora';
3
+
4
+ // Colors.
5
+ const boldWhite = chalk.bold.white;
6
+ const wpPrimary = boldWhite.bgHex( '#00669b' );
7
+ const wpGreen = boldWhite.bgHex( '#4ab866' );
8
+ const wpRed = boldWhite.bgHex( '#d94f4f' );
9
+ const wpYellow = boldWhite.bgHex( '#f0b849' );
10
+
11
+ // Spinner.
12
+ const withSpinner =
13
+ ( command ) =>
14
+ ( ...args ) => {
15
+ const spinner = ora().start();
16
+
17
+ // commander passes arguments/options differently than yargs.
18
+ // lets combine arguments with options
19
+ const cmd = args.pop();
20
+
21
+ cmd.registeredArguments.forEach(arg => {
22
+ // get arg from list.
23
+ let v = args.shift();
24
+
25
+ // add arg to options.
26
+ args[args.length - 1][arg._name] = v;
27
+ });
28
+
29
+ // add any global options.
30
+ args[0] = {
31
+ ...args[0],
32
+ ...cmd.parent.opts()
33
+ }
34
+
35
+ args[ 0 ].spinner = spinner;
36
+ let time = process.hrtime();
37
+ return command( ...args ).then(
38
+ ( message ) => {
39
+ time = process.hrtime( time );
40
+ spinner.succeed(
41
+ `${ message || spinner.text } (in ${ time[ 0 ] }s ${ (
42
+ time[ 1 ] / 1e6
43
+ ).toFixed( 0 ) }ms)`
44
+ );
45
+ process.exit( 0 );
46
+ },
47
+ ( error ) => {
48
+ if( error ){
49
+ // Error is an unknown error. That means there was a bug in our code.
50
+ spinner.fail(
51
+ typeof error === 'string' ? error : error.message
52
+ );
53
+ // Disable reason: Using console.error() means we get a stack trace.
54
+ console.error( error );
55
+ process.exit( 1 );
56
+ }else{
57
+ spinner.fail( 'An unknown error occurred.' );
58
+ process.exit( 1 );
59
+ }
60
+ }
61
+ );
62
+ };
63
+
64
+ export {
65
+ wpPrimary,
66
+ wpGreen,
67
+ wpRed,
68
+ wpYellow,
69
+ withSpinner
70
+ }
package/lib/wordpress.js CHANGED
@@ -1,57 +1,71 @@
1
- 'use strict';
2
- const { writeFile } = require('fs-extra');
3
- const path = require( 'path' );
4
- const { execSync } = require( 'child_process' );
5
-
6
1
  /**
7
2
  * External dependencies
8
3
  */
4
+ import fs from 'fs-extra';
5
+ import path from 'node:path';
9
6
 
10
7
  /**
11
8
  * Internal dependencies
12
9
  */
13
- const {runDockerCmds} = require('./docker');
10
+ import {runCLICmds} from './docker.js';
11
+
12
+ /**
13
+ * Promisified dependencies
14
+ */
15
+ const { writeFile } = fs.promises;
14
16
 
15
17
  /**
16
18
  * Checks whether WordPress environment is a multisite installation.
17
19
  *
18
20
  * @param {string} environment Which environment to check for multisite installation.
19
21
  * @param {WPConfig} config The wp-env config object.
22
+ * @param {Object} spinner A CLI spinner which indicates progress.
20
23
  * @returns
21
24
  */
22
- async function isMultisite( environment, config ){
23
- return await runDockerCmds(
25
+ async function isMultisite( environment, config, spinner ){
26
+
27
+ const result = await runCLICmds(
24
28
  environment,
25
29
  [ 'wp config get MULTISITE' ],
26
- config
30
+ config,
31
+ spinner
27
32
  )
33
+
34
+ /**
35
+ * If the constant doesn't exist the wp cli returns an exit code of 1
36
+ * we have to return false, otherwise we can just return the cli result.
37
+ */
38
+ return typeof result === 'object' && result.exitCode ? false : result;
28
39
  }
29
40
 
30
41
  /**
31
42
  * Transforms an existing single-site installation into a multisite installation.
32
43
  *
33
- * @param {string} environment Which environment to convert into a multisite installation.
34
- * @param {WPConfig} config The wp-env config object.
35
- * @param {boolean} subdomain True if converting to multisite subdomain.
44
+ * @param {string} environment Which environment to convert into a multisite installation.
45
+ * @param {WPConfig} config The wp-env config object.
46
+ * @param {boolean} subdomain True if converting to multisite subdomain.
47
+ * @param {Object} spinner A CLI spinner which indicates progress.
36
48
  * @returns
37
49
  */
38
- async function convertToMultisite( environment, config, subdomain ){
50
+ async function convertToMultisite( environment, config, subdomain, spinner ){
39
51
  // before we can conver to multisite all plugins must be deactivated.
40
52
  // first lets get all plugins
41
- let activePlugins = await runDockerCmds(
53
+ let activePlugins = await runCLICmds(
42
54
  environment,
43
55
  ['wp option get active_plugins --format=json'],
44
- config
56
+ config,
57
+ spinner
45
58
  )
46
59
 
47
60
  activePlugins = Object.values(JSON.parse( activePlugins ));
48
61
 
49
62
  // deactivate all active plugins.
50
63
  if( activePlugins.length ){
51
- await runDockerCmds(
64
+ await runCLICmds(
52
65
  environment,
53
66
  ['wp plugin deactivate ' + activePlugins.join(' ')],
54
- config
67
+ config,
68
+ spinner
55
69
  )
56
70
  }
57
71
 
@@ -62,10 +76,11 @@ async function convertToMultisite( environment, config, subdomain ){
62
76
  command += ' --subdomains'
63
77
  }
64
78
 
65
- await runDockerCmds(
79
+ await runCLICmds(
66
80
  environment,
67
81
  [command],
68
- config
82
+ config,
83
+ spinner
69
84
  )
70
85
 
71
86
  // generate .htaccess
@@ -73,10 +88,11 @@ async function convertToMultisite( environment, config, subdomain ){
73
88
 
74
89
  // network activate all active plugins again.
75
90
  if( activePlugins.length ){
76
- await runDockerCmds(
91
+ await runCLICmds(
77
92
  environment,
78
93
  ['wp plugin activate ' + activePlugins.join(' ') + ' --network'],
79
- config
94
+ config,
95
+ spinner
80
96
  )
81
97
  }
82
98
 
@@ -85,8 +101,8 @@ async function convertToMultisite( environment, config, subdomain ){
85
101
  /**
86
102
  * Generates .htaccess file content.
87
103
  *
88
- * @param {string} environment Which environment to generate .htaccess for.
89
- * @param {boolean} subdomain True if converting to multisite subdomain.
104
+ * @param {string} environment Which environment to generate .htaccess for.
105
+ * @param {boolean} subdomain True if converting to multisite subdomain.
90
106
  */
91
107
  async function generateHTAccess(environment, workDirectoryPath, subdomain){
92
108
  let trailingSlash = subdomain ? '^wp-admin$ wp-admin/ [R=301,L]' : '^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/';
@@ -115,6 +131,7 @@ async function generateHTAccess(environment, workDirectoryPath, subdomain){
115
131
  await writeFile(path.join(workDirectoryPath, folder, '.htaccess'), htaccess);
116
132
 
117
133
  }
134
+
118
135
  /**
119
136
  * Configures WordPress for the given environment by installing WordPress,
120
137
  * activating all plugins, and activating the first theme. These steps are
@@ -137,20 +154,21 @@ async function configureWordPress(environment, config, spinner, multisite, subdo
137
154
  if( multisite ){
138
155
  spinner.text = 'Converting to multisite ' + ( subdomain ? 'subdomain' : 'subdirectory') + '...'
139
156
 
140
- await convertToMultisite( environment, config, subdomain )
157
+ await convertToMultisite( environment, config, subdomain, spinner )
141
158
  }
142
159
 
143
160
  // rewrite and flush permalink structure.
144
- await runDockerCmds(
161
+ await runCLICmds(
145
162
  environment,
146
163
  [
147
164
  `wp rewrite structure ${WP_PERMALINK} --hard`
148
165
  ],
149
- config
166
+ config,
167
+ spinner
150
168
  );
151
169
  }
152
170
 
153
- module.exports = {
171
+ export {
154
172
  configureWordPress,
155
173
  isMultisite,
156
174
  convertToMultisite,
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@caweb/cli",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "CAWebPublishing Command Line Interface.",
5
- "main": "lib/env.js",
5
+ "exports": "./lib/env.js",
6
+ "type": "module",
7
+ "node": ">=16",
6
8
  "files": [
7
9
  "bin",
8
10
  "lib"
@@ -16,6 +18,7 @@
16
18
  },
17
19
  "scripts": {
18
20
  "caweb": "caweb",
21
+ "wp-env": "wp-env",
19
22
  "local": "npm pack && npm i caweb-cli-%npm_package_version%.tgz",
20
23
  "test": "echo \"Error: run tests from root\" && exit 0"
21
24
  },
@@ -39,6 +42,7 @@
39
42
  "dependencies": {
40
43
  "@wordpress/env": "^9.0.0",
41
44
  "chalk": "^4.0.0",
45
+ "commander": "^11.1.0",
42
46
  "fs-extra": "^11.1.1"
43
47
  },
44
48
  "config": {
@@ -56,7 +60,8 @@
56
60
  "ADMIN_COOKIE_PATH": "/",
57
61
  "COOKIE_DOMAIN": "",
58
62
  "COOKIEPATH": "",
59
- "SITECOOKIEPATH": ""
63
+ "SITECOOKIEPATH": "",
64
+ "CONCATENATE_SCRIPTS": false
60
65
  }
61
66
  }
62
67
  }
@@ -1,13 +0,0 @@
1
- 'use strict';
2
- /**
3
- * External dependencies
4
- */
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- const updatePlugins = require('./update-plugins')
10
-
11
- module.exports = {
12
- updatePlugins,
13
- };