Skip to content

Commit 8e93229

Browse files
authored
fix: typeless bot updates for a fully functioning packaged version (#3461)
* docs: add a readme * fix: get the typeless bot command line working * fix: import the correct 'main' * tests: snapshot for one test was wrong * fix: preserve the module path for Babel
1 parent c8c43bb commit 8e93229

7 files changed

Lines changed: 108 additions & 20 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# typeless-sample-bot
2+
3+
This bot will automatically convert sample snippets written in TypeScript into JavaScript
4+
sample snippets. This lets you focus on writing TypeScript samples instead of having to
5+
do only JavaScript, or having to maintain both.
6+
7+
## Installation
8+
9+
You will want to install this library as a globally-available, standalone binary.
10+
11+
`npm i @google-cloud/typeless-sample-bot -g`
12+
13+
## Usage
14+
15+
The bot can convert single files or an entire tree of files. In the latter case, it
16+
will attempt to guess which files are actually snippets before doing anything with them.
17+
18+
For individual files, you can specify them as targets:
19+
20+
```bash
21+
typeless-sample-bot --targets file1.ts,file2.ts,etc
22+
```
23+
24+
The output will be written with the same filename stem, but a js extension.
25+
26+
You can also ask for recursive processing:
27+
28+
```bash
29+
typeless-sample-bot --targets samples --recursive
30+
```
31+
32+
Note that -t may be an array, still, for processing multiple recursive trees.
33+
34+
Various utility flags are also available:
35+
36+
```bash
37+
# turn on verbose output
38+
typeless-sample-bot --verbose
39+
40+
# turn on full debug output
41+
typeless-sample-bot --debug
42+
43+
# turn off ANSI colour/emojis
44+
typeless-sample-bot --no-art
45+
```
46+
47+
### Options
48+
49+
| Option | Description | Default |
50+
| ------ | ----------- | ------- |
51+
| targets | List of target files or directory trees | *Required* |
52+
| recursive | If set, targets will be directory trees processed recursively | False |
53+
| verbose | If set, verbose output will be enabled | False |
54+
| debug | If set, full debug output will be enabled | False |
55+
| art | If set, ANSI colour and emojis will be used in the command output | True, use --no-art to cancel |
56+
57+
## Running tests:
58+
59+
`npm test`
60+
61+
## Contributing
62+
63+
If you have suggestions for how `typeless-sample-bot` could be improved, or want to report a bug, open an issue! We'd love all and any contributions.
64+
65+
For more, check out the Contributing Guide.
66+
67+
License: Apache 2.0
68+
69+
© 2022 Google LLC.

packages/typeless-sample-bot/__snapshots__/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typeless-sample-bot/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"node": ">=16.0.0"
99
},
1010
"repository": "googleapis/google-cloud-node",
11-
"main": "node build/src/bin/samples-ts-to-js.js",
11+
"bin": "./build/src/bin/typeless-sample-bot.js",
12+
"main": "./build/src/app.js",
1213
"type": "module",
1314
"files": [
1415
"build/src"

packages/typeless-sample-bot/src/bin/samples-ts-to-js.ts renamed to packages/typeless-sample-bot/src/app.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
// Copyright 2022 Google LLC
23
//
34
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,18 +14,18 @@
1314
// limitations under the License.
1415

1516
import yargs from 'yargs';
16-
import loggers from '../loggers.js';
17-
import {createLoggers} from '../loggers.js';
17+
import loggers from './loggers.js';
18+
import {createLoggers} from './loggers.js';
1819
import util from 'node:util';
19-
import {symbols} from '../symbols.js';
20+
import {symbols} from './symbols.js';
2021
import {
2122
filterByContents,
2223
findSamples,
2324
transformSamples,
2425
writeSamples,
2526
waitForAllSamples,
2627
fromArray,
27-
} from '../samples.js';
28+
} from './samples.js';
2829
import url from 'node:url';
2930

3031
let returnValue = 0;
@@ -182,15 +183,3 @@ export async function main(args: string[]): Promise<number> {
182183

183184
return returnValue;
184185
}
185-
186-
// Only activate our command line mode if this is the "main" module.
187-
if (import.meta.url === url.pathToFileURL(process.argv[1]).href) {
188-
main(process.argv.slice(2))
189-
.then(e => process.exit(e))
190-
.catch((e: Error) => {
191-
console.error(
192-
`Top level exception: ${e.toString()} ${e.stack?.toString()}`
193-
);
194-
process.exit(1);
195-
});
196-
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env node
2+
// Copyright 2022 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
import {main} from '../app.js';
17+
18+
main(process.argv.slice(2))
19+
.then(e => process.exit(e))
20+
.catch((e: Error) => {
21+
console.error(
22+
`Top level exception: ${e.toString()} ${e.stack?.toString()}`
23+
);
24+
process.exit(1);
25+
});

packages/typeless-sample-bot/src/samples.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import loggers from './loggers.js';
1919
import {treeWalk} from './tree-walk.js';
2020
import {readFile, writeFile} from 'fs/promises';
2121
import babel from '@babel/core';
22+
import {fileURLToPath} from 'node:url';
23+
import path from 'node:path';
2224

2325
// Converts an async iterable into an array of the same type.
2426
export async function toArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
@@ -78,9 +80,11 @@ export async function* filterByContents(
7880

7981
// Instead of a babelrc, this is used so that we can get more control over
8082
// the transform process.
83+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
84+
const itrPath = path.join(__dirname, 'import-to-require');
8185
const babelConfig = {
8286
presets: [['@babel/preset-typescript', {}]],
83-
plugins: [['./build/src/import-to-require']],
87+
plugins: [[itrPath]],
8488
parserOpts: {} as babel.ParserOptions,
8589
generatorOpts: {
8690
// Ensures that Babel keeps newlines so that comments end up

packages/typeless-sample-bot/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as assert from 'assert';
1616
import {describe, it} from 'mocha';
1717
import snapshot from 'snap-shot-it';
1818
import * as samples from '../src/samples.js';
19-
import * as main from '../src/bin/samples-ts-to-js.js';
19+
import * as main from '../src/app.js';
2020
import {readFile, rm, stat} from 'node:fs/promises';
2121
import * as url from 'node:url';
2222
import * as path from 'node:path';

0 commit comments

Comments
 (0)