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

Normalize styling: Part 2: Run formatter #301

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
18 changes: 9 additions & 9 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ListValueFlag,
NoParams,
OutputInterface,
ValueFlag
ValueFlag,
} from "@2fd/command";
import Bluebird from "bluebird";
import fs from "fs";
Expand All @@ -17,15 +17,15 @@ import {
httpSchemaLoader,
idlSchemaLoader,
jsonSchemaLoader,
jsSchemaLoader
jsSchemaLoader,
} from "./schema-loader";
import { createData, getFilenameOf, Output, Plugin } from "./utility";
import {
createBuildDirectory,
readFile,
removeBuildDirectory,
resolve,
writeFile
writeFile,
} from "./utility/fs";

// tslint:disable-next-line:no-var-requires
Expand Down Expand Up @@ -116,7 +116,7 @@ export class GraphQLDocumentGenerator extends Command<IFlags, {}> {
"Delete outputDirectory if exists."
),
new BooleanFlag("verbose", ["-v", "--verbose"], "Output more information."),
new BooleanFlag("version", ["-V", "--version"], "Show graphdoc version.")
new BooleanFlag("version", ["-V", "--version"], "Show graphdoc version."),
];

public async action(input: Input, out: OutputInterface) {
Expand All @@ -143,13 +143,13 @@ export class GraphQLDocumentGenerator extends Command<IFlags, {}> {
graphdocPackageJSON
);

projectPackageJSON.graphdoc.plugins.forEach(plugin =>
projectPackageJSON.graphdoc.plugins.forEach((plugin) =>
output.info("use plugin", plugin)
);

// Collect assets
const assets: string[] = await Plugin.collectAssets(plugins);
assets.forEach(asset =>
assets.forEach((asset) =>
output.info("use asset", path.relative(process.cwd(), asset))
);

Expand Down Expand Up @@ -261,7 +261,7 @@ export class GraphQLDocumentGenerator extends Command<IFlags, {}> {
projectPackageJSON: object,
pluginGraphdocPackageJSON: object
): PluginInterface[] {
return paths.map(p => {
return paths.map((p) => {
const absolutePaths = resolve(p);
const plugin = require(absolutePaths).default;

Expand All @@ -284,10 +284,10 @@ export class GraphQLDocumentGenerator extends Command<IFlags, {}> {
)("**/*.mustache", { cwd: templateDir });

await Promise.all(
files.map(file => {
files.map((file) => {
const name = path.basename(file, ".mustache");
return readFile(path.resolve(templateDir, file), "utf8").then(
content => (partials[name] = content)
(content) => (partials[name] = content)
);
})
);
Expand Down
4 changes: 2 additions & 2 deletions lib/schema-loader/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GraphQLIntrospection,
Introspection,
Schema,
SchemaLoader
SchemaLoader,
} from "../interface";
import { query as introspectionQuery } from "../utility";

Expand Down Expand Up @@ -62,7 +62,7 @@ export const httpSchemaLoader: SchemaLoader = async (
url: options.endpoint,
method: "POST",
body: { query: introspectionQuery },
json: true
json: true,
};

requestOptions.headers = options.headers.reduce(
Expand Down
2 changes: 1 addition & 1 deletion lib/schema-loader/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ApolloIntrospection,
GraphQLIntrospection,
Introspection,
SchemaLoader
SchemaLoader,
} from "../interface";
import { query as introspectionQuery } from "../utility";
import { readFile } from "../utility/fs";
Expand Down
4 changes: 2 additions & 2 deletions lib/schema-loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export { IHttpSchemaLoaderOptions, httpSchemaLoader } from "./http";
export { IIdlSchemaLoaderOptions, idlSchemaLoader } from "./idl";
export {
IJsSchemaLoaderOptions as TJsSchemaLoaderOptions,
jsSchemaLoader
jsSchemaLoader,
} from "./js";
export {
IJsonSchemaLoaderOptions as TJsonSchemaLoaderOptions,
jsonSchemaLoader
jsonSchemaLoader,
} from "./json";
2 changes: 1 addition & 1 deletion lib/schema-loader/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ApolloIntrospection,
GraphQLIntrospection,
Introspection,
SchemaLoader
SchemaLoader,
} from "../interface";
import { query as introspectionQuery } from "../utility";

Expand Down
2 changes: 1 addition & 1 deletion lib/schema-loader/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GraphQLIntrospection,
Introspection,
Schema,
SchemaLoader
SchemaLoader,
} from "../interface";

export interface IJsonSchemaLoaderOptions {
Expand Down
6 changes: 3 additions & 3 deletions lib/utility/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export async function createBuildDirectory(
files

// ignore *.mustache templates
.filter(file => path.extname(file) !== ".mustache")
.filter((file) => path.extname(file) !== ".mustache")

// copy recursive
.map(file =>
.map((file) =>
copyAll(
path.resolve(templateDirectory, file),
path.resolve(buildDirectory, file)
Expand All @@ -71,7 +71,7 @@ export async function createBuildDirectory(
await mkDir(path.resolve(buildDirectory, "assets"));

await Bluebird.all(
assets.map(asset =>
assets.map((asset) =>
copyAll(
asset,
path.resolve(buildDirectory, "assets", path.basename(asset))
Expand Down
207 changes: 117 additions & 90 deletions lib/utility/html.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Description, Field, InputValue, SchemaType } from '../interface'
import { split, HTML } from './html'
import { data } from '../../test/empty.schema.json'

test('utility/html.split', () => {
const LOREM_IPSU = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.`
expect(split('', 0)).toEqual([''])
expect(split(LOREM_IPSU, 1)).toEqual(LOREM_IPSU.split(' '))
import { Description, Field, InputValue, SchemaType } from "../interface";
import { split, HTML } from "./html";
import { data } from "../../test/empty.schema.json";

test("utility/html.split", () => {
const LOREM_IPSU = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.`;
expect(split("", 0)).toEqual([""]);
expect(split(LOREM_IPSU, 1)).toEqual(LOREM_IPSU.split(" "));
expect(split(LOREM_IPSU, 10)).toEqual([
"Lorem Ipsum",
"is simply dummy",
Expand All @@ -25,89 +25,116 @@ test('utility/html.split', () => {
"it to make",
"a type specimen",
"book.",
])
]);
expect(split(LOREM_IPSU, 100)).toEqual([
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's",
"standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled",
"it to make a type specimen book.",
])
})


describe('lib/utility/html#HTML', () => {
test('.code', () => {
const html = new HTML
expect(html.code('CODE')).toBe('<code class="highlight"><table class="code"><tbody>CODE</tbody></table></code>')
})

test('.highlight', () => {
const html = new HTML
expect(html.highlight('CODE')).toBe('<strong>CODE</strong>')
})

test('.sup', () => {
const html = new HTML
expect(html.sup('CODE')).toBe(' <sup>CODE</sup>')
})

test('.line', () => {
const html = new HTML
expect(html.index).toBe(1)
expect(html.line('CODE')).toBe('<tr class="row"><td id="L1" class="td-index">1</td><td id="LC1" class="td-code">CODE</td></tr>')
expect(html.index).toBe(2)
})

test('.tab', () => {
const html = new HTML
expect(html.tab('CODE')).toBe('<span class="tab">CODE</span>')
})

test('.keyword', () => {
const html = new HTML
expect(html.keyword('CODE')).toBe('<span class="keyword operator ts">CODE</span>')
})

test('.comment', () => {
const html = new HTML
expect(html.comment('CODE')).toBe('<span class="comment line"># CODE</span>')
})

test('.identifier', () => {
const html = new HTML
const type = data.__schema.types.find(t => t.name === 'Query')
expect(html.identifier(type as Description)).toBe('<span class="identifier">Query</span>')
})

test('.parameter', () => {
const html = new HTML
const input: InputValue = data.__schema.types.find(t => t.name === 'AddCommentInput') as any
expect(html.parameter(input)).toBe('<span class="variable parameter">AddCommentInput</span>')
})

test('.property', () => {
const html = new HTML
expect(html.property('PROPERTY')).toBe('<span class="meta">PROPERTY</span>')
})

test('.useIdentifier', () => {
const html = new HTML
const schema: SchemaType = data.__schema.types.find(t => t.name === '__Schema') as any
const field: Field = (schema.fields || []).find(f => f.name === 'types') as any

expect(html.useIdentifier(field.type, 'HREF')).toBe('[<a class="support type" href="HREF">__Type</a>!]!')
})

test('.useIdentifierLength', () => {
const html = new HTML
const schema: SchemaType = data.__schema.types.find(t => t.name === '__Schema') as any
const field: Field = (schema.fields || []).find(f => f.name === 'types') as any

expect(html.useIdentifierLength(field.type)).toBe(10)
})

test('.value', () => {
const html = new HTML
expect(html.value('"STRING"')).toBe('<span class="string">"STRING"</span>')
expect(html.value('NUMBER')).toBe('<span class="constant numeric">NUMBER</span>')
})
})
]);
});

describe("lib/utility/html#HTML", () => {
test(".code", () => {
const html = new HTML();
expect(html.code("CODE")).toBe(
'<code class="highlight"><table class="code"><tbody>CODE</tbody></table></code>'
);
});

test(".highlight", () => {
const html = new HTML();
expect(html.highlight("CODE")).toBe("<strong>CODE</strong>");
});

test(".sup", () => {
const html = new HTML();
expect(html.sup("CODE")).toBe(" <sup>CODE</sup>");
});

test(".line", () => {
const html = new HTML();
expect(html.index).toBe(1);
expect(html.line("CODE")).toBe(
'<tr class="row"><td id="L1" class="td-index">1</td><td id="LC1" class="td-code">CODE</td></tr>'
);
expect(html.index).toBe(2);
});

test(".tab", () => {
const html = new HTML();
expect(html.tab("CODE")).toBe('<span class="tab">CODE</span>');
});

test(".keyword", () => {
const html = new HTML();
expect(html.keyword("CODE")).toBe(
'<span class="keyword operator ts">CODE</span>'
);
});

test(".comment", () => {
const html = new HTML();
expect(html.comment("CODE")).toBe(
'<span class="comment line"># CODE</span>'
);
});

test(".identifier", () => {
const html = new HTML();
const type = data.__schema.types.find((t) => t.name === "Query");
expect(html.identifier(type as Description)).toBe(
'<span class="identifier">Query</span>'
);
});

test(".parameter", () => {
const html = new HTML();
const input: InputValue = data.__schema.types.find(
(t) => t.name === "AddCommentInput"
) as any;
expect(html.parameter(input)).toBe(
'<span class="variable parameter">AddCommentInput</span>'
);
});

test(".property", () => {
const html = new HTML();
expect(html.property("PROPERTY")).toBe(
'<span class="meta">PROPERTY</span>'
);
});

test(".useIdentifier", () => {
const html = new HTML();
const schema: SchemaType = data.__schema.types.find(
(t) => t.name === "__Schema"
) as any;
const field: Field = (schema.fields || []).find(
(f) => f.name === "types"
) as any;

expect(html.useIdentifier(field.type, "HREF")).toBe(
'[<a class="support type" href="HREF">__Type</a>!]!'
);
});

test(".useIdentifierLength", () => {
const html = new HTML();
const schema: SchemaType = data.__schema.types.find(
(t) => t.name === "__Schema"
) as any;
const field: Field = (schema.fields || []).find(
(f) => f.name === "types"
) as any;

expect(html.useIdentifierLength(field.type)).toBe(10);
});

test(".value", () => {
const html = new HTML();
expect(html.value('"STRING"')).toBe('<span class="string">"STRING"</span>');
expect(html.value("NUMBER")).toBe(
'<span class="constant numeric">NUMBER</span>'
);
});
});
5 changes: 3 additions & 2 deletions lib/utility/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export class HTML {

line(code?: string): string {
const row = this.index++;
return `<tr class="row"><td id="L${row}" class="td-index">${row}</td><td id="LC${row}" class="td-code">${code ||
""}</td></tr>`;
return `<tr class="row"><td id="L${row}" class="td-index">${row}</td><td id="LC${row}" class="td-code">${
code || ""
}</td></tr>`;
}

tab(code: string): string {
Expand Down