uni-run 1.0.3 → 1.0.4

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.
@@ -1,6 +1,5 @@
1
1
  import NoArg from 'noarg';
2
2
  import type { app } from './arg';
3
- import { ExecuteOptions } from './execution';
4
3
  import Executor from './builtin-bin/Executor';
5
4
  export declare const executionConfig: {
6
5
  readonly flags: {
@@ -78,6 +77,11 @@ export declare const executionConfig: {
78
77
  default: false;
79
78
  description: "Set NODE_ENV to \"development\"";
80
79
  }>;
80
+ readonly tsn: import("noarg/dist/schema/TypeBoolean.cjs").TypeBoolean<{
81
+ required: true;
82
+ default: false;
83
+ description: "Run the script with ts-node";
84
+ }>;
81
85
  };
82
86
  readonly listArgument: {
83
87
  readonly name: "args for script";
@@ -89,4 +93,19 @@ export declare const executionConfig: {
89
93
  readonly helpUsageTrailingArgsLabel: "...[args/flags for script]";
90
94
  };
91
95
  };
92
- export declare function mapFlagsToOptions(flags: NoArg.InferFlags<typeof app>, bin?: Executor): ExecuteOptions;
96
+ export type ExecuteOptions = ReturnType<typeof mapFlagsToOptions>;
97
+ export declare function mapFlagsToOptions(flags: NoArg.InferFlags<typeof app>, bin?: Executor): {
98
+ cwd: string;
99
+ shell: boolean;
100
+ showInfo: boolean;
101
+ showTime: boolean;
102
+ benchmark: boolean;
103
+ clearOnReload: boolean;
104
+ readlineReload: boolean;
105
+ watch: boolean;
106
+ watchDelay: number;
107
+ watchIgnore: string[];
108
+ watchExtensions: string[];
109
+ tsNode: boolean;
110
+ env: NodeJS.ProcessEnv;
111
+ };
@@ -50,6 +50,9 @@ exports.executionConfig = noarg_1.default.defineConfig({
50
50
  nodeDev: noarg_1.default.boolean()
51
51
  .default(false)
52
52
  .description('Set NODE_ENV to "development"'),
53
+ tsn: noarg_1.default.boolean()
54
+ .default(false)
55
+ .description('Run the script with ts-node'),
53
56
  },
54
57
  listArgument: {
55
58
  name: 'args for script',
@@ -62,7 +65,6 @@ exports.executionConfig = noarg_1.default.defineConfig({
62
65
  },
63
66
  });
64
67
  function mapFlagsToOptions(flags, bin) {
65
- var _a, _b;
66
68
  return {
67
69
  cwd: flags.cwd,
68
70
  shell: flags.shell,
@@ -73,8 +75,9 @@ function mapFlagsToOptions(flags, bin) {
73
75
  readlineReload: flags.reloadKey,
74
76
  watch: flags.watch,
75
77
  watchDelay: flags.delay,
76
- watchExtensions: (_b = (_a = (flags.ext.length ? flags.ext : bin === null || bin === void 0 ? void 0 : bin.config.watchExtensions)) !== null && _a !== void 0 ? _a : bin === null || bin === void 0 ? void 0 : bin.config.extensions) !== null && _b !== void 0 ? _b : [],
77
78
  watchIgnore: flags.ignore,
79
+ watchExtensions: (flags.ext.length ? flags.ext : bin === null || bin === void 0 ? void 0 : bin.getRelatedExts()) || [],
80
+ tsNode: flags['tsn'],
78
81
  env: Object.assign(Object.assign({}, flags.env.reduce((acc, env) => {
79
82
  const [key, value] = env.split('=');
80
83
  acc[key] = value;
package/dist/arg.d.ts CHANGED
@@ -96,6 +96,11 @@ export declare const app: NoArg<"uni-run", {
96
96
  default: false;
97
97
  description: "Set NODE_ENV to \"development\"";
98
98
  }>;
99
+ readonly tsn: import("noarg/dist/schema/TypeBoolean.cjs").TypeBoolean<{
100
+ required: true;
101
+ default: false;
102
+ description: "Run the script with ts-node";
103
+ }>;
99
104
  };
100
105
  readonly customRenderHelp: {
101
106
  readonly helpUsageTrailingArgsLabel: "...[args/flags for script]";
@@ -193,13 +198,18 @@ export declare const exec: import("noarg/dist/NoArg/NoArgProgram.cjs").NoArgProg
193
198
  default: false;
194
199
  description: "Set NODE_ENV to \"development\"";
195
200
  }>;
201
+ readonly tsn: import("noarg/dist/schema/TypeBoolean.cjs").TypeBoolean<{
202
+ required: true;
203
+ default: false;
204
+ description: "Run the script with ts-node";
205
+ }>;
196
206
  };
197
207
  readonly customRenderHelp: {
198
208
  readonly helpUsageTrailingArgsLabel: "...[args/flags for script]";
199
209
  };
200
210
  globalFlags: {};
201
211
  }>;
202
- export declare const support: import("noarg/dist/NoArg/NoArgProgram.cjs").NoArgProgram<"support", {
212
+ export declare const list: import("noarg/dist/NoArg/NoArgProgram.cjs").NoArgProgram<"list", {
203
213
  readonly allowEqualAssign: true;
204
214
  readonly booleanNotSyntaxEnding: "\\";
205
215
  readonly allowDuplicateFlagForList: true;
package/dist/arg.js CHANGED
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.support = exports.exec = exports.app = void 0;
3
+ exports.list = exports.exec = exports.app = void 0;
4
4
  const noarg_1 = require("noarg");
5
5
  const arg_helper_1 = require("./arg-helper");
6
6
  exports.app = noarg_1.default.create('uni-run', Object.assign(Object.assign({}, arg_helper_1.executionConfig), { description: 'A universal runner for scripts', system: { splitListByComma: true }, arguments: [
7
7
  { name: 'script', type: noarg_1.default.string(), description: 'Run a script' },
8
8
  ] }));
9
9
  exports.exec = exports.app.create('exec', Object.assign(Object.assign({}, arg_helper_1.executionConfig), { description: 'Execute a script with the given binary' }));
10
- exports.support = exports.app.create('support', {
10
+ exports.list = exports.app.create('list', {
11
11
  description: 'List supported scripts',
12
12
  });
@@ -1,22 +1,21 @@
1
- type ExecutionBinConfig = {
2
- command: string;
1
+ import { ExecuteOptions } from '../arg-helper';
2
+ type BinConfig = {
3
3
  extensions: string[];
4
4
  watchExtensions?: string[];
5
- runArgs?: string[];
6
- checkInstallationArgs?: string[];
7
- howToInstall?: () => void;
8
- installCommands?: {
9
- command: string;
10
- args: string[];
11
- }[];
5
+ run: (args: string[], options: ExecuteOptions) => string[];
6
+ installMessage?: string;
7
+ installCommands?: string[] | ((options: ExecuteOptions) => string[]);
8
+ checkInstallation?: string[] | ((options: ExecuteOptions) => string[]);
12
9
  };
13
10
  export default class Executor {
14
- name: string;
15
- config: ExecutionBinConfig;
16
- constructor(name: string, config: ExecutionBinConfig);
17
- getArgs(...args: string[]): string[];
11
+ private name;
12
+ private config;
13
+ constructor(name: string, config: BinConfig);
14
+ getName(): string;
15
+ getRelatedExts(): string[];
16
+ start(options: ExecuteOptions, args: string[]): void;
18
17
  isSupported(script: string): boolean;
19
- isInstalled(): boolean;
20
- renderHowToInstall(): void;
18
+ private isInstalled;
19
+ private renderHowToInstall;
21
20
  }
22
21
  export {};
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const execution_1 = require("../execution");
3
4
  const confirm_1 = require("@inquirer/confirm");
4
5
  const cross_spawn_1 = require("cross-spawn");
5
6
  class Executor {
@@ -9,33 +10,50 @@ class Executor {
9
10
  this.config = config;
10
11
  (_a = this.config.watchExtensions) === null || _a === void 0 ? void 0 : _a.push(...this.config.extensions);
11
12
  }
12
- getArgs(...args) {
13
+ getName() {
14
+ return this.name;
15
+ }
16
+ getRelatedExts() {
13
17
  var _a;
14
- return [this.config.command, ...((_a = this.config.runArgs) !== null && _a !== void 0 ? _a : []), ...args];
18
+ return [...this.config.extensions, ...((_a = this.config.watchExtensions) !== null && _a !== void 0 ? _a : [])];
19
+ }
20
+ start(options, args) {
21
+ if (!this.isInstalled(options)) {
22
+ return this.renderHowToInstall(options);
23
+ }
24
+ execution_1.default.start(this.config.run(args, options), options);
15
25
  }
16
26
  isSupported(script) {
17
27
  return this.config.extensions.includes(script.split('.').pop() || '');
18
28
  }
19
- isInstalled() {
20
- const result = (0, cross_spawn_1.sync)(this.config.command, [...(this.config.checkInstallationArgs || [])], { stdio: 'ignore' });
29
+ isInstalled(options) {
30
+ if (!this.config.checkInstallation)
31
+ return true;
32
+ const [command, ...args] = typeof this.config.checkInstallation === 'function'
33
+ ? this.config.checkInstallation(options)
34
+ : this.config.checkInstallation || [];
35
+ const result = (0, cross_spawn_1.sync)(command, args, {
36
+ stdio: 'ignore',
37
+ });
21
38
  if (result.status === 1)
22
39
  return false;
23
40
  return true;
24
41
  }
25
- renderHowToInstall() {
26
- var _a;
42
+ renderHowToInstall(options) {
27
43
  console.error(`${this.name} is not installed.`);
28
- if (this.config.howToInstall) {
44
+ if (this.config.installMessage) {
29
45
  console.log('How to install:');
30
- this.config.howToInstall();
46
+ console.log(this.config.installMessage);
31
47
  }
32
- if (!((_a = this.config.installCommands) === null || _a === void 0 ? void 0 : _a.length))
48
+ const commands = typeof this.config.installCommands === 'function'
49
+ ? this.config.installCommands(options)
50
+ : this.config.installCommands;
51
+ if (!commands || !commands.length)
33
52
  return;
34
53
  (0, confirm_1.default)({ message: 'Do you want to install it?' }).then((ans) => {
35
- var _a;
36
54
  if (!ans)
37
55
  return;
38
- (_a = this.config.installCommands) === null || _a === void 0 ? void 0 : _a.forEach(({ command, args }) => {
56
+ commands.forEach(([command, ...args]) => {
39
57
  if (!command)
40
58
  return;
41
59
  (0, cross_spawn_1.sync)(command, args, { stdio: 'inherit' });
@@ -3,103 +3,94 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const Executor_1 = require("./Executor");
4
4
  exports.default = [
5
5
  new Executor_1.default('Node.js', {
6
- command: 'node',
7
6
  extensions: ['js', 'javascript', 'jsx', 'cjs', 'cjsx', 'mjs', 'mjsx'],
8
- watchExtensions: ['html', 'css', 'json'],
9
- checkInstallationArgs: ['--version'],
10
- howToInstall: () => {
11
- console.log('Please install Node.js from https://nodejs.org');
7
+ checkInstallation: ['node', '--version'],
8
+ installMessage: 'Please install Node.js from https://nodejs.org',
9
+ run(args) {
10
+ return ['node', ...args];
12
11
  },
13
12
  }),
14
13
  new Executor_1.default('TypeScript', {
15
- command: 'ts-node',
16
14
  extensions: ['ts', 'tsx', 'cts', 'ctsx', 'mts', 'mtsx'],
17
- watchExtensions: [
18
- 'html',
19
- 'css',
20
- 'json',
21
- 'js',
22
- 'javascript',
23
- 'jsx',
24
- 'cjs',
25
- 'cjsx',
26
- 'mjs',
27
- 'mjsx',
28
- ],
29
- checkInstallationArgs: ['--version'],
30
- installCommands: [{ command: 'npm', args: ['install', '-g', 'ts-node'] }],
31
- howToInstall: () => {
32
- console.log('Please install ts-node from https://www.npmjs.com/package/ts-node');
15
+ watchExtensions: ['js', 'javascript', 'jsx', 'cjs', 'cjsx', 'mjs', 'mjsx'],
16
+ checkInstallation(options) {
17
+ return options.tsNode ? ['ts-node', '--version'] : ['tsx', '--version'];
18
+ },
19
+ installCommands(options) {
20
+ return options.tsNode
21
+ ? ['npm install -g ts-node']
22
+ : ['npm install -g tsx'];
23
+ },
24
+ installMessage: 'Please install tsx. More: https://tsx.is',
25
+ run(args, options) {
26
+ return options.tsNode ? ['ts-node', ...args] : ['tsx', ...args];
33
27
  },
34
28
  }),
35
29
  new Executor_1.default('Python', {
36
- command: 'python',
37
30
  extensions: ['py'],
38
- checkInstallationArgs: ['--version'],
39
- howToInstall: () => {
40
- console.log('Please install Python from https://www.python.org');
31
+ checkInstallation: ['python', '--version'],
32
+ installMessage: 'Please install Python from https://www.python.org',
33
+ run(args) {
34
+ return ['python', ...args];
41
35
  },
42
36
  }),
43
37
  new Executor_1.default('Java - Oracle', {
44
- command: 'java',
45
38
  extensions: ['java'],
46
- checkInstallationArgs: ['--version'],
47
- howToInstall: () => {
48
- console.log('Please install Java from https://www.oracle.com/java');
39
+ checkInstallation: ['java', '--version'],
40
+ installMessage: 'Please install Java from https://www.oracle.com/java',
41
+ run(args) {
42
+ return ['java', ...args];
49
43
  },
50
44
  }),
51
45
  new Executor_1.default('Powershell', {
52
- command: 'powershell',
53
46
  extensions: ['ps1'],
54
- checkInstallationArgs: ['-command', '"Get-Host"'],
55
- howToInstall: () => {
56
- console.log('Please install Powershell from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell');
47
+ checkInstallation: ['powershell', '-command', 'echo ok'],
48
+ installMessage: 'Please install Powershell from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell',
49
+ run(args) {
50
+ return ['powershell', '-File', ...args];
57
51
  },
58
52
  }),
59
53
  new Executor_1.default('Command Prompt', {
60
- command: 'cmd',
61
- runArgs: ['/c'],
62
54
  extensions: ['cmd', 'bat'],
63
- checkInstallationArgs: ['/c', 'echo', 'ok'],
64
- howToInstall: () => {
65
- console.log('Please install Command Prompt from Windows');
55
+ checkInstallation: ['cmd', '/c', 'echo ok'],
56
+ installMessage: 'Please install Command Prompt from Windows',
57
+ run(args) {
58
+ return ['cmd', '/c', ...args];
66
59
  },
67
60
  }),
68
61
  new Executor_1.default('Shell', {
69
- command: 'bash',
70
62
  extensions: ['sh'],
71
- checkInstallationArgs: ['--version'],
72
- howToInstall: () => {
73
- console.log('Please install Bash from https://www.gnu.org/software/bash');
63
+ checkInstallation: ['bash', '--version'],
64
+ installMessage: 'Please install Bash from https://www.gnu.org/software/bash',
65
+ run(args) {
66
+ return ['bash', ...args];
74
67
  },
75
68
  }),
76
69
  new Executor_1.default('Lua', {
77
- command: 'lua',
78
70
  extensions: ['lua'],
79
- checkInstallationArgs: ['--version'],
80
- howToInstall: () => {
81
- console.log('Please install Lua from https://www.lua.org');
71
+ checkInstallation: ['lua', '--version'],
72
+ installMessage: 'Please install Lua from https://www.lua.org',
73
+ run(args) {
74
+ return ['lua', ...args];
82
75
  },
83
76
  }),
84
77
  new Executor_1.default('SASS (CSS)', {
85
- command: 'sass',
86
78
  extensions: ['sass', 'scss'],
87
- checkInstallationArgs: ['--version'],
88
- installCommands: [{ command: 'npm', args: ['install', '-g', 'sass'] }],
89
- howToInstall: () => {
90
- console.log('Please install SASS from https://sass-lang.com');
79
+ checkInstallation: ['sass', '--version'],
80
+ installCommands: ['npm install -g sass'],
81
+ installMessage: 'Please install SASS from https://sass-lang.com',
82
+ run(args) {
83
+ return ['sass', ...args];
91
84
  },
92
85
  }),
93
86
  new Executor_1.default('HTML Server', {
94
- command: 'http-server',
95
87
  extensions: ['html', 'htm'],
96
88
  watchExtensions: ['css', 'js', 'javascript', 'json'],
97
- checkInstallationArgs: ['--version'],
98
- installCommands: [
99
- { command: 'npm', args: ['install', '-g', 'http-server'] },
100
- ],
101
- howToInstall: () => {
102
- console.log('Please install http-server from https://www.npmjs.com/package/http-server');
89
+ checkInstallation: ['http-server', '--version'],
90
+ installCommands: ['npm install -g http-server'],
91
+ installMessage: 'Please install http-server from https://www.npmjs.com/package/http-server',
92
+ run(args) {
93
+ return ['http-server', ...args];
103
94
  },
104
95
  }),
105
96
  ];
@@ -1,3 +1,4 @@
1
+ import { ExecuteOptions } from '../arg-helper';
1
2
  export default class Execution {
2
3
  private command;
3
4
  private args;
@@ -10,17 +11,3 @@ export default class Execution {
10
11
  private killProcess;
11
12
  private clearBeforeStart;
12
13
  }
13
- export type ExecuteOptions = {
14
- cwd: string;
15
- clearOnReload: boolean;
16
- readlineReload: boolean;
17
- watch: boolean;
18
- watchDelay: number;
19
- watchIgnore: string[];
20
- watchExtensions: string[];
21
- env: Record<string, string>;
22
- shell: boolean;
23
- showInfo: boolean;
24
- showTime: boolean;
25
- benchmark: boolean;
26
- };
package/dist/index.js CHANGED
@@ -10,25 +10,25 @@ arg.app.on(([script, listArs, trailingArgs], flags) => {
10
10
  console.log('Unsupported script:', script);
11
11
  return console.log('You may try "ur exec bin script.ext - --flags"');
12
12
  }
13
- if (!bin.isInstalled()) {
14
- return bin.renderHowToInstall();
15
- }
16
- execution_1.default.start(bin.getArgs(script, ...listArs, ...trailingArgs), (0, arg_helper_1.mapFlagsToOptions)(flags, bin));
13
+ const executionOptions = (0, arg_helper_1.mapFlagsToOptions)(flags, bin);
14
+ bin.start(executionOptions, [script, ...listArs, ...trailingArgs]);
17
15
  });
18
16
  arg.exec.on(([listArs, trailingArgs], flags) => {
19
17
  execution_1.default.start([...listArs, ...trailingArgs], (0, arg_helper_1.mapFlagsToOptions)(flags));
20
18
  });
21
- arg.support.on(() => {
19
+ arg.list.on(() => {
22
20
  console.log('Supported scripts:');
23
21
  builtin_bin_1.default
24
22
  .sort((a, b) => {
25
- if (a.name < b.name)
23
+ const aName = a.getName();
24
+ const bName = b.getName();
25
+ if (aName < bName)
26
26
  return -1;
27
- if (a.name > b.name)
27
+ if (aName > bName)
28
28
  return 1;
29
29
  return 0;
30
30
  })
31
31
  .forEach((bin) => {
32
- console.log(`- ${bin.name}`);
32
+ console.log(`- ${bin.getName()}`);
33
33
  });
34
34
  });
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "uni-run",
3
3
  "description": "Universal Runner for many language",
4
- "version": "1.0.3",
4
+ "version": "1.0.4",
5
5
  "type": "commonjs",
6
6
  "scripts": {
7
- "ts": "run ./src/__lab__/index.ts",
8
- "js": "run ./dist/__lab__/index.js",
7
+ "ts": "tsx --watch ./src/__lab__/index.ts",
8
+ "js": "node --watch ./dist/__lab__/index.js",
9
+ "tsc": "tsc --watch --noEmit",
9
10
  "dev": "tsc --watch",
10
11
  "build": "node ./build.cjs"
11
12
  },