From f74344ea3e70c1a2d1fc235cea0fde9f068decc6 Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Tue, 6 Sep 2022 15:03:53 -1000 Subject: [PATCH] feat(lib): remove "DO NOT EDIT" file header (#34) --- src/cli/constants.ts | 2 ++ src/cli/index.ts | 21 ++++++++++++--------- src/constants.ts | 6 ------ src/generateTypes.ts | 3 +-- test/generateTypes.test.ts | 10 ---------- 5 files changed, 15 insertions(+), 27 deletions(-) create mode 100644 src/cli/constants.ts diff --git a/src/cli/constants.ts b/src/cli/constants.ts new file mode 100644 index 0000000..c9a2fc1 --- /dev/null +++ b/src/cli/constants.ts @@ -0,0 +1,2 @@ +export const NON_EDITABLE_FILE_HEADER = + "// Code generated by prismic-ts-codegen. DO NOT EDIT."; diff --git a/src/cli/index.ts b/src/cli/index.ts index 1c88a11..9d061d7 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -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( ` @@ -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, @@ -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 { diff --git a/src/constants.ts b/src/constants.ts index 695765e..7229807 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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"; diff --git a/src/generateTypes.ts b/src/generateTypes.ts index a21f02e..cc8be27 100644 --- a/src/generateTypes.ts +++ b/src/generateTypes.ts @@ -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"; @@ -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); diff --git a/test/generateTypes.test.ts b/test/generateTypes.test.ts index f862855..b153daa 100644 --- a/test/generateTypes.test.ts +++ b/test/generateTypes.test.ts @@ -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);