Skip to content

Commit 85cb296

Browse files
committed
-- initial commit --
0 parents  commit 85cb296

File tree

9 files changed

+294
-0
lines changed

9 files changed

+294
-0
lines changed

.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/tmp
6+
/out-tsc
7+
/release
8+
/lib
9+
/bin
10+
11+
# dependencies
12+
/node_modules
13+
14+
# IDEs and editors
15+
/.idea
16+
.project
17+
.classpath
18+
.c9/
19+
*.launch
20+
.settings/
21+
*.sublime-workspace
22+
23+
# IDE - VSCode
24+
.vscode/*
25+
!.vscode/settings.json
26+
!.vscode/tasks.json
27+
!.vscode/launch.json
28+
!.vscode/extensions.json
29+
30+
# misc
31+
/.sass-cache
32+
/connect.lock
33+
/coverage
34+
/libpeerconnection.log
35+
npm-debug.log
36+
yarn-error.log
37+
testem.log
38+
/typings
39+
40+
# System Files
41+
.DS_Store
42+
Thumbs.db
43+
44+
package-lock.json
45+
*.tgz
46+
examples/**/*-test.csv
47+
examples/**/*-test.json
48+
/examples/project/node_modules

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/src
2+
/node_modules
3+
.gitignore
4+
rollup.config.js
5+
rollup.config.dev.js
6+
*.tgz
7+
examples/*-test.csv
8+
examples/*-test.json

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"*.jspy": "python"
4+
},
5+
"typescript.tsdk": "node_modules\\typescript\\lib"
6+
}

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# jspython CLI.
2+
3+
Command line interface to run [jspython](https://github.com/jspython-dev/jspython) files (*.jspy)
4+
5+
### Install
6+
7+
```
8+
npm install -g @jspython-dev/jspython-cli
9+
```
10+
11+
### Run in terminal
12+
```
13+
jspython-cli --file path/to/jspython/file
14+
jspython-cli --file test.jsy
15+
```
16+
17+
### Development
18+
Run example using node. (Works only if you have `@jspython-dev/jspython-cli` devDependencies in you project)
19+
```
20+
node ./bin/jspython-cli --file=examples/project/axios-test.jspy
21+
node ./bin/jspython-cli --file examples/project/parse.jspy
22+
node ./bin/jspython-cli --file examples/project/p-test.jspy
23+
```
24+

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@jspython-dev/jspython-cli",
3+
"version": "0.0.1",
4+
"description": "CLI for jspython. Allows you to run jspython (*.jspy) files",
5+
"main": "src/index.ts",
6+
"bin": {
7+
"JSPython-cli": "bin/JSPython-cli"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"dev": "npx rollup -c rollup.config.dev.js",
12+
"build": "npx rollup -c rollup.config.js"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/jspython-dev/jspython-cli.git"
17+
},
18+
"keywords": [
19+
"jspython",
20+
"python",
21+
"interpreter",
22+
"cli"
23+
],
24+
"author": "Pavlo Paska - ppaska@falconsoft-ltd.com",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/jspython-dev/jspython-cli/issues"
28+
},
29+
"homepage": "https://github.com/jspython-dev/jspython-cli#readme",
30+
"dependencies": {
31+
"@jspython-dev/jspython": "0.0.1",
32+
"arg": "^4.1.1"
33+
},
34+
"devDependencies": {
35+
"rollup": "^1.26.5",
36+
"rollup-plugin-copy": "^3.1.0",
37+
"rollup-plugin-typescript2": "^0.25.2",
38+
"typescript": "^3.7.2"
39+
}
40+
}

rollup.config.dev.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import typescript from 'rollup-plugin-typescript2';
2+
3+
export default {
4+
5+
input: 'src/index.ts',
6+
output: {
7+
name: 'JSPython-cli',
8+
file: 'bin/JSPython-cli',
9+
format: 'cjs',
10+
sourcemap: true,
11+
banner: '#!/usr/bin/env node'
12+
},
13+
plugins: [
14+
typescript()
15+
],
16+
external: ['arg', 'fs', 'JSPython', 'json5'],
17+
watch: {
18+
exclude: ['node_modules/**'],
19+
include: 'src/**'
20+
}
21+
};

rollup.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import typescript from 'rollup-plugin-typescript2';
2+
import copy from 'rollup-plugin-copy'
3+
4+
const pkg = require('./package.json');
5+
6+
export default {
7+
input: pkg.main,
8+
output: { file: pkg.bin['JSPython-cli'], format: 'cjs', sourcemap: true, compact: true, banner: '#!/usr/bin/env node' },
9+
plugins: [
10+
typescript({
11+
clean: true
12+
}),
13+
copy({
14+
targets: [
15+
{ src: 'src/examples', dest: 'dist' }
16+
]
17+
})
18+
],
19+
external: ['arg', 'fs', 'JSPython']
20+
};

src/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import arg from 'arg';
2+
import fs from 'fs';
3+
import { jsPython, PackageToImport } from '@jspython-dev/jspython';
4+
5+
const pkg = require('../package.json');
6+
7+
export const interpreter = jsPython();
8+
9+
run();
10+
11+
async function run() {
12+
const options = getOptionsFromArguments(process.argv);
13+
if (options.version) {
14+
console.log(`Version:\n${pkg.version}\n`);
15+
}
16+
17+
if (options.file) {
18+
interpreter.registerPackagesLoader(packageLoader);
19+
const scripts = fs.readFileSync(options.file, 'utf8');
20+
const res = await interpreter.evaluate(scripts);
21+
console.log('Execution result:\n', res);
22+
console.log('Finish');
23+
}
24+
}
25+
26+
function getOptionsFromArguments(rawArgs: string[]) {
27+
const args = arg({
28+
'--file': String,
29+
'--version': Boolean,
30+
'-f': '--file',
31+
'-v': '--version'
32+
}, {
33+
argv: rawArgs.slice(2)
34+
});
35+
36+
return {
37+
file: args['--file'] || (rawArgs.length === 3 && !rawArgs[2].startsWith('-') ? rawArgs[2] : ''),
38+
version: args['--version']
39+
};
40+
}
41+
42+
43+
function packageLoader(packages: PackageToImport[]): object {
44+
const libraries: any = {};
45+
packages.forEach(({ name, as, properties }: PackageToImport) => {
46+
const lib = require(name);
47+
if (properties?.length) {
48+
properties.forEach((prop) => {
49+
libraries[prop.as || prop.name] = lib[prop.name];
50+
})
51+
} else if (as) {
52+
libraries[as] = lib;
53+
} else {
54+
libraries[name] = lib;
55+
}
56+
if (as) {
57+
libraries[as] = lib;
58+
}
59+
});
60+
return libraries;
61+
}

tsconfig.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
// "incremental": true, /* Enable incremental compilation */
5+
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
6+
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
7+
// "lib": [], /* Specify library files to be included in the compilation. */
8+
// "allowJs": true, /* Allow javascript files to be compiled. */
9+
// "checkJs": true, /* Report errors in .js files. */
10+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
11+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
12+
"declarationMap": false, /* Generates a sourcemap for each corresponding '.d.ts' file. */
13+
"sourceMap": false, /* Generates corresponding '.map' file. */
14+
// "outFile": "./", /* Concatenate and emit output to single file. */
15+
// "outDir": "./", /* Redirect output structure to the directory. */
16+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
17+
// "composite": true, /* Enable project compilation */
18+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
19+
// "removeComments": true, /* Do not emit comments to output. */
20+
// "noEmit": true, /* Do not emit outputs. */
21+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
22+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
23+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24+
25+
/* Strict Type-Checking Options */
26+
"strict": true, /* Enable all strict type-checking options. */
27+
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
28+
// "strictNullChecks": true, /* Enable strict null checks. */
29+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
30+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
31+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
32+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
33+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34+
35+
/* Additional Checks */
36+
// "noUnusedLocals": true, /* Report errors on unused locals. */
37+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
38+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
40+
41+
/* Module Resolution Options */
42+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
43+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
44+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
45+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
46+
// "typeRoots": [], /* List of folders to include type definitions from. */
47+
// "types": [], /* Type declaration files to be included in compilation. */
48+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
49+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
50+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
52+
53+
/* Source Map Options */
54+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
55+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
56+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
57+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
58+
59+
/* Experimental Options */
60+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
61+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
62+
63+
/* Advanced Options */
64+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
65+
}
66+
}

0 commit comments

Comments
 (0)