Skip to content

Commit

Permalink
Handle parsing/transforming .graphql and .gql files
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Nov 12, 2024
1 parent b756f9e commit ebf0d01
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions scripts/codemods/data-masking/unmask.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import type { Collection, Transform, TemplateLiteral } from "jscodeshift";
import type { DirectiveNode, DocumentNode } from "graphql";
import { Kind, parse, visit, print } from "graphql";
import path from "node:path";

const LEADING_WHITESPACE = /^[\s\t]*(?=[\S\n])/;
const TRAILING_WHITESPACE = /(?<=[\S\n])[\s\t]*$/;

const DEFAULT_TAGS = ["gql", "graphql"];

const transform: Transform = function transform(file, api, options) {
const j = api.jscodeshift;
const source = j(file.source);

const { tag = DEFAULT_TAGS, mode } = options;

if (mode && mode !== "migrate") {
Expand All @@ -19,6 +17,15 @@ const transform: Transform = function transform(file, api, options) {
);
}

const extname = path.extname(file.path);

if (extname === ".graphql" || extname === ".gql") {
return transformGraphQLFile(file.source, mode);
}

const j = api.jscodeshift;
const source = j(file.source);

const tagNames = Array.isArray(tag) ? tag : [tag];

tagNames.forEach((tagName) => {
Expand Down Expand Up @@ -148,4 +155,8 @@ function getMatch(str: string, match: RegExp) {
return str.match(match)?.at(0) ?? "";
}

function transformGraphQLFile(source: string, mode: string) {
return print(addUnmaskDirective(parse(source), mode));
}

export default transform;

0 comments on commit ebf0d01

Please sign in to comment.