Skip to content

Commit

Permalink
feat: new typeless sample bot, initial commit (#3203)
Browse files Browse the repository at this point in the history
This is the bot I've been working on, which converts from TypeScript snippets to JavaScript snippets. I'm calling it the "typeless bot" since it removes types, and a few other changes. It's still a work in progress.

This is just to put some of this in front of people to look at, not meant to be merged yet.
  • Loading branch information
feywind authored Aug 5, 2022
1 parent 7cd7d50 commit c57a722
Show file tree
Hide file tree
Showing 26 changed files with 1,755 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/fixtures
build/
docs/
protos/
packages/
13 changes: 13 additions & 0 deletions .github/header-checker-lint.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
allowedCopyrightHolders:
- 'Google LLC'
allowedLicenses:
- 'Apache-2.0'
- 'MIT'
- 'BSD-3'
sourceFileExtensions:
- 'ts'
- 'js'
- 'java'
ignoreFiles:
- "packages/*/webpack.config.js"
- "packages/*/__snapshots__/**/*.js"
- "packages/typeless-sample-bot/test/fixtures/**/*.ts"
- "packages/typeless-sample-bot/test/fixtures/**/*.js"
3 changes: 3 additions & 0 deletions .github/snippet-bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignoreFiles:
- packages/typeless-sample-bot/test/fixtures/**/*.js
- packages/typeless-sample-bot/test/fixtures/**/*.ts
5 changes: 5 additions & 0 deletions packages/typeless-sample-bot/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules
**/coverage
test/fixtures
build/
__snapshots__/
6 changes: 6 additions & 0 deletions packages/typeless-sample-bot/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./node_modules/gts",
"rules": {
"no-process-exit": 0
}
}
2 changes: 2 additions & 0 deletions packages/typeless-sample-bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
17 changes: 17 additions & 0 deletions packages/typeless-sample-bot/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module.exports = {
...require('gts/.prettierrc.json')
}
124 changes: 124 additions & 0 deletions packages/typeless-sample-bot/__snapshots__/index.js

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions packages/typeless-sample-bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@google-cloud/typeless-sample-bot",
"description": "Google Cloud GitHub bot that converts TypeScript snippets into JavaScript",
"version": "0.0.1",
"license": "Apache-2.0",
"author": "Google LLC",
"engines": {
"node": ">=16.0.0"
},
"repository": "googleapis/google-cloud-node",
"main": "node build/src/bin/samples-ts-to-js.js",
"type": "module",
"files": [
"build/src"
],
"scripts": {
"compile": "tsc",
"test": "c8 mocha build/test --recursive",
"snapshots-update": "SNAPSHOT_UPDATE=1 npm test",
"pretest": "npm run compile",
"system-test": "echo no system tests yet 🙀",
"lint": "gts check",
"fix": "gts fix"
},
"dependencies": {
"@babel/core": "^7.18.10",
"@babel/plugin-transform-modules-commonjs": "^7.18.0",
"@babel/preset-env": "^7.18.0",
"@babel/preset-typescript": "^7.18.0",
"@babel/traverse": "^7.18.10",
"chalk": "^5.0.1",
"debug": "^4.3.4",
"recast": "^0.21.1",
"yargs": "^17.4.1"
},
"devDependencies": {
"@babel/cli": "^7.16.0",
"@babel/types": "^7.17.0",
"@types/babel__core": "^7.1.19",
"@types/babel__traverse": "^7.14.2",
"@types/mocha": "^9.1.1",
"@types/node": "^17.0.23",
"@types/sinon": "^10.0.13",
"@types/yargs": "^17.0.10",
"c8": "^7.11.3",
"gts": "^3.1.0",
"mocha": "^10.0.0",
"sinon": "^14.0.0",
"snap-shot-it": "^7.9.6",
"typescript": "^4.6.3"
}
}
196 changes: 196 additions & 0 deletions packages/typeless-sample-bot/src/bin/samples-ts-to-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import yargs from 'yargs';
import loggers from '../loggers.js';
import {createLoggers} from '../loggers.js';
import util from 'node:util';
import {symbols} from '../symbols.js';
import {
filterByContents,
findSamples,
transformSamples,
writeSamples,
waitForAllSamples,
fromArray,
} from '../samples.js';
import url from 'node:url';

let returnValue = 0;

// Converts an array of unknown-type items into a string suitable for
// printing to the console.
export function consolize(args: unknown[]): string {
const strings = args.map(a => {
if (typeof a === 'string') {
return a;
} else if (typeof a === 'number') {
return `${a}`;
} else {
return util.inspect(a, false, 2, true);
}
});
return strings.join(' ');
}

async function processArgs(args: string[]) {
const argv = await yargs(args)
.options({
targets: {
demandOption: true,
type: 'array',
describe: 'one or more items to process',
alias: ['t'],
},
recursive: {
demandOption: false,
boolean: true,
describe: 'process the target(s) as directories, recursively',
alias: ['r'],
},
verbose: {
demandOption: false,
default: false,
boolean: true,
describe:
'set flag to get verbose output about actions (GCP_DEBUG=verbose)',
alias: ['v'],
},
debug: {
demandOption: false,
default: false,
boolean: true,
describe: 'same as using GCP_DEBUG=*',
alias: ['d'],
},
art: {
demandOption: false,
boolean: true,
default: true,
describe: 'allow ASCII/ANSI art/colours',
alias: ['c'],
},
})
.help().argv;

if (argv.mode === 'help') {
return undefined;
}

symbols.art = argv.art;

return argv;
}

let loggersSetUp = false;
async function setupLoggers(verbose: boolean) {
// This may be needed on subsequent testing runs.
returnValue = 0;

// Don't double-create or .on() anything. This can happen
// during testing.
if (loggersSetUp) {
return;
}

await createLoggers();

// Set up our log outputs as needed
if (verbose) {
loggers.verbose.on('log', (args: unknown[]) =>
console.debug(symbols.grey(consolize([symbols.bug, ...args])))
);
}
loggers.step.on('log', (args: unknown[]) =>
console.log(consolize([symbols.step, ...args]))
);
loggers.error.on('log', (args: unknown[]) => {
console.error(symbols.red(consolize([symbols.failure, ...args])));

// Also cause main() to return a failure.
returnValue = 1;
});

loggersSetUp = true;
}

export async function main(args: string[]): Promise<number> {
console.log(
symbols.green('Typeless sample bot, converts TS to JS sample snippets')
);

// Process command line args and get loggers configured.
const argv = await processArgs(args);
if (!argv) {
return 0;
}
await setupLoggers(argv.verbose);

// Find all of the samples we're interested in working on.
let sampleFns: AsyncIterable<string>;
if (argv.recursive) {
sampleFns = findSamples(
argv.targets.map(i => i.toString()),
/(?!node_modules).*\.ts$/
);
} else {
sampleFns = fromArray(argv.targets.map(i => i.toString()));
}

// Filter down to samples that have a snippet tag.
const filtered = filterByContents(sampleFns);

// Transform those samples using Babel.
const transformed = transformSamples(filtered);

// Write out all of the output samples.
const written = writeSamples(transformed);

try {
// Wait for the pipeline to complete.
const count = await waitForAllSamples(written);

if (!count) {
// Should this be considred a failure?
loggers.error('No samples were selected.');
}
} catch (e) {
loggers.error('Exception during processing:', e);
}

if (!returnValue) {
console.log(symbols.success, symbols.green('Generation complete!'));
} else {
console.log(
symbols.failure,
symbols.redBright(
'Something failed. (Maybe not everything, check the log above.)'
)
);
}

return returnValue;
}

// Only activate our command line mode if this is the "main" module.
if (import.meta.url === url.pathToFileURL(process.argv[1]).href) {
main(process.argv.slice(2))
.then(e => process.exit(e))
.catch((e: Error) => {
console.error(
`Top level exception: ${e.toString()} ${e.stack?.toString()}`
);
process.exit(1);
});
}
16 changes: 16 additions & 0 deletions packages/typeless-sample-bot/src/debug-js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Sadly this debug-js module doesn't seem to have typings.
declare module 'debug';
Loading

0 comments on commit c57a722

Please sign in to comment.