Skip to content

Commit

Permalink
feat(node): auto-detect "bin"
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Ben-Israel committed Jun 19, 2020
1 parent 5eddfa2 commit 8c42f8e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const project = new JsiiProject({
authorName: 'Elad Ben-Israel',
authorEmail: 'benisrae@amazon.com',
stability: 'experimental',
bin: {
projen: 'bin/projen'
},
dependencies: {
constructs: Semver.pinned('2.0.1'),
yaml: Semver.caret('1.9.2'),
Expand Down
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,41 @@ are manually modified, your CI build will fail.

## API Reference

See [API Reference](./API.md) for more details.
See [API Reference](./API.md) for API details.

### JsiiProject

Directory structure:

* `src/` - `.ts` files, after compilation they will go under `lib/`.
* `test/` - `.ts` files for jest tests. Those will not be included in your npm module.

#### Executables/CLIs (`bin`)

You should create executable scripts under `bin/`, but **do not** include
typescript files there. Those must be under `src/` or otherwise they won't be
compiled and included in your output module.

Let's walk through a simple example. Say my CLI should be called `mycli`:

1. Create a file `lib/mycli.ts` with the actual code of the CLI. No need to export this file from your `index.ts` file.
2. Create a file `bin/mycli` with the following content:

```js
#!/usr/bin/env node
require('../lib/mycli.js');
```

That's it. projen will auto-detect `bin/mycli` and will add it to your
`package.json` under the `bin` section. You can disable this behavior by setting
`autoDetectBin: false`.
## Contributing
Expand Down
35 changes: 35 additions & 0 deletions src/node-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { GENERATION_DISCLAIMER, PROJEN_RC, PROJEN_VERSION } from './common';
import { Lazy } from 'constructs';
import { Version } from './version';
import { GithubWorkflow } from './github-workflow';
import * as fs from 'fs-extra';
import * as path from 'path';

const ANTITAMPER_COMMAND = [
{
Expand All @@ -21,7 +23,24 @@ export interface CommonOptions {
readonly devDependencies?: Record<string, Semver>;
readonly peerDependencies?: Record<string, Semver>;
readonly peerDependencyOptions?: PeerDependencyOptions;

/**
* Binary programs vended with your module.
*
* You can use this option to add/customize how binaries are represented in
* your `package.json`, but unless `autoDetectBin` is `false`, every
* executable file under `bin` will automatically be added to this section.
*/
readonly bin?: Record<string, string>;

/**
* Automatically add all executables under the `bin` directory to your
* `package.json` file under the `bin` section.
*
* @default true
*/
readonly autoDetectBin?: boolean;

readonly keywords?: string[];

/**
Expand Down Expand Up @@ -266,6 +285,22 @@ export class NodeProject extends Project {
});
}
}

// automatically add all executable files under "bin"
if (options.autoDetectBin ?? true) {
const bindir = 'bin';

if (fs.existsSync(bindir)) {
for (const file of fs.readdirSync(bindir)) {
try {
fs.accessSync(path.join(bindir, file), fs.constants.X_OK);
this.bin[file] = path.join(bindir, file);
} catch (e) {
// not executable, skip
}
}
}
}
}

/**
Expand Down

0 comments on commit 8c42f8e

Please sign in to comment.