@dainprotocol/cli 1.0.4 → 1.0.6

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.
@@ -68,19 +68,20 @@ var utils_1 = require("../utils");
68
68
  var ora_1 = __importDefault(require("ora"));
69
69
  var fs_extra_1 = __importDefault(require("fs-extra"));
70
70
  var path_1 = __importDefault(require("path"));
71
- function build() {
71
+ function build(options) {
72
72
  return __awaiter(this, void 0, void 0, function () {
73
- var spinner, config, staticDir, error_1;
73
+ var spinner, config, outDir, staticDir, error_1;
74
74
  return __generator(this, function (_a) {
75
75
  switch (_a.label) {
76
76
  case 0:
77
77
  spinner = (0, ora_1.default)('Building project...').start();
78
- config = (0, utils_1.getDainConfig)();
78
+ config = (0, utils_1.getDainConfig)(options.config);
79
+ outDir = config['out-dir'] || 'build';
79
80
  _a.label = 1;
80
81
  case 1:
81
82
  _a.trys.push([1, 7, , 8]);
82
83
  // Ensure build directory exists
83
- return [4 /*yield*/, fs_extra_1.default.ensureDir('build')];
84
+ return [4 /*yield*/, fs_extra_1.default.ensureDir(outDir)];
84
85
  case 2:
85
86
  // Ensure build directory exists
86
87
  _a.sent();
@@ -90,26 +91,28 @@ function build() {
90
91
  bundle: true,
91
92
  platform: 'node',
92
93
  target: 'node14',
93
- outfile: 'build/index.js',
94
+ outfile: path_1.default.join(outDir, 'index.js'),
94
95
  minify: true,
95
96
  sourcemap: true,
96
97
  })];
97
98
  case 3:
98
99
  // Build the main application
99
100
  _a.sent();
100
- staticDir = path_1.default.join(process.cwd(), 'static');
101
+ staticDir = config['static-dir']
102
+ ? path_1.default.join(process.cwd(), config['static-dir'])
103
+ : path_1.default.join(process.cwd(), 'static');
101
104
  if (!fs_extra_1.default.existsSync(staticDir)) return [3 /*break*/, 5];
102
- return [4 /*yield*/, fs_extra_1.default.copy(staticDir, 'build/static')];
105
+ return [4 /*yield*/, fs_extra_1.default.copy(staticDir, path_1.default.join(outDir, 'static'))];
103
106
  case 4:
104
107
  _a.sent();
105
- (0, utils_1.logInfo)('Static files copied to build/static');
108
+ (0, utils_1.logInfo)("Static files copied to ".concat(path_1.default.join(outDir, 'static')));
106
109
  return [3 /*break*/, 6];
107
110
  case 5:
108
111
  (0, utils_1.logInfo)('No static directory found. Skipping static file copy.');
109
112
  _a.label = 6;
110
113
  case 6:
111
114
  spinner.succeed('Build completed successfully.');
112
- (0, utils_1.logSuccess)('Project built and ready for deployment.');
115
+ (0, utils_1.logSuccess)("Project built and ready for deployment in ".concat(outDir, "."));
113
116
  return [3 /*break*/, 8];
114
117
  case 7:
115
118
  error_1 = _a.sent();
@@ -44,25 +44,28 @@ var build_1 = __importDefault(require("./build"));
44
44
  var utils_1 = require("../utils");
45
45
  var ora_1 = __importDefault(require("ora"));
46
46
  var fs_extra_1 = __importDefault(require("fs-extra"));
47
- function deploy() {
47
+ var path_1 = __importDefault(require("path"));
48
+ function deploy(options) {
48
49
  return __awaiter(this, void 0, void 0, function () {
49
- var spinner, staticFilesPath, error_1;
50
+ var config, spinner, outDir, staticFilesPath, error_1;
50
51
  return __generator(this, function (_a) {
51
52
  switch (_a.label) {
52
53
  case 0:
54
+ config = (0, utils_1.getDainConfig)(options.config);
53
55
  spinner = (0, ora_1.default)("Deploying project...").start();
54
56
  _a.label = 1;
55
57
  case 1:
56
58
  _a.trys.push([1, 3, , 4]);
57
59
  // First, build the project
58
- return [4 /*yield*/, (0, build_1.default)()];
60
+ return [4 /*yield*/, (0, build_1.default)({ config: options.config })];
59
61
  case 2:
60
62
  // First, build the project
61
63
  _a.sent();
64
+ outDir = config['out-dir'] || 'build';
62
65
  // Here you would add your deployment logic
63
- // For example, uploading the build/index.js file and static files to your server
66
+ // For example, uploading the outDir/index.js file and static files to your server
64
67
  (0, utils_1.logInfo)("Deployment logic to be implemented");
65
- staticFilesPath = (0, utils_1.getStaticFilesPath)();
68
+ staticFilesPath = path_1.default.join(outDir, 'static');
66
69
  if (fs_extra_1.default.existsSync(staticFilesPath)) {
67
70
  (0, utils_1.logInfo)("Static files are available at: ".concat(staticFilesPath));
68
71
  // Here you would implement the logic to deploy these static files
@@ -78,6 +81,7 @@ function deploy() {
78
81
  error_1 = _a.sent();
79
82
  spinner.fail("Deployment failed.");
80
83
  (0, utils_1.logError)("Error during deployment", error_1);
84
+ process.exit(1);
81
85
  return [3 /*break*/, 4];
82
86
  case 4: return [2 /*return*/];
83
87
  }
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -47,13 +58,13 @@ var chokidar_1 = __importDefault(require("chokidar"));
47
58
  var path_1 = __importDefault(require("path"));
48
59
  var childProcess = null;
49
60
  var watcher = null;
50
- function startProcess(command) {
61
+ function startProcess(command, env) {
51
62
  var _a, _b;
52
63
  if (childProcess) {
53
64
  childProcess.kill();
54
65
  }
55
66
  var spinner = (0, ora_1.default)('Starting development server...').start();
56
- childProcess = (0, child_process_1.exec)(command);
67
+ childProcess = (0, child_process_1.exec)(command, { env: __assign(__assign({}, process.env), env) });
57
68
  (_a = childProcess.stdout) === null || _a === void 0 ? void 0 : _a.on('data', function (data) {
58
69
  spinner.succeed('Development server started.');
59
70
  console.log(data);
@@ -82,27 +93,36 @@ function cleanup() {
82
93
  }
83
94
  function dev(options) {
84
95
  return __awaiter(this, void 0, void 0, function () {
85
- var config, command, proxyServer, watchPaths, error_1;
96
+ var config, command, envVars, proxyServer, watchPaths, error_1;
86
97
  return __generator(this, function (_a) {
87
98
  switch (_a.label) {
88
99
  case 0:
89
- process.env.PORT = options.port;
90
- config = (0, utils_1.getDainConfig)();
100
+ config = (0, utils_1.getDainConfig)(options.config);
91
101
  command = "ts-node ".concat(config['main-file']);
102
+ envVars = {
103
+ PORT: options.port,
104
+ DAIN_API_KEY: config['api-key'],
105
+ DAIN_PROJECT_ID: config['project-id'],
106
+ DAIN_ENVIRONMENT: config['environment'],
107
+ DAIN_OUT_DIR: config['out-dir']
108
+ };
92
109
  proxyServer = null;
93
110
  _a.label = 1;
94
111
  case 1:
95
112
  _a.trys.push([1, 4, , 5]);
96
- startProcess(command);
113
+ startProcess(command, envVars);
97
114
  if (!!options.noproxy) return [3 /*break*/, 3];
98
- return [4 /*yield*/, (0, utils_1.setupProxy)(options.port)];
115
+ if (!config['api-key']) {
116
+ throw new Error("'api-key' is required when using development proxy");
117
+ }
118
+ return [4 /*yield*/, (0, utils_1.setupProxy)(options.port, config['api-key'])];
99
119
  case 2:
100
120
  proxyServer = _a.sent();
101
121
  _a.label = 3;
102
122
  case 3:
103
123
  watchPaths = [
104
124
  path_1.default.dirname(config['main-file']),
105
- (0, utils_1.getStaticFilesPath)()
125
+ config['static-dir'] ? path_1.default.join(process.cwd(), config['static-dir']) : (0, utils_1.getStaticFilesPath)()
106
126
  ];
107
127
  watcher = chokidar_1.default.watch(watchPaths, {
108
128
  ignored: /(^|[\/\\])\../, // ignore dotfiles
@@ -110,7 +130,7 @@ function dev(options) {
110
130
  });
111
131
  watcher.on('change', function (path) {
112
132
  (0, utils_1.logInfo)("File ".concat(path, " has been changed. Restarting..."));
113
- startProcess(command);
133
+ startProcess(command, envVars);
114
134
  });
115
135
  (0, utils_1.logInfo)('Watching for file changes...');
116
136
  // Add signal handlers for graceful shutdown
package/dist/index.js CHANGED
@@ -30,20 +30,23 @@ program
30
30
  .description('Run the project in development mode')
31
31
  .option('--port <port>', 'Set the port number', '2022')
32
32
  .option('--noproxy', 'Disable localtunnel proxy')
33
+ .option('-c, --config <path>', 'Path to custom configuration file')
33
34
  .action(function (options) {
34
35
  (0, dev_1.default)(options);
35
36
  });
36
37
  program
37
38
  .command('deploy')
38
39
  .description('Deploy the project')
39
- .action(function () {
40
- (0, deploy_1.default)();
40
+ .option('-c, --config <path>', 'Path to custom configuration file')
41
+ .action(function (options) {
42
+ (0, deploy_1.default)(options);
41
43
  });
42
44
  program
43
45
  .command('build')
44
46
  .description('Build the project')
45
- .action(function () {
46
- (0, build_1.default)();
47
+ .option('-c, --config <path>', 'Path to custom configuration file')
48
+ .action(function (options) {
49
+ (0, build_1.default)(options);
47
50
  });
48
51
  program
49
52
  .command('start')
@@ -1,5 +1,10 @@
1
1
  {
2
2
  "project-id": "",
3
- "project-api-key": "",
4
- "main-file": "src/index.ts"
3
+ "api-key": "",
4
+ "main-file": "src/index.ts",
5
+ "static-dir": "static",
6
+ "out-dir": "dist",
7
+ "environment": "development",
8
+ "version": "1.0.0",
9
+ "tunnel-base-url": "wss:///devtunnel.dain-local.com"
5
10
  }
@@ -16,6 +16,7 @@
16
16
  "devDependencies": {
17
17
  "@types/express": "^4.17.13",
18
18
  "ts-node": "^10.4.0",
19
- "typescript": "^4.5.4"
19
+ "typescript": "^4.5.4",
20
+ "@types/node": "^22.5.4"
20
21
  }
21
22
  }
package/dist/utils.js CHANGED
@@ -50,13 +50,54 @@ var path_1 = __importDefault(require("path"));
50
50
  var client_1 = require("@dainprotocol/tunnel/client");
51
51
  var ora_1 = __importDefault(require("ora"));
52
52
  var chalk_1 = __importDefault(require("chalk"));
53
- function getDainConfig() {
54
- var configPath = path_1.default.join(process.cwd(), 'dain.json');
55
- return JSON.parse(fs_1.default.readFileSync(configPath, 'utf-8'));
53
+ var dotenv_1 = __importDefault(require("dotenv"));
54
+ function loadEnvFiles() {
55
+ var envFiles = ['.env.local', '.env', '.env.development', '.env.production'];
56
+ envFiles.forEach(function (file) {
57
+ var envPath = path_1.default.join(process.cwd(), file);
58
+ if (fs_1.default.existsSync(envPath)) {
59
+ dotenv_1.default.config({ path: envPath });
60
+ }
61
+ });
62
+ }
63
+ function getDainConfig(configFile) {
64
+ loadEnvFiles(); // Load environment variables from files
65
+ var defaultConfigPath = path_1.default.join(process.cwd(), 'dain.json');
66
+ var configPath = configFile ? path_1.default.join(process.cwd(), configFile) : defaultConfigPath;
67
+ if (!fs_1.default.existsSync(configPath)) {
68
+ throw new Error("Configuration file not found: ".concat(configPath));
69
+ }
70
+ try {
71
+ var configData = fs_1.default.readFileSync(configPath, 'utf8');
72
+ var config = JSON.parse(configData);
73
+ // Validate required fields
74
+ if (!config['main-file']) {
75
+ throw new Error("Configuration must include 'main-file'");
76
+ }
77
+ // Set default values for optional fields
78
+ config['environment'] = config['environment'] || 'development';
79
+ config['version'] = config['version'] || '1.0.0';
80
+ config['out-dir'] = config['out-dir'] || 'dist'; // Default to 'dist' if not specified
81
+ config['tunnel-base-url'] = config['tunnel-base-url'] || 'wss:///devtunnel.dain-local.com'; // Default value if not specified
82
+ // Handle API key
83
+ if (config['api-key'] === 'env') {
84
+ config['api-key'] = process.env.DAIN_API_KEY;
85
+ if (!config['api-key']) {
86
+ throw new Error("DAIN_API_KEY environment variable is not set");
87
+ }
88
+ }
89
+ else if (config['api-key'] && typeof config['api-key'] !== 'string') {
90
+ throw new Error("'api-key' must be a string or 'env'");
91
+ }
92
+ return config;
93
+ }
94
+ catch (error) {
95
+ throw new Error("Error reading or parsing configuration file: ".concat(error));
96
+ }
56
97
  }
57
- function setupProxy(port) {
98
+ function setupProxy(port, apiKey) {
58
99
  return __awaiter(this, void 0, void 0, function () {
59
- var spinner, client, tunnelUrl, error_1;
100
+ var spinner, config, client, tunnelUrl, error_1;
60
101
  return __generator(this, function (_a) {
61
102
  switch (_a.label) {
62
103
  case 0:
@@ -64,7 +105,8 @@ function setupProxy(port) {
64
105
  _a.label = 1;
65
106
  case 1:
66
107
  _a.trys.push([1, 3, , 4]);
67
- client = new client_1.DainTunnel('wss:///devtunnel.dain-local.com');
108
+ config = getDainConfig();
109
+ client = new client_1.DainTunnel(config['tunnel-base-url'] || 'wss:///tunnel.dain-local.com', apiKey);
68
110
  return [4 /*yield*/, client.start(parseInt(port))];
69
111
  case 2:
70
112
  tunnelUrl = _a.sent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/cli",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "CLI for Dain Protocol",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -15,12 +15,13 @@
15
15
  "templates"
16
16
  ],
17
17
  "dependencies": {
18
- "@dainprotocol/tunnel": "^1.0.1",
18
+ "@dainprotocol/tunnel": "^1.0.2",
19
19
  "@types/fs-extra": "^11.0.4",
20
20
  "@types/localtunnel": "^2.0.4",
21
21
  "chalk": "^4.1.2",
22
22
  "chokidar": "^3.6.0",
23
23
  "commander": "^11.1.0",
24
+ "dotenv": "^16.4.5",
24
25
  "esbuild": "^0.23.1",
25
26
  "fs-extra": "^11.2.0",
26
27
  "localtunnel": "^2.0.2",
@@ -1,5 +1,10 @@
1
1
  {
2
2
  "project-id": "",
3
- "project-api-key": "",
4
- "main-file": "src/index.ts"
3
+ "api-key": "",
4
+ "main-file": "src/index.ts",
5
+ "static-dir": "static",
6
+ "out-dir": "dist",
7
+ "environment": "development",
8
+ "version": "1.0.0",
9
+ "tunnel-base-url": "wss:///devtunnel.dain-local.com"
5
10
  }
@@ -16,6 +16,7 @@
16
16
  "devDependencies": {
17
17
  "@types/express": "^4.17.13",
18
18
  "ts-node": "^10.4.0",
19
- "typescript": "^4.5.4"
19
+ "typescript": "^4.5.4",
20
+ "@types/node": "^22.5.4"
20
21
  }
21
22
  }