Skip to content

Commit

Permalink
feat(lib): remove "DO NOT EDIT" file header (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Sep 7, 2022
1 parent 7d513e9 commit f74344e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/cli/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const NON_EDITABLE_FILE_HEADER =
"// Code generated by prismic-ts-codegen. DO NOT EDIT.";
21 changes: 12 additions & 9 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { configSchema } from "./configSchema";
import { loadLocaleIDs } from "./loadLocaleIDs";
import { loadModels } from "./loadModels";
import { loadConfig } from "./loadConfig";
import { NON_EDITABLE_FILE_HEADER } from "./constants";

const cli = meow(
`
Expand Down Expand Up @@ -102,6 +103,15 @@ const main = async () => {

const hasCustomTypeModels = customTypeModels.length > 0;

if (
config.clientIntegration?.includeCreateClientInterface &&
!hasCustomTypeModels
) {
console.info(
"[INFO]: prismic-ts-codegen was configured to automatically integrate with `@prismicio/client`, but the integration was not generated because no Custom Type models were found. Automatic integration requires at least one Custom Type model.",
);
}

const types = generateTypes({
customTypeModels,
sharedSliceModels,
Expand All @@ -116,17 +126,10 @@ const main = async () => {
},
});

if (
config.clientIntegration?.includeCreateClientInterface &&
!hasCustomTypeModels
) {
console.info(
"[INFO]: prismic-ts-codegen was configured to automatically integrate with `@prismicio/client`, but the integration was not generated because no Custom Type models were found. Automatic integration requires at least one Custom Type model.",
);
}
const fileContents = `${NON_EDITABLE_FILE_HEADER}\n\n${types}`;

if (config.output) {
writeFileSync(resolvePath(config.output), types);
writeFileSync(resolvePath(config.output), fileContents);

console.info(`\nGenerated types in: ${config.output}`);
} else {
Expand Down
6 changes: 0 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { CustomTypeModelFieldType } from "@prismicio/types";
import { stripIndent } from "common-tags";

export const BLANK_LINE_IDENTIFIER = "// ___BLANK_LINE_TO_BE_REPLACED___";

export const NON_EDITABLE_FILE_HEADER = stripIndent`
// Code generated by prismic-ts-codegen. DO NOT EDIT.
${BLANK_LINE_IDENTIFIER}
`;

export const CUSTOM_TYPES_DOCUMENTATION_URL =
"https://prismic.io/docs/core-concepts/custom-types";

Expand Down
3 changes: 1 addition & 2 deletions src/generateTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Project, ModuleDeclarationKind } from "ts-morph";
import type { CustomTypeModel, SharedSliceModel } from "@prismicio/types";

import { BLANK_LINE_IDENTIFIER, NON_EDITABLE_FILE_HEADER } from "./constants";
import { BLANK_LINE_IDENTIFIER } from "./constants";
import { addTypeAliasForCustomType } from "./lib/addTypeAliasForCustomType";
import { addTypeAliasForSharedSlice } from "./lib/addTypeAliasForSharedSlice";
import { getSourceFileText } from "./lib/getSourceFileText";
Expand Down Expand Up @@ -30,7 +30,6 @@ export const generateTypes = (config: GenerateTypesConfig = {}) => {
moduleSpecifier: "@prismicio/types",
namespaceImport: "prismicT",
isTypeOnly: true,
leadingTrivia: NON_EDITABLE_FILE_HEADER,
});

sourceFile.addStatements(BLANK_LINE_IDENTIFIER);
Expand Down
10 changes: 0 additions & 10 deletions test/generateTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ import { parseSourceFile } from "./__testutils__/parseSourceFile";

import * as lib from "../src";

test('includes "DO NOT EDIT" header', (t) => {
const res = lib.generateTypes();
const file = parseSourceFile(res);

const headerStatement = file.getStatementsWithComments()[0];

t.is(headerStatement.getKindName(), "SingleLineCommentTrivia");
t.regex(headerStatement.getText(), /DO NOT EDIT/);
});

test("imports @prismicio/types as prismicT", (t) => {
const res = lib.generateTypes();
const file = parseSourceFile(res);
Expand Down

0 comments on commit f74344e

Please sign in to comment.