-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add starter script for micro-gen (#49)
* init * add yargs dependency * format * feedback * gts check * plugin path * add __dirname to PATH * add test for starter script * gts fix * feedback: allow -I in command line * remove used files * gts fix * npm link * circle CI failure * feedback * throw error * copy protos to client * gts fix * non zero exit code * test failure * fix * add error msg before exit * loop proto.list to copy proto files * check common gax proto list in real time * feedback * fix * exec params * format * try catch block
- Loading branch information
1 parent
eda7c44
commit 9bad51d
Showing
4 changed files
with
168 additions
and
2 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
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
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,96 @@ | ||
#!/usr/bin/env node | ||
// Copyright 2019 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. | ||
|
||
import { execFileSync } from 'child_process'; | ||
import * as path from 'path'; | ||
import { argv } from 'yargs'; | ||
import * as fs from 'fs-extra'; | ||
const fileSystem = require('file-system'); | ||
|
||
const GOOGLE_GAX_PROTOS_DIR = path.join( | ||
__dirname, | ||
'..', | ||
'..', | ||
'node_modules', | ||
'google-gax', | ||
'protos' | ||
); | ||
|
||
// Add folder of plugin to PATH | ||
process.env['PATH'] = __dirname + path.delimiter + process.env['PATH']; | ||
|
||
let outputDir = ''; | ||
if (argv.output_dir) { | ||
outputDir = argv.output_dir as string; | ||
} else { | ||
console.error('Output directory is required: --output_dir path'); | ||
process.exit(1); | ||
} | ||
|
||
const protoDirs: string[] = []; | ||
if (argv.I) { | ||
if (Array.isArray(argv.I)) { | ||
protoDirs.push(...argv.I); | ||
} else { | ||
protoDirs.push(argv.I as string); | ||
} | ||
} | ||
const protoDirsArg = protoDirs.map(dir => `-I${dir}`); | ||
|
||
const protoFiles = []; | ||
if (Array.isArray(argv._)) { | ||
protoFiles.push(...argv._); | ||
} else { | ||
protoFiles.push(argv._); | ||
} | ||
|
||
// run protoc command to generate client library | ||
const protocCommand = [ | ||
`-I${GOOGLE_GAX_PROTOS_DIR}`, | ||
`--typescript_gapic_out=${outputDir}`, | ||
]; | ||
protocCommand.push(...protoDirsArg); | ||
protocCommand.push(...protoFiles); | ||
try { | ||
execFileSync(`protoc`, protocCommand, { stdio: 'inherit' }); | ||
} catch (err) { | ||
console.error(err.toString()); | ||
process.exit(1); | ||
} | ||
|
||
try { | ||
// create protos folder to copy proto file | ||
const copyProtoDir = path.join(outputDir, 'protos'); | ||
if (!fs.existsSync(copyProtoDir)) { | ||
fs.mkdirSync(copyProtoDir); | ||
} | ||
// copy proto file to generated folder | ||
const protoList = path.join(outputDir, 'proto.list'); | ||
fs.readFileSync(protoList) | ||
.toString() | ||
.split('\n') | ||
.filter(proto => !fs.existsSync(path.join(GOOGLE_GAX_PROTOS_DIR, proto))) | ||
.forEach(proto => { | ||
protoDirs.forEach(dir => { | ||
const protoFile = path.join(dir, proto); | ||
if (fs.existsSync(protoFile)) { | ||
fileSystem.copyFileSync(protoFile, path.join(copyProtoDir, proto)); | ||
} | ||
}); | ||
}); | ||
} catch (err) { | ||
console.error(err.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,63 @@ | ||
// Copyright 2019 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. | ||
|
||
import * as assert from 'assert'; | ||
import { execSync } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
const OUTPUT_DIR = path.join(__dirname, '..', '..', '..', '.client_library'); | ||
const PROTOS_DIR = path.join(process.cwd(), 'build', 'test', 'protos'); | ||
const PROTO_FILE = path.join( | ||
PROTOS_DIR, | ||
'google', | ||
'showcase', | ||
'v1beta1', | ||
'echo.proto' | ||
); | ||
const CLIENT_LIBRARY_BASELINE = path.join( | ||
__dirname, | ||
'..', | ||
'..', | ||
'..', | ||
'typescript', | ||
'test', | ||
'testdata', | ||
'echo_client_baseline.ts.txt' | ||
); | ||
|
||
describe('StarterScriptTest', () => { | ||
describe('use start script for generating showcase library ', () => { | ||
it('use custom folder for generated client library.', function() { | ||
this.timeout(10000); | ||
fs.mkdirSync(OUTPUT_DIR); | ||
execSync( | ||
`gapic-generator-typescript` + | ||
` -I${PROTOS_DIR}` + | ||
` ${PROTO_FILE}` + | ||
` --output_dir=${OUTPUT_DIR}` | ||
); | ||
const GENERATED_CLIENT_FILE = path.join( | ||
OUTPUT_DIR, | ||
'src', | ||
'v1beta1', | ||
'echo_client.ts' | ||
); | ||
assert.strictEqual( | ||
fs.readFileSync(GENERATED_CLIENT_FILE).toString(), | ||
fs.readFileSync(CLIENT_LIBRARY_BASELINE).toString() | ||
); | ||
}); | ||
}); | ||
}); |