@caweb/cli 1.3.12 → 1.3.13

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.
@@ -11,9 +11,6 @@ import inquirer from 'inquirer';
11
11
  import {runCmd, projectPath} from '../../lib/index.js';
12
12
  import updateBlock from './update-block.js';
13
13
 
14
- const localFile = path.join(projectPath, 'package.json');
15
- const pkg = JSON.parse( fs.readFileSync(localFile) );
16
-
17
14
  /**
18
15
  * Get NPM Package Latest Version
19
16
  * @param {string} options.slug Block slug.
@@ -26,9 +23,10 @@ async function getNPMPackageVersion(pkg, spinner){
26
23
  'view',
27
24
  `${pkg}`,
28
25
  'version'
29
- ],
30
- spinner
31
- )
26
+ ]
27
+ ).then(({stdout, stderr}) => {
28
+ return ! stdout.toString() ? false : stdout.toString();
29
+ })
32
30
 
33
31
  }
34
32
 
@@ -45,6 +43,8 @@ export default async function createBlock({
45
43
  debug,
46
44
  slug
47
45
  } ) {
46
+ spinner.stop();
47
+
48
48
  // if block directory already exists.
49
49
  if( fs.existsSync(path.resolve(process.cwd(), slug)) ){
50
50
  spinner.info(`${slug} already exists.`)
@@ -75,7 +75,6 @@ export default async function createBlock({
75
75
  slug,
76
76
  '--template=' + path.join(projectPath, 'template', 'index.cjs')
77
77
  ],
78
- spinner,
79
78
  {
80
79
  stdio: 'inherit'
81
80
  }
@@ -93,7 +92,6 @@ export default async function createBlock({
93
92
  'install',
94
93
  `@cagov/${slug}@${version}`,
95
94
  ],
96
- spinner,
97
95
  {
98
96
  cwd: path.join(process.cwd(), slug ),
99
97
  stdio: 'inherit'
@@ -2,13 +2,27 @@
2
2
  * External dependencies
3
3
  */
4
4
  import path from 'path';
5
- import fs from 'fs';
5
+ import fs from 'fs-extra';
6
6
 
7
7
  /**
8
8
  * Internal dependencies
9
9
  */
10
+ import {runCmd} from '../../lib/index.js';
10
11
  import createBlock from './create-block.js';
11
12
 
13
+ /**
14
+ * Returns the WordPress Plugin Header.
15
+ *
16
+ * @param {String} content Content to search for plugin header.
17
+ * @returns String
18
+ */
19
+ function getPluginHeader( content ){
20
+ return content.substring(
21
+ content.indexOf('<?php') + '<?php'.length,
22
+ content.indexOf('*/') + '*/'.length
23
+ )
24
+ }
25
+
12
26
  /**
13
27
  * Update Block
14
28
  *
@@ -22,10 +36,10 @@ export default async function updateBlock({
22
36
  debug,
23
37
  slug
24
38
  } ) {
25
-
26
39
  // if block directory exists.
27
40
  if( fs.existsSync(path.resolve(process.cwd(), slug)) ){
28
- spinner.text = `Updating ${slug} block plugin.`;
41
+ console.log( `Updating ${slug} block plugin.` );
42
+ //spinner.text = `Updating ${slug} block plugin.`;
29
43
 
30
44
  // make tmp directory
31
45
  fs.ensureDir( `${slug}.tmp`);
@@ -36,6 +50,12 @@ export default async function updateBlock({
36
50
  // move src directory to tmp directory
37
51
  fs.copySync(`${slug}/src/`, `${slug}.tmp/src/`);
38
52
 
53
+ // move package.json to tmp directory
54
+ fs.copySync(`${slug}/package.json`, `${slug}.tmp/package.json`);
55
+
56
+ // move plugin main entry file to tmp directory
57
+ fs.copySync(`${slug}/${slug}.php`, `${slug}.tmp/${slug}.php`);
58
+
39
59
  // delete old block.
40
60
  fs.removeSync( `${slug}` );
41
61
 
@@ -48,10 +68,94 @@ export default async function updateBlock({
48
68
  // move src directory back to block directory
49
69
  fs.copySync(`${slug}.tmp/src/`, `${slug}/src/`);
50
70
 
71
+ // we get new and old package.json files
72
+ let oldPkg = JSON.parse( fs.readFileSync(path.join(`${slug}.tmp`, 'package.json')) )
73
+ let newPkg = JSON.parse( fs.readFileSync(path.join(slug, 'package.json')) )
74
+
75
+ /**
76
+ *
77
+ * @since 1.3.0 Gulp is no longer used.
78
+ */
79
+ if( oldPkg.version < '1.3.0' ){
80
+
81
+ // remove the old postbuild script.
82
+ if( oldPkg.scripts.postbuild && 'gulp build' === oldPkg.scripts.postbuild ){
83
+ delete oldPkg.scripts.postbuild;
84
+ }
85
+
86
+ // remove old gulp related packages
87
+ for(const i in oldPkg.devDependencies ){
88
+ if( [
89
+ 'del', 'fancy-log', 'gulp', 'gulp-cli', 'gulp-concat', 'gulp-file', 'gulp-line-ending-corrector',
90
+ 'gulp-sass', 'gulp-tap', 'gulp-uglify-es', 'sass'
91
+ ].includes( i ) ){
92
+ delete oldPkg.devDependencies[i]
93
+ }
94
+ }
95
+ }
96
+
97
+ // we only need the dependencies and devDependencies updated.
98
+ fs.writeFileSync(
99
+ path.join(slug, 'package.json'),
100
+ JSON.stringify( {
101
+ ...oldPkg,
102
+ dependencies: {
103
+ ...oldPkg.dependencies,
104
+ ...newPkg.dependencies,
105
+ },
106
+ devDependencies: {
107
+ ...oldPkg.devDependencies,
108
+ ...newPkg.devDependencies,
109
+ }
110
+ }, null, 4 )
111
+ );
112
+
113
+ let oldEntry = fs.readFileSync(path.join(`${slug}.tmp`, `${slug}.php`)).toString()
114
+ let oldHeader = getPluginHeader(oldEntry);
115
+ let newEntry = fs.readFileSync(path.join(slug, `${slug}.php`)).toString();
116
+ let newHeader = getPluginHeader(newEntry);
117
+
118
+ // we dont want to update the header case changes we made.
119
+ // we only update the plugin headers require fields.
120
+ let correctedHeader = oldHeader
121
+ .replace(/Requires at .*/, newHeader.match(/Requires at .*/) )
122
+ .replace(/Requires PHP.*/, newHeader.match(/Requires PHP.*/) )
123
+
124
+ // replace the header in the new entry with the corrected header and write the file back
125
+ fs.writeFileSync(
126
+ path.join(slug, `${slug}.php`),
127
+ newEntry.replace(newHeader, correctedHeader)
128
+ );
129
+
51
130
  // delete tmp directory.
52
131
  fs.removeSync( `${slug}.tmp` );
53
132
 
54
- spinner.succeed(`${slug} has been updated!`);
133
+ // install block npm packages
134
+ await runCmd(
135
+ 'npm',
136
+ [
137
+ 'install',
138
+ ],
139
+ {
140
+ cwd: path.join(process.cwd(), slug ),
141
+ stdio: 'inherit'
142
+ }
143
+ )
144
+
145
+ // build block
146
+ await runCmd(
147
+ 'npm',
148
+ [
149
+ 'run',
150
+ 'build',
151
+ ],
152
+ {
153
+ cwd: path.join(process.cwd(), slug ),
154
+ stdio: 'inherit'
155
+ }
156
+ )
157
+
158
+ spinner.text = `${slug} has been updated!`;
55
159
 
56
160
  }else{
57
161
  spinner.fail(`${slug} plugin directory not found.`)
package/commands/build.js CHANGED
@@ -68,6 +68,13 @@ export default async function build({
68
68
  await runCmd(
69
69
  'webpack',
70
70
  webPackArgs,
71
- )
71
+ ).then(({stdout, stderr}) => {
72
+ // if an error was thrown, and no output
73
+ if( stderr && ! stdout.toString() ){
74
+ console.log( stderr.toString() )
75
+ }else{
76
+ spinner.text = 'Done'
77
+ }
78
+ });
72
79
 
73
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@caweb/cli",
3
- "version": "1.3.12",
3
+ "version": "1.3.13",
4
4
  "description": "CAWebPublishing Command Line Interface.",
5
5
  "exports": "./lib/env.js",
6
6
  "type": "module",
@@ -40,7 +40,6 @@
40
40
  "config": {
41
41
  "WP_VER": "6.4.3",
42
42
  "PHP_VER": "8.1",
43
- "CREATE_BLOCK_VER": "4.32.0",
44
43
  "DEFAULTS": {
45
44
  "FS_METHOD": "direct",
46
45
  "WP_DEBUG": true,
@@ -57,6 +56,7 @@
57
56
  }
58
57
  },
59
58
  "dependencies": {
59
+ "@wordpress/create-block": "^4.32.0",
60
60
  "@wordpress/env": "^9.6.0",
61
61
  "@wordpress/scripts": "^27.5.0",
62
62
  "accessibility-checker": "^3.1.68",
@@ -68,6 +68,7 @@
68
68
  "cross-spawn": "^7.0.3",
69
69
  "css-loader": "^6.10.0",
70
70
  "docker-compose": "^0.24.7",
71
+ "fs-extra": "^11.2.0",
71
72
  "handlebars-loader": "^1.7.3",
72
73
  "html-to-json-parser": "^2.0.1",
73
74
  "html-webpack-plugin": "^5.6.0",
@@ -13,10 +13,12 @@ const blockSlugTitle = capitalCase( blockSlug );
13
13
  const customScripts = {};
14
14
 
15
15
  const npmDependencies = [
16
- '@wordpress/icons@9.22.0'
16
+ '@wordpress/icons@9.44.0'
17
17
  ];
18
18
 
19
- const npmDevDependencies = [];
19
+ const npmDevDependencies = [
20
+ '@wordpress/scripts@27.6.0'
21
+ ];
20
22
 
21
23
  // assetsPath: join( __dirname, 'assets' ),
22
24
  module.exports = {
@@ -26,7 +28,7 @@ module.exports = {
26
28
  pluginURI: `https://github.com/CAWebPublishing/${ blockSlug }`,
27
29
  plugin: true,
28
30
  description: `${ blockSlugTitle } Gutenberg Block`,
29
- version: '1.2.0',
31
+ version: '1.3.0',
30
32
  author: 'CAWebPublishing',
31
33
  license: 'GPL-2.0-or-later',
32
34
  licenseURI: 'https://www.gnu.org/licenses/gpl-2.0.html',