@caweb/cli 1.1.0 → 1.3.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/README.md +62 -27
- package/bin/caweb +1 -1
- package/bin/wp-cli.phar +0 -0
- package/commands/a11y.js +74 -0
- package/commands/blocks/create-block.js +109 -0
- package/commands/blocks/update-block.js +60 -0
- package/commands/build.js +73 -0
- package/commands/env/destroy.js +49 -0
- package/{lib/commands → commands/env}/start.js +30 -27
- package/{lib/commands → commands/env}/stop.js +4 -10
- package/{lib/commands → commands}/index.js +51 -39
- package/commands/serve.js +78 -0
- package/commands/sync.js +498 -0
- package/{lib/commands → commands}/tasks/update-plugins.js +2 -2
- package/commands/test.js +100 -0
- package/configs/aceconfig.js +28 -0
- package/{lib/configs.js → configs/docker-compose.js} +32 -55
- package/configs/webpack.config.js +119 -0
- package/configs/wp-env.js +76 -0
- package/gen/parser.js +166 -0
- package/gen/site-generator.js +111 -0
- package/lib/admin.js +40 -0
- package/lib/cli.js +129 -63
- package/lib/env.js +3 -11
- package/lib/helpers.js +109 -0
- package/lib/index.js +28 -0
- package/lib/spinner.js +10 -7
- package/lib/{caweb.js → wordpress/caweb.js} +1 -1
- package/lib/{divi.js → wordpress/divi.js} +1 -1
- package/lib/{download-sources.js → wordpress/download-sources.js} +75 -79
- package/lib/wordpress/index.js +16 -0
- package/lib/{options.js → wordpress/options.js} +1 -1
- package/lib/{wordpress.js → wordpress/wordpress.js} +4 -8
- package/package.json +42 -27
- package/template/assets/css/popover.css +80 -0
- package/template/assets/js/popover.js +30 -0
- package/template/block/edit.js.mustache +48 -0
- package/template/block/editor.scss.mustache +5 -0
- package/template/block/index.js.mustache +40 -0
- package/template/block/save.js.mustache +21 -0
- package/template/block/style.scss.mustache +6 -0
- package/template/index.cjs +48 -0
- package/template/plugin/$slug.php.mustache +135 -0
- package/template/plugin/core/cdec-api.php.mustache +44 -0
- package/template/plugin/core/filters.php.mustache +111 -0
- package/template/plugin/core/functions.php.mustache +25 -0
- package/template/plugin/inc/renderer.php.mustache +25 -0
- package/lib/commands/destroy.js +0 -66
- package/lib/commands/test.js +0 -46
- package/lib/docker.js +0 -68
- /package/{lib/commands → commands/tasks}/shell.js +0 -0
package/commands/test.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import fs from 'fs';
|
|
7
|
+
import loadConfig from '@wordpress/env/lib/config/load-config.js';
|
|
8
|
+
import cp from 'child_process';
|
|
9
|
+
|
|
10
|
+
//var WP = require('wp-cli');
|
|
11
|
+
/**
|
|
12
|
+
* Internal dependencies
|
|
13
|
+
*/
|
|
14
|
+
import { runCLICmds, runCmd, projectPath } from '../lib/index.js';
|
|
15
|
+
/*
|
|
16
|
+
import generateOverridesMD from '../admin.js';
|
|
17
|
+
import { CAWEB_OPTIONS, DIVI_OPTIONS } from '../options.js';
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Promisified dependencies
|
|
21
|
+
*/
|
|
22
|
+
import os from 'os';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Test code.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} options
|
|
28
|
+
* @param {Object} options.spinner A CLI spinner which indicates progress.
|
|
29
|
+
* @param {boolean} options.environment Which environment to test in.
|
|
30
|
+
* @param {boolean} options.debug True if debug mode is enabled.
|
|
31
|
+
*/
|
|
32
|
+
export default async function test({
|
|
33
|
+
spinner,
|
|
34
|
+
debug,
|
|
35
|
+
environment
|
|
36
|
+
} ) {
|
|
37
|
+
|
|
38
|
+
spinner.text = "Testing Code Functionality";
|
|
39
|
+
const config = await loadConfig(path.resolve('.'));
|
|
40
|
+
|
|
41
|
+
process.env.WP_CLI_CONFIG_PATH = path.join(config.workDirectoryPath, 'config.yml');
|
|
42
|
+
// update post meta.
|
|
43
|
+
let id = 35;
|
|
44
|
+
let newId = 30
|
|
45
|
+
let tbl = `wp_term_taxonomy`;
|
|
46
|
+
|
|
47
|
+
let result = await runCmd(
|
|
48
|
+
'php',
|
|
49
|
+
[
|
|
50
|
+
path.join(projectPath, 'bin', 'wp-cli.phar'),
|
|
51
|
+
'--ssh=docker:' + path.basename(config.workDirectoryPath) + '-cli-1',
|
|
52
|
+
`db query 'UPDATE ${tbl} SET term_taxonomy_id=${newId},term_id=${newId} WHERE term_id=${id}'`,
|
|
53
|
+
]
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
console.log( result );
|
|
57
|
+
/*
|
|
58
|
+
let q = `UPDATE wp_posts SET id=1 WHERE id=10 `;
|
|
59
|
+
let u = 'http://danny.com/wp-json/wp/v2/pages/2827';
|
|
60
|
+
let result = await axios.request({
|
|
61
|
+
url: u,
|
|
62
|
+
method: 'POST',
|
|
63
|
+
data: {
|
|
64
|
+
title: 'Audio',
|
|
65
|
+
meta: {
|
|
66
|
+
_et_pb_use_builder: 'on'
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
headers: {
|
|
70
|
+
Authorization: 'Basic ' + Buffer.from(`admin:9RUG nPuo u581 cY2A uSJd FE2P`).toString('base64')
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
.then( async (res) => { return res.data; } )
|
|
74
|
+
.catch( async (err) => { return err; } )
|
|
75
|
+
|
|
76
|
+
*/
|
|
77
|
+
/*
|
|
78
|
+
let result = await runCmd(
|
|
79
|
+
'php',
|
|
80
|
+
[
|
|
81
|
+
//'-r',
|
|
82
|
+
path.join(projectPath, 'bin', 'wp-cli.phar'),
|
|
83
|
+
'--ssh=' + `docker:${path.basename(config.workDirectoryPath)}-cli-1`,
|
|
84
|
+
//'@dev',
|
|
85
|
+
//`db query '${q}'`,
|
|
86
|
+
'post list',
|
|
87
|
+
//'--info',
|
|
88
|
+
'--skip-themes',
|
|
89
|
+
//`--url=${new URL(u).origin}`
|
|
90
|
+
// '--format=json'
|
|
91
|
+
//'--skip-column-names',
|
|
92
|
+
//'post list path=/nas/content/live/cawebdev/ --http=https://dev.sites.ca.gov --url=https://dev.sites.ca.gov',
|
|
93
|
+
],
|
|
94
|
+
config
|
|
95
|
+
)*/
|
|
96
|
+
|
|
97
|
+
//console.log( 'Result', result.toString() );
|
|
98
|
+
//console.log( 'Output', result.stdout.toString() );
|
|
99
|
+
//console.log( 'Error', result.stderr.toString() );
|
|
100
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for Accessibility Checker
|
|
3
|
+
* @link https://www.npmjs.com/package/accessibility-checker
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
ruleArchive: "latest",
|
|
8
|
+
policies: [
|
|
9
|
+
'WCAG_2_1'
|
|
10
|
+
],
|
|
11
|
+
failLevels: [
|
|
12
|
+
'violation',
|
|
13
|
+
'potentialviolation'
|
|
14
|
+
],
|
|
15
|
+
reportLevels: [
|
|
16
|
+
'violation',
|
|
17
|
+
'potentialviolation',
|
|
18
|
+
'recommendation',
|
|
19
|
+
'potentialrecommendation',
|
|
20
|
+
'manual',
|
|
21
|
+
'pass'
|
|
22
|
+
],
|
|
23
|
+
outputFolder: "a11y",
|
|
24
|
+
outputFormat: [
|
|
25
|
+
'html'
|
|
26
|
+
],
|
|
27
|
+
outputFilenameTimestamp: false
|
|
28
|
+
}
|
|
@@ -1,48 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import path from '
|
|
5
|
-
import fs from 'fs
|
|
6
|
-
import yaml from 'js-yaml';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import fs from 'fs';
|
|
7
6
|
import getHostUser from '@wordpress/env/lib/get-host-user.js';
|
|
7
|
+
import yaml from 'js-yaml';
|
|
8
|
+
import os from 'os';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
+
* Internal Dependencies
|
|
11
12
|
*/
|
|
12
|
-
const { writeFile } = fs.promises;
|
|
13
|
-
|
|
14
|
-
const localPath = path.resolve( path.join(process.cwd(), 'node_modules/@caweb/cli/package.json') )
|
|
15
|
-
const pkg = JSON.parse( await fs.readFile(localPath) );
|
|
16
13
|
|
|
17
14
|
/**
|
|
18
|
-
*
|
|
15
|
+
* Generate config.yml
|
|
19
16
|
*
|
|
20
|
-
* @param {
|
|
21
|
-
* @param {boolean} multisite True if converting to multisite.
|
|
17
|
+
* @param {string} workDirectoryPath Path to the work directory located in ~/.wp-env.
|
|
22
18
|
*
|
|
23
|
-
* @returns
|
|
19
|
+
* @returns void
|
|
24
20
|
*/
|
|
25
|
-
|
|
26
|
-
let
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
async function generateCLIConfig(workDirectoryPath){
|
|
22
|
+
let yml = {
|
|
23
|
+
path: '/var/www/html',
|
|
24
|
+
apache_modules: ['mod_rewrite']
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let { homedir } = os.userInfo();
|
|
28
|
+
|
|
29
|
+
// add any ssh hosts to the config
|
|
30
|
+
if( fs.existsSync( path.join( homedir, '.ssh', 'config' )) ){
|
|
31
|
+
|
|
32
|
+
let ssh_hosts = fs.readFileSync(path.join( homedir, '.ssh', 'config' )).toString().match(/Host\s+(.*)[^\n\r]/g);
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
config['themes'] = [`CA-CODE-Works/CAWeb`];
|
|
37
|
-
}
|
|
34
|
+
ssh_hosts.forEach((host) => {
|
|
35
|
+
let s = host.replace(/Host\s+/, '');
|
|
38
36
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
37
|
+
yml[`@${s}`] = { ssh: s};
|
|
38
|
+
})
|
|
39
|
+
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
|
|
42
|
+
fs.writeFileSync(
|
|
43
|
+
path.join(workDirectoryPath, 'config.yml'),
|
|
44
|
+
yaml.dump(yml));
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
/**
|
|
@@ -51,8 +50,8 @@ const buildWPEnvConfig = (bare, multisite) => {
|
|
|
51
50
|
* @param {string} workDirectoryPath Path to the work directory located in ~/.wp-env.
|
|
52
51
|
* @returns object
|
|
53
52
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
export default function dockerConfig ( workDirectoryPath ) {
|
|
54
|
+
const {name} = getHostUser();
|
|
56
55
|
|
|
57
56
|
// generate cli config file for cli containers.
|
|
58
57
|
generateCLIConfig(workDirectoryPath);
|
|
@@ -145,27 +144,5 @@ const buildDockerComposeConfig = (workDirectoryPath) => {
|
|
|
145
144
|
}
|
|
146
145
|
|
|
147
146
|
return dockerConfig;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Generate config.yml
|
|
152
|
-
*
|
|
153
|
-
* @param {string} workDirectoryPath Path to the work directory located in ~/.wp-env.
|
|
154
|
-
*
|
|
155
|
-
* @returns void
|
|
156
|
-
*/
|
|
157
|
-
async function generateCLIConfig(workDirectoryPath){
|
|
158
|
-
const yml = {
|
|
159
|
-
path: '/var/www/html',
|
|
160
|
-
apache_modules: ['mod_rewrite']
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
await writeFile(
|
|
164
|
-
path.join(workDirectoryPath, 'config.yml'),
|
|
165
|
-
yaml.dump(yml));
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export {
|
|
169
|
-
buildWPEnvConfig,
|
|
170
|
-
buildDockerComposeConfig
|
|
147
|
+
|
|
171
148
|
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebPack Configuration for California Department of Technology
|
|
3
|
+
*
|
|
4
|
+
* Utilizes WordPress Scripts Webpack configuration as base.
|
|
5
|
+
* s
|
|
6
|
+
* @link https://webpack.js.org/configuration/
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* External dependencies
|
|
11
|
+
*/
|
|
12
|
+
import baseConfig from '@wordpress/scripts/config/webpack.config.js';
|
|
13
|
+
import path from 'path';
|
|
14
|
+
import fs from 'fs';
|
|
15
|
+
import MiniCssExtractPlugin from "mini-css-extract-plugin";
|
|
16
|
+
import CopyWebpackPlugin from 'copy-webpack-plugin';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Internal dependencies
|
|
20
|
+
*/
|
|
21
|
+
import SiteGenerator from '../gen/site-generator.js';
|
|
22
|
+
|
|
23
|
+
// fallback to env variable
|
|
24
|
+
let isProduction = "production" === process.env.NODE_ENV;
|
|
25
|
+
|
|
26
|
+
// because we use WordPress default WebPack config
|
|
27
|
+
// 'mode' is defined by process.env.NODE_ENV
|
|
28
|
+
// some of the webpack cli flags are ignored
|
|
29
|
+
// so let's make some corrections.
|
|
30
|
+
let corrections = {};
|
|
31
|
+
|
|
32
|
+
process.argv.splice( 2 ).forEach(element => {
|
|
33
|
+
// if flag
|
|
34
|
+
if( element.startsWith( '--' ) ){
|
|
35
|
+
let splitterIndex = element.indexOf( '=' );
|
|
36
|
+
let flag = element.substring(2, splitterIndex );
|
|
37
|
+
let value = element.substring( splitterIndex + 1 );
|
|
38
|
+
|
|
39
|
+
// if flag is a webpack flag add corrections.
|
|
40
|
+
switch( flag ){
|
|
41
|
+
case 'mode':
|
|
42
|
+
// if cli arg was passed use that value
|
|
43
|
+
isProduction = "production" === value;
|
|
44
|
+
|
|
45
|
+
corrections[flag] = value
|
|
46
|
+
break
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// update the WordPress default webpack rules with ours.
|
|
55
|
+
baseConfig.module.rules.forEach((rule, i) => {
|
|
56
|
+
const r = new RegExp(rule.test).toString();
|
|
57
|
+
|
|
58
|
+
// WordPress adds a hash to asset file names we remove that hash
|
|
59
|
+
if( r === new RegExp(/\.(bmp|png|jpe?g|gif|webp)$/i).toString() ){
|
|
60
|
+
baseConfig.module.rules[i].generator = {
|
|
61
|
+
filename: 'images/[name][ext]'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if( r === new RegExp(/\.(woff|woff2|eot|ttf|otf)$/i).toString() ){
|
|
65
|
+
baseConfig.module.rules[i].generator = {
|
|
66
|
+
filename: 'fonts/[name][ext]'
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// SVG rules
|
|
70
|
+
if( r === new RegExp(/\.svg$/).toString() ){
|
|
71
|
+
// we don't want SVG to be inline move them to fonts folder
|
|
72
|
+
if( 'asset/inline' === rule.type ){
|
|
73
|
+
baseConfig.module.rules[i].type = 'asset/resource';
|
|
74
|
+
baseConfig.module.rules[i].generator = {
|
|
75
|
+
filename: 'fonts/[name][ext]'
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
// Our Webpack Configuration.
|
|
82
|
+
let webpackConfig = {
|
|
83
|
+
...baseConfig,
|
|
84
|
+
target: 'browserslist',
|
|
85
|
+
devtool: false,
|
|
86
|
+
output: {
|
|
87
|
+
...baseConfig.output,
|
|
88
|
+
publicPath: `/`,
|
|
89
|
+
clean: false
|
|
90
|
+
},
|
|
91
|
+
plugins: [
|
|
92
|
+
...baseConfig.plugins,
|
|
93
|
+
new MiniCssExtractPlugin(
|
|
94
|
+
{
|
|
95
|
+
linkType: "text/css",
|
|
96
|
+
filename: '[name].css'
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
],
|
|
100
|
+
module: {
|
|
101
|
+
rules: [
|
|
102
|
+
...baseConfig.module.rules,
|
|
103
|
+
{
|
|
104
|
+
test: /\.html$/,
|
|
105
|
+
loader: 'handlebars-loader'
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
performance: {
|
|
110
|
+
maxAssetSize: 500000,
|
|
111
|
+
maxEntrypointSize: 500000
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
if( ! isProduction ){
|
|
116
|
+
SiteGenerator( webpackConfig );
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default webpackConfig;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import fs from 'fs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal Dependencies
|
|
9
|
+
*/
|
|
10
|
+
import {
|
|
11
|
+
projectPath
|
|
12
|
+
} from '../lib/helpers.js';
|
|
13
|
+
|
|
14
|
+
const localFile = path.join(projectPath, 'package.json');
|
|
15
|
+
const pkg = JSON.parse( fs.readFileSync(localFile) );
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Build .wp-env.json
|
|
19
|
+
*
|
|
20
|
+
* @param {boolean} bare True if excluding any CAWeb Configurations.
|
|
21
|
+
* @param {boolean} multisite True if converting to multisite.
|
|
22
|
+
* @param {boolean} plugin True if root directory is a plugin.
|
|
23
|
+
* @param {boolean} theme True if root directory is a theme.
|
|
24
|
+
*
|
|
25
|
+
* @returns object
|
|
26
|
+
*/
|
|
27
|
+
export default function wpEnvConfig ( bare, multisite, plugin, theme ) {
|
|
28
|
+
let themes = [];
|
|
29
|
+
let plugins = [];
|
|
30
|
+
|
|
31
|
+
let config = {
|
|
32
|
+
core: `WordPress/WordPress#${pkg.config.WP_VER}`,
|
|
33
|
+
phpVersion: `${pkg.config.PHP_VER}`,
|
|
34
|
+
config: {
|
|
35
|
+
...pkg.config.DEFAULTS
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// if not bare then include our theme.
|
|
40
|
+
if( ! bare ){
|
|
41
|
+
themes.push('CAWebPublishing/CAWeb')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// if root directory is a theme
|
|
45
|
+
if( theme ){
|
|
46
|
+
themes.push( '.' );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// if is multisite
|
|
50
|
+
if( multisite ){
|
|
51
|
+
config.config['WP_ALLOW_MULTISITE'] = true;
|
|
52
|
+
|
|
53
|
+
// set CAWeb as Default Theme if it was downloaded
|
|
54
|
+
if( ! bare ){
|
|
55
|
+
config.config['WP_DEFAULT_THEME'] = 'CAWeb';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// if root directory is a plugin
|
|
60
|
+
if( plugin ){
|
|
61
|
+
plugins.push( '.' );
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// add plugins if sources were added
|
|
65
|
+
if( plugins.length ){
|
|
66
|
+
config['plugins'] = plugins;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// add themes if sources were added
|
|
70
|
+
if( themes.length ){
|
|
71
|
+
config['themes'] = themes;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return config;
|
|
75
|
+
|
|
76
|
+
}
|
package/gen/parser.js
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { HTMLToJSON } from 'html-to-json-parser';
|
|
7
|
+
import jsdom from 'jsdom';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Internal dependencies
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Path Directory Locations
|
|
15
|
+
* - appPath - Project Application Path
|
|
16
|
+
* - srcPath - Project Application Src Path
|
|
17
|
+
* - publicPath - Project Application Public Path
|
|
18
|
+
* - dataPath - Project Application Data Path
|
|
19
|
+
*/
|
|
20
|
+
const appPath = path.resolve(process.cwd());
|
|
21
|
+
const srcPath = path.join( appPath, 'src');
|
|
22
|
+
const dataPath = path.join( srcPath, 'data');
|
|
23
|
+
const assetsPath = path.join( srcPath, 'assets');
|
|
24
|
+
|
|
25
|
+
const fallbackPath = path.join( appPath, 'node_modules', '@cdt', 'template');
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Generate Pages
|
|
29
|
+
*
|
|
30
|
+
* @async
|
|
31
|
+
* @param {Object} siteData Data to use when creating pages
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
async function generatePages(siteData){
|
|
35
|
+
|
|
36
|
+
// we start with a blank html and schema
|
|
37
|
+
let page = '<html><html>';
|
|
38
|
+
let schema = {};
|
|
39
|
+
|
|
40
|
+
// if src/index.html exists
|
|
41
|
+
if(fs.existsSync(path.join(srcPath, 'index.html'))){
|
|
42
|
+
|
|
43
|
+
// get index path file location
|
|
44
|
+
page = path.join(srcPath, 'index.html')
|
|
45
|
+
|
|
46
|
+
// generate page schema
|
|
47
|
+
schema = await convertHTMLtoJson(page, false);
|
|
48
|
+
|
|
49
|
+
// read contents of index file, we strip any newlines
|
|
50
|
+
//page = fs.readFileSync( page ).toString().replace(/\n[\s]+/g,'');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// convert page to jsdom object
|
|
54
|
+
let dom = new jsdom.JSDOM( page );
|
|
55
|
+
|
|
56
|
+
// if we should be using the template
|
|
57
|
+
if( process.env.CDT_TEMPLATE ){
|
|
58
|
+
|
|
59
|
+
// read contents of template index file, we strip any newlines
|
|
60
|
+
page = fs.readFileSync( path.join(fallbackPath, 'src', 'index.html') ).toString().replace(/\n[\s]+/g,'');
|
|
61
|
+
|
|
62
|
+
// create template jsdom object
|
|
63
|
+
let templateDom = new jsdom.JSDOM( page );
|
|
64
|
+
|
|
65
|
+
// colorscheme
|
|
66
|
+
let colorCSS = path.join(fallbackPath, 'build', 'oceanside.css');
|
|
67
|
+
let colorJS = path.join(fallbackPath, 'build', 'oceanside.js');
|
|
68
|
+
|
|
69
|
+
dom.window.document.querySelector('head').append(
|
|
70
|
+
`<link rel="stylesheet" type="text/css" href="${colorCSS}">`
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// update template dom by appending contents of existing dom body
|
|
75
|
+
// after the header
|
|
76
|
+
templateDom.window.document.querySelector('header').append(
|
|
77
|
+
dom.window.document.querySelector('body')
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// update existing dom with templated changes.
|
|
81
|
+
dom = new jsdom.JSDOM( templateDom.window.document.documentElement.outerHTML);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// generate any data attributes
|
|
85
|
+
populateDataAttrs( dom, siteData, schema );
|
|
86
|
+
|
|
87
|
+
return dom.window.document.documentElement.outerHTML;
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Populate data-cagov attributes with siteData
|
|
94
|
+
*
|
|
95
|
+
* @param {jsdom.JSDOM} dom HTML
|
|
96
|
+
* @param {Object} siteData
|
|
97
|
+
* @param {Object} schema
|
|
98
|
+
*/
|
|
99
|
+
function populateDataAttrs(dom, siteData, schema){
|
|
100
|
+
for( const [attr, data] of Object.entries(siteData) ){
|
|
101
|
+
// if attribute isn't in the schema don't do anything
|
|
102
|
+
if( ! schema.data || ! Object.keys(schema.data).includes(attr) ){
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// any data- attributes should be prefixed with cagov
|
|
107
|
+
// get any elements with the appropriate data- attr
|
|
108
|
+
let elements = dom.window.document.querySelectorAll(`[data-cagov-${attr}]`);
|
|
109
|
+
|
|
110
|
+
// if the data is an array|object pass as data
|
|
111
|
+
let value = Array.isArray(data) || 'object' === typeof data ? JSON.stringify( data ) : data;
|
|
112
|
+
|
|
113
|
+
elements.forEach(element => {
|
|
114
|
+
element.setAttribute(`data-cagov-${attr}`, value);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Generates a page schema including any data attributes in src/data/index.json
|
|
123
|
+
*
|
|
124
|
+
* @async
|
|
125
|
+
* @param {*} file
|
|
126
|
+
* @param {boolean} [write=true]
|
|
127
|
+
* @param {string} [outputPath='']
|
|
128
|
+
* @returns {unknown}
|
|
129
|
+
*/
|
|
130
|
+
async function convertHTMLtoJson( file, write = true, outputPath = '' ){
|
|
131
|
+
if( fs.existsSync( file )){
|
|
132
|
+
let template = fs.readFileSync( file ).toString().replace(/\n[\s]+/g,'');
|
|
133
|
+
let markup = await HTMLToJSON(template, false )
|
|
134
|
+
|
|
135
|
+
let data = fs.existsSync( path.join(dataPath, 'index.json') ) ?
|
|
136
|
+
JSON.parse( fs.readFileSync( path.join(dataPath, 'index.json') ) ) : {};
|
|
137
|
+
|
|
138
|
+
// remove any examples data
|
|
139
|
+
delete data.examples;
|
|
140
|
+
|
|
141
|
+
let schema = {
|
|
142
|
+
title: "California Department of Technology Schema",
|
|
143
|
+
$schema: "http://json-schema.org/2019-09/schema",
|
|
144
|
+
$id: "https://json-schema.org/draft/2019-09/schema",
|
|
145
|
+
type: "object",
|
|
146
|
+
description: "California Department of Technology Schema",
|
|
147
|
+
data,
|
|
148
|
+
markup,
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// Write schema file.
|
|
152
|
+
if( write ){
|
|
153
|
+
fs.writeFileSync(
|
|
154
|
+
path.join(outputPath, 'schema.json'),
|
|
155
|
+
JSON.stringify( schema, null, 4 )
|
|
156
|
+
);
|
|
157
|
+
}else{
|
|
158
|
+
return schema;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export {
|
|
165
|
+
generatePages
|
|
166
|
+
};
|