mythix-cli 1.3.9 → 2.0.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Wyatt Greenway
3
+ Copyright (c) 2023 Wyatt Greenway
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1,8 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const Path = require('path');
4
- const { spawn } = require('child_process');
5
- const { CMDed, Types } = require('cmded');
3
+ import Path from 'node:path';
4
+ import { spawn } from 'node:child_process';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { CMDed, Types } from 'cmded';
7
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = Path.dirname(__filename);
6
10
 
7
11
  function spawnCommand(command, args, options) {
8
12
  return new Promise((resolve, reject) => {
@@ -47,7 +51,7 @@ function spawnCommand(command, args, options) {
47
51
  });
48
52
 
49
53
  try {
50
- config = require(argOptions.config);
54
+ config = await import(argOptions.config);
51
55
  } catch (error) {
52
56
  config = {};
53
57
  }
@@ -62,7 +66,7 @@ function spawnCommand(command, args, options) {
62
66
  let command = commands[i];
63
67
 
64
68
  try {
65
- await spawnCommand(command, runtimeArgs.concat([ Path.resolve(__dirname, 'runner.js') ], args));
69
+ await spawnCommand(command, runtimeArgs.concat([ Path.resolve(__dirname, 'runner.mjs') ], args));
66
70
  break;
67
71
  } catch (error) {
68
72
  if (error.code === 'ENOENT' && (i + 1) < commands.length)
@@ -1,17 +1,23 @@
1
- const Nife = require('nife');
2
- const Path = require('path');
3
- const FileSystem = require('fs');
4
- const { spawn } = require('child_process');
5
-
6
- const {
1
+ import Path from 'node:path';
2
+ import FileSystem from 'node:fs';
3
+ import { spawn } from 'node:child_process';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { createRequire } from 'node:module';
6
+ import Nife from 'nife';
7
+
8
+ const require = createRequire(import.meta.url);
9
+ const __filename = fileURLToPath(import.meta.url);
10
+ const __dirname = Path.dirname(__filename);
11
+
12
+ import {
7
13
  CMDed,
8
14
  showHelp,
9
- } = require('cmded');
15
+ } from 'cmded';
10
16
 
11
- const {
17
+ import {
12
18
  createHash,
13
19
  randomFillSync,
14
- } = require('crypto');
20
+ } from 'node:crypto';
15
21
 
16
22
  function randomBytes(length) {
17
23
  let buffer = Buffer.alloc(length);
@@ -47,7 +53,6 @@ function walkDir(rootPath, _options, _callback, _allFiles, _depth) {
47
53
  else if (filterFunc instanceof RegExp && !filterFunc.match(fullFileName))
48
54
  continue;
49
55
 
50
-
51
56
  if (stats.isDirectory()) {
52
57
  walkDir(fullFileName, options, callback, allFiles, depth + 1);
53
58
 
@@ -102,7 +107,29 @@ function getFormattedAppDisplayName(appName) {
102
107
  return Nife.capitalize(getFormattedAppName(appName).replace(/[^a-zA-Z0-9]+/g, ' ').trim(), true);
103
108
  }
104
109
 
105
- function createTemplateEngineContext(templateClonePath, _appName) {
110
+ function resolveJavascriptFileName(filePathWithoutExtension) {
111
+ if (FileSystem.existsSync(filePathWithoutExtension)) {
112
+ let stats = FileSystem.statSync(filePathWithoutExtension);
113
+ if (stats.isDirectory())
114
+ return resolveJavascriptFileName(`${filePathWithoutExtension}/index`);
115
+
116
+ return filePathWithoutExtension;
117
+ }
118
+
119
+ let filePath = `${filePathWithoutExtension}.mjs`;
120
+ if (FileSystem.existsSync(filePath))
121
+ return filePath;
122
+
123
+ filePath = `${filePathWithoutExtension}.cjs`;
124
+ if (FileSystem.existsSync(filePath))
125
+ return filePath;
126
+
127
+ filePath = `${filePathWithoutExtension}.js`;
128
+ if (FileSystem.existsSync(filePath))
129
+ return filePath;
130
+ }
131
+
132
+ async function createTemplateEngineContext(templateClonePath, _appName) {
106
133
  let context = Object.create(null);
107
134
  let appName = getFormattedAppName(_appName);
108
135
  let appDisplayName = getFormattedAppDisplayName(_appName);
@@ -112,24 +139,26 @@ function createTemplateEngineContext(templateClonePath, _appName) {
112
139
  context.RANDOM_SHA256 = () => randomHash('sha256');
113
140
 
114
141
  try {
115
- let helpersPath = require.resolve(Path.join(templateClonePath, 'mythix-cli-template-helpers'));
116
- let projectHelpers = require(helpersPath);
142
+ let helpersPath = resolveJavascriptFileName(Path.join(templateClonePath, 'mythix-cli-template-helpers'));
143
+ let projectHelpers = await import(helpersPath);
117
144
  if (projectHelpers && projectHelpers.default)
118
145
  projectHelpers = projectHelpers.default;
119
146
 
120
147
  context = Object.assign({}, projectHelpers, context);
121
148
 
122
149
  FileSystem.unlinkSync(helpersPath);
123
- } catch (error) {}
150
+ } catch (error) {
151
+ console.error('Unable to import helpers: ', error);
152
+ }
124
153
 
125
154
  return context;
126
155
  }
127
156
 
128
157
  function getTemplatedFileName(fileName, context) {
129
- return fileName.replace(/__([A-Z0-9_]+)__/g, function(m, varName) {
158
+ return fileName.replace(/\b__([A-Z0-9_-]+)__\b/g, function(m, varName) {
130
159
  let func = context[varName];
131
160
  if (typeof func !== 'function')
132
- return '';
161
+ return varName;
133
162
 
134
163
  return func();
135
164
  }).replace(/__/g, '');
@@ -210,7 +239,7 @@ async function createApplication(args) {
210
239
 
211
240
  await spawnProcess('npm', [ 'i' ], { env: { PWD: templateClonePath, CWD: templateClonePath }, cwd: templateClonePath });
212
241
 
213
- runTemplateEngineOnProject(templateClonePath, createTemplateEngineContext(templateClonePath, args.appName));
242
+ runTemplateEngineOnProject(templateClonePath, await createTemplateEngineContext(templateClonePath, args.appName));
214
243
 
215
244
  console.log(`Mythix application created at ${templateClonePath}`);
216
245
  console.log('To finalize setup you need to:');
@@ -295,9 +324,20 @@ async function commandRunners(application, commandsObj, context, showHelp) {
295
324
  return false;
296
325
  }
297
326
 
327
+ function loadJSON(filePath, defaultValue) {
328
+ try {
329
+ let content = FileSystem.readFileSync(filePath, 'utf8');
330
+ return JSON.parse(content);
331
+ } catch (error) {
332
+ console.error(`Error loading JSON file "${filePath}": `, error);
333
+ return (arguments.length > 1) ? defaultValue : {};
334
+ }
335
+ }
336
+
337
+
298
338
  (async function() {
299
339
  const packageJSONPath = Path.resolve(__dirname, '..', 'package.json');
300
- const packageJSON = require(packageJSONPath);
340
+ const packageJSON = loadJSON(packageJSONPath, {});
301
341
 
302
342
  // Windows hack
303
343
  if(!process.env.PWD)
@@ -389,29 +429,30 @@ async function commandRunners(application, commandsObj, context, showHelp) {
389
429
 
390
430
  let rootOptions;
391
431
  let mythixPath;
392
- let mythixCLIPAth;
432
+ let mythixCLIPath;
393
433
  let mythixCLI;
394
434
  let config;
395
435
 
396
436
  try {
397
437
  rootOptions = { help, showHelp: customShowHelp, helpArgPattern: null };
398
- mythixPath = Path.dirname(require.resolve('mythix', { paths: [ process.env.PWD, Path.resolve(process.env.PWD, 'node_modules') ] }));
399
- mythixCLIPAth = Path.resolve(mythixPath, 'cli');
400
- mythixCLI = require(mythixCLIPAth);
401
- config = mythixCLI.loadMythixConfig(argOptions.config);
438
+ mythixCLIPath = Path.resolve(require.resolve('mythix', { paths: [ process.env.PWD, Path.resolve(process.env.PWD, 'node_modules') ] }));
439
+ mythixPath = Path.dirname(mythixCLIPath);
440
+ mythixCLI = (await import(mythixCLIPath)).CLI;
441
+ config = await mythixCLI.loadMythixConfig(argOptions.config);
402
442
  } catch (error) {
443
+ console.error('THERE WAS AN ERROR: ', error);
403
444
  customShowHelp(help);
404
445
  process.exit(1);
405
446
  }
406
447
 
407
- let Application = config.getApplicationClass(config);
448
+ let Application = await config.getApplicationClass(config);
408
449
  if (typeof Application !== 'function')
409
450
  throw new Error('Expected to find an Application class from "getApplicationClass", but none was returned.');
410
451
 
411
- let application = await mythixCLI.createApplication(Application, { autoReload: false, database: false, httpServer: false });
452
+ let application = await mythixCLI.createApplication(Application, { cli: true, database: false, httpServer: false });
412
453
  let applicationOptions = application.getOptions();
413
454
 
414
- let commands = await mythixCLI.loadCommands(applicationOptions.commandsPath);
455
+ let commands = Application.getCommandList();
415
456
  await generateCommandHelp(application, commands, help);
416
457
 
417
458
  let commandContext = await CMDed(async (context) => {
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "bin": {
3
- "mythix-cli": "./bin/mythix-cli.js"
3
+ "mythix-cli": "./bin/mythix-cli.mjs"
4
4
  },
5
5
  "name": "mythix-cli",
6
- "version": "1.3.9",
6
+ "version": "2.0.0",
7
7
  "description": "Mythix CLI utility",
8
- "main": "src/index.js",
8
+ "main": "src/index.mjs",
9
+ "type": "module",
9
10
  "scripts": {
10
11
  "test": "echo \"Error: no test specified\" && exit 1"
11
12
  },
@@ -27,13 +28,13 @@
27
28
  },
28
29
  "homepage": "https://github.com/th317erd/mythix-cli#readme",
29
30
  "peerDependencies": {
30
- "mythix": "^2.4.14"
31
+ "mythix": "^4.0.1"
31
32
  },
32
33
  "dependencies": {
33
34
  "cmded": "^1.2.5",
34
- "nife": "^1.11.3"
35
+ "nife": "^1.12.1"
35
36
  },
36
37
  "devDependencies": {
37
- "eslint": "^8.23.0"
38
+ "eslint": "^8.43.0"
38
39
  }
39
40
  }
package/src/index.mjs ADDED
@@ -0,0 +1 @@
1
+ export {};
package/src/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = {};