mythix-cli 1.3.6 → 1.3.8

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 (2) hide show
  1. package/bin/runner.js +62 -41
  2. package/package.json +1 -1
package/bin/runner.js CHANGED
@@ -94,11 +94,33 @@ function spawnProcess(name, args, options) {
94
94
  });
95
95
  }
96
96
 
97
- function createTemplateEngineContext(appName) {
98
- let context = Object.create(null);
97
+ function getFormattedAppName(appName) {
98
+ return appName.trim().replace(/[^\w-]+/g, '-').replace(/^[^a-zA-Z0-9]+/g, '').replace(/[^a-zA-Z0-9]+$/g, '').toLowerCase();
99
+ }
100
+
101
+ function getFormattedAppDisplayName(appName) {
102
+ return Nife.capitalize(getFormattedAppName(appName).replace(/[^a-zA-Z0-9]+/g, ' ').trim(), true);
103
+ }
99
104
 
100
- context.APP_NAME = () => appName;
101
- context.RANDOM_SHA256 = () => randomHash('sha256');
105
+ function createTemplateEngineContext(templateClonePath, _appName) {
106
+ let context = Object.create(null);
107
+ let appName = getFormattedAppName(_appName);
108
+ let appDisplayName = getFormattedAppDisplayName(_appName);
109
+
110
+ context.APP_NAME = () => appName;
111
+ context.APP_DISPLAY_NAME = () => appDisplayName;
112
+ context.RANDOM_SHA256 = () => randomHash('sha256');
113
+
114
+ try {
115
+ let helpersPath = require.resolve(Path.join(templateClonePath, 'mythix-cli-template-helpers'));
116
+ let projectHelpers = require(helpersPath);
117
+ if (projectHelpers && projectHelpers.default)
118
+ projectHelpers = projectHelpers.default;
119
+
120
+ context = Object.assign({}, projectHelpers, context);
121
+
122
+ FileSystem.unlinkSync(helpersPath);
123
+ } catch (error) {}
102
124
 
103
125
  return context;
104
126
  }
@@ -163,14 +185,14 @@ function runTemplateEngineOnProject(projectPath, context) {
163
185
  );
164
186
  }
165
187
 
166
- async function initApplication(args) {
188
+ async function createApplication(args) {
167
189
  if (!args.dir || !('' + args.dir).match(/\S/))
168
190
  args.dir = Path.resolve(process.env.PWD);
169
191
  else
170
192
  args.dir = Path.resolve(args.dir);
171
193
 
172
194
  try {
173
- let templateClonePath = Path.resolve(args.dir, args.appName);
195
+ let templateClonePath = Path.resolve(args.dir, getFormattedAppName(args.appName));
174
196
  let processArgs = [ args.template, templateClonePath ];
175
197
  let tag;
176
198
 
@@ -186,24 +208,26 @@ async function initApplication(args) {
186
208
 
187
209
  FileSystem.rmSync(Path.resolve(templateClonePath, '.git'), { recursive: true, force: true });
188
210
 
189
- runTemplateEngineOnProject(templateClonePath, createTemplateEngineContext(args.appName));
190
-
191
211
  await spawnProcess('npm', [ 'i' ], { env: { PWD: templateClonePath, CWD: templateClonePath }, cwd: templateClonePath });
192
212
 
193
- console.log(`Empty mythix project created at ${templateClonePath}`);
213
+ runTemplateEngineOnProject(templateClonePath, createTemplateEngineContext(templateClonePath, args.appName));
214
+
215
+ console.log(`Mythix application created at ${templateClonePath}`);
194
216
  console.log('To finalize setup you need to:');
195
- console.log(' 1) Select and configure the correct database driver for mythix-orm');
217
+ console.log(' 1) Install the correct database driver (default is mythix-orm-postgresql), and update configuration files:');
218
+ console.log(` a) Open and modify ${Path.join(templateClonePath, 'app', 'config', 'db-config.js')} for database configuration`);
219
+ console.log(` b) Open and modify ${Path.join(templateClonePath, 'app', 'config', 'sensitive.js')} for API keys`);
196
220
  console.log(' 2) Define the models for your application');
197
- console.log(' 3) Create an initial migration for your models: `npx mythix-cli makemigrations --name initial`');
198
- console.log(' 4) Run migrations: `npx mythix-cli migrate`');
199
- console.log(' 5) Finally run your application: `npx mythix-cli serve`');
221
+ console.log(' 3) Run migrations: `npx mythix-cli migrate`');
222
+ console.log(' 4) Run the DB seeder: `mythix-cli shell` + `await seedDB();`');
223
+ console.log(' 5) Finally run your application: `npm run -s start`');
200
224
  } catch (error) {
201
225
  console.error('ERROR: ', error);
202
226
  process.exit(1);
203
227
  }
204
228
  }
205
229
 
206
- function generateCommandHelp(commandsObj, globalHelp) {
230
+ async function generateCommandHelp(application, commandsObj, globalHelp) {
207
231
  let commandNames = Object.keys(commandsObj || {});
208
232
  for (let i = 0, il = commandNames.length; i < il; i++) {
209
233
  let commandName = commandNames[i];
@@ -211,7 +235,7 @@ function generateCommandHelp(commandsObj, globalHelp) {
211
235
  let help = null;
212
236
 
213
237
  if (typeof Klass.commandArguments === 'function') {
214
- let result = (Klass.commandArguments() || {});
238
+ let result = ((await Klass.commandArguments(application, 'help')) || {});
215
239
  help = result.help;
216
240
  }
217
241
 
@@ -230,7 +254,7 @@ function generateCommandHelp(commandsObj, globalHelp) {
230
254
  }
231
255
  }
232
256
 
233
- function commandRunners(commandsObj, context, showHelp) {
257
+ async function commandRunners(application, commandsObj, context, showHelp) {
234
258
  let commandNames = Object.keys(commandsObj);
235
259
 
236
260
  for (let i = 0, il = commandNames.length; i < il; i++) {
@@ -239,16 +263,16 @@ function commandRunners(commandsObj, context, showHelp) {
239
263
  let runner = null;
240
264
 
241
265
  if (typeof Klass.commandArguments === 'function') {
242
- let result = (Klass.commandArguments() || {});
266
+ let result = ((await Klass.commandArguments(application, 'runner')) || {});
243
267
  runner = result.runner;
244
268
  }
245
269
 
246
- let result = context.match(commandName, ({ scope, store, fetch }, parserResult) => {
270
+ let result = await context.match(commandName, async ({ scope, store, fetch, showHelp }, parserResult) => {
247
271
  store({ command: commandName });
248
272
 
249
- return scope(commandName, (context) => {
273
+ return await scope(commandName, async (context) => {
250
274
  if (typeof runner === 'function') {
251
- let runnerResult = runner(context, parserResult);
275
+ let runnerResult = await runner(context, parserResult);
252
276
  if (!runnerResult) {
253
277
  showHelp(commandName);
254
278
  return false;
@@ -296,8 +320,8 @@ function commandRunners(commandsObj, context, showHelp) {
296
320
  // Consume to VOID
297
321
  $('--', () => {});
298
322
 
299
- $('init', ({ scope }) => {
300
- return scope('init', ({ $ }) => {
323
+ $('create', ({ scope }) => {
324
+ return scope('create', ({ $ }) => {
301
325
  $('--dir', Types.STRING({ format: Path.resolve }), { name: 'dir' })
302
326
  || $('-d', Types.STRING({ format: Path.resolve }), { name: 'dir' })
303
327
  || store({ dir: Path.resolve('./') });
@@ -306,12 +330,12 @@ function commandRunners(commandsObj, context, showHelp) {
306
330
  || $('-t', Types.STRING(), { name: 'template' })
307
331
  || store({ template: 'https://github.com/th317erd/mythix-app-template.git' });
308
332
 
309
- return $(/^([\w](?:[\w-]+)?)$/, ({ store }, parserResult) => {
310
- store({ name: parserResult.name });
333
+ return $(/^.*$/, ({ store }, parserResult) => {
334
+ store({ appName: parserResult.appName });
311
335
  return true;
312
336
  }, {
313
- formatParserResult: (value) => {
314
- return { name: value[1] };
337
+ formatParsedResult: (value) => {
338
+ return { appName: value[0] };
315
339
  },
316
340
  });
317
341
  });
@@ -331,22 +355,22 @@ function commandRunners(commandsObj, context, showHelp) {
331
355
  '--config={config file path} | --config {config file path}': 'Specify the path to ".mythix-config.js". Default = "{CWD}/.mythix-config.js".',
332
356
  '-e={environment} | -e {environment} | --env={environment} | --env {environment}': 'Specify the default environment to use. Default = "development".',
333
357
  '--runtime={runtime} | --runtime {runtime}': 'Specify the runtime to use to launch the command. Default = "node"',
334
- 'init': {
335
- '@usage': 'mythix-cli init app-name [options]',
358
+ 'create': {
359
+ '@usage': 'mythix-cli create [app name] [options]',
336
360
  '@title': 'Initialize a blank mythix application',
337
- '@see': 'See: \'mythix-cli init --help\' for more help',
361
+ '@see': 'See: \'mythix-cli create --help\' for more help',
338
362
  '-d={path} | -d {path} | --dir={path} | --dir {path}': 'Specify directory to create new application in. Default = "./"',
339
363
  '-t={url} | -t {url} | --template={url} | --template {url}': 'Specify a git repository URL to use for a template to create the application with. Default = "https://github.com/th317erd/mythix-app-template.git".'
340
364
  },
341
365
  };
342
366
 
343
- if (argOptions.init) {
344
- if (Nife.isEmpty(argOptions.init)) {
345
- showHelp(help.init);
367
+ if (argOptions.create) {
368
+ if (Nife.isEmpty(argOptions.create)) {
369
+ showHelp(help.create);
346
370
  return process.exit(1);
347
371
  }
348
372
 
349
- await initApplication(argOptions.init);
373
+ await createApplication(argOptions.create);
350
374
 
351
375
  return;
352
376
  }
@@ -354,13 +378,13 @@ function commandRunners(commandsObj, context, showHelp) {
354
378
  try {
355
379
  let helpShown = false;
356
380
 
357
- const customShowHelp = (scope) => {
381
+ const customShowHelp = (subHelp) => {
358
382
  if (helpShown)
359
383
  return;
360
384
 
361
385
  helpShown = true;
362
386
 
363
- showHelp(help[scope] || help)
387
+ showHelp(subHelp)
364
388
  };
365
389
 
366
390
  let rootOptions = { help, showHelp: customShowHelp, helpArgPattern: null };
@@ -377,9 +401,9 @@ function commandRunners(commandsObj, context, showHelp) {
377
401
  let applicationOptions = application.getOptions();
378
402
 
379
403
  let commands = await mythixCLI.loadCommands(applicationOptions.commandsPath);
380
- generateCommandHelp(commands, help);
404
+ await generateCommandHelp(application, commands, help);
381
405
 
382
- let commandContext = CMDed((context) => {
406
+ let commandContext = await CMDed(async (context) => {
383
407
  let { $, Types, store } = context;
384
408
 
385
409
  store('mythixApplication', application);
@@ -395,7 +419,7 @@ function commandRunners(commandsObj, context, showHelp) {
395
419
 
396
420
  $('--help', Types.BOOLEAN());
397
421
 
398
- return commandRunners(commands, context, customShowHelp);
422
+ return await commandRunners(application, commands, context, customShowHelp);
399
423
  }, rootOptions);
400
424
 
401
425
  if (!commandContext) {
@@ -415,7 +439,4 @@ function commandRunners(commandsObj, context, showHelp) {
415
439
  } catch (error) {
416
440
  console.error(error);
417
441
  }
418
- // rootCommand = SimpleYargs.buildCommands(rootCommand, initApplication, [ 'init(Create a new application with the given name) <appName:string(Specify application name)> [-d,-dir:string(Path at which to create application)] [-t,-template:string(Git URL to use to clone and create new project from)=https://github.com/th317erd/mythix-app-template.git(Default "https://github.com/th317erd/mythix-app-template.git")] [-tag:string(Specify tag or commit hash to clone template from)]' ]);
419
-
420
- // rootCommand.version(packageJSON.version).strictCommands().wrap(120).parse();
421
442
  })();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "mythix-cli": "./bin/mythix-cli.js"
4
4
  },
5
5
  "name": "mythix-cli",
6
- "version": "1.3.6",
6
+ "version": "1.3.8",
7
7
  "description": "Mythix CLI utility",
8
8
  "main": "src/index.js",
9
9
  "scripts": {