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

fix: add missing bare workflow notes to schema #20

Merged
merged 2 commits into from
Aug 30, 2020
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
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ module.exports = {
singleQuote: true,
jsxBracketSameLine: true,
trailingComma: 'es5',
endOfLine: 'auto',
};
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
37 changes: 29 additions & 8 deletions src/manifest/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ 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);
}

/**
* Create a vscode compatible JSON schema object from XDL schema.
* It also adds a `meta` property to know when this schema was generated.
* This will try to make some adjustments to the downloaded schema:
* 1. wrap the properties into an `expo` property for managed validation
* 2. copy `description` properties into `markdownDescription` to render content as markdown
* 2. append `schema.meta.bareWorkflow` notes to the `schema.description`
* 2. copy `schema.description` properties into `schema.markdownDescription` to render as markdown
*
* @remark When something fails in #2, it falls back to the original schema and only apply #1.
* @remark When something fails in 2..3, it falls back to the original schema and only apply 1.
* @see https://github.com/microsoft/vscode/blob/1dff50d211472de4667db626ef2d6d32265eb0e6/src/vs/workbench/services/configuration/common/configurationExtensionPoint.ts#L64
*/
export function createFromXdl(sdkVersion: string, xdlSchema: JsonSchema): ManifestSchema {
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