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

feat(emit-schema): add support for recursive directory creation #269

Merged
merged 29 commits into from Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
443d4f9
feat(emit-schema): add support for recursive directory creation
Mar 3, 2019
849b127
fix(emit-schema): remove unnecessary export
mszekiel Mar 3, 2019
ed82a72
fix(emit-schema): add async function and path parser
Mar 3, 2019
0659e81
test(emit-schema): update tests, add mock-fs
Mar 3, 2019
a18a025
fix(emit-schema): update test names
Mar 3, 2019
8e67c52
test(emit-schema): use resolved path while checking file
Mar 3, 2019
6aa3e5a
fix(emit-schema): fix writing to files in emit functions
Mar 3, 2019
a463ee9
test(emit-schema): add travis dir to mock
Mar 3, 2019
8563245
test(emit-schema): remove not working and redundant tests
Mar 3, 2019
58b14cd
test(emit-schema): try adding mockfs.restore
mszekiel Mar 4, 2019
1684ed8
test(emit-schema): work on mockfs.restore
mszekiel Mar 4, 2019
1681f0e
test(emit-schema): remove mock-fs, add fs-extra, fix tests
mszekiel Mar 4, 2019
987261c
test(emit-schema): add logs for ci checks
mszekiel Mar 4, 2019
07ff6d3
fix(emit-schema): remove custom functions and add fs-extra
mszekiel Mar 4, 2019
745de1e
test(emit-schema): update tests, tweak checkSchema function
mszekiel Mar 4, 2019
1c54f70
fix(emit-schema): clear imports of fs-extra
mszekiel Mar 4, 2019
f271e17
fix(emit-schema): resolve conflicts
mszekiel Mar 4, 2019
dbfda21
fix(emit-schema): remove unnecessary import
Mar 4, 2019
58075c2
Merge branch 'master' into emit-schema
Mar 4, 2019
0edd179
fix(emit-schema): remove fs-extra, make own functions
mszekiel Mar 5, 2019
c7528dc
test(emit-schema): add test, more coverage
mszekiel Mar 5, 2019
151397d
test(emit-schema): missing tests added
mszekiel Mar 5, 2019
d130ea9
Merge pull request #2 from xanaxx/emit-schema
mszekiel Mar 5, 2019
6d11f9c
fix(emit-schema): remove unused dependencies
mszekiel Mar 5, 2019
34c6760
Merge branch 'master' into master
mszekiel Mar 5, 2019
6f12c34
Refactor file operation helpers to async/await
MichalLytek Mar 10, 2019
233659b
Merge branch 'master' into emit-schema-dir
MichalLytek Mar 10, 2019
af03b45
Update changelog
MichalLytek Mar 11, 2019
4ce5c44
Merge branch 'master' into master
MichalLytek Mar 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 39 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -42,6 +42,7 @@
"@types/ioredis": "^4.0.8",
"@types/jest": "^23.3.14",
"@types/node": "^11.9.4",
"@types/rimraf": "^2.0.2",
"@types/ws": "^6.0.1",
"apollo-cache-control": "^0.5.2",
"apollo-cache-inmemory": "^1.4.3",
Expand Down Expand Up @@ -70,6 +71,7 @@
"mysql": "^2.16.0",
"prettier": "^1.16.4",
"reflect-metadata": "^0.1.13",
"rimraf": "^2.6.3",
"subscriptions-transport-ws": "^0.9.15",
"ts-jest": "^24.0.0",
"ts-node": "^8.0.2",
Expand Down
3 changes: 2 additions & 1 deletion src/utils/buildSchema.ts
Expand Up @@ -4,11 +4,11 @@ import { Options as PrintSchemaOptions } from "graphql/utilities/schemaPrinter";
import { SchemaGenerator, SchemaGeneratorOptions } from "../schema/schema-generator";
import { loadResolversFromGlob } from "../helpers/loadResolversFromGlob";
import {
defaultSchemaFilePath,
emitSchemaDefinitionFileSync,
emitSchemaDefinitionFile,
defaultPrintSchemaOptions,
} from "./emitSchemaDefinitionFile";
import { resolve } from "path";

interface EmitSchemaFileOptions extends PrintSchemaOptions {
path?: string;
Expand Down Expand Up @@ -61,6 +61,7 @@ function getEmitSchemaDefinitionFileOptions(
schemaFileName: string;
printSchemaOptions: PrintSchemaOptions;
} {
const defaultSchemaFilePath = resolve(process.cwd(), "schema.gql");
return {
schemaFileName:
typeof buildSchemaOptions.emitSchemaFile === "string"
Expand Down
11 changes: 3 additions & 8 deletions src/utils/emitSchemaDefinitionFile.ts
@@ -1,9 +1,6 @@
import { writeFile, writeFileSync } from "fs";
import { GraphQLSchema, printSchema } from "graphql";
import { Options as PrintSchemaOptions } from "graphql/utilities/schemaPrinter";
import * as path from "path";

export const defaultSchemaFilePath = path.resolve(process.cwd(), "schema.gql");
import { outputFile, outputFileSync } from "./fileWriter";

export const defaultPrintSchemaOptions: PrintSchemaOptions = { commentDescriptions: false };

Expand All @@ -21,7 +18,7 @@ export function emitSchemaDefinitionFileSync(
options: PrintSchemaOptions = defaultPrintSchemaOptions,
) {
const schemaFileContent = generatedSchemaWarning + printSchema(schema, options);
writeFileSync(schemaFilePath, schemaFileContent);
outputFileSync(schemaFilePath, schemaFileContent);
}

export async function emitSchemaDefinitionFile(
Expand All @@ -30,7 +27,5 @@ export async function emitSchemaDefinitionFile(
options: PrintSchemaOptions = defaultPrintSchemaOptions,
) {
const schemaFileContent = generatedSchemaWarning + printSchema(schema, options);
return new Promise<void>((resolve, reject) =>
writeFile(schemaFilePath, schemaFileContent, err => (err ? reject(err) : resolve())),
);
return outputFile(schemaFilePath, schemaFileContent);
}
88 changes: 88 additions & 0 deletions src/utils/fileWriter.ts
@@ -0,0 +1,88 @@
import * as path from "path";
import * as fs from "fs";

function parsePath(targetPath: string) {
const directoryArray: string[] = [];
const parsedPath = path.parse(path.resolve(targetPath));
const splitPath = parsedPath.dir.split(path.sep);
if (parsedPath.root === "/") {
splitPath[0] = `/${splitPath[0]}`;
}
splitPath.reduce((previous: string, next: string) => {
const directory = path.join(previous, next);
directoryArray.push(directory);
return path.join(directory);
});
return directoryArray;
}

function mkdirRecursive(filePath: string) {
const directoryArray = parsePath(filePath);
return directoryArray
.map(directory => {
return () =>
new Promise((res, rej) => {
fs.mkdir(directory, err => {
if (err && err.code !== "EEXIST") {
rej(err);
} else {
res();
}
});
});
})
.reduce((promise, func) => {
return promise.then(
() => func(),
err => {
throw err;
},
);
}, Promise.resolve({}));
}

function mkdirRecursiveSync(filePath: string) {
const directoryArray = parsePath(filePath);
directoryArray.forEach(directory => {
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
});
}

export function outputFile(filePath: string, fileContent: any) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, fileContent, err => {
if (err && err.code !== "ENOENT") {
reject(err);
} else {
mkdirRecursive(filePath)
.then(() => {
fs.writeFile(filePath, fileContent, error => {
if (error) {
reject(error);
} else {
resolve();
}
});
})
.catch(e => {
reject(e);
});
}
});
});
}

export function outputFileSync(filePath: string, fileContent: any) {
try {
fs.writeFileSync(filePath, fileContent);
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
} else {
mkdirRecursiveSync(filePath);
fs.writeFileSync(filePath, fileContent);
}
}
}