@penskemediacorp/twig-to-php-parser 1.14.0 → 1.27.1

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,7 +1,7 @@
1
1
  module.exports = {
2
2
  parser: {
3
- twigDir: `${__dirname}/src/patterns/`,
4
- phpDir: `${__dirname}/template-parts/patterns/`,
3
+ twigDir: `${ __dirname }/src/patterns/`,
4
+ phpDir: `${ __dirname }/template-parts/patterns/`,
5
5
  isUsingPlugin: true,
6
- }
7
- };
6
+ },
7
+ };
@@ -3,106 +3,116 @@ const assert = require( 'assert' );
3
3
  const parserMethods = require( '../index.js' ).methods;
4
4
 
5
5
  const expectations = {
6
- childInclude: '<?php \\PMC::render_template( CHILD_THEME_PATH . \'/template-parts/patterns/objects/o-nav.php\', $o_nav, true ); ?>',
7
- larvaInclude: '<?php \\PMC::render_template( PMC_CORE_PATH . \'/template-parts/patterns/objects/o-nav.php\', $o_nav, true ); ?>',
8
- pluginEnabled: '<?php \\PMC\\Larva\\Pattern::get_instance()->render_pattern_template( \'objects/o-nav\', $o_nav, true ); ?>'
6
+ childInclude:
7
+ "<?php \\PMC::render_template( CHILD_THEME_PATH . '/template-parts/patterns/objects/o-nav.php', $o_nav, true ); ?>",
8
+ larvaInclude:
9
+ "<?php \\PMC::render_template( PMC_CORE_PATH . '/template-parts/patterns/objects/o-nav.php', $o_nav, true ); ?>",
10
+ pluginEnabled:
11
+ "<?php \\PMC\\Larva\\Pattern::get_instance()->render_pattern_template( 'objects/o-nav', $o_nav, true ); ?>",
9
12
  };
10
13
 
11
- describe( 'parse include statements', function() {
12
-
14
+ describe( 'parse include statements', function () {
13
15
  it( 'twig includes are parsed to PMC::render_template calls', ( done ) => {
14
- parserMethods.parseIncludePath(
15
- '{% include "o-nav/o-nav.twig" with o_nav %}',
16
- 'o-nav',
17
- 'o_nav' )
18
- .catch( e => console.log( e ) )
19
- .then( ( result ) => {
20
- assert.equal( result, expectations.childInclude );
21
- done();
22
- });
23
- });
16
+ parserMethods
17
+ .parseIncludePath(
18
+ '{% include "o-nav/o-nav.twig" with o_nav %}',
19
+ 'o-nav',
20
+ 'o_nav'
21
+ )
22
+ .catch( ( e ) => console.log( e ) )
23
+ .then( ( result ) => {
24
+ assert.equal( result, expectations.childInclude );
25
+ done();
26
+ } );
27
+ } );
24
28
 
25
29
  it( 'if larva namespace, parsed path references the parent theme', ( done ) => {
26
-
27
- parserMethods.parseIncludePath(
28
- '{% include "@larva/objects/o-nav.twig" with o_nav %}',
29
- 'o-nav',
30
- 'o_nav'
31
- )
32
- .catch( e => console.log( e ) )
33
- .then( ( result ) => {
34
- assert.equal( result, expectations.larvaInclude );
35
- done();
36
- } );
30
+ parserMethods
31
+ .parseIncludePath(
32
+ '{% include "@larva/objects/o-nav.twig" with o_nav %}',
33
+ 'o-nav',
34
+ 'o_nav'
35
+ )
36
+ .catch( ( e ) => console.log( e ) )
37
+ .then( ( result ) => {
38
+ assert.equal( result, expectations.larvaInclude );
39
+ done();
40
+ } );
37
41
  } );
38
42
 
39
43
  it( 'if plugin is enabled, use the render_pattern_template method from Larva', ( done ) => {
40
-
41
- parserMethods.parseIncludePath(
42
- '{% include "@project/objects/o-nav.twig" with o_nav %}',
43
- 'o-nav',
44
- 'o_nav',
45
- true )
46
- .catch( e => console.log( e ) )
44
+ parserMethods
45
+ .parseIncludePath(
46
+ '{% include "@project/objects/o-nav.twig" with o_nav %}',
47
+ 'o-nav',
48
+ 'o_nav',
49
+ true
50
+ )
51
+ .catch( ( e ) => console.log( e ) )
47
52
  .then( ( result ) => {
48
53
  assert.equal( result, expectations.pluginEnabled );
49
54
  done();
50
55
  } );
51
56
  } );
52
-
53
- });
57
+ } );
54
58
 
55
59
  describe( 'parse SVG includes', function () {
56
60
  it( 'parses includes to brand_directory when using plugin', ( done ) => {
57
-
58
- const expectedPluginEnabled = '<?php \\PMC::render_template( \\PMC\\Larva\\Config::get_instance()->get( \'brand_directory\' ) . \'/build/svg/\' . ( $c_logo_svg ?? \'\' ) . \'.svg\', [], true ); ?>';
59
-
60
- parserMethods.parseSvgPath(
61
- "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
62
- 'c_logo_svg',
63
- true
64
- )
65
- .catch( e => console.log( e ) )
66
- .then( ( result ) => {
67
- assert.equal( result, expectedPluginEnabled );
68
- done();
69
- });
61
+ const expectedPluginEnabled =
62
+ "<?php \\PMC::render_template( \\PMC\\Larva\\Config::get_instance()->get( 'brand_directory' ) . '/build/svg/' . ( $c_logo_svg ?? '' ) . '.svg', [], true ); ?>";
63
+
64
+ parserMethods
65
+ .parseSvgPath(
66
+ "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
67
+ 'c_logo_svg',
68
+ true
69
+ )
70
+ .catch( ( e ) => console.log( e ) )
71
+ .then( ( result ) => {
72
+ assert.equal( result, expectedPluginEnabled );
73
+ done();
74
+ } );
70
75
  } );
71
76
  it( 'parses includes to CHILD_THEME_PATH when not using plugin', ( done ) => {
72
-
73
- const expected = '<?php \\PMC::render_template( CHILD_THEME_PATH . \'/assets/build/svg/\' . ( $c_logo_svg ?? \'\' ) . \'.svg\', [], true ); ?>';
74
-
75
- parserMethods.parseSvgPath(
76
- "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
77
- 'c_logo_svg'
78
- )
79
- .catch( e => console.log( e ) )
80
- .then( ( result ) => {
81
- assert.equal( result, expected );
82
- done();
83
- });
84
- });
77
+ const expected =
78
+ "<?php \\PMC::render_template( CHILD_THEME_PATH . '/assets/build/svg/' . ( $c_logo_svg ?? '' ) . '.svg', [], true ); ?>";
79
+
80
+ parserMethods
81
+ .parseSvgPath(
82
+ "{% include '../../build/svg/' ~ c_logo_svg ~ '.svg' %}",
83
+ 'c_logo_svg'
84
+ )
85
+ .catch( ( e ) => console.log( e ) )
86
+ .then( ( result ) => {
87
+ assert.equal( result, expected );
88
+ done();
89
+ } );
90
+ } );
85
91
  } );
86
92
 
87
93
  describe( 'parse custom functions', function () {
88
94
  it( 'parses wp_action function', ( done ) => {
89
-
90
- parserMethods.parseWpAction("{{wp_action('test','param',variable)}}")
91
- .catch( e => console.log( e ) )
95
+ parserMethods
96
+ .parseWpAction( "{{wp_action('test','param',variable)}}" )
97
+ .catch( ( e ) => console.log( e ) )
92
98
  .then( ( result ) => {
93
- assert.equal( result, "<?php do_action( 'test', 'param', $variable ); ?>" );
99
+ assert.equal(
100
+ result,
101
+ "<?php do_action( 'test', 'param', $variable ); ?>"
102
+ );
94
103
  done();
95
- });
104
+ } );
96
105
 
97
106
  // Test spacing
98
- parserMethods.parseWpAction("{{ wp_action( 'test', 'param', variable ) }}")
99
- .catch( e => console.log( e ) )
107
+ parserMethods
108
+ .parseWpAction( "{{ wp_action( 'test', 'param', variable ) }}" )
109
+ .catch( ( e ) => console.log( e ) )
100
110
  .then( ( result ) => {
101
- assert.equal( result, "<?php do_action( 'test', 'param', $variable ); ?>" );
111
+ assert.equal(
112
+ result,
113
+ "<?php do_action( 'test', 'param', $variable ); ?>"
114
+ );
102
115
  done();
103
- });
104
-
116
+ } );
105
117
  } );
106
-
107
118
  } );
108
-
@@ -5,7 +5,7 @@ const twigToPhpParser = require( '../index' ).twigToPhpParser;
5
5
  const exec = require( 'child_process' ).exec;
6
6
 
7
7
  const fixture = path.join( __dirname, 'fixtures' );
8
- const expectedDir = `${fixture}/template-parts-expected/patterns/`;
8
+ const expectedDir = `${ fixture }/template-parts-expected/patterns/`;
9
9
 
10
10
  const getAppConfiguration = require( '../lib/getConfig' );
11
11
  const appConfiguration = getAppConfiguration( 'parser' );
@@ -22,44 +22,58 @@ const patternShortPaths = [
22
22
  'modules/html-tag-interpolation.php',
23
23
  ];
24
24
 
25
- describe( 'twig to php parser', function() {
26
-
25
+ describe( 'twig to php parser', function () {
27
26
  beforeEach( ( done ) => {
28
-
29
- exec( 'mkdir ' + appConfiguration.phpDir, ( err ) => {
27
+ exec( 'mkdir ' + appConfiguration.phpDir, ( err ) => {
30
28
  if ( err ) {
31
29
  console.error( err );
32
30
  }
33
- });
31
+ } );
34
32
 
35
33
  return twigToPhpParser( appConfiguration )
36
- .catch( ( e ) => console.log( e ) )
37
- .then( ( result ) => done() ); // Catch PHP errors.
38
- });
34
+ .catch( ( e ) => console.log( e ) )
35
+ .then( ( result ) => done() ); // Catch PHP errors.
36
+ } );
39
37
 
40
38
  it( 'creates new files and directories for objects, components, and modules', ( done ) => {
41
- assert.equal( fs.existsSync( appConfiguration.phpDir + 'objects/o-nav.php' ), true );
42
- assert.equal( fs.existsSync( appConfiguration.phpDir + 'components/c-nav-link.php' ), true );
43
- assert.equal( fs.existsSync( appConfiguration.phpDir + 'modules/breadcrumbs.php' ), true );
39
+ assert.equal(
40
+ fs.existsSync( appConfiguration.phpDir + 'objects/o-nav.php' ),
41
+ true
42
+ );
43
+ assert.equal(
44
+ fs.existsSync(
45
+ appConfiguration.phpDir + 'components/c-nav-link.php'
46
+ ),
47
+ true
48
+ );
49
+ assert.equal(
50
+ fs.existsSync(
51
+ appConfiguration.phpDir + 'modules/breadcrumbs.php'
52
+ ),
53
+ true
54
+ );
44
55
  done();
45
- });
56
+ } );
46
57
 
47
58
  it( 'parses patterns as expected', ( done ) => {
48
59
  patternShortPaths.forEach( ( shortpath ) => {
49
- let expectedContents = fs.readFileSync( expectedDir + shortpath ).toString();
50
- let actualContents = fs.readFileSync( appConfiguration.phpDir + shortpath ).toString();
60
+ const expectedContents = fs
61
+ .readFileSync( expectedDir + shortpath )
62
+ .toString();
63
+ const actualContents = fs
64
+ .readFileSync( appConfiguration.phpDir + shortpath )
65
+ .toString();
51
66
 
52
67
  assert.equal( actualContents, expectedContents );
53
- });
68
+ } );
54
69
  done();
55
- });
70
+ } );
56
71
 
57
72
  afterEach( () => {
58
73
  exec( 'rm -r ' + appConfiguration.phpDir, ( err ) => {
59
74
  if ( err ) {
60
75
  console.error( err );
61
76
  }
62
- });
63
- });
64
-
65
- });
77
+ } );
78
+ } );
79
+ } );
package/index.js CHANGED
@@ -7,12 +7,11 @@ const config = getAppConfiguration( 'parser' );
7
7
  /**
8
8
  * Twig to PHP Parser
9
9
  *
10
- * @param {object} config Optional object containing configuration. See the
10
+ * @param {Object} config Optional object containing configuration. See the
11
11
  * package README for supported configuration.
12
12
  */
13
13
 
14
14
  function twigToPhpParser( config = {} ) {
15
-
16
15
  // TODO: We need some kind of wrapper here for config...or better:
17
16
  // move this to pmc-plugins/pmc-larva and scrap this awkward JS
18
17
  // execPhp wrapper situation, and use Larva\Config for this config.
@@ -26,12 +25,10 @@ function twigToPhpParser( config = {} ) {
26
25
  } else {
27
26
  twigDir = config.twigDir;
28
27
  }
29
- }
30
- else if ( 'undefined' !== typeof config.relativeSrcOverride ) {
28
+ } else if ( 'undefined' !== typeof config.relativeSrcOverride ) {
31
29
  if ( config.relativeSrcOverride.startsWith( '/' ) ) {
32
30
  twigDir = config.relativeSrcOverride;
33
- }
34
- else {
31
+ } else {
35
32
  twigDir = path.join( process.cwd(), config.relativeSrcOverride );
36
33
  }
37
34
  }
@@ -45,119 +42,131 @@ function twigToPhpParser( config = {} ) {
45
42
  }
46
43
 
47
44
  return new Promise( ( resolve, reject ) => {
48
-
49
- execPhp( path.resolve( __dirname, './lib/twig-to-php-parser.php' ), config.phpBinaryPath, function( error, php, output ) {
50
-
51
- if ( error ) {
52
- reject( error );
53
- }
54
-
55
- php.twig_to_php_parser( twigDir, phpDir, isUsingPlugin, function( error, result, output, printed ) {
56
-
45
+ execPhp(
46
+ path.resolve( __dirname, './lib/twig-to-php-parser.php' ),
47
+ config.phpBinaryPath,
48
+ function ( error, php, output ) {
57
49
  if ( error ) {
58
50
  reject( error );
59
51
  }
60
52
 
61
- resolve( result );
62
-
63
- });
64
-
65
- });
66
-
67
- });
68
-
69
- };
70
-
71
- function parseIncludePath( twigIncludeStr, patternName, dataName, isUsingPlugin = false ) {
72
-
73
- return new Promise( ( resolve, reject ) => {
74
-
75
- execPhp( path.resolve( __dirname, './lib/twig-to-php-parser.php' ), config.phpBinaryPath, function( error, php, output ) {
76
-
77
- if ( error ) {
78
- reject( error );
53
+ php.twig_to_php_parser(
54
+ twigDir,
55
+ phpDir,
56
+ isUsingPlugin,
57
+ function ( error, result, output, printed ) {
58
+ if ( error ) {
59
+ reject( error );
60
+ }
61
+
62
+ resolve( result );
63
+ }
64
+ );
79
65
  }
66
+ );
67
+ } );
68
+ }
80
69
 
81
- php.parse_include_path( twigIncludeStr, patternName, dataName, isUsingPlugin, function( error, result, output, printed ) {
82
-
70
+ function parseIncludePath(
71
+ twigIncludeStr,
72
+ patternName,
73
+ dataName,
74
+ isUsingPlugin = false
75
+ ) {
76
+ return new Promise( ( resolve, reject ) => {
77
+ execPhp(
78
+ path.resolve( __dirname, './lib/twig-to-php-parser.php' ),
79
+ config.phpBinaryPath,
80
+ function ( error, php, output ) {
83
81
  if ( error ) {
84
82
  reject( error );
85
83
  }
86
84
 
87
- resolve( result );
88
-
89
- });
90
-
91
- });
92
-
93
- });
94
-
95
- };
85
+ php.parse_include_path(
86
+ twigIncludeStr,
87
+ patternName,
88
+ dataName,
89
+ isUsingPlugin,
90
+ function ( error, result, output, printed ) {
91
+ if ( error ) {
92
+ reject( error );
93
+ }
94
+
95
+ resolve( result );
96
+ }
97
+ );
98
+ }
99
+ );
100
+ } );
101
+ }
96
102
 
97
103
  function parseSvgPath( twigSvgIncludeStr, svgName, isUsingPlugin = false ) {
98
-
99
104
  return new Promise( ( resolve, reject ) => {
100
-
101
- execPhp( path.resolve( __dirname, './lib/twig-to-php-parser.php' ), config.phpBinaryPath, function( error, php, output ) {
102
-
103
- if ( error ) {
104
- reject( error );
105
- }
106
-
107
- php.parse_svg_path( twigSvgIncludeStr, svgName, isUsingPlugin, function( error, result, output, printed ) {
108
-
105
+ execPhp(
106
+ path.resolve( __dirname, './lib/twig-to-php-parser.php' ),
107
+ config.phpBinaryPath,
108
+ function ( error, php, output ) {
109
109
  if ( error ) {
110
110
  reject( error );
111
111
  }
112
112
 
113
- resolve( result );
114
-
115
- });
116
-
117
- });
118
-
119
- });
120
-
121
- };
113
+ php.parse_svg_path(
114
+ twigSvgIncludeStr,
115
+ svgName,
116
+ isUsingPlugin,
117
+ function ( error, result, output, printed ) {
118
+ if ( error ) {
119
+ reject( error );
120
+ }
121
+
122
+ resolve( result );
123
+ }
124
+ );
125
+ }
126
+ );
127
+ } );
128
+ }
122
129
 
123
130
  function parseWpAction( twigMarkup ) {
124
131
  return new Promise( ( resolve, reject ) => {
125
-
126
- execPhp( path.resolve( __dirname, './lib/twig-to-php-parser.php' ), config.phpBinaryPath, function( error, php, output ) {
127
-
128
- if ( error ) {
129
- reject( error );
130
- }
131
-
132
- php.parse_wp_action( twigMarkup, function( error, result, output, printed ) {
133
-
132
+ execPhp(
133
+ path.resolve( __dirname, './lib/twig-to-php-parser.php' ),
134
+ config.phpBinaryPath,
135
+ function ( error, php, output ) {
134
136
  if ( error ) {
135
137
  reject( error );
136
138
  }
137
139
 
138
- resolve( result );
139
-
140
- });
141
-
142
- });
143
-
144
- });
140
+ php.parse_wp_action(
141
+ twigMarkup,
142
+ function ( error, result, output, printed ) {
143
+ if ( error ) {
144
+ reject( error );
145
+ }
145
146
 
147
+ resolve( result );
148
+ }
149
+ );
150
+ }
151
+ );
152
+ } );
146
153
  }
147
154
 
148
155
  module.exports = {
149
- twigToPhpParser: twigToPhpParser,
156
+ twigToPhpParser,
150
157
  methods: {
151
- parseIncludePath: parseIncludePath,
152
- parseSvgPath: parseSvgPath,
153
- parseWpAction: parseWpAction
158
+ parseIncludePath,
159
+ parseSvgPath,
160
+ parseWpAction,
154
161
  },
155
162
  run: () => {
156
163
  twigToPhpParser( config )
157
- .catch( e => console.log( e ) ) // PHP errors
158
- .then( result => console.log(
159
- chalk.green( 'Completed parsing Twig templates to PHP.' )
160
- ) )
161
- .catch( e => console.log( e ) );
162
- }
164
+ .catch( ( e ) => console.log( e ) ) // PHP errors
165
+ .then( ( result ) =>
166
+ console.log(
167
+ chalk.green( 'Completed parsing Twig templates to PHP.' )
168
+ )
169
+ )
170
+ .catch( ( e ) => console.log( e ) );
171
+ },
163
172
  };
package/lib/getConfig.js CHANGED
@@ -20,7 +20,6 @@ const path = require( 'path' );
20
20
  */
21
21
 
22
22
  module.exports = function getAppConfiguration( key ) {
23
-
24
23
  try {
25
24
  let appRoot = process.cwd();
26
25
 
@@ -28,15 +27,22 @@ module.exports = function getAppConfiguration( key ) {
28
27
  appRoot = path.join( __dirname, '../__tests__/fixtures' );
29
28
  }
30
29
 
31
- let config = require( `${appRoot}/larva.config.js` )[ key ];
30
+ const config = require( `${ appRoot }/larva.config.js` )[ key ];
32
31
 
33
32
  if ( undefined === config ) {
34
- throw new Error( `Configuration for \`${key}\` is required in larva.config.js. \nPlease refer to the Larva's docs for adding configuration: https://github.com/penske-media-corp/pmc-larva` );
33
+ throw new Error(
34
+ `Configuration for \`${ key }\` is required in larva.config.js. \nPlease refer to the Larva's docs for adding configuration: https://github.com/penske-media-corp/pmc-larva`
35
+ );
35
36
  }
36
37
 
37
38
  return config;
38
-
39
39
  } catch ( error ) {
40
- console.error( chalk.red( chalk.bold( 'There is no larva.config.js in this directory.\n' ) + error ) );
40
+ console.error(
41
+ chalk.red(
42
+ chalk.bold(
43
+ 'There is no larva.config.js in this directory.\n'
44
+ ) + error
45
+ )
46
+ );
41
47
  }
42
48
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penskemediacorp/twig-to-php-parser",
3
- "version": "1.14.0",
3
+ "version": "1.27.1",
4
4
  "description": "> TODO: description",
5
5
  "author": "laras126 <laras126@gmail.com>",
6
6
  "homepage": "https://github.com/penske-media-corp/pmc-larva#readme",
@@ -26,5 +26,5 @@
26
26
  "chalk": "^4.1.0",
27
27
  "exec-php": "0.0.5"
28
28
  },
29
- "gitHead": "fbf545499333b58c945ad92d4be8ffa8a233bae3"
29
+ "gitHead": "cb5a91282f5e1f4f023f2c2f8a97752c2918868c"
30
30
  }