Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from superfaceai/feature/play-command
Browse files Browse the repository at this point in the history
Feature/play command
  • Loading branch information
lukas-valenta authored Dec 18, 2020
2 parents cdf7551 + cf93b14 commit d2b47de
Show file tree
Hide file tree
Showing 28 changed files with 2,421 additions and 851 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
rules: {
'newline-before-return': 'error',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'simple-import-sort/sort': 'error',
'simple-import-sort/imports': 'error',
'sort-imports': 'off',
'import/first': 'error',
'import/newline-after-import': 'error',
Expand Down
3 changes: 3 additions & 0 deletions fixtures/playgrounds/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/package-lock.json
**/*.ast.json
**/*.js
6 changes: 6 additions & 0 deletions fixtures/playgrounds/invalid/invalid.default.suma
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
profile = "https://example.com/pubs"
provider = "https://example.com/pubs/Noop"

map PubOpeningHours {
return map result []
}
Empty file.
1 change: 1 addition & 0 deletions fixtures/playgrounds/valid/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@superfaceai:registry=https://npm.pkg.github.com
10 changes: 10 additions & 0 deletions fixtures/playgrounds/valid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "valid",
"private": true,
"dependencies": {
"@superfaceai/sdk": "^0.0.3"
},
"devDependencies": {
"typescript": "^4"
}
}
6 changes: 6 additions & 0 deletions fixtures/playgrounds/valid/valid.noop.suma
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
profile = "https://example.com/pubs"
provider = "https://example.com/pubs/Noop"

map PubOpeningHours {
return map result []
}
42 changes: 42 additions & 0 deletions fixtures/playgrounds/valid/valid.noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as fs from 'fs';
import { promisify, inspect } from 'util';
import { Provider } from '@superfaceai/sdk';

const readFile = promisify(fs.readFile);

async function main() {
const profileAst = JSON.parse(
await readFile('valid.supr.ast.json', { encoding: 'utf-8' })
);
const mapAst = JSON.parse(
await readFile('valid.noop.suma.ast.json', { encoding: 'utf-8' })
);

const provider = new Provider(
profileAst, mapAst,
'PubOpeningHours'
);

const boundProvider = await provider.bind(
{
// TODO: Add your auth keys here
}
);

const result = await boundProvider.perform(
{
city: "Praha",
nameRegex: "Diego"
}
);

console.log(
"pubs/Noop result:",
inspect(result, {
depth: 5,
colors: true
})
);
}

main()
37 changes: 37 additions & 0 deletions fixtures/playgrounds/valid/valid.osm.suma
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
profile = "https://example.com/pubs"
provider = "https://example.com/pubs/OSM"

map PubOpeningHours {
http POST "/api/interpreter" {
request "application/x-www-form-urlencoded" {
body {
data = call BuildQuery(city = input.city, nameRegex = input.nameRegex)
}
}

response 200 {
return map error if (body.remarks && body.remarks.includes('Query timed out')) "TIMEOUT"

return map result body.elements.map(
node => {
return {
name: node.tags.name,
openingHours: node.tags.opening_hours
}
}
)
}
}
}

operation BuildQuery {
regexLine = input.nameRegex ? `node._["name"~"${input.nameRegex}", i];` : '';
query = (`[out:json][timeout:10];
area[boundary=administrative][admin_level=8][name="${input.city}"];
node[amenity="pub"][opening_hours](area);
${regexLine}

out;`);

return query;
}
37 changes: 37 additions & 0 deletions fixtures/playgrounds/valid/valid.osm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as fs from 'fs';
import { promisify, inspect } from 'util';
import { Provider } from '@superfaceai/sdk';

const readFile = promisify(fs.readFile);

async function main() {
const profileAst = JSON.parse(
await readFile('valid.supr.ast.json', { encoding: 'utf-8' })
);
const mapAst = JSON.parse(
await readFile('valid.osm.suma.ast.json', { encoding: 'utf-8' })
);

const provider = new Provider(profileAst, mapAst, 'https://overpass-api.de');

const boundProvider = await provider.bind({
// TODO: Add your auth keys here
// No keys are needed for OSM
});

const result = await boundProvider.perform('PubOpeningHours', {
city: 'Praha',
nameRegex: 'Diego',
});

console.log(
'pubs/OSM result:',
inspect(result, {
depth: 5,
colors: true,
})
);
}

main();

20 changes: 20 additions & 0 deletions fixtures/playgrounds/valid/valid.supr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
profile = "https://example.com/pubs"

"""
List pub opening hours
"""
usecase PubOpeningHours {
input {
city! string!
nameRegex string!
}

result [{ name string, openingHours string }]

error PubOpeningHoursError
}

model PubOpeningHoursError enum {
TIMEOUT
}

21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"main": "dist/index.js",
"repository": "[email protected]:superfaceai/cli.git",
"author": "Superface Team",
"private": true,
"private": false,
"bin": {
"superface": "./bin/superface",
"sf": "./bin/superface"
"superface": "bin/superface",
"sf": "bin/superface"
},
"files": [
"./bin/"
"bin/",
"dist/"
],
"scripts": {
"build": "tsc -p tsconfig.release.json --outDir dist",
Expand All @@ -23,15 +24,18 @@
"prepush": "yarn test && yarn lint && yarn format"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/inquirer": "^7.3.1",
"@types/jest": "^26.0.15",
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.7.0",
"@typescript-eslint/parser": "^4.7.0",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.2",
"eslint-plugin-simple-import-sort": "^5.0.3",
"eslint-plugin-simple-import-sort": "^6.0.1",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"stdout-stderr": "TheEdward162/stdout-stderr#fb3e22a",
Expand All @@ -41,7 +45,12 @@
"dependencies": {
"@oclif/command": "^1.8.0",
"@oclif/config": "^1.17.0",
"@superfaceai/parser": "0.0.6"
"@superfaceai/parser": "0.0.6",
"chalk": "^4.1.0",
"debug": "^4.3.1",
"inquirer": "^7.3.3",
"inquirer-file-tree-selection-prompt": "^1.0.7",
"rimraf": "^3.0.2"
},
"oclif": {
"commands": "./dist/commands",
Expand Down
14 changes: 5 additions & 9 deletions src/commands/compile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stderr, stdout } from 'stdout-stderr';

import { readFilePromise } from '../common/io';
import { readFile } from '../common/io';
import Compile from './compile';

describe('Compile CLI command', () => {
Expand All @@ -18,7 +18,7 @@ describe('Compile CLI command', () => {
it('compiles map', async () => {
const mapASTFixture = JSON.parse(
(
await readFilePromise('./fixtures/transpiled/testMap.suma.ast.json')
await readFile('./fixtures/transpiled/testMap.suma.ast.json')
).toString()
) as unknown;
await Compile.run(['./fixtures/testMap.suma', '-o', '-']);
Expand All @@ -29,9 +29,7 @@ describe('Compile CLI command', () => {
it('compiles profile', async () => {
const profileASTFixture = JSON.parse(
(
await readFilePromise(
'./fixtures/transpiled/testProfile.supr.ast.json'
)
await readFile('./fixtures/transpiled/testProfile.supr.ast.json')
).toString()
) as unknown;
await Compile.run(['./fixtures/testProfile.supr', '-o', '-2']);
Expand All @@ -42,14 +40,12 @@ describe('Compile CLI command', () => {
it('compiles two files into one stream', async () => {
const mapASTFixture = JSON.parse(
(
await readFilePromise('./fixtures/transpiled/testMap.suma.ast.json')
await readFile('./fixtures/transpiled/testMap.suma.ast.json')
).toString()
) as unknown;
const profileASTFixture = JSON.parse(
(
await readFilePromise(
'./fixtures/transpiled/testProfile.supr.ast.json'
)
await readFile('./fixtures/transpiled/testProfile.supr.ast.json')
).toString()
) as unknown;

Expand Down
23 changes: 10 additions & 13 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Command, flags } from '@oclif/command';
import { CLIError } from '@oclif/errors';
import { Source } from '@superfaceai/parser';
import * as nodePath from 'path';

Expand All @@ -8,8 +7,9 @@ import {
DocumentType,
inferDocumentTypeWithFlag,
} from '../common/document';
import { assertIsIOError, userError } from '../common/error';
import { DocumentTypeFlag, documentTypeFlag } from '../common/flags';
import { lstatPromise, OutputStream, readFilePromise } from '../common/io';
import { lstat, OutputStream, readFile } from '../common/io';

export default class Compile extends Command {
static description = 'Compiles the given profile or map to AST.';
Expand Down Expand Up @@ -53,16 +53,13 @@ export default class Compile extends Command {
if (outputPath !== undefined) {
let isDirectory = false;
try {
const lstat = await lstatPromise(outputPath);
isDirectory = lstat.isDirectory();
} catch (e) {
const lstatInfo = await lstat(outputPath);
isDirectory = lstatInfo.isDirectory();
} catch (err: unknown) {
// eat ENOENT error and keep isDirectory false
if (e !== null && typeof e === 'object' && 'code' in e) {
// All the checks done and eslint still complains
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (e.code !== 'ENOENT') {
throw e;
}
assertIsIOError(err);
if (err.code !== 'ENOENT') {
throw err;
}
}

Expand Down Expand Up @@ -126,11 +123,11 @@ export default class Compile extends Command {
): Promise<unknown> {
const documentType = inferDocumentTypeWithFlag(documentTypeFlag, path);
if (documentType === DocumentType.UNKNOWN) {
throw new CLIError('Could not infer document type', { exit: 1 });
throw userError('Could not infer document type', 1);
}

const parseFunction = DOCUMENT_PARSE_FUNCTION[documentType];
const content = (await readFilePromise(path)).toString();
const content = (await readFile(path)).toString();
const source = new Source(content, nodePath.basename(path));

return parseFunction(source);
Expand Down
Loading

0 comments on commit d2b47de

Please sign in to comment.