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 #24 from superfaceai/feature/codegen
Browse files Browse the repository at this point in the history
feat: Add Generate command
  • Loading branch information
lukas-valenta authored Apr 23, 2021
2 parents 35818ad + e0ca952 commit 7f19db9
Show file tree
Hide file tree
Showing 45 changed files with 846 additions and 84 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ jobs:
steps:
# Setup environment and checkout the project master
- name: Setup Node.js environment
uses: actions/setup-node@v1.4.4
uses: actions/setup-node@v2
with:
registry-url: "https://npm.pkg.github.com"
scope: "@superfaceai"
always-auth: true
node-version: '14'

- name: Checkout
uses: actions/[email protected]
Expand Down Expand Up @@ -42,11 +43,12 @@ jobs:
steps:
# Setup environment and checkout the project master
- name: Setup Node.js environment
uses: actions/setup-node@v1.4.4
uses: actions/setup-node@v2
with:
registry-url: "https://npm.pkg.github.com"
scope: "@superfaceai"
always-auth: true
node-version: '14'

- name: Checkout
uses: actions/[email protected]
Expand Down Expand Up @@ -76,11 +78,12 @@ jobs:
steps:
# Setup environment and checkout the project master
- name: Setup Node.js environment
uses: actions/setup-node@v1.4.4
uses: actions/setup-node@v2
with:
registry-url: "https://npm.pkg.github.com"
scope: "@superfaceai"
always-auth: true
node-version: '14'

- name: Checkout
uses: actions/[email protected]
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ jobs:
- name: Checkout
uses: actions/[email protected]
- name: Setup Node.js environment
uses: actions/setup-node@v1.4.4
uses: actions/setup-node@v2
with:
registry-url: "https://npm.pkg.github.com"
# Defaults to the user or organization that owns the workflow file
scope: "@superfaceai"
node-version: '14'


# Install dependencies and run test
- name: Install dependencies
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## [Unreleased]
### Added
* Generate command

### Added
* Support for installing local files with `install` command
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ OPTIONS
-s, --scan=scan When number provided, scan for super.json outside cwd within range represented by this
number.
-t, --types When set to true, generates TypeScript typings for profiles
EXAMPLES
$ superface install
Expand Down
4 changes: 2 additions & 2 deletions fixtures/consumer/starwars/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { inspect } = require('util');
const { Provider } = require('@superfaceai/sdk');
const { Provider } = require('@superfaceai/one-sdk');

async function execute(characterName) {
// 1. Create the provider object - the build artifacts are located by the sdk according to super.json
Expand All @@ -15,4 +15,4 @@ async function execute(characterName) {
}));
}

execute(process.argv[2]);
execute(process.argv[2]);
2 changes: 1 addition & 1 deletion fixtures/consumer/starwars/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "playground",
"private": true,
"dependencies": {
"@superfaceai/sdk": "0.0.9"
"@superfaceai/one-sdk": "0.0.17"
},
"devDependencies": {
"@types/node": "^14",
Expand Down
6 changes: 6 additions & 0 deletions fixtures/install/playground/superface/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createTypedClient } from "@superfaceai/one-sdk";
import { starwarsCharacterInformation } from "./types/starwars/character-information";
export const typeDefinitions = {
...starwarsCharacterInformation
};
export const SuperfaceClient = createTypedClient(typeDefinitions);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { typeHelper } from '@superfaceai/one-sdk';
/** Starwars **/
export interface RetrieveCharacterInformationInput {
characterName?: unknown;
}
/** Starwars **/
export interface RetrieveCharacterInformationResult {
height?: unknown;
weight?: unknown;
yearOfBirth?: unknown;
}
export const starwarsCharacterInformation = {
"starwars/character-information": {
"RetrieveCharacterInformation": typeHelper<RetrieveCharacterInformationInput, RetrieveCharacterInformationResult>()
}
};
2 changes: 1 addition & 1 deletion fixtures/playgrounds/pub-hours/superface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "playground",
"private": true,
"dependencies": {
"@superfaceai/sdk": "0.0.11"
"@superfaceai/one-sdk": "0.0.17"
},
"devDependencies": {
"@types/node": "^14",
Expand Down
12 changes: 4 additions & 8 deletions fixtures/playgrounds/pub-hours/superface/play/pub-hours.play.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inspect } from 'util';

import { SuperfaceClient } from '@superfaceai/sdk';
import { SuperfaceClient } from '@superfaceai/one-sdk';

/** Execute one specific pair of profile and map. */
async function execute(
Expand All @@ -25,8 +25,8 @@ async function execute(
// 3. Get usecase from the profile and execute it with the given provider
const result = await profile.getUseCase('PubOpeningHours').perform(
{
city: "Praha",
nameRegex: "Diego"
city: 'Praha',
nameRegex: 'Diego',
},
{ provider }
);
Expand Down Expand Up @@ -72,11 +72,7 @@ async function main() {
variant = nameSplit[2];
}

execute(
scope,
name,
provider
);
execute(scope, name, provider);
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
"@oclif/config": "^1.17.0",
"@superfaceai/ast": "^0.0.22",
"@superfaceai/parser": "0.0.15",
"@superfaceai/sdk": "0.0.14",
"@superfaceai/one-sdk": "0.0.17",
"chalk": "^4.1.0",
"debug": "^4.3.1",
"inquirer": "^7.3.3",
"inquirer-file-tree-selection-prompt": "^1.0.7",
"rimraf": "^3.0.2",
"typescript": "^4.0.5",
"superagent": "^6.1.0"
},
"oclif": {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/configure.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
HttpScheme,
SecurityType,
SuperJson,
} from '@superfaceai/sdk';
} from '@superfaceai/one-sdk';
import { join as joinPath } from 'path';
import { mocked } from 'ts-jest/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/commands/configure.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import { mocked } from 'ts-jest/utils';

import { validateDocumentName } from '../common/document';
Expand Down
2 changes: 1 addition & 1 deletion src/commands/create.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import * as fs from 'fs';
import { join as joinPath } from 'path';

Expand Down
2 changes: 1 addition & 1 deletion src/commands/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CLIError } from '@oclif/errors';
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import inquirer from 'inquirer';
import { mocked } from 'ts-jest/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/commands/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import inquirer from 'inquirer';
import { mocked } from 'ts-jest/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class Init extends Command {
//Warn user
this.warn(
yellow(
'You are using a hidden command. This command is not intended for public consumption yet. It might be broken, hard to use or simply redundant. Thread with care.'
'You are using a hidden command. This command is not intended for public consumption yet. It might be broken, hard to use or simply redundant. Tread with care.'
)
);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/install.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import { join as joinPath } from 'path';

import { EXTENSIONS, GRID_DIR, SUPER_PATH } from '../common/document';
Expand Down
7 changes: 6 additions & 1 deletion src/commands/install.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CLIError } from '@oclif/errors';
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import { mocked } from 'ts-jest/utils';

import { installProvider } from '../logic/configure';
Expand Down Expand Up @@ -59,6 +59,7 @@ describe('Install CLI command', () => {
logCb: expect.anything(),
warnCb: expect.anything(),
force: false,
typings: true,
}
);
}, 10000);
Expand All @@ -76,6 +77,7 @@ describe('Install CLI command', () => {
logCb: expect.anything(),
warnCb: expect.anything(),
force: false,
typings: true,
}
);
}, 10000);
Expand All @@ -93,6 +95,7 @@ describe('Install CLI command', () => {
logCb: undefined,
warnCb: undefined,
force: false,
typings: true,
}
);
}, 10000);
Expand All @@ -106,6 +109,7 @@ describe('Install CLI command', () => {
logCb: expect.any(Function),
warnCb: expect.any(Function),
force: false,
typings: true,
});
}, 10000);

Expand Down Expand Up @@ -167,6 +171,7 @@ describe('Install CLI command', () => {
logCb: expect.anything(),
warnCb: expect.anything(),
force: false,
typings: true,
}
);
}, 10000);
Expand Down
9 changes: 9 additions & 0 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ export default class Install extends Command {
'When number provided, scan for super.json outside cwd within range represented by this number.',
required: false,
}),
help: flags.help({ char: 'h' }),
types: flags.boolean({
char: 't',
description:
'When set to true, generates TypeScript typings for profiles',
default: true,
required: false,
}),
};

static examples = [
Expand Down Expand Up @@ -161,6 +169,7 @@ export default class Install extends Command {
logCb: this.logCallback,
warnCb: this.warnCallback,
force: flags.force,
typings: flags.types,
});

this.logCallback?.(`\n\nConfiguring providers`);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ clean: the \`superface/node_modules\` folder and \`superface/build\` build artif
//Warn user
this.warn(
yellow(
'You are using a hidden command. This command is not intended for public consumption yet. It might be broken, hard to use or simply redundant. Thread with care.'
'You are using a hidden command. This command is not intended for public consumption yet. It might be broken, hard to use or simply redundant. Tread with care.'
)
);

Expand Down
10 changes: 5 additions & 5 deletions src/common/document.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ProfileDocumentNode } from '@superfaceai/ast';
import {
ProfileEntry,
ProfileProviderEntry,
ProviderSettings,
} from '@superfaceai/one-sdk';
import {
DocumentVersion,
parseMap,
parseProfile,
parseProfileId,
Source,
} from '@superfaceai/parser';
import {
ProfileEntry,
ProfileProviderEntry,
ProviderSettings,
} from '@superfaceai/sdk';
import { basename, join as joinPath } from 'path';

import { CreateMode, DocumentType } from './document.interfaces';
Expand Down
5 changes: 5 additions & 0 deletions src/common/error-codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const enum ErrorCodes {
INVALID_PROFILE_NAME = 1001,
INVALID_VALUE_UNTYPED_TYPE = 1002,
INVALID_PROFILE = 1003,
}
2 changes: 1 addition & 1 deletion src/common/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProfileDocumentNode } from '@superfaceai/ast';
import { parseProviderJson, ProviderJson } from '@superfaceai/sdk';
import { parseProviderJson, ProviderJson } from '@superfaceai/one-sdk';
import superagent, { Response } from 'superagent';

import { userError } from './error';
Expand Down
2 changes: 1 addition & 1 deletion src/common/io.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SuperJson } from '@superfaceai/sdk';
import { SuperJson } from '@superfaceai/one-sdk';
import { join } from 'path';
import { Writable } from 'stream';

Expand Down
24 changes: 23 additions & 1 deletion src/common/io.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as childProcess from 'child_process';
import * as fs from 'fs';
import { basename } from 'path';
import rimrafCallback from 'rimraf';
import { Writable } from 'stream';
import { promisify } from 'util';
Expand All @@ -13,7 +14,6 @@ export const stat = promisify(fs.stat);
export const readdir = promisify(fs.readdir);
export const mkdir = promisify(fs.mkdir);
export const realpath = promisify(fs.realpath);

export const rimraf = promisify(rimrafCallback);

export interface WritingOptions {
Expand Down Expand Up @@ -41,6 +41,20 @@ export async function exists(path: string): Promise<boolean> {
return true;
}

/**
* Reads a file and converts to string.
* Returns `undefined` if reading fails for any reason.
*/
export async function readFileQuiet(path: string): Promise<string | undefined> {
try {
const file = await readFile(path, { encoding: 'utf8' });

return file.toString();
} catch (_) {
return undefined;
}
}

/**
* Creates a directory without erroring if it already exists.
* Returns `true` if the directory was created.
Expand Down Expand Up @@ -206,3 +220,11 @@ export async function isAccessible(path: string): Promise<boolean> {

return true;
}

/**
* Returns file name with path and all extensions stripped
*/
export function basenameWithoutExt(path: string): string {
// NOTE: Naive implementation, but should work for any case
return basename(path).split('.')[0];
}
1 change: 1 addition & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type LogCallback = (message: string) => void;
Loading

0 comments on commit 7f19db9

Please sign in to comment.