Skip to content

Commit

Permalink
fix: add missing bare workflow notes to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Aug 30, 2020
1 parent 539fe5f commit 88b7240
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/manifest/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('activateGlobalSchema', () => {
const storagePath = vscode.Uri.file('path/to/file');

jest.spyOn(xdlVersions, 'newestReleasedSdkVersionAsync').mockResolvedValue(versionData);
const schemaSpy = jest.spyOn(schema, 'getSchema').mockResolvedValue(schemaData);
const schemaSpy = jest.spyOn(schema, 'create').mockResolvedValue(schemaData);
const storageSpy = jest.spyOn(storage, 'storeSchema').mockResolvedValue(storagePath);
const configSpy = jest.spyOn(config, 'registerGlobalSchema').mockResolvedValue();

Expand Down
49 changes: 47 additions & 2 deletions src/manifest/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import * as schema from '../schema';
// note: this is the same value as `traverse`, but typed as mock
const mockedTraverse = (traverse as unknown) as jest.Mock<[object, TraverseCallback]>;

describe('getSchema', () => {
describe('create', () => {
it('fetches schema by expo sdk version', async () => {
const xdlSchema = tools.getFixtureFile('schema-xdl-39.0.0.json');
const spy = jest.spyOn(xdl, 'getSchemaAsync').mockResolvedValue(xdlSchema);

expect(await schema.getSchema('39.0.0')).toBeDefined();
expect(await schema.create('39.0.0')).toBeDefined();
expect(spy).toBeCalledWith('39.0.0');
});
});
Expand All @@ -35,3 +35,48 @@ describe('createFromXdl', () => {
expect(createdSchema).toStrictEqual(expect.objectContaining(simpleSchema));
});
});

describe('mutateSchemaBareWorkflowDescription', () => {
const description = 'Info about a property';
const bareWorkflow = 'Check this other property out instead';

it('skips without `schema.meta.bareWorkflow`', () => {
const property = { description };
schema.mutateSchemaBareWorkflowDescription(property);
expect(property.description).toBe(description);
});

it('appends `schema.meta.bareWorkflow` to missing `schema.description`', () => {
const property = { meta: { bareWorkflow } };
schema.mutateSchemaBareWorkflowDescription(property);
expect(property).toHaveProperty('description', `**Bare workflow** - ${bareWorkflow}`);
});

it('appends `schema.meta.bareWorkflow` to existing `schema.description`', () => {
const property = { description, meta: { bareWorkflow } };
schema.mutateSchemaBareWorkflowDescription(property);
expect(property.description).toBe(`${description}\n\n**Bare workflow** - ${bareWorkflow}`);
});
});

describe('mutateSchemaMarkdownDescription', () => {
const description = 'Info about a property';

it('skips without `schema.description`', () => {
const property = {};
schema.mutateSchemaMarkdownDescription(property);
expect(property).not.toHaveProperty('markdownDescription');
});

it('copies `schema.description` to `schema.markdownDescription`', () => {
const property = { description };
schema.mutateSchemaMarkdownDescription(property);
expect(property).toHaveProperty('markdownDescription', description);
});

it('skips copy `schema.description` to `schema.markdownDescription` if it exists already', () => {
const property = { description, markdownDescription: 'Something else' };
schema.mutateSchemaMarkdownDescription(property);
expect(property).toHaveProperty('markdownDescription', 'Something else');
});
});
2 changes: 1 addition & 1 deletion src/manifest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as storage from './storage';
*/
export async function activateGlobalSchema(context: vscode.ExtensionContext) {
const latestSdk = await xdlVersions.newestReleasedSdkVersionAsync();
const schemaFile = await schema.getSchema(latestSdk.version);
const schemaFile = await schema.create(latestSdk.version);
const storagePath = await storage.storeSchema(context, schemaFile);
await config.registerGlobalSchema(storagePath);
}
Expand Down
31 changes: 26 additions & 5 deletions src/manifest/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export type ManifestSchema = {
};

/**
* Fetch the XDL schema for a specific Expo SDK version.
* Create a vscode compatible JSON schema by fetching the versioned XDL schema.
*/
export async function getSchema(sdkVersion: string) {
export async function create(sdkVersion: string) {
const xdlSchema = await xdl.getSchemaAsync(sdkVersion);
return createFromXdl(sdkVersion, xdlSchema);
}
Expand All @@ -40,9 +40,8 @@ export function createFromXdl(sdkVersion: string, xdlSchema: JsonSchema): Manife

try {
jsonSchemaTraverse(enhancedSchema, (nestedSchema: JsonSchema) => {
if (nestedSchema.description && !nestedSchema.markdownDescription) {
nestedSchema.markdownDescription = nestedSchema.description;
}
mutateSchemaBareWorkflowDescription(nestedSchema);
mutateSchemaMarkdownDescription(nestedSchema);
});
} catch (error) {
// todo: add telemetry, fallback to original schema
Expand All @@ -60,3 +59,25 @@ export function createFromXdl(sdkVersion: string, xdlSchema: JsonSchema): Manife
},
};
}

/**
* Appends the `meta.bareWorkflow` to the `descrption` with a `**Bare Workflow**` prefix.
* If there is no `meta.bareWorkflow` value, it's skipped.
*/
export function mutateSchemaBareWorkflowDescription(schema: JsonSchema) {
if (schema.meta?.bareWorkflow) {
const description = schema.description || '';
const bareNotes = schema.meta.bareWorkflow;
schema.description = `${description}\n\n**Bare workflow** - ${bareNotes}`.trim();
}
}

/**
* Copies the `description` property to `markdownDescription` for vscode markdown rendering.
* If there is no `description` value, it skips the `markdownDescription`.
*/
export function mutateSchemaMarkdownDescription(schema: JsonSchema) {
if (schema.description && !schema.markdownDescription) {
schema.markdownDescription = schema.description;
}
}
Loading

0 comments on commit 88b7240

Please sign in to comment.