Skip to content

Commit

Permalink
feat(banner-required): ensuring build works
Browse files Browse the repository at this point in the history
  • Loading branch information
inkblotty authored and skovy committed Oct 8, 2020
1 parent 5249c70 commit b07660f
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ describe.only("cli", () => {
describe("examples", () => {
it("should run the basic example without errors", () => {
const result = execSync(
`yarn tsm "examples/basic/**/*.scss" --includePaths examples/basic/core --aliases.~alias variables`
`yarn tsm "examples/basic/**/*.scss" --includePaths examples/basic/core --aliases.~alias variables --banner '// example banner\n'`
).toString();

expect(result).toContain("Found 3 files. Generating type definitions...");
});
it("should run the default-export example without errors", () => {
const result = execSync(
`yarn tsm "examples/default-export/**/*.scss" --exportType default --nameFormat kebab`
`yarn tsm "examples/default-export/**/*.scss" --exportType default --nameFormat kebab --banner '// example banner\n'`
).toString();

expect(result).toContain("Found 1 file. Generating type definitions...");
Expand Down
1 change: 1 addition & 0 deletions __tests__/core/attemptPrettier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("attemptPrettier", () => {

it("should match snapshot", async () => {
const typeDefinition = classNamesToTypeDefinitions({
banner: "",
classNames: ["nestedAnother", "nestedClass", "someStyles"],
exportType: "default"
});
Expand Down
1 change: 1 addition & 0 deletions __tests__/core/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describeAllImplementations(implementation => {
const pattern = `${__dirname}/../**/*.scss`;

await generate(pattern, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down
2 changes: 2 additions & 0 deletions __tests__/core/list-different.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describeAllImplementations(implementation => {
const pattern = `${__dirname}/../**/*.scss`;

await listDifferent(pattern, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down Expand Up @@ -51,6 +52,7 @@ describeAllImplementations(implementation => {
const pattern = `${__dirname}/../**/style.scss`;

await listDifferent(pattern, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down
2 changes: 2 additions & 0 deletions __tests__/core/write-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describeAllImplementations(implementation => {
const typesFile = getTypeDefinitionPath(testFile);

await writeFile(testFile, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand All @@ -44,6 +45,7 @@ describeAllImplementations(implementation => {
const testFile = `${__dirname}/../empty.scss`;

await writeFile(testFile, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down
2 changes: 2 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describeAllImplementations(implementation => {
const pattern = `${__dirname}`;

await main(pattern, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down Expand Up @@ -46,6 +47,7 @@ describeAllImplementations(implementation => {
const pattern = `${__dirname}`;

await main(pattern, {
banner: "",
watch: false,
ignoreInitial: false,
exportType: "named",
Expand Down
23 changes: 22 additions & 1 deletion __tests__/typescript/class-names-to-type-definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("classNamesToTypeDefinitions", () => {
describe("named", () => {
it("converts an array of class name strings to type definitions", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "named"
});
Expand All @@ -19,6 +20,7 @@ describe("classNamesToTypeDefinitions", () => {

it("returns null if there are no class names", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: [],
exportType: "named"
});
Expand All @@ -28,6 +30,7 @@ describe("classNamesToTypeDefinitions", () => {

it("prints a warning if a classname is a reserved keyword and does not include it in the type definitions", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "if"],
exportType: "named"
});
Expand All @@ -40,6 +43,7 @@ describe("classNamesToTypeDefinitions", () => {

it("prints a warning if a classname is invalid and does not include it in the type definitions", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "invalid-variable"],
exportType: "named"
});
Expand All @@ -54,6 +58,7 @@ describe("classNamesToTypeDefinitions", () => {
describe("default", () => {
it("converts an array of class name strings to type definitions", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "default"
});
Expand All @@ -65,6 +70,7 @@ describe("classNamesToTypeDefinitions", () => {

it("returns null if there are no class names", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: [],
exportType: "default"
});
Expand All @@ -76,6 +82,7 @@ describe("classNamesToTypeDefinitions", () => {
describe("invalid export type", () => {
it("returns null", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass"],
exportType: "invalid" as ExportType
});
Expand All @@ -87,6 +94,7 @@ describe("classNamesToTypeDefinitions", () => {
describe("quoteType", () => {
it("uses double quotes for default exports when specified", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "default",
quoteType: "double"
Expand All @@ -99,6 +107,7 @@ describe("classNamesToTypeDefinitions", () => {

it("does not affect named exports", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "named",
quoteType: "double"
Expand All @@ -113,6 +122,7 @@ describe("classNamesToTypeDefinitions", () => {
describe("exportType name and type attributes", () => {
it("uses custom value for ClassNames type name", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "default",
exportTypeName: "Classes"
Expand All @@ -125,6 +135,7 @@ describe("classNamesToTypeDefinitions", () => {

it("uses custom value for Styles type name", () => {
const definition = classNamesToTypeDefinitions({
banner: "",
classNames: ["myClass", "yourClass"],
exportType: "default",
exportTypeInterface: "IStyles"
Expand All @@ -137,7 +148,7 @@ describe("classNamesToTypeDefinitions", () => {
});

describe("Banner support", () => {
it("appends the banner to the top of the output file", () => {
it("appends the banner to the top of the output file: default", () => {
const banner = "// Example banner";
const definition = classNamesToTypeDefinitions({
banner,
Expand All @@ -146,5 +157,15 @@ describe("classNamesToTypeDefinitions", () => {
});
expect(definition).toContain(banner);
});

it("appends the banner to the top of the output file: named", () => {
const banner = "// Example banner";
const definition = classNamesToTypeDefinitions({
banner,
classNames: ["myClass", "yourClass"],
exportType: "named"
});
expect(definition).toContain(banner);
});
});
});
1 change: 1 addition & 0 deletions examples/basic/feature-a/style.scss.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// example banner
export const text: string;
export const textHighlighted: string;
1 change: 1 addition & 0 deletions examples/basic/feature-b/style.scss.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// example banner
export const topBanner: string;
1 change: 1 addition & 0 deletions examples/default-export/feature-a/style.scss.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// example banner
export type Styles = {
i: string;
"i-am-kebab-cased": string;
Expand Down
7 changes: 7 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import yargs from "yargs";

import { nameFormatDefault, Aliases, NAME_FORMATS } from "./sass";
import {
bannerTypeDefault,
exportTypeDefault,
exportTypeInterfaceDefault,
exportTypeNameDefault,
Expand Down Expand Up @@ -140,6 +141,12 @@ const { _: patterns, ...rest } = yargs
default: logLevelDefault,
alias: "L",
describe: "Verbosity level of console output"
})
.options("banner", {
string: true,
default: bannerTypeDefault,
describe:
"Inserts text at the top of every output file for documentation purposes."
}).argv;

main(patterns[0], { ...rest });
2 changes: 1 addition & 1 deletion lib/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Options } from "../sass";
import { ExportType, QuoteType, LogLevel } from "../typescript";

export interface MainOptions extends Options {
banner?: string;
banner: string;
ignore: string[];
ignoreInitial: boolean;
exportType: ExportType;
Expand Down
3 changes: 2 additions & 1 deletion lib/typescript/class-names-to-type-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type QuoteType = "single" | "double";
export const QUOTE_TYPES: QuoteType[] = ["single", "double"];

export interface TypeDefinitionOptions {
banner?: string;
banner: string;
classNames: ClassNames;
exportType: ExportType;
exportTypeName?: string;
Expand All @@ -22,6 +22,7 @@ export const exportTypeDefault: ExportType = "named";
export const exportTypeNameDefault: string = "ClassNames";
export const exportTypeInterfaceDefault: string = "Styles";
export const quoteTypeDefault: QuoteType = "single";
export const bannerTypeDefault: string = "";

const classNameToNamedTypeDefinition = (className: ClassName) =>
`export const ${className}: string;`;
Expand Down
1 change: 1 addition & 0 deletions lib/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {
bannerTypeDefault,
classNamesToTypeDefinitions,
exportTypeDefault,
exportTypeInterfaceDefault,
Expand Down

0 comments on commit b07660f

Please sign in to comment.