@caweb/cli 1.4.2 → 1.4.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.
Files changed (76) hide show
  1. package/README.md +2 -180
  2. package/bin/css-audit/.editorconfig +12 -0
  3. package/bin/css-audit/.github/workflows/build-report.yml +46 -0
  4. package/bin/css-audit/.github/workflows/merge-trunk-to-report.yml +17 -0
  5. package/bin/css-audit/.github/workflows/node.yaml +32 -0
  6. package/bin/css-audit/.nvmrc +1 -0
  7. package/bin/css-audit/README.md +131 -0
  8. package/bin/css-audit/css-audit.config.js +13 -0
  9. package/bin/css-audit/index.js +38 -0
  10. package/bin/css-audit/package-lock.json +6689 -0
  11. package/bin/css-audit/package.json +56 -0
  12. package/bin/css-audit/public/.gitkeep +1 -0
  13. package/bin/css-audit/src/__tests__/alphas.js +128 -0
  14. package/bin/css-audit/src/__tests__/colors.js +115 -0
  15. package/bin/css-audit/src/__tests__/display-none.js +52 -0
  16. package/bin/css-audit/src/__tests__/important.js +88 -0
  17. package/bin/css-audit/src/__tests__/media-queries.js +84 -0
  18. package/bin/css-audit/src/__tests__/property-values.js +55 -0
  19. package/bin/css-audit/src/__tests__/run.js +25 -0
  20. package/bin/css-audit/src/__tests__/selectors.js +66 -0
  21. package/bin/css-audit/src/audits/alphas.js +70 -0
  22. package/bin/css-audit/src/audits/colors.js +83 -0
  23. package/bin/css-audit/src/audits/display-none.js +39 -0
  24. package/bin/css-audit/src/audits/important.js +60 -0
  25. package/bin/css-audit/src/audits/media-queries.js +96 -0
  26. package/bin/css-audit/src/audits/property-values.js +65 -0
  27. package/bin/css-audit/src/audits/selectors.js +67 -0
  28. package/bin/css-audit/src/audits/typography.js +41 -0
  29. package/bin/css-audit/src/formats/cli-table.js +81 -0
  30. package/bin/css-audit/src/formats/html/_audit-alpha.twig +23 -0
  31. package/bin/css-audit/src/formats/html/_audit-colors.twig +23 -0
  32. package/bin/css-audit/src/formats/html/_audit-default.twig +24 -0
  33. package/bin/css-audit/src/formats/html/index.twig +88 -0
  34. package/bin/css-audit/src/formats/html/style.css +341 -0
  35. package/bin/css-audit/src/formats/html.js +52 -0
  36. package/bin/css-audit/src/formats/json.js +9 -0
  37. package/bin/css-audit/src/run.js +76 -0
  38. package/bin/css-audit/src/utils/__tests__/cli.js +70 -0
  39. package/bin/css-audit/src/utils/__tests__/example-config.config.js +12 -0
  40. package/bin/css-audit/src/utils/__tests__/get-specificity.js +39 -0
  41. package/bin/css-audit/src/utils/cli.js +133 -0
  42. package/bin/css-audit/src/utils/format-report.js +37 -0
  43. package/bin/css-audit/src/utils/get-specificity.js +97 -0
  44. package/bin/css-audit/src/utils/get-values-count.js +17 -0
  45. package/commands/index.js +15 -5
  46. package/commands/test.js +0 -3
  47. package/commands/webpack/webpack.js +166 -0
  48. package/configs/webpack.config.js +151 -81
  49. package/lib/cli.js +71 -35
  50. package/lib/helpers.js +3 -1
  51. package/lib/webpack/plugins/a11y/aceconfig.js +44 -0
  52. package/lib/webpack/plugins/a11y/index.js +272 -0
  53. package/lib/webpack/plugins/a11y/package.json +12 -0
  54. package/lib/webpack/plugins/css-audit/css-audit.config.cjs +5 -0
  55. package/lib/webpack/plugins/css-audit/default.config.js +19 -0
  56. package/lib/webpack/plugins/css-audit/index.js +297 -0
  57. package/lib/webpack/plugins/css-audit/package.json +12 -0
  58. package/lib/webpack/plugins/jshint/.jshintrc +31 -0
  59. package/lib/webpack/plugins/jshint/index.js +286 -0
  60. package/lib/webpack/plugins/jshint/package-lock.json +22 -0
  61. package/lib/webpack/plugins/jshint/package.json +15 -0
  62. package/lib/webpack/plugins/jshint/reporter.cjs +663 -0
  63. package/package.json +18 -12
  64. package/assets/logo.ico +0 -0
  65. package/commands/a11y.js +0 -95
  66. package/commands/build.js +0 -80
  67. package/commands/serve.js +0 -95
  68. package/configs/aceconfig.js +0 -28
  69. package/docs/CREDITS.MD +0 -27
  70. package/docs/ISSUES.MD +0 -7
  71. package/docs/OVERRIDES.md +0 -53
  72. package/docs/ROADMAP.MD +0 -19
  73. package/docs/SYNC.MD +0 -29
  74. package/docs/tool/index.js +0 -45
  75. package/gen/parser.js +0 -166
  76. package/gen/site-generator.js +0 -144
@@ -0,0 +1,133 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ const minimist = require( 'minimist' );
5
+ const path = require( 'path' );
6
+ const { cosmiconfigSync } = require( 'cosmiconfig' );
7
+
8
+ const getArgsFromCLI = ( excludePrefixes ) => {
9
+ const args = process.argv.slice( 2 );
10
+ if ( excludePrefixes ) {
11
+ return args.filter( ( arg ) => {
12
+ return ! excludePrefixes.some( ( prefix ) =>
13
+ arg.startsWith( prefix )
14
+ );
15
+ } );
16
+ }
17
+ return args;
18
+ };
19
+
20
+ const getFileArgsFromCLI = () => minimist( getArgsFromCLI() )._;
21
+
22
+ /**
23
+ * Get configuration using cosmiconfig.
24
+ *
25
+ * @param {string} env
26
+ */
27
+ const getConfig = ( env ) => {
28
+ const moduleName = 'test' === env ? 'example-config' : 'css-audit';
29
+ const searchFrom =
30
+ 'test' === env ? path.join( __dirname, '__tests__' ) : process.cwd();
31
+
32
+ const explorerSync = cosmiconfigSync( moduleName );
33
+ const { config } = explorerSync.search( searchFrom );
34
+
35
+ try {
36
+ return config;
37
+ } catch ( e ) {
38
+ console.error( e, 'Error retrieving config file.' );
39
+ }
40
+ };
41
+
42
+ /**
43
+ * Get the argument required for running the audit,
44
+ *
45
+ * First get the argument from CLI, and fallback to the
46
+ * config if its not present.
47
+ *
48
+ * @param {string} arg
49
+ * @param {boolean} cliArgOnly
50
+ */
51
+
52
+ const getArg = ( arg, cliArgOnly = false ) => {
53
+ for ( const cliArg of getArgsFromCLI() ) {
54
+ const [ name, value ] = cliArg.split( '=' );
55
+
56
+ if ( name === arg ) {
57
+ return 'undefined' === typeof value ? true : value || null;
58
+ }
59
+ }
60
+
61
+ if ( true === cliArgOnly ) {
62
+ return false;
63
+ }
64
+
65
+ const config = getConfig( process.env.NODE_ENV );
66
+
67
+ const term = arg.substr( 2 );
68
+
69
+ // This is a simple property: value arg e.g. format: json
70
+ const isSimplePropertyValueArg = config.hasOwnProperty( term );
71
+
72
+ if ( isSimplePropertyValueArg ) {
73
+ return 'undefined' === typeof config[ term ]
74
+ ? true
75
+ : config[ term ] || null;
76
+ }
77
+
78
+ if ( config.hasOwnProperty( 'audits' ) ) {
79
+ // Separate the basic audits from property-values.
80
+ const basicAudits = config.audits.filter(
81
+ ( audit ) => term === audit && 'string' === typeof audit
82
+ );
83
+
84
+ // Create an array of values of the property-value audits.
85
+ const propertyValueAudits = config.audits.filter(
86
+ ( audit ) => 'object' === typeof audit && term === audit[ 0 ]
87
+ );
88
+
89
+ const propertyValueValues = ( () => {
90
+ if ( propertyValueAudits.length > 0 ) {
91
+ return propertyValueAudits
92
+ .flat()
93
+ .filter( ( item ) => 'property-values' !== item );
94
+ }
95
+ return [];
96
+ } )();
97
+
98
+ if ( 'undefined' !== basicAudits[ 0 ] && term === basicAudits[ 0 ] ) {
99
+ return true;
100
+ }
101
+
102
+ if ( propertyValueValues.length > 0 ) {
103
+ return propertyValueValues;
104
+ }
105
+ }
106
+
107
+ // The argument cannot be retrieved from CLI or config.
108
+ return false;
109
+ };
110
+
111
+ const getHelp = () => {
112
+ return `Usage: css-audit -- <files...> [options]
113
+
114
+ --colors Run colors audit.
115
+ --important Run !important audit.
116
+ --display-none Run display: none audit.
117
+ --selectors Run selectors audit.
118
+ --media-queries Run media queries audit.
119
+ --property-values Run audit for a given set of property values, comma-separated.
120
+ --recommended Run recommended audits (colors, important, selectors). Default: true.
121
+ --all Run all audits (except property values, as it requires a value).
122
+ --format Format to use for displaying report.
123
+ --help Show this message.
124
+ `;
125
+ };
126
+
127
+ module.exports = {
128
+ getArgsFromCLI,
129
+ getFileArgsFromCLI,
130
+ getArg,
131
+ getConfig,
132
+ getHelp,
133
+ };
@@ -0,0 +1,37 @@
1
+ const FMT_CLI_TABLE = 'cli-table';
2
+ const FMT_JSON = 'json';
3
+ const FMT_HTML = 'html';
4
+
5
+ /**
6
+ * Format the reports using the specified reporter format.
7
+ *
8
+ * @param {Array<Array<Object>>} reports The list of report data.
9
+ * @param {FMT_CLI_TABLE|FMT_JSON} format One of the predefined formats. Defaults to FMT_CLI_TABLE.
10
+ * @return {string} The formatted reports.
11
+ */
12
+ function formatReport( reports, format = FMT_CLI_TABLE ) {
13
+ let formatCallback = false;
14
+ switch ( format ) {
15
+ case FMT_JSON:
16
+ formatCallback = require( '../formats/json' );
17
+ break;
18
+ case FMT_HTML:
19
+ formatCallback = require( '../formats/html' );
20
+ break;
21
+ case FMT_CLI_TABLE:
22
+ default:
23
+ formatCallback = require( '../formats/cli-table' );
24
+ }
25
+
26
+ const formattedReports = formatCallback( reports );
27
+ if ( Array.isArray( formattedReports ) ) {
28
+ return formattedReports.join( '\n' );
29
+ }
30
+
31
+ return formattedReports;
32
+ }
33
+
34
+ module.exports = {
35
+ formats: [ FMT_CLI_TABLE, FMT_JSON ],
36
+ formatReport,
37
+ };
@@ -0,0 +1,97 @@
1
+ const csstree = require( 'css-tree' );
2
+
3
+ /**
4
+ * A recursive callback used to build up the specificity of a selector.
5
+ *
6
+ * This is intented to be used as a `reduce` callback, building up the
7
+ * specificity in the array accumulator as it processes each part of a selector.
8
+ *
9
+ * @param {Array<number>} specificity The specificity as an array.
10
+ * @param {Object} selector A selector node from the css-tree AST.
11
+ * @return {Array} The calculated specificity value.
12
+ */
13
+ function calculateSpecificity( [ a, b, c ], selector ) {
14
+ if ( ! selector.type ) {
15
+ return;
16
+ }
17
+ if ( 'lang' !== selector.name && selector.children ) {
18
+ return selector.children
19
+ .toArray()
20
+ .reduce( calculateSpecificity, [ a, b, c ] );
21
+ }
22
+
23
+ switch ( selector.type ) {
24
+ case 'IdSelector':
25
+ a++;
26
+ break;
27
+ case 'ClassSelector':
28
+ case 'AttributeSelector':
29
+ case 'Nth':
30
+ b++;
31
+ break;
32
+ case 'PseudoClassSelector':
33
+ if ( 'not' === selector.name ) {
34
+ break;
35
+ }
36
+ b++;
37
+ break;
38
+ case 'TypeSelector':
39
+ case 'PseudoElementSelector':
40
+ if ( '*' === selector.name ) {
41
+ break;
42
+ }
43
+ c++;
44
+ break;
45
+ case 'WhiteSpace':
46
+ case 'Combinator':
47
+ case 'Identifier':
48
+ // Whitespace, adjacent selectors (>, ~), … do not impact specificity.
49
+ break;
50
+ case 'Percentage':
51
+ // Part of a keyframe, not to be calculated.
52
+ break;
53
+ default:
54
+ console.warn( 'Unhandled selector type:', selector.type );
55
+ }
56
+ return [ a, b, c ];
57
+ }
58
+
59
+ /**
60
+ * Get the specificity value for a given CSS selector.
61
+ *
62
+ * @param {string} selector A valid CSS selector.
63
+ * @return {number} The calculated specificity value.
64
+ */
65
+ function getSpecificity( selector ) {
66
+ const node = csstree.parse( selector, { context: 'selector' } );
67
+ const selectorList = node.children.toArray();
68
+ const [ a, b, c ] = selectorList.reduce( calculateSpecificity, [
69
+ 0,
70
+ 0,
71
+ 0,
72
+ ] );
73
+ return 100 * a + 10 * b + c;
74
+ }
75
+
76
+ /**
77
+ * Get the specificity value for a given CSS selector, as an array.
78
+ *
79
+ * @param {string} selector A valid CSS selector.
80
+ * @return {Array} The calculated specificity value.
81
+ */
82
+ function getSpecificityArray( selector ) {
83
+ const node = csstree.parse( selector, { context: 'selector' } );
84
+ const selectorList = node.children.toArray();
85
+ const [ a, b, c ] = selectorList.reduce( calculateSpecificity, [
86
+ 0,
87
+ 0,
88
+ 0,
89
+ ] );
90
+ return [ a, b, c ];
91
+ }
92
+
93
+ module.exports = {
94
+ calculateSpecificity,
95
+ getSpecificity,
96
+ getSpecificityArray,
97
+ };
@@ -0,0 +1,17 @@
1
+ module.exports = function ( values ) {
2
+ const uniqueValues = [ ...new Set( values ) ];
3
+
4
+ return uniqueValues
5
+ .map( ( val ) => {
6
+ // Count up how many times this item appears in the full list.
7
+ const count = values.filter( ( c ) => c === val ).length;
8
+ return {
9
+ name: val,
10
+ count,
11
+ };
12
+ } )
13
+ .sort( ( a, b ) => {
14
+ // Reverse sort
15
+ return b.count - a.count;
16
+ } );
17
+ };
package/commands/index.js CHANGED
@@ -12,9 +12,18 @@ import installPath from '@wordpress/env/lib/commands/install-path.js';
12
12
  /**
13
13
  * Internal dependencies
14
14
  */
15
- import build from './build.js';
16
- import serve from './serve.js';
17
- import a11y from './a11y.js';
15
+ import webpack from './webpack/webpack.js';
16
+
17
+
18
+ import A11yPlugin from '../lib/webpack/plugins/a11y/index.js';
19
+ const a11y = new A11yPlugin().a11yCheck;
20
+
21
+ import CSSAuditPlugin from '../lib/webpack/plugins/css-audit/index.js';
22
+ const audit = new CSSAuditPlugin({}).audit;
23
+
24
+ import JSHintPlugin from '../lib/webpack/plugins/jshint/index.js';
25
+ const hint = new JSHintPlugin().hint;
26
+
18
27
  import shell from './tasks/shell.js';
19
28
 
20
29
  import sync from './sync.js';
@@ -40,9 +49,10 @@ export {
40
49
  start,
41
50
  stop,
42
51
  destroy,
43
- build,
44
- serve,
52
+ webpack,
45
53
  a11y,
54
+ audit,
55
+ hint,
46
56
  sync,
47
57
  updatePlugins,
48
58
  shell,
package/commands/test.js CHANGED
@@ -12,9 +12,6 @@ import os from 'os';
12
12
  */
13
13
  import { runCLICmds, runCmd, projectPath } from '../lib/index.js';
14
14
  import { CAWEB_OPTIONS, DIVI_OPTIONS } from '../lib/wordpress/index.js';
15
- /*
16
- import generateOverridesMD from '../admin.js';
17
- */
18
15
  /**
19
16
  * Promisified dependencies
20
17
  */
@@ -0,0 +1,166 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * External dependencies
5
+ */
6
+ import path from 'path';
7
+ import fs, { watch } from 'fs';
8
+ import deepmerge from 'deepmerge';
9
+ import Webpack from 'webpack';
10
+ import webpackServer from 'webpack-dev-server';
11
+
12
+ /**
13
+ * Internal dependencies
14
+ */
15
+ import {
16
+ runCmd,
17
+ projectPath,
18
+ appPath,
19
+ wpPrimary,
20
+ wpGreen,
21
+ wpRed
22
+ } from '../../lib/index.js';
23
+
24
+
25
+ /**
26
+ * Build the current project
27
+ *
28
+ * @param {Object} options
29
+ * @param {boolean} options.debug True if debug mode is enabled.
30
+ * @param {boolean} options.audit Add CSS-Audit Page to pages served.
31
+ */
32
+ export default async function webpack({
33
+ spinner,
34
+ debug,
35
+ audit,
36
+ a11y,
37
+ selectors
38
+ } ) {
39
+ const webpackCommand = 'build' === process.argv[2] ? 'build' : 'serve' ;
40
+
41
+ const defaultConfigPath = path.join( projectPath, 'configs', 'webpack.config.js' );
42
+ let webpackConfig = await import('file://' + defaultConfigPath);
43
+ let customConfig = {};
44
+
45
+ // Since we use @wordpress/scripts webpack config we can leverage
46
+ // the environment variables as well.
47
+ process.env.WP_COPY_PHP_FILES_TO_DIST = true;
48
+
49
+ // pass any arguments from the cli
50
+ // add our default config as an extension.
51
+ // users can overwrite any values by creating a webconfig of their own.
52
+ let webPackArgs = [
53
+ '--config',
54
+ defaultConfigPath,
55
+ ];
56
+
57
+ // CommonJS
58
+ if( fs.existsSync( path.join(appPath, 'webpack.config.cjs' ))){
59
+ webPackArgs.push(
60
+ '--config',
61
+ path.join(appPath, 'webpack.config.cjs' ),
62
+ '--merge'
63
+ )
64
+
65
+ customConfig = await import('file://' + path.join(appPath, 'webpack.config.cjs' )).default;
66
+
67
+ // ESM
68
+ }else if( fs.existsSync(path.join(appPath, 'webpack.config.js' )) ){
69
+ webPackArgs.push(
70
+ '--config',
71
+ path.join(appPath, 'webpack.config.js' ),
72
+ '--merge'
73
+ )
74
+
75
+ customConfig = await import('file://' + path.join(appPath, 'webpack.config.js' ));
76
+ }
77
+
78
+ if( customConfig.length ){
79
+ webpackConfig = deepmerge(webpackConfig.default, customConfig.default);
80
+ }
81
+
82
+ // we always run the build command.
83
+ let result = await runCmd(
84
+ 'webpack',
85
+ ['build', ...webPackArgs],
86
+ {stdio:'pipe'}
87
+ ).then(({stdout, stderr}) => {
88
+ // we hide the punycode deprecation warning.
89
+ // this is caused by the wp-scripts package
90
+ // if an error was thrown
91
+ if( stderr ){
92
+ //console.log( stderr.toString().replace(/.*`punycode`.*\n.*/, '') )
93
+ }
94
+
95
+ if(stdout){
96
+ return stdout.toString().replace(/.*`punycode`.*\n.*/, '');
97
+ }
98
+
99
+ });
100
+
101
+ // if serving.
102
+ if( 'serve' === webpackCommand ){
103
+ let hostUrl = 'localhost' === webpackConfig.default.devServer.host ? `http://${webpackConfig.default.devServer.host}`: webpackConfig.default.devServer.host;
104
+ let hostPort = webpackConfig.default.devServer.port;
105
+
106
+ if( hostPort && 80 !== hostPort )
107
+ {
108
+ hostUrl = `${hostUrl}:${hostPort}`;
109
+ }
110
+
111
+ console.log( `Webpack Server is preparing to launch ${ wpGreen(hostUrl) }` );
112
+ console.log( `Press ${ wpRed('Ctrl + C') } to shutdown the server.\n` );
113
+
114
+ // add a11y plugin
115
+ if( a11y ){
116
+ /*webpackConfig.default.plugins.push(new A11yPlugin({
117
+ outputFilename: 'a11y'
118
+ }) )
119
+
120
+ webpackConfig.default.devServer.static.push({
121
+ directory: path.join(appPath, 'a11y')
122
+ })*/
123
+ }
124
+
125
+ // add css-auditor plugin
126
+ if( audit ){
127
+ }
128
+
129
+ const compiler = Webpack(webpackConfig.default);
130
+ const server = new webpackServer({...webpackConfig.default.devServer}, compiler);
131
+
132
+ await server.start();
133
+
134
+ //process.stdin.resume();
135
+
136
+ // run webpack serve command
137
+ /*await runCmd(
138
+ 'webpack',
139
+ ['serve', ...webPackArgs],
140
+ {
141
+ stdio: 'inherit'
142
+ }
143
+ ).then(({stdout, stderr}) => {
144
+ // if an error was thrown, and no output
145
+ if( stderr && ! stdout){
146
+ console.log( stderr.toString() )
147
+ }
148
+
149
+ if( stdout ){
150
+ spinner.text = "Webpack Server was closed.";
151
+ }
152
+
153
+ });*/
154
+
155
+ /*
156
+ const compiler = Webpack(webpackConfig.default);
157
+ const server = new webpackServer({...webpackConfig.default.devServer}, compiler);
158
+ */
159
+
160
+ // only build was ran.
161
+ }else{
162
+ //spinner.prefixText = result;
163
+ //spinner.text = "Done!";
164
+ }
165
+
166
+ };