@neurospeech/jex 1.0.9

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.
@@ -0,0 +1,23 @@
1
+ name: Build
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '*'
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v1
13
+ - uses: actions/setup-node@v1
14
+ with:
15
+ node-version: 20
16
+ registry-url: https://registry.npmjs.org/
17
+ - run: npm install -g typescript
18
+ - run: npm install
19
+ - run: tsc
20
+ - run: npm run test
21
+ - run: npm publish --access public
22
+ env:
23
+ NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
@@ -0,0 +1,45 @@
1
+ {
2
+ "terminal.integrated.profiles.windows": {
3
+ "PowerShell": {
4
+ "source": "PowerShell",
5
+ "args": [
6
+ "-ExecutionPolicy",
7
+ "Bypass"
8
+ ]
9
+ }
10
+ },
11
+ "terminal.integrated.defaultProfile.windows": "PowerShell",
12
+ "cSpell.words": [
13
+ "Akash",
14
+ "castas",
15
+ "Cloner",
16
+ "DATEDIFF",
17
+ "DATETIME",
18
+ "DATETIMEFROMPARTS",
19
+ "DATETIMEOFFSETFROMPARTS",
20
+ "FREETEXT",
21
+ "FROMPARTS",
22
+ "ISDATE",
23
+ "ISNUMERIC",
24
+ "Linq",
25
+ "navigations",
26
+ "Neuro",
27
+ "syslanguages"
28
+ ], "eslint.alwaysShowStatus": true,
29
+ "editor.defaultFormatter": "dbaeumer.vscode-eslint",
30
+ "eslint.format.enable": true,
31
+ "emmet.showSuggestionsAsSnippets": false,
32
+ "emmet.showAbbreviationSuggestions": false,
33
+ "emmet.excludeLanguages": [
34
+ "markdown",
35
+ "typescript",
36
+ "typescriptreact"
37
+ ],
38
+ "[typescriptreact]": {
39
+ "editor.defaultFormatter": "vscode.typescript-language-features"
40
+ },
41
+ "[typescript]": {
42
+ "editor.defaultFormatter": "vscode.typescript-language-features"
43
+ }
44
+
45
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 NeuroSpeech Technologies Pvt Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # jex
2
+ Easy shell scripting with JavaScript and E4X (Similar to JSX)
package/bin/jex ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('../index.js')
package/index.js ADDED
@@ -0,0 +1 @@
1
+ import "./dist/index.js";
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@neurospeech/jex",
3
+ "version": "1.0.9",
4
+ "description": "Easy shell scripting with JavaScript and E4X (Similar to JSX)",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 0",
9
+ "postversion": "git push --follow-tags"
10
+ },
11
+ "bin": {
12
+ "jex": "bin/jex"
13
+ },
14
+ "author": "",
15
+ "license": "ISC",
16
+ "dependencies": {
17
+ "@babel/core": "^7.25.2",
18
+ "@babel/preset-react": "^7.24.7",
19
+ "console-log-colors": "^0.5.0"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^22.3.0"
23
+ }
24
+ }
@@ -0,0 +1,7 @@
1
+ import XNode from "./XNode.js";
2
+
3
+ export default async function Batch({}, ... commands: XNode[]) {
4
+ for (const element of commands) {
5
+ await element.execute();
6
+ }
7
+ }
@@ -0,0 +1,43 @@
1
+ import { spawnPromise } from "./spawnPromise.js";
2
+ import XNode from "./XNode.js";
3
+
4
+ export interface IProcessResult {
5
+ all: string;
6
+ pid: number;
7
+ status: number;
8
+ }
9
+
10
+ export default async function Run({ path, args,
11
+ cwd = void 0,
12
+ detached = false,
13
+ logCommand = true,
14
+ logData = false,
15
+ logError = true,
16
+ log = void 0 as (data: Buffer) => void,
17
+ error = void 0 as (data: Buffer) => void,
18
+ started = void 0 as (pid: number) => void,
19
+ finished = void 0 as (r: IProcessResult) => void,
20
+ failed = void 0 as (r: IProcessResult) => void
21
+ }) {
22
+ const r = await spawnPromise(path, args, {
23
+ cwd,
24
+ detached,
25
+ logCommand,
26
+ logData,
27
+ logError,
28
+ log,
29
+ error
30
+ });
31
+
32
+ let fxr = void 0;
33
+
34
+ if (r.status === 0 && finished) {
35
+ fxr = finished(r);
36
+ } else {
37
+ fxr = failed(r);
38
+ }
39
+
40
+ if (fxr) {
41
+ await fxr;
42
+ }
43
+ }
@@ -0,0 +1,23 @@
1
+ export default class XNode {
2
+
3
+ public static create(
4
+ // eslint-disable-next-line @typescript-eslint/ban-types
5
+ name: Function,
6
+ attribs: Record<string, any>,
7
+ ... nodes: (XNode | string)[]): XNode {
8
+ return new XNode(name, attribs, nodes);
9
+ }
10
+
11
+ private constructor(
12
+ public readonly name: Function,
13
+ public readonly attributes: Record<string, any>,
14
+ public readonly children: (XNode | string)[]
15
+ ) {
16
+
17
+ }
18
+
19
+ async execute() {
20
+ return this.name(this.attributes, this.children);
21
+ }
22
+
23
+ }
@@ -0,0 +1,29 @@
1
+ import { transformFileAsync } from "@babel/core";
2
+ import { writeFile } from "fs/promises";
3
+ import { format, parse } from "path";
4
+
5
+ const presets = {
6
+ sourceType: "module",
7
+ sourceMaps: "inline",
8
+ compact: false,
9
+ comments: false,
10
+ importSource: "jex/dist/XNode.js",
11
+ pragma: "XNode.create",
12
+ getModuleId: () => "v",
13
+ "plugins": [
14
+ ]
15
+ };
16
+
17
+ export class Babel {
18
+
19
+
20
+ static async transformJSX(file: string) {
21
+ const result = await transformFileAsync(file, presets);
22
+ const path = parse(file);
23
+ path.base += ".js";
24
+ const js = format(path);
25
+ await writeFile(js, result.code, "utf8");
26
+ return js;
27
+ }
28
+
29
+ }
@@ -0,0 +1,71 @@
1
+ /* eslint-disable no-console */
2
+ import { SpawnOptionsWithoutStdio, spawn } from "node:child_process";
3
+
4
+ import { color } from "console-log-colors";
5
+
6
+ export const spawnPromise = (path, args?: string[], options?: SpawnOptionsWithoutStdio & {
7
+ logCommand?: boolean,
8
+ logData?: boolean,
9
+ logError?: boolean,
10
+ log?: (data: Buffer) => void,
11
+ error?: (data: Buffer) => void,
12
+ throwOnFail?: boolean }) => new Promise<{
13
+ get all(): string;
14
+ pid: number;
15
+ status: number;
16
+ }>((resolve, reject) => {
17
+ const all = [];
18
+ const { logCommand = true, throwOnFail = false, logData = true, logError = true, log, error } = options ?? {};
19
+ const cd = spawn(path, args, options);
20
+ const pid = cd.pid;
21
+ cd.stdout.on("data", (data) => {
22
+ if (log) {
23
+ log(data);
24
+ }
25
+ if (logData) {
26
+ data = data.toString("utf8");
27
+ all.push(data);
28
+ }
29
+ });
30
+ cd.stderr.on("data", (data) => {
31
+ if (error) {
32
+ log(data);
33
+ }
34
+ if(logError) {
35
+ data = data.toString("utf8");
36
+ all.push(color.red(data));
37
+ }
38
+ });
39
+
40
+ cd.on("error", (error) => {
41
+ const errorText = color.red(error.stack ?? error.toString());
42
+ all.push(error.stack ?? error.toString());
43
+ if (logData || logError) {
44
+ console.error(errorText);
45
+ }
46
+ reject(error);
47
+ });
48
+ cd.on("close", (status) => {
49
+ if (status>0) {
50
+ if (throwOnFail) {
51
+ if (logError) {
52
+ console.error(all);
53
+ }
54
+ reject(new Error(all.join("\n")));
55
+ return;
56
+ }
57
+ }
58
+ if (logCommand) {
59
+ console.log(`Spawn: ${path} ${JSON.stringify(args, undefined, 2)}`);
60
+ if (logData) {
61
+ console.log(all.join("\n"));
62
+ }
63
+ }
64
+ resolve({
65
+ get all() {
66
+ return all.join("\n");
67
+ },
68
+ pid,
69
+ status });
70
+ });
71
+ });
package/src/index.ts ADDED
@@ -0,0 +1,71 @@
1
+ // load and execute script...
2
+
3
+ import { unlinkSync } from "fs";
4
+ import { Babel } from "./core/babel.js";
5
+ import XNode from "./core/XNode.js";
6
+
7
+ export { default as XNode } from "./core/XNode.js";
8
+
9
+ export { default as Batch } from "./core/Batch.js";
10
+
11
+ export { default as Run } from "./core/Run.js";
12
+
13
+ // execute passed script...
14
+
15
+ export const invoke = async (name: string , args: string[]) => {
16
+ let js = "";
17
+
18
+ try {
19
+ if (name.endsWith(".jsx")) {
20
+ js = await Babel.transformJSX(name);
21
+ name = js;
22
+ }
23
+
24
+ const {default: fx} = await import(name);
25
+ const options: any = {
26
+ };
27
+
28
+ for (let index = 0; index < args.length; index++) {
29
+ let element = args[index];
30
+ let key = element;
31
+ if (element.startsWith("--")) {
32
+ element = element.substring(2);
33
+ key = element;
34
+ index++;
35
+ options[key] = args[index];
36
+ }
37
+ }
38
+
39
+ let node = fx;
40
+ if (!(node instanceof XNode)) {
41
+ node = fx(options);
42
+ } else {
43
+ for (const key in options) {
44
+ if (Object.prototype.hasOwnProperty.call(options, key)) {
45
+ const element = options[key];
46
+ node.children[key] = element;
47
+ }
48
+ }
49
+ }
50
+ await node.execute();
51
+ } catch (error) {
52
+ console.error(error);
53
+ }
54
+
55
+ if (js) {
56
+ unlinkSync(js);
57
+ }
58
+
59
+ }
60
+
61
+ if (process.argv.length) {
62
+ // check passed script arguments...
63
+
64
+ // first arg will be the node
65
+ // second arg will be the jex runner script
66
+
67
+ // third arg onwards should be the batch that we would like to execute...
68
+ const [program, script, ... args] = process.argv;
69
+
70
+
71
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "jsx": "react",
4
+ "target": "ES2021",
5
+ "module":"NodeNext",
6
+ "incremental": true,
7
+ "sourceMap": true,
8
+ "declaration": true,
9
+ "declarationMap": true,
10
+ "outDir": "dist",
11
+ "skipDefaultLibCheck": true,
12
+ "skipLibCheck": true,
13
+ "experimentalDecorators": true,
14
+ "emitDecoratorMetadata": true,
15
+ "jsxFactory": "XNode.create",
16
+ "lib": [
17
+ "ES2018",
18
+ "ES2021.WeakRef",
19
+ "esnext.disposable",
20
+ "ES2021.String",
21
+ "ES2022.Object"
22
+ ]
23
+ },
24
+ "include": [
25
+ "src/**/*"
26
+ ],
27
+ "exclude": [
28
+ "node_modules",
29
+ "tests"
30
+ ]
31
+ }