Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A code gen for typescript #63

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-bananas-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@featureboard/code-generator': minor
---

Add typescript code generator template
4 changes: 2 additions & 2 deletions apps/cli/src/commands/code-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import { promptForOrganization } from '../lib/prompt-for-organization'
import { titleText } from '../lib/title-text'

// Code Gen
const templateChoices: Template[] = ['dotnet-api']
const templateChoices: Template[] = ['dotnet-api', 'typescript']

export function codeGenCommand() {
return new Command('code-gen')
.description(`A Code generator for FeatureBoard`)
.option('-o, --output <path>', 'Output path')
.option('-g, --organizationId <id>', 'The Orgnization Id')
.option('-g, --organizationId <id>', 'The Organization Id')
.addOption(
new Option(
'-t, --template <template>',
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function loginCommand() {
!!process.env['CI'],
)
.action(
actionRunner(async function codeGen(options) {
actionRunner(async function login(options) {
if (!options.nonInteractive) {
console.log(titleText)
}
Expand All @@ -46,7 +46,7 @@ export function loginCommand() {
})
})

server.listen(REDIRECT_PORT, async () => {
server.listen(REDIRECT_PORT, 'localhost', async () => {
if (options.nonInteractive) {
console.log(
`Please authenticate at: http://localhost:${REDIRECT_PORT}`,
Expand Down
2 changes: 1 addition & 1 deletion libs/code-generator/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"passWithNoTests": true,
"passWithNoTests": false,
"reportsDirectory": "../../coverage/libs/code-generator"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,52 @@ public class FeatureFilterAttribute : FeatureFilterAttributeBase
"
`;

exports[`code-generator > template: dotnet-api > should produce the expected features files > apps/my-app/Project.FeatureBoard.MyApp.csproj 1`] = `""`;
exports[`code-generator > template: typescript > should produce the expected features files > apps/my-app/features.ts 1`] = `
"import '@featureboard/js-sdk'

export interface SaaSyIconsFeatures {

/**
* Requires attribution
* @description
*/
'requires-attribution': boolean

/**
* Allow edits
* @description Allows the user to edit colour, backgrounds
*/
'allow-edits': boolean

/**
* Number custom collections
* @description
*/
'number-custom-collections': number

/**
* Team management
* @description
*/
'team-management': boolean

/**
* Custom collections
* @description
*/
'custom-collections': 'Disabled' | 'Enabled' | 'Shared'

/**
* View subscribers
* @description
*/
'view-subscribers': boolean
}

declare module '@featureboard/js-sdk' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Features extends SaaSyIconsFeatures {}
}

export type SaaSyIconsFeature = keyof SaaSyIconsFeatures"
`;
150 changes: 44 additions & 106 deletions libs/code-generator/src/lib/__snapshots__/code-generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -108,114 +108,52 @@ public class FeatureFilterAttribute : FeatureFilterAttributeBase
"
`;

exports[`code-generator > template: dotnet-api > should produce the expected features files > apps/my-app/Project.FeatureBoard.MyApp.csproj 1`] = `""`;

exports[`code-generator template: dotnet-api should produce the expected features files: apps/my-app/Features.cs 1`] = `
"using FeatureBoard.DotnetSdk.Attributes;
using FeatureBoard.DotnetSdk.Models;
using System.Runtime.Serialization;

namespace Project.FeatureBoard.MyApp;

// Features
public class Features : IFeatures
{
[FeatureKeyName("requires-attribution")]
public bool RequiresAttribution { get; set; }
[FeatureKeyName("allow-edits")]
public bool AllowEdits { get; set; }
[FeatureKeyName("number-custom-collections")]
public decimal NumberCustomCollections { get; set; }
[FeatureKeyName("team-management")]
public bool TeamManagement { get; set; }
[FeatureKeyName("custom-collections")]
public CustomCollections CustomCollections { get; set; }
[FeatureKeyName("view-subscribers")]
public bool ViewSubscribers { get; set; }
}


// Options Enums

//Custom collections
public enum CustomCollections
{
[EnumMember(Value = "Disabled")]
Disabled,
[EnumMember(Value = "Enabled")]
Enabled,
[EnumMember(Value = "Shared")]
Shared,
exports[`code-generator > template: typescript > should produce the expected features files > apps/my-app/features.ts 1`] = `
"import '@featureboard/js-sdk'

export interface SaaSyIconsFeatures {

/**
* Requires attribution
* @description
*/
'requires-attribution': boolean

/**
* Allow edits
* @description Allows the user to edit colour, backgrounds
*/
'allow-edits': boolean

/**
* Number custom collections
* @description
*/
'number-custom-collections': number

/**
* Team management
* @description
*/
'team-management': boolean

/**
* Custom collections
* @description
*/
'custom-collections': 'Disabled' | 'Enabled' | 'Shared'

/**
* View subscribers
* @description
*/
'view-subscribers': boolean
}

// Type Enums
public enum BooleanFeature
{
RequiresAttribution,
AllowEdits,
TeamManagement,
ViewSubscribers,
}
public enum NumberFeature
{
NumberCustomCollections,
declare module '@featureboard/js-sdk' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Features extends SaaSyIconsFeatures {}
}
public enum StringFeature
{
}

// Feature Gate attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class FeatureFilterAttribute : FeatureFilterAttributeBase
{
protected override string Feature { get; }
protected override object DefaultValue { get; }
protected override object? IsEqualTo { get; }
private readonly Dictionary<string, string> _featureKeyLookup = new()
{
{ "RequiresAttribution", "requires-attribution" },
{ "AllowEdits", "allow-edits" },
{ "NumberCustomCollections", "number-custom-collections" },
{ "TeamManagement", "team-management" },
{ "ViewSubscribers", "view-subscribers" },
};

public FeatureFilterAttribute(BooleanFeature feature, bool defaultValue)
{
Feature = _featureKeyLookup.GetValueOrDefault(feature.ToString()) ?? feature.ToString();
DefaultValue = defaultValue;
}

public FeatureFilterAttribute(BooleanFeature feature, bool isEqualTo, bool defaultValue)
{
Feature = _featureKeyLookup.GetValueOrDefault(feature.ToString()) ?? feature.ToString();
IsEqualTo = isEqualTo;
DefaultValue = defaultValue;
}

public FeatureFilterAttribute(StringFeature feature, string isEqualTo, string defaultValue)
{
Feature = _featureKeyLookup.GetValueOrDefault(feature.ToString()) ?? feature.ToString();
IsEqualTo = isEqualTo;
DefaultValue = defaultValue;
}

public FeatureFilterAttribute(NumberFeature feature, decimal isEqualTo, decimal defaultValue)
{
Feature = _featureKeyLookup.GetValueOrDefault(feature.ToString()) ?? feature.ToString();
IsEqualTo = isEqualTo;
DefaultValue = defaultValue;
}

public FeatureFilterAttribute(CustomCollections isEqualTo, CustomCollections defaultValue)
{
Feature = "custom-collections";
IsEqualTo = isEqualTo;
DefaultValue = defaultValue;
}

}
"
export type SaaSyIconsFeature = keyof SaaSyIconsFeatures"
`;

exports[`code-generator template: dotnet-api should produce the expected features files: apps/my-app/Project.FeatureBoard.MyApp.csproj 1`] = `""`;
4 changes: 4 additions & 0 deletions libs/code-generator/src/lib/api/get-project-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export async function getProjectFeatures(
}>(apiEndpoint, `projects/${project.id}/features`, auth)
}

export interface FeatureBoardProject {
id: string
name: string
}
export interface FeatureBoardFeature {
name: string
created: string
Expand Down
Loading