uni-run 1.1.4 → 1.1.5

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/dist/app.js CHANGED
@@ -36,35 +36,37 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  const arg = __importStar(require("./arg"));
39
+ const colors_1 = __importDefault(require("./lib/colors"));
39
40
  const execution_1 = __importDefault(require("./execution"));
40
41
  const getConfig_1 = __importDefault(require("./helpers/getConfig"));
41
42
  const argHelper_1 = require("./argHelper");
42
43
  const scriptExecutors_1 = __importDefault(require("./scriptExecutors"));
43
44
  const checkRuntime_1 = __importDefault(require("./scriptExecutors/checkRuntime"));
44
45
  const getUserExecutors_1 = __importDefault(require("./helpers/getUserExecutors"));
45
- const colors_1 = __importDefault(require("./lib/colors"));
46
46
  arg.app.on((_a, flags_1) => __awaiter(void 0, [_a, flags_1], void 0, function* ([script, listArs, trailingArgs], flags) {
47
47
  var _b;
48
- const userExecutors = (0, getUserExecutors_1.default)(flags.cwd);
49
48
  const executionConfig = (0, getConfig_1.default)(flags.cwd);
49
+ const userExecutors = (0, getUserExecutors_1.default)(flags.cwd);
50
50
  const totalExecutors = [
51
51
  ...(Array.isArray(userExecutors) ? userExecutors : []),
52
52
  ...scriptExecutors_1.default,
53
53
  ];
54
54
  const scriptExecutor = totalExecutors.find((executor) => executor.exts.includes(script.split('.').pop()));
55
55
  if (!scriptExecutor) {
56
- console.error('Unsupported script:', script);
57
- return console.log('You may try "run exec YOUR_BIN script.ext -- --flags"');
56
+ console.error(colors_1.default.red('Unsupported script:'), colors_1.default.yellow(script));
57
+ return console.log(colors_1.default.bgGreen('TIPS:'), 'You may try', colors_1.default.cyan(`run exec <YOUR_BIN> ${colors_1.default.yellow(script)}`));
58
58
  }
59
59
  const executionOptions = (0, argHelper_1.mapFlagsToOptions)(flags);
60
60
  const runtime = scriptExecutor.getRuntime([script, ...listArs, ...trailingArgs], executionOptions, executionConfig);
61
- (0, checkRuntime_1.default)(runtime);
62
- execution_1.default.start(runtime.start, Object.assign(Object.assign({}, executionOptions), { watchExtensions: executionOptions.watchExtensions.length
61
+ if (!(yield (0, checkRuntime_1.default)(runtime)))
62
+ return;
63
+ const exec = new execution_1.default(Object.assign(Object.assign({}, executionOptions), { watchExtensions: executionOptions.watchExtensions.length
63
64
  ? executionOptions.watchExtensions
64
- : [...scriptExecutor.exts, ...((_b = runtime.watchExts) !== null && _b !== void 0 ? _b : [])] }));
65
+ : [...scriptExecutor.exts, ...((_b = runtime.watchExts) !== null && _b !== void 0 ? _b : [])] }), runtime.exec, runtime.compile);
66
+ exec.start();
65
67
  }));
66
68
  arg.exec.on(([listArs, trailingArgs], flags) => {
67
- execution_1.default.start([...listArs, ...trailingArgs], (0, argHelper_1.mapFlagsToOptions)(flags));
69
+ new execution_1.default((0, argHelper_1.mapFlagsToOptions)(flags), [...listArs, ...trailingArgs]).start();
68
70
  });
69
71
  arg.list.on(() => {
70
72
  const userExecutors = (0, getUserExecutors_1.default)();
@@ -1,14 +1,16 @@
1
1
  import { ExecuteOptions } from '../argHelper';
2
2
  export default class Execution {
3
- private command;
4
- private args;
5
3
  private options;
4
+ private startArgs;
5
+ private preStartArgs?;
6
6
  private child;
7
7
  private benchMarkText;
8
8
  private isBenchmarkRunning;
9
9
  private isExecutionExecutedAnyTime;
10
- static start([command, ...args]: string[], options: ExecuteOptions): Execution;
11
- constructor(command: string, args: string[], options: ExecuteOptions);
10
+ constructor(options: ExecuteOptions, startArgs: string[], preStartArgs?: string[] | undefined);
11
+ start(): void;
12
+ private spawnSync;
13
+ private spawnAsync;
12
14
  private setup;
13
15
  private startProcess;
14
16
  private killProcess;
@@ -26,26 +26,45 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- const cross_spawn_1 = require("cross-spawn");
30
29
  const readline = __importStar(require("readline"));
30
+ const cross_spawn_1 = require("cross-spawn");
31
31
  const watcher_1 = __importDefault(require("./watcher"));
32
32
  const colors_1 = __importDefault(require("../lib/colors"));
33
33
  const kill_process_1 = __importDefault(require("./kill-process"));
34
34
  class Execution {
35
- static start([command, ...args], options) {
36
- return new Execution(command, args, options);
37
- }
38
- constructor(command, args, options) {
39
- this.command = command;
40
- this.args = args;
35
+ constructor(options, startArgs, preStartArgs) {
41
36
  this.options = options;
37
+ this.startArgs = startArgs;
38
+ this.preStartArgs = preStartArgs;
42
39
  this.child = null;
43
40
  this.benchMarkText = colors_1.default.dim.green('# Execution time');
44
41
  this.isBenchmarkRunning = false;
45
42
  this.isExecutionExecutedAnyTime = false;
46
43
  this.setup();
44
+ }
45
+ start() {
47
46
  this.startProcess();
48
47
  }
48
+ spawnSync(args) {
49
+ const [command, ...args_] = args;
50
+ return (0, cross_spawn_1.sync)(command, args_, {
51
+ argv0: command,
52
+ cwd: this.options.cwd,
53
+ shell: this.options.shell,
54
+ env: Object.assign(Object.assign({}, process.env), this.options.env),
55
+ stdio: this.options.silent ? 'ignore' : 'inherit',
56
+ });
57
+ }
58
+ spawnAsync(args) {
59
+ const [command, ...args_] = args;
60
+ return (0, cross_spawn_1.spawn)(command, args_, {
61
+ argv0: command,
62
+ cwd: this.options.cwd,
63
+ shell: this.options.shell,
64
+ env: Object.assign(Object.assign({}, process.env), this.options.env),
65
+ stdio: this.options.silent ? 'ignore' : 'inherit',
66
+ });
67
+ }
49
68
  setup() {
50
69
  var _a, _b;
51
70
  if (this.options.benchmarkPrefix) {
@@ -80,13 +99,9 @@ class Execution {
80
99
  this.clearBeforeStart();
81
100
  this.renderInfoLogs();
82
101
  this.isExecutionExecutedAnyTime = true;
83
- this.child = (0, cross_spawn_1.spawn)(this.command, this.args, {
84
- argv0: this.command,
85
- cwd: this.options.cwd,
86
- shell: this.options.shell,
87
- env: Object.assign(Object.assign({}, process.env), this.options.env),
88
- stdio: this.options.silent ? 'ignore' : 'inherit',
89
- });
102
+ if (this.preStartArgs)
103
+ this.spawnSync(this.preStartArgs);
104
+ this.child = this.spawnAsync(this.startArgs);
90
105
  this.child.on('exit', (code) => {
91
106
  if (code && code > 0) {
92
107
  console.log(colors_1.default.red(`Process exited with code: ${colors_1.default.yellow(String(code))}`));
@@ -7,7 +7,7 @@ exports.default = default_1;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const utils_1 = require("./utils");
10
- function default_1(...extraDirs) {
10
+ function inner(...extraDirs) {
11
11
  const userPath = (0, utils_1.getUserPath)();
12
12
  if (!userPath)
13
13
  return;
@@ -18,7 +18,9 @@ function default_1(...extraDirs) {
18
18
  const config = fs_1.default.readFileSync(configFile, 'utf-8');
19
19
  return JSON.parse(config);
20
20
  }
21
- catch (_a) {
22
- return {};
23
- }
21
+ catch (_a) { }
22
+ }
23
+ function default_1(...extraDirs) {
24
+ var _a;
25
+ return (_a = inner(...extraDirs)) !== null && _a !== void 0 ? _a : {};
24
26
  }
Binary file
@@ -1,2 +1,2 @@
1
1
  import { RuntimeOptions } from './types.t';
2
- export default function (runtime: RuntimeOptions): void;
2
+ export default function (runtime: RuntimeOptions): Promise<boolean>;
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
13
  };
@@ -8,25 +17,30 @@ const colors_1 = __importDefault(require("../lib/colors"));
8
17
  const confirm_1 = __importDefault(require("@inquirer/confirm"));
9
18
  const cross_spawn_1 = require("cross-spawn");
10
19
  function default_1(runtime) {
11
- var _a, _b, _c, _d;
12
- if (!((_b = (_a = runtime.install) === null || _a === void 0 ? void 0 : _a.check) === null || _b === void 0 ? void 0 : _b.length))
13
- return;
14
- const [command, ...args] = runtime.install.check;
15
- const result = (0, cross_spawn_1.sync)(command, args, { stdio: 'ignore' });
16
- if (result.status === 0)
17
- return;
18
- if ((_c = runtime.install.hints) === null || _c === void 0 ? void 0 : _c.length) {
19
- console.error(`${runtime.start[0]} is not installed.`);
20
- console.log(colors_1.default.bold('How to install:'));
21
- runtime.install.hints.forEach((hint) => console.log(hint));
22
- }
23
- if ((_d = runtime.install.command) === null || _d === void 0 ? void 0 : _d.length) {
24
- (0, confirm_1.default)({ message: 'Do you want to install it?' }).then((ans) => {
25
- var _a;
26
- if (!ans)
27
- return;
28
- const [command, ...args] = (_a = runtime.install) === null || _a === void 0 ? void 0 : _a.command;
29
- (0, cross_spawn_1.sync)(command, args, { stdio: 'inherit' });
30
- });
31
- }
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ var _a, _b, _c;
22
+ const install = runtime.install;
23
+ if (!install)
24
+ return true;
25
+ if (!((_a = install.check) === null || _a === void 0 ? void 0 : _a.length))
26
+ return true;
27
+ const [checkCmd, ...checkArgs] = install.check;
28
+ const result = (0, cross_spawn_1.sync)(checkCmd, checkArgs, { stdio: 'ignore' });
29
+ if (result.status === 0)
30
+ return true;
31
+ console.error(colors_1.default.yellow(runtime.exec[0]) + colors_1.default.red(' is not installed.'));
32
+ if (!((_b = install.command) === null || _b === void 0 ? void 0 : _b.length))
33
+ return false;
34
+ const ans = yield (0, confirm_1.default)({ message: 'Do you want to install it?' });
35
+ if (ans) {
36
+ const [installCmd, ...installArgs] = install.command;
37
+ const result = (0, cross_spawn_1.sync)(installCmd, installArgs, { stdio: 'inherit' });
38
+ return result.status === 0;
39
+ }
40
+ if ((_c = install.hints) === null || _c === void 0 ? void 0 : _c.length) {
41
+ console.log(colors_1.default.bold('How to install:'));
42
+ install.hints.forEach((hint) => console.log(hint));
43
+ }
44
+ return false;
45
+ });
32
46
  }
@@ -23,7 +23,8 @@ exports.default = (0, as_1.default)([
23
23
  break;
24
24
  }
25
25
  return {
26
- start: [runtime, ...args],
26
+ exec: [runtime, ...args],
27
+ compile: ['echo', 'hello'],
27
28
  install: {
28
29
  check: [runtime, '--version'],
29
30
  hints: installHints,
@@ -56,7 +57,7 @@ exports.default = (0, as_1.default)([
56
57
  break;
57
58
  }
58
59
  return {
59
- start: [runtime, ...args],
60
+ exec: [runtime, ...args],
60
61
  watchExts: ['js', 'javascript', 'jsx', 'cjs', 'cjsx', 'mjs', 'mjsx'],
61
62
  install: {
62
63
  check: [runtime, '--version'],
@@ -70,7 +71,7 @@ exports.default = (0, as_1.default)([
70
71
  exts: ['py'],
71
72
  getRuntime(args, options, config) {
72
73
  return {
73
- start: ['python', ...args],
74
+ exec: ['python', ...args],
74
75
  install: {
75
76
  check: ['python', '--version'],
76
77
  hints: ['Please install Python from https://www.python.org'],
@@ -83,7 +84,7 @@ exports.default = (0, as_1.default)([
83
84
  exts: ['java'],
84
85
  getRuntime(args, options, config) {
85
86
  return {
86
- start: ['java', ...args],
87
+ exec: ['java', ...args],
87
88
  install: {
88
89
  check: ['java', '--version'],
89
90
  hints: ['Please install Java from https://www.oracle.com/java'],
@@ -96,7 +97,7 @@ exports.default = (0, as_1.default)([
96
97
  exts: ['ps1'],
97
98
  getRuntime(args, options, config) {
98
99
  return {
99
- start: ['powershell', '-File', ...args],
100
+ exec: ['powershell', '-File', ...args],
100
101
  install: {
101
102
  check: ['powershell', '-command', 'echo ok'],
102
103
  hints: [
@@ -111,7 +112,7 @@ exports.default = (0, as_1.default)([
111
112
  exts: ['cmd', 'bat'],
112
113
  getRuntime(args, options, config) {
113
114
  return {
114
- start: ['cmd', '/c', ...args],
115
+ exec: ['cmd', '/c', ...args],
115
116
  install: {
116
117
  check: ['cmd', '/c', 'echo ok'],
117
118
  hints: ['Please install Command Prompt from Windows'],
@@ -124,7 +125,7 @@ exports.default = (0, as_1.default)([
124
125
  exts: ['sh'],
125
126
  getRuntime(args, options, config) {
126
127
  return {
127
- start: ['bash', ...args],
128
+ exec: ['bash', ...args],
128
129
  install: {
129
130
  check: ['bash', '--version'],
130
131
  hints: ['Please install Bash from https://www.gnu.org/software/bash'],
@@ -137,7 +138,7 @@ exports.default = (0, as_1.default)([
137
138
  exts: ['fish'],
138
139
  getRuntime(args, options, config) {
139
140
  return {
140
- start: ['fish', ...args],
141
+ exec: ['fish', ...args],
141
142
  install: {
142
143
  check: ['fish', '--version'],
143
144
  hints: ['Please install Fish from https://fishshell.com'],
@@ -150,7 +151,7 @@ exports.default = (0, as_1.default)([
150
151
  exts: ['lua'],
151
152
  getRuntime(args, options, config) {
152
153
  return {
153
- start: ['lua', ...args],
154
+ exec: ['lua', ...args],
154
155
  install: {
155
156
  check: ['lua', '-v'],
156
157
  hints: ['Please install Lua from https://www.lua.org'],
@@ -163,7 +164,7 @@ exports.default = (0, as_1.default)([
163
164
  exts: ['rb'],
164
165
  getRuntime(args, options, config) {
165
166
  return {
166
- start: ['ruby', ...args],
167
+ exec: ['ruby', ...args],
167
168
  install: {
168
169
  check: ['ruby', '-v'],
169
170
  hints: ['Please install Ruby from https://www.ruby-lang.org'],
@@ -176,7 +177,7 @@ exports.default = (0, as_1.default)([
176
177
  exts: ['go'],
177
178
  getRuntime(args, options, config) {
178
179
  return {
179
- start: ['go', 'run', ...args],
180
+ exec: ['go', 'run', ...args],
180
181
  install: {
181
182
  check: ['go', '-v'],
182
183
  hints: ['Please install Ruby from https://www.ruby-lang.org'],
@@ -184,14 +185,57 @@ exports.default = (0, as_1.default)([
184
185
  };
185
186
  },
186
187
  },
188
+ {
189
+ name: 'C - GCC',
190
+ exts: ['c'],
191
+ getRuntime([script, ...args], options, config) {
192
+ return {
193
+ compile: ['gcc', script, '-o', 'dist/output'],
194
+ exec: ['./dist/output', ...args],
195
+ install: {
196
+ check: ['gcc', '--version'],
197
+ hints: ['Please install GCC from https://gcc.gnu.org'],
198
+ },
199
+ };
200
+ },
201
+ },
202
+ {
203
+ name: 'C++ - GCC',
204
+ exts: ['cpp'],
205
+ getRuntime([script, ...args], options, config) {
206
+ return {
207
+ compile: ['g++', script, '-o', 'dist/output'],
208
+ exec: ['./dist/output', ...args],
209
+ install: {
210
+ check: ['g++', '--version'],
211
+ hints: ['Please install GCC from https://gcc.gnu.org'],
212
+ },
213
+ };
214
+ },
215
+ },
216
+ {
217
+ name: 'C# - Mono',
218
+ exts: ['cs'],
219
+ getRuntime([script, ...args], options, config) {
220
+ return {
221
+ compile: ['mcs', script, '-out:dist/output.exe'],
222
+ exec: ['./dist/output'],
223
+ install: {
224
+ check: ['mcs', '--version'],
225
+ hints: ['Please install Mono from https://www.mono-project.com'],
226
+ },
227
+ };
228
+ },
229
+ },
187
230
  {
188
231
  name: 'SASS (CSS)',
189
232
  exts: ['sass', 'scss'],
190
233
  getRuntime(args, options, config) {
191
234
  return {
192
- start: ['sass', ...args],
235
+ exec: ['sass', ...args],
193
236
  install: {
194
237
  check: ['sass', '--version'],
238
+ command: ['npm', 'install', '-g', 'sass'],
195
239
  hints: ['Please install SASS from https://sass-lang.com'],
196
240
  },
197
241
  };
@@ -200,14 +244,15 @@ exports.default = (0, as_1.default)([
200
244
  {
201
245
  name: 'HTML Server',
202
246
  exts: ['html', 'htm'],
203
- getRuntime(args, options, config) {
247
+ getRuntime([script, ...args], options, config) {
204
248
  return {
205
- start: ['http-server', ...args],
206
- watchExts: ['css', 'js', 'javascript', 'json'],
249
+ exec: ['lite-server', script, ...args],
250
+ watchExts: ['css', 'js', 'javascript'],
207
251
  install: {
208
- check: ['http-server', '--version'],
252
+ check: ['lite-server', '--version'],
253
+ command: ['npm', 'install', '-g', 'lite-server'],
209
254
  hints: [
210
- 'Please install http-server from https://www.npmjs.com/package/http-server',
255
+ 'Please install lite-server from https://www.npmjs.com/package/lite-server',
211
256
  ],
212
257
  },
213
258
  };
@@ -4,10 +4,11 @@ export type ExecutorConfig = Partial<{
4
4
  'typescript-runtime': string;
5
5
  }>;
6
6
  export type RuntimeOptions = {
7
- start: string[];
7
+ exec: string[];
8
+ compile?: string[];
8
9
  watchExts?: string[];
9
10
  install?: {
10
- check?: string[];
11
+ check: string[];
11
12
  hints?: string[];
12
13
  command?: string[];
13
14
  };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "uni-run",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "Universal Runner for many language",
5
5
  "type": "commonjs",
6
6
  "scripts": {
7
7
  "build": "tsc",
8
8
  "dev": "tsc --watch",
9
9
  "tsc": "tsc --watch --noEmit",
10
+ "live": "node --watch ./dist/__lab__/index.js",
10
11
  "lab": "run ./src/__lab__/index.ts --focus ./src"
11
12
  },
12
13
  "main": "./dist/index.js",