-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement a CLI to run multiple JSON files (#66)
- Loading branch information
Showing
5 changed files
with
133 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
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,43 @@ | ||
/** | ||
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 | ||
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 { parse, createRunner } from './main.js'; | ||
import { readFileSync } from 'fs'; | ||
|
||
export function getFilenames(argv: string[]) { | ||
return argv.slice(2); | ||
} | ||
|
||
export async function runFiles( | ||
files: string[], | ||
opts = { log: false } | ||
): Promise<boolean> { | ||
for (const file of files) { | ||
opts.log && console.log(`Running ${file}...`); | ||
try { | ||
const content = readFileSync(file, 'utf-8'); | ||
const object = JSON.parse(content); | ||
const recording = parse(object); | ||
const runner = await createRunner(recording); | ||
await runner.run(); | ||
opts.log && console.log(`Finished running ${file}`); | ||
} catch (err) { | ||
opts.log && console.error(`Error running ${file}`, err); | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
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,27 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
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 | ||
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 { getFilenames, runFiles } from './CLIUtils.js'; | ||
|
||
const recordings = getFilenames(process.argv); | ||
if (!recordings.length) { | ||
console.log(`Usage: replay filename [filename...]`); | ||
} | ||
await runFiles(recordings, { | ||
log: true, | ||
}); |
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,45 @@ | ||
/** | ||
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 | ||
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 { getFilenames, runFiles } from '../src/CLIUtils.js'; | ||
import { assert } from 'chai'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
|
||
const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); | ||
|
||
describe('cli', () => { | ||
describe('getFilenames', () => { | ||
it('extracts filenames from process.argv', () => { | ||
assert.deepStrictEqual(getFilenames(['node', 'script.js']), []); | ||
assert.deepStrictEqual(getFilenames(['node', 'script.js', 'file1']), [ | ||
'file1', | ||
]); | ||
assert.deepStrictEqual( | ||
getFilenames(['node', 'script.js', 'file1', 'file2', 'file3']), | ||
['file1', 'file2', 'file3'] | ||
); | ||
}); | ||
}); | ||
|
||
describe('runFiles', () => { | ||
it('is able to run successfully', async () => { | ||
assert.isTrue( | ||
await runFiles([path.join(__dirname, 'resources', 'replay.json')]) | ||
); | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
{ | ||
"title": "wiki", | ||
"steps": [ | ||
{ | ||
"type": "navigate", | ||
"url": "https://www.wikipedia.org/" | ||
} | ||
] | ||
} |