frontfire 0.5.0 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frontfire",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -56,8 +56,8 @@ const entryPoints = [
56
56
  path.resolve( './src/app/main.js' ),
57
57
  path.resolve( './src/app/main.css' )
58
58
  */
59
- `.${path.sep}src${path.sep}app${path.sep}main.js`,
60
- `.${path.sep}src${path.sep}app${path.sep}main.css`
59
+ `./src/app/main.js`,
60
+ `./src/app/main.css`
61
61
  ];
62
62
 
63
63
  const outDirDebug = `${buildDir}/debug/app/`
@@ -66,16 +66,6 @@ const outDirRelease = `${buildDir}/release/app`;
66
66
  const staticAssetsDestDebug = `${buildDir}/debug/assets`;
67
67
  const staticAssetsDestRelease = `${buildDir}/release/assets`;
68
68
 
69
- /*
70
- console.log( "Build folder:" );
71
- console.log( buildDir );
72
-
73
-
74
- console.log( "Release folder:" );
75
- console.log( outDirRelease );
76
-
77
- console.log( "First entry point", entryPoints[ 0 ] );
78
- */
79
69
  export default {
80
70
  "buildDir" : buildDir,
81
71
  "debug" : {
package/src/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #! /usr/bin/env node
2
2
 
3
3
  import fs from "node:fs";
4
+ import { readdir, readFile } from 'node:fs/promises';
4
5
  import ejs from "ejs";
5
6
  import chalk from "chalk";
6
7
 
@@ -17,6 +18,7 @@ import wcTemplate from "./templates/wc-template.html.js";
17
18
  import stateClass from "./templates/state-class.js";
18
19
  import stateTemplate from "./templates/state-template.html.js";
19
20
  import poIndex from "./templates/po-index.js";
21
+ import dictTemplate from "./templates/dictionary.js";
20
22
 
21
23
  async function performInit()
22
24
  {
@@ -36,6 +38,11 @@ async function performInit()
36
38
  }
37
39
  )
38
40
  );
41
+
42
+ if ( false === fs.existsSync( './src/assets' ) )
43
+ {
44
+ fs.mkdirSync( './src/assets' );
45
+ }
39
46
  }
40
47
 
41
48
  async function createInfrontJsStarter( appName = null )
@@ -143,7 +150,7 @@ async function generatesWebComponent( name = null )
143
150
  wcName = name.replace(/[A-Z]/g, (match, offset) => (offset > 0 ? '-' : '') + match.toLowerCase());
144
151
  fileName = fileName.charAt(0).toLowerCase() + fileName.slice(1);
145
152
 
146
- const srcFolder = path.resolve( '.' ) + path.sep + fileName;
153
+ const srcFolder = path.resolve( '.' ) + path.sep + wcName;
147
154
  if ( true === fs.existsSync( srcFolder ) )
148
155
  {
149
156
  console.error( `Folder "${srcFolder}" already exists!` );
@@ -156,13 +163,115 @@ async function generatesWebComponent( name = null )
156
163
  ejs.render( wcIndex, { wcName: wcName, className: className } )
157
164
  );
158
165
  fs.writeFileSync(
159
- srcFolder + path.sep + 'template.js',
166
+ srcFolder + path.sep + 'template.html',
160
167
  ejs.render( wcTemplate, { wcName: wcName } )
161
168
  );
162
169
 
163
170
  console.log( chalk.green.bold( `Web component ${wcName} successfully created.` ) );
164
171
  }
165
172
 
173
+ async function generateDictionary( pathToDictionary, options )
174
+ {
175
+ const defaultLang = 'en';
176
+ const countryCodes = options.countrycodes.split( "," );
177
+ const defaultCountryCode = options.defaulcountrycode;
178
+ const rootFolder = path.resolve( options.rootpath );
179
+ const newDict = [];
180
+ const lKeys = [];
181
+ let currentDict = {};
182
+
183
+ const indexOfDefaultLang = countryCodes.indexOf( defaultLang );
184
+ if ( -1 < indexOfDefaultLang )
185
+ {
186
+ countryCodes.splice( indexOfDefaultLang, 1 );
187
+ }
188
+
189
+ try
190
+ {
191
+ if ( fs.existsSync( pathToDictionary ) )
192
+ {
193
+ let dictContent = fs.readFileSync( pathToDictionary, { encoding: 'utf8' } );
194
+ dictContent = dictContent.replace( "export default", "" );
195
+ dictContent = dictContent.trim();
196
+ dictContent = dictContent.replace( new RegExp(';$', 'gm'), "" );
197
+ try {
198
+ currentDict = JSON.parse( dictContent );
199
+ } catch( e ) {
200
+ console.error( `Cannot parse current dictionary.`, e );
201
+ currentDict = {};
202
+ }
203
+ }
204
+
205
+ const files = await readdir( rootFolder, { recursive : true } );
206
+ for ( let fi = 0; fi < files.length; fi++ )
207
+ {
208
+ const file = files[ fi ];
209
+ if ( false === fs.lstatSync( file ).isFile() )
210
+ {
211
+ continue;
212
+ }
213
+
214
+ const regex = /_lcs\(\s*?[\'|\"|\`](.+)[\'|\"|`]\s*?\)/gm;
215
+ if ( -1 < [ '.html', '.js' ].indexOf( ( '.' + file.split( '.' ).pop() ) ) )
216
+ {
217
+ const str = await readFile( file, 'utf8' );
218
+
219
+ let m;
220
+ while ((m = regex.exec(str)) !== null) {
221
+
222
+ // This is necessary to avoid infinite loops with zero-width matches
223
+ if (m.index === regex.lastIndex) {
224
+ regex.lastIndex++;
225
+ }
226
+
227
+ // The result can be accessed through the `m`-variable.
228
+ m.forEach((match, groupIndex) => {
229
+
230
+ if ( false === match.includes( '_lcs' ) )
231
+ {
232
+ if ( -1 === lKeys.indexOf( match ) )
233
+ {
234
+ lKeys.push( match );
235
+ let newEntry = { "key" : match, trans : [] };
236
+ if ( currentDict.hasOwnProperty( match ) && currentDict[ match ].hasOwnProperty( defaultCountryCode ) && currentDict[ match ][ defaultCountryCode ] !== null )
237
+ {
238
+ newEntry.trans.push( { "cc" : defaultCountryCode, "val" : currentDict[ match ][ defaultCountryCode ] } );
239
+ }
240
+ else
241
+ {
242
+ newEntry.trans.push( { "cc" : defaultCountryCode, "val" : match } );
243
+ }
244
+
245
+ for ( let ci = 0; ci < countryCodes.length; ci++ )
246
+ {
247
+ if ( currentDict.hasOwnProperty( match ) && currentDict[ match ].hasOwnProperty( countryCodes[ ci ] ) && currentDict[ match ][ countryCodes[ ci ] ] !== null )
248
+ {
249
+ newEntry.trans.push( { "cc" : countryCodes[ ci ], "val" : currentDict[ match ][ countryCodes[ ci ] ] } );
250
+ }
251
+ else
252
+ {
253
+ newEntry.trans.push( { "cc" : countryCodes[ ci ], "val" : null } );
254
+ }
255
+ }
256
+ newDict.push( newEntry );
257
+ }
258
+ }
259
+ });
260
+ }
261
+ }
262
+ }
263
+
264
+ const newDictFileContent = ejs.render( dictTemplate, { newDict: newDict } );
265
+ fs.writeFileSync( pathToDictionary, newDictFileContent, { encoding: 'utf8' } );
266
+
267
+ console.log( chalk.green.bold( `Dictionary successfully created under ${pathToDictionary}. Total keys: ${newDict.length}.` ) );
268
+ }
269
+ catch( e )
270
+ {
271
+ console.error( e );
272
+ }
273
+ }
274
+
166
275
  // Try to load custom config
167
276
  let customConfig = null;
168
277
 
@@ -204,6 +313,15 @@ program
204
313
  .description( 'Generates a webcomponent with specific name.' )
205
314
  .action( function( name ) { generatesWebComponent( name ); } );
206
315
 
316
+ program
317
+ .command( 'gd' )
318
+ .argument( '<pathToDictionary>', 'Path to dictionary file. If it exists, it gets overwritten.' )
319
+ .option( '-cc, --countrycodes <ccodes>', 'Comma seperated list of country codes.', 'en,de' )
320
+ .option( '-dcc, --defaulcountrycode <ccode>', 'Default country code', 'en' )
321
+ .option( '-rp, --rootpath <rootpath>', 'Root path to parse files for translations.', './' )
322
+ .description( 'Generates a dictionary file.' )
323
+ .action( function( pathToDictionary, options ) { generateDictionary( pathToDictionary, options ); } );
324
+
207
325
  program
208
326
  .command( 'gs' )
209
327
  .argument( '<name>', 'Name of state.' )
@@ -0,0 +1,16 @@
1
+ export default 'export default {\n'+
2
+ '<% for ( let ki = 0; ki < newDict.length; ki++ ) { %>\n' +
3
+ ' "<%= newDict[ ki ].key %>" : {\n' +
4
+ '<% for( let ti = 0; ti < newDict[ ki ].trans.length; ti++ ) { %>' +
5
+ '<% if ( newDict[ ki ].trans[ ti ].val === null ) { %>' +
6
+ ' "<%= newDict[ ki ].trans[ ti ].cc; %>" : null<%= ((ti+1) < newDict[ ki ].trans.length) ? "," : "" %>' +
7
+ '<% } else { %>' +
8
+ ' "<%= newDict[ ki ].trans[ ti ].cc; %>" : "<%= newDict[ ki ].trans[ ti ].val; %>"<%= ((ti+1) < newDict[ ki ].trans.length) ? "," : "" %>' +
9
+ '<% } %>' +
10
+ '<% if ((ti+1) < newDict[ ki ].trans.length) { %> ' +
11
+ '\n' +
12
+ '<% } %>' +
13
+ '<% } %>\n' +
14
+ ' }<%= ((ki+1) < newDict.length) ? "," : "" %>\n' +
15
+ '<% } %>\n' +
16
+ '};';
@@ -1,6 +1,6 @@
1
- export default 'import { BaseState } from "infrontjs";\n' +
1
+ export default 'import { State } from "infrontjs";\n' +
2
2
  'import template from \'./<%= stateName %>Template.html\';\n' +
3
- 'export class <%= stateName %> extends BaseState\n' +
3
+ 'export class <%= stateName %>State extends State\n' +
4
4
  '{\n' +
5
5
  ' static ROUTE = "/<%= stateId %>";\n' +
6
6
  ' static ID = "<%= stateId %>";\n' +
@@ -29,4 +29,4 @@ export default '' +
29
29
  ' }\n' +
30
30
  '}\n' +
31
31
  '\n' +
32
- 'customElements.define(<%= wcName %>, <%= className %> );'
32
+ 'customElements.define( "<%= wcName %>", <%= className %> );'