|
1 | 1 | # jsii-srcmak |
2 | 2 |
|
3 | | -> Generates jsii source files for multiple languages from TypeScript. |
| 3 | +> Generates [jsii] source files for multiple languages from TypeScript. |
| 4 | +
|
| 5 | +[jsii]: https://github.com/aws/jsii |
4 | 6 |
|
5 | 7 | ## Usage |
6 | 8 |
|
7 | | -Say I have a TypeScript file `source/index.ts`: |
| 9 | +This package can be either used as a library or through a CLI. |
| 10 | + |
| 11 | +The library entry point is the `srcmak` function: |
8 | 12 |
|
9 | 13 | ```ts |
10 | | -export interface Operands { |
11 | | - readonly lhs: number; |
12 | | - readonly rhs: number; |
13 | | -} |
14 | | - |
15 | | -export class Calc { |
16 | | - public add(ops: Operands): number { |
17 | | - return ops.lhs + ops.rhs; |
18 | | - } |
| 14 | +import { srcmak } from 'jsii-srcmak'; |
| 15 | +await srcmak(srcdir[, options]); |
| 16 | +``` |
| 17 | + |
| 18 | +The CLI is `jsii-srcmak`: |
| 19 | + |
| 20 | +```bash |
| 21 | +$ jsii-srcmak srcdir [OPTIONS] |
| 22 | +``` |
| 23 | + |
| 24 | +The `srcdir` argument points to a directory tree that includes TypeScript files |
| 25 | +which will be translated through jsii to one of the supported languages. |
| 26 | + |
| 27 | +### Compile only |
| 28 | + |
| 29 | +If called with no additional arguments, `srcmak` will only jsii-compile the source. If compilation fails, it will throw an error. This is a nice way to check if generated typescript code is jsii-compatible: |
| 30 | + |
| 31 | +```ts |
| 32 | +const srcdir = generateSomeTypeScriptCode(); |
| 33 | + |
| 34 | +// verify it is jsii-compatible (throws otherwise) |
| 35 | +await srcmak(srcdir); |
| 36 | +``` |
| 37 | + |
| 38 | +CLI: |
| 39 | + |
| 40 | +```bash |
| 41 | +$ jsii-srcmak /source/directory |
| 42 | +``` |
19 | 43 |
|
20 | | - public mul(ops: Operands): number { |
21 | | - return ops.lhs * opts.rhs; |
| 44 | +### Python Output |
| 45 | + |
| 46 | +To produce a Python module from your source, use the `python` option: |
| 47 | + |
| 48 | +```ts |
| 49 | +await srcmak('srcdir', { |
| 50 | + python: { |
| 51 | + outdir: '/path/to/project/root', |
| 52 | + moduleName: 'name.of.python.module' |
22 | 53 | } |
23 | | -} |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +Or the `--python-*` switches in the CLI: |
| 58 | + |
| 59 | +```bash |
| 60 | +$ jsii-srcmak /src/dir --python-outdir=dir --python-module-name=module.name |
24 | 61 | ``` |
25 | 62 |
|
26 | | -The following invocation will generate `target/my_python_module` with `Calc` in python: |
| 63 | +* The `outdir`/`--python-outdir` option points to the root directory of your Python project. |
| 64 | +* The `moduleName`/`--python-module-name` option is the python module name. Dots (`.`) delimit submodules. |
| 65 | + |
| 66 | +The output directory will include a python module that corresponds to the |
| 67 | +original module. This code depends on the following python modules: |
| 68 | + |
| 69 | +- [jsii](https://pypi.org/project/jsii/) |
| 70 | +- [publication](https://pypi.org/project/publication/) |
| 71 | + |
| 72 | +### Entrypoint |
| 73 | + |
| 74 | +The `entrypoint` option can be used to customize the name of the typescript entrypoint (default is `index.ts`). |
| 75 | + |
| 76 | +For example, if the code's entry point is under `/srcdir/foobar/lib/index.ts` then I can specify: |
27 | 77 |
|
28 | 78 | ```ts |
29 | | -import { srcmak } from 'jsii-srcmak'; |
| 79 | +await srcmak('/srcdir', { |
| 80 | + entrypoint: 'foobar/lib/index.ts' |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +Or through the CLI: |
| 85 | + |
| 86 | +```bash |
| 87 | +$ jsii-srcmak /srcdir --entrypoint lib/main.ts |
| 88 | +``` |
30 | 89 |
|
31 | | -await srcmak('source', 'target', { |
32 | | - pythonName: 'my_python_module' |
| 90 | +### Dependencies |
| 91 | + |
| 92 | +The `deps` option can be used to specify a list of node module **directories** (must have a `package.json` file) which will be symlinked into the workspace when compiling your code. |
| 93 | + |
| 94 | +This is required if your code references types from other modules. |
| 95 | + |
| 96 | +Use this idiom to resolve a set of modules directories from the calling process: |
| 97 | + |
| 98 | +```ts |
| 99 | +const modules = [ |
| 100 | + '@types/node', // commonly needed |
| 101 | + 'foobar' // a node module in *my* closure |
| 102 | +]; |
| 103 | + |
| 104 | +const getModuleDir = m => |
| 105 | + path.dirname(require.resolve(`${m}/package.json`)); |
| 106 | + |
| 107 | +await srcmak('srcdir', { |
| 108 | + deps: modules.map(getModuleDir) |
33 | 109 | }); |
34 | 110 | ``` |
35 | 111 |
|
| 112 | +Or through the CLI: |
| 113 | + |
| 114 | +```bash |
| 115 | +$ jsii-srcmak /src/dir --dep node_modules/@types/node --dep node_modules/constructs |
| 116 | +``` |
| 117 | + |
| 118 | +## What's with this name? |
| 119 | + |
| 120 | +It's a silly little pun that stems from another pun: jsii has `jsii-pacmak` |
| 121 | +which stands for "package maker". That's the tool that takes in a .jsii manifest |
| 122 | +and produces language-idiomatic *packages* from it. This tool produces *sources* |
| 123 | +from a .jsii manifest. Hence, "source maker". Yeah, it's lame. |
| 124 | + |
36 | 125 | ## License |
37 | 126 |
|
38 | 127 | Distributed under the [Apache 2.0](./LICENSE) license. |
0 commit comments