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

fix: ensure posix path sep is used #728

Merged
merged 3 commits into from
Oct 2, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const path = require('path');

function getRelativeTypesPath(opsGenDirectory, generatedFileName) {
if (generatedFileName) {
const relativePath = path.relative(opsGenDirectory, generatedFileName);
const relativePath = path
.relative(opsGenDirectory, generatedFileName)
// ensure posix path separators are used
.split(path.win32.sep)
.join(path.posix.sep);
Comment on lines +5 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this is OK. But, is there any possibility that this affects any other downstream consumers of the relativePath in unintended (negative) ways?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All path and fs functions accept posix style paths even on a windows machine. So I think it should be fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i.e.

> path.resolve('./packages/graphql-generator/package.json')
D:\\Users\\dppilche\\amplify-codegen\\packages\\graphql-generator\\package.json

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more so wondering if we do any path splitting or merging that could break due to it looking for os delimiting.


// generatedFileName is in same directory as opsGenDirectory
// i.e. generatedFileName: src/graphql/API.ts, opsGenDirectory: src/graphql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ describe('GraphQL statements Formatter', () => {
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for TS frontend', () => {
it('Generates formatted output for TS frontend with posix path', () => {
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../API.ts').format(statements);
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for TS frontend with windows path', () => {
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '..\\API.ts').format(statements);
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for Flow frontend', () => {
const formattedOutput = new GraphQLStatementsFormatter('flow').format(statements);
expect(formattedOutput).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,33 @@ export const getProject = /* GraphQL */ \`
"
`;

exports[`GraphQL statements Formatter Generates formatted output for TS frontend 1`] = `
exports[`GraphQL statements Formatter Generates formatted output for TS frontend with posix path 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten

import * as APITypes from \\"../API\\";
type GeneratedQuery<InputType, OutputType> = string & {
__generatedQueryInput: InputType;
__generatedQueryOutput: OutputType;
};

export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
getProject(id: $id) {
id
name
createdAt
updatedAt
}
}
\` as GeneratedQuery<
APITypes.GetProjectQueryVariables,
APITypes.GetProjectQuery
>;
"
`;

exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import prettier, { BuiltInParserName } from 'prettier';
import {
interfaceNameFromOperation,
Expand Down Expand Up @@ -34,7 +35,12 @@ export class GraphQLStatementsFormatter {
}[operation];
this.lintOverrides = [];
this.headerComments = [];
this.typesPath = typesPath ? typesPath.replace(/.ts/i, '') : null;
this.typesPath = typesPath
? typesPath.replace(/.ts/i, '')
// ensure posix path separators are used
.split(path.win32.sep)
.join(path.posix.sep)
: null;
this.includeTypeScriptTypes = !!(this.language === 'typescript' && this.opTypeName && this.typesPath);
}

Expand Down