chisel-scripts 2.0.0-alpha.3.0 → 2.0.0-alpha.4

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/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  <!-- INSERT-NEW-ENTRIES-HERE -->
4
4
 
5
+ ## 2.0.0-alpha.4 (2024-11-12)
6
+
7
+ - V2: Add Devcontainers (#538) ([b878421](https://github.com/xfiveco/generator-chisel/commit/b878421)), closes [#538](https://github.com/xfiveco/generator-chisel/issues/538)
8
+ - composer, wp-config, plugins list updates ([642f983](https://github.com/xfiveco/generator-chisel/commit/642f983))
9
+
10
+ ## 2.0.0-alpha.3.1 (2024-10-31)
11
+
12
+ - add assets index ([fed4fd3](https://github.com/xfiveco/generator-chisel/commit/fed4fd3))
13
+
5
14
  ## 2.0.0-alpha.3.0 (2024-10-07)
6
15
 
7
16
  - update chisel-scripts version ([2957e4c](https://github.com/xfiveco/generator-chisel/commit/2957e4c))
package/composer.phar CHANGED
Binary file
package/index.js CHANGED
@@ -37,6 +37,29 @@ function adjustWebpackConfig(baseConfig, directory) {
37
37
  ...entriesFromFiles(stylesFiles),
38
38
  };
39
39
 
40
+ const getUrl = (() => {
41
+ let url;
42
+
43
+ return () =>
44
+ (url ||= (() => {
45
+ const { execFileSync } = require('node:child_process');
46
+ try {
47
+ const stdout = execFileSync(
48
+ 'npm',
49
+ ['run', '--silent', 'wp', 'option', 'get', 'home'],
50
+ {
51
+ cwd: directory,
52
+ encoding: 'utf8',
53
+ shell: true,
54
+ },
55
+ );
56
+ return stdout.trim();
57
+ } catch (e) {
58
+ throw new Error('Failed to get current website url', { cause: e });
59
+ }
60
+ })());
61
+ })();
62
+
40
63
  const updatedConfig = {
41
64
  ...baseConfig,
42
65
  entry,
@@ -49,7 +72,33 @@ function adjustWebpackConfig(baseConfig, directory) {
49
72
  },
50
73
  devServer: baseConfig.devServer && {
51
74
  ...baseConfig.devServer,
52
- allowedHosts: [new URL(packageJson.chisel.url).host],
75
+ allowedHosts: [new URL(getUrl()).host],
76
+ ...(process.env.CHISEL_PORT && {
77
+ host: '0.0.0.0',
78
+ port: Number(process.env.CHISEL_PORT) + 1,
79
+ ...(() => {
80
+ const webSocketURL = new URL(getUrl());
81
+ webSocketURL.protocol = webSocketURL.protocol.replace('http', 'ws');
82
+ webSocketURL.pathname = '/ws';
83
+ const webSocketURLString = webSocketURL.toString();
84
+
85
+ if (!webSocketURLString.includes(process.env.CHISEL_PORT)) {
86
+ return;
87
+ }
88
+
89
+ return {
90
+ client: {
91
+ ...baseConfig.devServer.client,
92
+ webSocketURL: webSocketURL
93
+ .toString()
94
+ .replace(
95
+ process.env.CHISEL_PORT,
96
+ Number(process.env.CHISEL_PORT) + 1,
97
+ ),
98
+ },
99
+ };
100
+ })(),
101
+ }),
53
102
  },
54
103
  optimization: {
55
104
  ...baseConfig.optimization,
@@ -1,11 +1,21 @@
1
+ const devContainerDefaultDatabaseCredentials = {
2
+ databaseHost: 'db',
3
+ databasePort: 3306,
4
+ databaseName: 'mariadb',
5
+ databaseUser: 'mariadb',
6
+ databasePassword: 'mariadb',
7
+ };
8
+
1
9
  module.exports = (api, options) => {
2
10
  api.registerCommand(
3
11
  'wp-config',
4
12
  (command) =>
5
- command.description(
6
- 'configure WP (writes wp/wp-config-local.php an dev-vhost.conf)',
7
- ),
8
- async () => {
13
+ command
14
+ .description(
15
+ 'configure WP (writes wp/wp-config-local.php an dev-vhost.conf)',
16
+ )
17
+ .option('--devcontainer', 'use default database credentials', false),
18
+ async ({ devcontainer }) => {
9
19
  const { runLocal, copy } = require('chisel-shared-utils');
10
20
  const wp = (args, opts) =>
11
21
  runLocal(['chisel-scripts', 'wp', ...args], {
@@ -45,7 +55,9 @@ module.exports = (api, options) => {
45
55
  ];
46
56
 
47
57
  const promptAndCreateDB = async () => {
48
- const answers = await inquirer.prompt(prompts);
58
+ const answers = devcontainer
59
+ ? devContainerDefaultDatabaseCredentials
60
+ : await inquirer.prompt(prompts);
49
61
 
50
62
  answers.databaseHostPort = `${answers.databaseHost}:${answers.databasePort}`;
51
63
 
@@ -54,15 +66,17 @@ module.exports = (api, options) => {
54
66
  console.log('Creating database...');
55
67
  console.log(api.resolve());
56
68
 
69
+ const templateData = {
70
+ ...answers,
71
+ documentRoot: api.resolveRoot(),
72
+ tablePrefix,
73
+ codespaces: process.env.CODESPACES === 'true',
74
+ };
75
+
57
76
  await copy({
58
77
  from: path.join(__dirname, '../template'),
59
78
  to: api.resolveRoot(),
60
- templateData: {
61
- ...answers,
62
- documentRoot: api.resolveRoot(),
63
- serverName: new URL(url).hostname,
64
- tablePrefix,
65
- },
79
+ templateData,
66
80
  });
67
81
 
68
82
  const res = await wp(['db', 'query', 'SELECT 1'], {
@@ -81,7 +95,7 @@ module.exports = (api, options) => {
81
95
  console.log(res.stderr);
82
96
  throw res;
83
97
  }
84
- } else {
98
+ } else if (!devcontainer) {
85
99
  // exists
86
100
  const { useExisting } = await inquirer.prompt([
87
101
  {
@@ -99,16 +113,20 @@ module.exports = (api, options) => {
99
113
  }
100
114
  };
101
115
 
102
- // eslint-disable-next-line no-constant-condition
103
- while (true) {
104
- try {
105
- await promptAndCreateDB();
106
- break;
107
- } catch (e) {
108
- console.log(e); // TODO: remove
109
- console.log('');
110
- console.log('Trying again...');
111
- console.log('');
116
+ if (devcontainer) {
117
+ await promptAndCreateDB();
118
+ } else {
119
+ // eslint-disable-next-line no-constant-condition
120
+ while (true) {
121
+ try {
122
+ await promptAndCreateDB();
123
+ break;
124
+ } catch (e) {
125
+ console.log(e); // TODO: remove
126
+ console.log('');
127
+ console.log('Trying again...');
128
+ console.log('');
129
+ }
112
130
  }
113
131
  }
114
132
  },
@@ -0,0 +1,68 @@
1
+ import { relative, join } from 'path';
2
+ import { relative as relativePosix } from 'path/posix';
3
+ import fastGlob from 'fast-glob';
4
+ import fs from 'fs/promises';
5
+ import { createHash } from 'crypto';
6
+ import json2php from 'json2php';
7
+
8
+ const { convertPathToPattern } = fastGlob;
9
+
10
+ const hashesCache = {};
11
+
12
+ export const build = async (api, { fromWatch = false } = {}) => {
13
+ const assetsDir = api.resolve('assets');
14
+ const assetsDirPattern = convertPathToPattern(assetsDir);
15
+ const pattern = assetsDirPattern + '/**';
16
+ const files = (
17
+ await fastGlob(pattern, {
18
+ onlyFiles: true,
19
+ stats: true,
20
+ })
21
+ ).sort((a, b) => a.path.localeCompare(b.path));
22
+ const hashesFile = join(assetsDir, 'hashes.php');
23
+ const hashesFilePattern = convertPathToPattern(hashesFile);
24
+
25
+ const hashes = {};
26
+
27
+ for (const file of files) {
28
+ if (file.path === hashesFilePattern) continue;
29
+
30
+ const id = `${file.path}-${file.stats.size}-${file.stats.mtimeMs}`;
31
+ const key = convertPathToPattern(relative(assetsDirPattern, file.path));
32
+
33
+ if (fromWatch) {
34
+ if (hashesCache[id]) {
35
+ hashes[key] = hashesCache[id];
36
+ continue;
37
+ }
38
+ }
39
+
40
+ const content = await fs.readFile(file.path);
41
+ const hash = createHash('md5').update(content).digest('hex');
42
+ hashes[key] = hash;
43
+ hashesCache[id] = hash;
44
+ }
45
+
46
+ const phpArray = json2php.make({ indent: ' ', linebreak: '\n' })(hashes);
47
+ const content = `<?php return ${phpArray};\n`;
48
+ await fs.writeFile(hashesFile, content);
49
+ };
50
+
51
+ export const start = async (api) => {
52
+ const { default: chokidar } = await import('chokidar');
53
+
54
+ const assetsDir = api.resolve('assets');
55
+ const hashesFile = join(assetsDir, 'hashes.php');
56
+ const pattern = convertPathToPattern(assetsDir);
57
+
58
+ const watcher = chokidar.watch(pattern, {
59
+ ignoreInitial: true,
60
+ awaitWriteFinish: true,
61
+ });
62
+ watcher.on('add', () => build(api, { fromWatch: true }));
63
+ watcher.on('unlink', () => build(api, { fromWatch: true }));
64
+ watcher.on('change', (file) => {
65
+ if (file === hashesFile) return;
66
+ build(api, { fromWatch: true });
67
+ });
68
+ };
@@ -1,6 +1,6 @@
1
1
  <VirtualHost *:80>
2
2
  DocumentRoot "<%= documentRoot %>"
3
- ServerName <%= serverName %>
3
+ ServerName YOUR_LOCAL_WORDPRESS_DOMAIN
4
4
  <Directory "<%= documentRoot %>">
5
5
  AllowOverride All
6
6
  Require all granted
@@ -9,7 +9,11 @@
9
9
  * * External settings when needed
10
10
  *
11
11
  */
12
-
12
+ <% if (codespaces) { %>
13
+ // Adjustment for Codespaces
14
+ $_SERVER['HTTPS'] = 'on';
15
+ $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
16
+ <% } %>
13
17
  // ** MySQL settings - You can get this info from your web host ** //
14
18
  /** The name of the database for WordPress */
15
19
  define( 'DB_NAME', '<%= databaseName %>' );
@@ -47,9 +51,9 @@ $table_prefix = '<%= tablePrefix %>';
47
51
  define( 'WP_DEBUG', true );
48
52
  define( 'WP_DEBUG_LOG', true );
49
53
  define( 'WP_DEBUG_DISPLAY', true );
50
- define( 'SCRIPT_DEBUG', true );
51
54
 
52
55
  // Required for the theme fast refresh mode.
56
+ define( 'SCRIPT_DEBUG', true );
53
57
  define( 'WP_ENVIRONMENT_TYPE', 'development' );
54
58
 
55
59
  /** The Database Collate type. Don't change this if in doubt. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chisel-scripts",
3
- "version": "2.0.0-alpha.3.0",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "TODO",
5
5
  "bin": {
6
6
  "chisel-scripts": "bin/chisel-scripts.js"
@@ -25,17 +25,18 @@
25
25
  "url": "https://github.com/xfiveco/generator-chisel/issues"
26
26
  },
27
27
  "dependencies": {
28
- "chisel-shared-utils": "^2.0.0-alpha.0",
28
+ "chisel-shared-utils": "^2.0.0-alpha.1",
29
29
  "chokidar": "^3.6.0",
30
30
  "commander": "^5.1.0",
31
31
  "fast-glob": "^3.3.2",
32
32
  "fs-extra": "^9.0.1",
33
33
  "globby": "^11.0.1",
34
34
  "inquirer": "^7.1.0",
35
+ "json2php": "^0.0.9",
35
36
  "lodash": "^4.17.15"
36
37
  },
37
38
  "peerDependencies": {
38
39
  "@wordpress/scripts": "^27.9.0"
39
40
  },
40
- "gitHead": "ad109c10db57cab568c05a4b9057004c3323c271"
41
+ "gitHead": "3209b735b8259ad3296f477cfe13ad3a14d130a9"
41
42
  }