-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new typeless sample bot, initial commit (#3203)
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
Showing
26 changed files
with
1,755 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ test/fixtures | |
build/ | ||
docs/ | ||
protos/ | ||
packages/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/node_modules | ||
**/coverage | ||
test/fixtures | ||
build/ | ||
__snapshots__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "./node_modules/gts", | ||
"rules": { | ||
"no-process-exit": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
196
packages/typeless-sample-bot/src/bin/samples-ts-to-js.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
Oops, something went wrong.