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

Feature: Add Glob Patterns to Typedefs Generation #104

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"form-data": "^3.0.0",
"fs-extra": "^9.0.1",
"git-clone": "^0.1.0",
"glob": "^11.0.0",
"inquirer": "^7.3.2",
"json-schema-to-typescript": "^13.1.2",
"lodash": "^4.17.21",
Expand Down
32 changes: 22 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,23 @@ program
)
.requiredOption("--source <SPACE_ID>", "Source space id")
.requiredOption("--target <SPACE_ID>", "Target space id")
.option('--starts-with <STARTS_WITH>', 'Sync only stories that starts with the given string')
.option('--filter', 'Enable filter options to sync only stories that match the given filter. Required options: --keys; --operations; --values')
.option('--keys <KEYS>', 'Field names in your story object which should be used for filtering. Multiple keys should separated by comma.')
.option('--operations <OPERATIONS>', 'Operations to be used for filtering. Can be: is, in, not_in, like, not_like, any_in_array, all_in_array, gt_date, lt_date, gt_int, lt_int, gt_float, lt_float. Multiple operations should be separated by comma.')
.option('--values <VALUES>', 'Values to be used for filtering. Any string or number. If you want to use multiple values, separate them with a comma. Multiple values should be separated by comma.')
.option("--starts-with <STARTS_WITH>", "Sync only stories that starts with the given string")
.option(
"--filter",
"Enable filter options to sync only stories that match the given filter. Required options: --keys; --operations; --values"
)
.option(
"--keys <KEYS>",
"Field names in your story object which should be used for filtering. Multiple keys should separated by comma."
)
.option(
"--operations <OPERATIONS>",
"Operations to be used for filtering. Can be: is, in, not_in, like, not_like, any_in_array, all_in_array, gt_date, lt_date, gt_int, lt_int, gt_float, lt_float. Multiple operations should be separated by comma."
)
.option(
"--values <VALUES>",
"Values to be used for filtering. Any string or number. If you want to use multiple values, separate them with a comma. Multiple values should be separated by comma."
)
.option("--components-groups <UUIDs>", "Synchronize components based on their group UUIDs separated by commas")
.option("--components-full-sync", "Synchronize components by overriding any property from source to target")
.action(async (options) => {
Expand All @@ -317,19 +329,19 @@ program
const {
type,
target,
source,
source,
startsWith,
filter,
keys,
operations,
values,
componentsGroups,
componentsFullSync
componentsFullSync,
} = options;

const _componentsGroups = componentsGroups ? componentsGroups.split(",") : null;
const _componentsFullSync = !!componentsFullSync;
const filterQuery = filter ? buildFilterQuery(keys, operations, values) : undefined
const filterQuery = filter ? buildFilterQuery(keys, operations, values) : undefined;
const token = creds.get().token || null;

const _types = type.split(",") || [];
Expand Down Expand Up @@ -556,8 +568,8 @@ program
.command(COMMANDS.GENERATE_TYPESCRIPT_TYPEDEFS)
// Providing backward-compatible flags with Storyblok Generate TS https://github.com/dohomi/storyblok-generate-ts
.requiredOption(
"--source, --sourceFilePaths <PATHS>",
"Path(s) to the components JSON file(s) as comma separated values",
"--source, --sourceFilePaths <PATHS | GLOB-PATTERN>",
"Path(s) to the components JSON file(s) as comma separated values OR a glob pattern as a string",
(paths, _previous) => paths.split(",")
)
.option(
Expand Down
7 changes: 4 additions & 3 deletions src/tasks/generate-typescript-typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from "fs";
import type { GenerateTypescriptTypedefsCLIOptions, JSONSchemaToTSOptions } from "../types";
import { GenerateTypesFromJSONSchemas } from "../utils/typescript/generateTypesFromJSONSchema";
import type { JSONSchema } from "json-schema-to-typescript";
import { glob } from "glob";

type GenerateTSTypedefs = (options: GenerateTypescriptTypedefsCLIOptions) => void;

Expand All @@ -24,7 +25,7 @@ const generateTypescriptTypedefs: GenerateTSTypedefs = async ({
return paths.map((sourceFilePath) => JSON.parse(fs.readFileSync(sourceFilePath, "utf8")));
} catch (e) {
console.error(
`${chalk.red("X")}
`${chalk.red("X")}
Could not load JSON files from the provided paths: ${paths}. Please check if those files exist.`
);
return null;
Expand All @@ -41,7 +42,7 @@ const generateTypescriptTypedefs: GenerateTSTypedefs = async ({
return JSON.parse(fs.readFileSync(path, "utf8"));
} catch (e) {
console.error(
`${chalk.red("X")}
`${chalk.red("X")}
Could not load options from the JSON file at ${path}. Please check if the file exists and if it's properly formatted.`
);
return null;
Expand All @@ -58,7 +59,7 @@ const generateTypescriptTypedefs: GenerateTSTypedefs = async ({
...JSONSchemaToTSCustomOptions,
};

const componentsJSONSchemaArray = getJSONSchemasFromPaths(sourceFilePaths)?.flatMap(
const componentsJSONSchemaArray = getJSONSchemasFromPaths(await glob(sourceFilePaths))?.flatMap(
(componentsJSONSchema) => componentsJSONSchema.components || componentsJSONSchema
);

Expand Down
Loading