@stepzen/transpiler 0.0.24 → 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ const debug = require("debug");
5
5
  const fs = require("fs-extra");
6
6
  const glob = require("glob");
7
7
  const inquirer = require("inquirer");
8
+ const path = require("path");
8
9
  const yaml = require("yaml");
9
10
  exports.default = async (source, silent = false, answers = {}) => {
10
11
  var e_1, _a, e_2, _b;
@@ -15,9 +16,10 @@ exports.default = async (source, silent = false, answers = {}) => {
15
16
  };
16
17
  // Now let's parse and add any config.yamls
17
18
  for (const y of glob.sync("**/config.yaml", { cwd: source })) {
18
- const file = fs.readFileSync(`${source}/${y}`, "utf8");
19
+ const filePath = path.join(source, y);
20
+ const file = fs.readFileSync(filePath, "utf8");
19
21
  const asYAML = yaml.parse(file);
20
- debug('stepzen:transpiler')(`Adding config.yaml ${source}/${y}`);
22
+ debug('stepzen:transpiler')(`Adding config.yaml ${filePath}`);
21
23
  debug('stepzen:transpiler')(`Contents: ${JSON.stringify(asYAML, null, 2)}`);
22
24
  if (asYAML.configurationset) {
23
25
  config.configurationset = config.configurationset.concat(asYAML.configurationset);
@@ -32,9 +34,10 @@ exports.default = async (source, silent = false, answers = {}) => {
32
34
  cwd: source,
33
35
  })), _h; _h = await _g.next(), !_h.done;) {
34
36
  const j = _h.value;
35
- const file = fs.readFileSync(`${source}/${j}`, "utf8");
37
+ const filePath = path.join(source, j);
38
+ const file = fs.readFileSync(filePath, "utf8");
36
39
  const asJSON = JSON.parse(file);
37
- debug('stepzen:transpiler')(`Adding stepzen.config.json ${source}/${j}`);
40
+ debug('stepzen:transpiler')(`Adding stepzen.config.json ${filePath}`);
38
41
  debug('stepzen:transpiler')(`Contents: ${JSON.stringify(asJSON, null, 2)}`);
39
42
  if ((_c = asJSON.config) === null || _c === void 0 ? void 0 : _c.questions) {
40
43
  try {
@@ -0,0 +1,4 @@
1
+ declare const _default: (source: string, options?: {
2
+ extensions: string;
3
+ }) => Promise<void>;
4
+ export default _default;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_fetch_1 = require("node-fetch");
4
+ const fs = require("fs-extra");
5
+ const os = require("os");
6
+ const path = require("path");
7
+ const shell = require("shelljs");
8
+ const stitch_1 = require("../actions/stitch");
9
+ exports.default = async (source, options = {
10
+ extensions: "",
11
+ }) => {
12
+ if (!fs.existsSync(source)) {
13
+ throw new Error(`Cannot find source directory ${source}`);
14
+ }
15
+ let extensions = options.extensions;
16
+ if (!extensions) {
17
+ const response = await node_fetch_1.default('https://www.steprz.net/directives.graphql');
18
+ extensions = await response.text();
19
+ }
20
+ const stitched = stitch_1.default(source);
21
+ const index = path.join(stitched, 'index.graphql');
22
+ const graphql = fs.readFileSync(index, 'utf8');
23
+ const tmp = path.join(os.tmpdir(), `stepzen-lint-${Date.now()}`);
24
+ fs.ensureDirSync(tmp);
25
+ const lintFile = path.join(tmp, 'index.graphql');
26
+ fs.writeFileSync(lintFile, `${extensions}${os.EOL}${os.EOL}${graphql}`);
27
+ shell.exec(`npx graphql-schema-linter ${lintFile}`, { silent: true }, (code, stdout, stderr) => {
28
+ console.log(stdout);
29
+ fs.removeSync(tmp);
30
+ });
31
+ };
@@ -5,6 +5,7 @@ const glob = require("glob");
5
5
  const merge_1 = require("@graphql-tools/merge");
6
6
  const graphql_1 = require("graphql");
7
7
  const os = require("os");
8
+ const path = require("path");
8
9
  const merge_helpers_1 = require("../utils/merge-helpers");
9
10
  const print_1 = require("./print");
10
11
  const set_files_in_sdl_1 = require("../utils/set-files-in-sdl");
@@ -27,7 +28,7 @@ exports.default = async (original, imported, options = {
27
28
  if (!options.answers)
28
29
  options.answers = {};
29
30
  if (!options.output)
30
- options.output = `${os.tmpdir()}/stepzen-tmp-${Date.now()}`;
31
+ options.output = path.join(os.tmpdir(), `stepzen-tmp-${Date.now()}`);
31
32
  if (!options.silent)
32
33
  options.silent = false;
33
34
  // Ensure original, importing and output directories exist
@@ -39,8 +40,9 @@ exports.default = async (original, imported, options = {
39
40
  // Copy the original into the output.
40
41
  fs.copySync(original, options.output);
41
42
  // Ensure an index.graphql exists
42
- if (!fs.existsSync(`${options.output}/index.graphql`)) {
43
- fs.writeFileSync(`${options.output}/index.graphql`, BLANK_INDEX_TEMPLATE);
43
+ const outputIndex = path.join(options.output, 'index.graphql');
44
+ if (!fs.existsSync(outputIndex)) {
45
+ fs.writeFileSync(outputIndex, BLANK_INDEX_TEMPLATE);
44
46
  }
45
47
  const merged = merge_1.mergeTypeDefs([
46
48
  await merge_helpers_1.getSchema(options.output),
@@ -100,19 +102,24 @@ exports.default = async (original, imported, options = {
100
102
  };
101
103
  // Write the files to the output directory
102
104
  cleaned.original.forEach((a) => {
103
- fs.ensureFileSync(`${options.output}/${a.file}`);
104
- fs.writeFileSync(`${options.output}/${a.file}`, print_1.default(a.ast));
105
+ // eslint-disable-next-line
106
+ const file = path.join(options.output, a.file);
107
+ fs.ensureFileSync(file);
108
+ fs.writeFileSync(file, print_1.default(a.ast));
105
109
  });
106
110
  cleaned.imported.forEach((a) => {
107
- fs.ensureFileSync(`${options.output}/${imported.name}/${a.file}`);
108
- fs.writeFileSync(`${options.output}/${imported.name}/${a.file}`, print_1.default(a.ast));
111
+ // eslint-disable-next-line
112
+ const file = path.join(options.output, imported.name, a.file);
113
+ fs.ensureFileSync(file);
114
+ fs.writeFileSync(file, print_1.default(a.ast));
109
115
  });
110
116
  // Make sure all files are referenced in @sdl
111
117
  set_files_in_sdl_1.default(options.output);
112
118
  // Generate configuration
113
119
  const config = await merge_helpers_1.getConfiguration([options.output, imported.source], options.silent, options.answers);
114
120
  if (config) {
115
- fs.writeFileSync(`${options.output}/config.yaml`, config);
121
+ const configFile = path.join(options.output, 'config.yaml');
122
+ fs.writeFileSync(configFile, config);
116
123
  }
117
124
  // Return a merged schema!
118
125
  return options.output;
@@ -5,9 +5,10 @@ const graphql_1 = require("graphql");
5
5
  const fs = require("fs-extra");
6
6
  const glob = require("glob");
7
7
  const os = require("os");
8
+ const path = require("path");
8
9
  const prettier = require("prettier");
9
10
  const dedupe_query_and_mutation_types_1 = require("../utils/dedupe-query-and-mutation-types");
10
- exports.default = (source, output = `${os.tmpdir()}/stepzen-tmp-${Date.now()}`) => {
11
+ exports.default = (source, output = path.join(os.tmpdir(), `stepzen-tmp-${Date.now()}`)) => {
11
12
  // Ensure source and output directories exist
12
13
  if (!fs.existsSync(source))
13
14
  throw new Error(`Cannot find source directory ${source}`);
@@ -16,8 +17,9 @@ exports.default = (source, output = `${os.tmpdir()}/stepzen-tmp-${Date.now()}`)
16
17
  let ast;
17
18
  let files = [];
18
19
  // If there's an index.graphQL - get files argument in @sdl directive
19
- if (fs.existsSync(`${source}/index.graphql`)) {
20
- const index = fs.readFileSync(`${source}/index.graphql`, "utf8");
20
+ const sourceIndex = path.join(source, 'index.graphql');
21
+ if (fs.existsSync(sourceIndex)) {
22
+ const index = fs.readFileSync(sourceIndex, "utf8");
21
23
  ast = graphql_1.parse(index);
22
24
  ast = graphql_1.visit(ast, {
23
25
  Directive(node) {
@@ -45,7 +47,7 @@ exports.default = (source, output = `${os.tmpdir()}/stepzen-tmp-${Date.now()}`)
45
47
  const graphql = fs.readFileSync(`${source}/${file}`, "utf8");
46
48
  return graphql;
47
49
  });
48
- ast = graphql_1.parse(content.join("\n"));
50
+ ast = graphql_1.parse(content.join(os.EOL));
49
51
  }
50
52
  // Strip @sdl directive
51
53
  ast = graphql_1.visit(ast, {
@@ -56,26 +58,31 @@ exports.default = (source, output = `${os.tmpdir()}/stepzen-tmp-${Date.now()}`)
56
58
  });
57
59
  // Check all the files exist
58
60
  for (const file of files) {
59
- if (!fs.existsSync(`${source}/${file}`)) {
60
- throw new Error(`Cannot find file ${file}`);
61
+ const find = path.join(source, file);
62
+ if (!fs.existsSync(find)) {
63
+ throw new Error(`Cannot find file ${find}`);
61
64
  }
62
65
  }
63
66
  // Get all the files and stitch
64
67
  const printed = graphql_1.print(ast);
65
- let stitched = `${printed}\n`;
68
+ let stitched = `${printed}${os.EOL}`;
66
69
  for (const file of files) {
67
- const content = fs.readFileSync(`${source}/${file}`, "utf8");
68
- stitched += `${content}\n`;
70
+ const find = path.join(source, file);
71
+ const content = fs.readFileSync(find, "utf8");
72
+ stitched += `${content}${os.EOL}`;
69
73
  }
70
74
  // Dedupe Query and Mutation types
71
75
  stitched = dedupe_query_and_mutation_types_1.default(stitched);
72
76
  // Format
73
77
  stitched = prettier.format(stitched, { parser: "graphql" });
74
78
  // Write to output folder
75
- fs.writeFileSync(`${output}/index.graphql`, stitched);
79
+ const outputIndex = path.join(output, 'index.graphql');
80
+ fs.writeFileSync(outputIndex, stitched);
76
81
  // Copy config if exists, too
77
- if (fs.existsSync(`${source}/config.yaml`)) {
78
- fs.copyFileSync(`${source}/config.yaml`, `${output}/config.yaml`);
82
+ const sourceConfig = path.join(source, 'config.yaml');
83
+ const outputConfig = path.join(output, 'config.yaml');
84
+ if (fs.existsSync(sourceConfig)) {
85
+ fs.copyFileSync(sourceConfig, outputConfig);
79
86
  }
80
87
  // Return output folder
81
88
  return output;
@@ -49,7 +49,8 @@ exports.default = async (source) => {
49
49
  let mutated = '';
50
50
  if (graphqlFiles.length > 0) {
51
51
  const stitched = await stitch_1.default(source);
52
- const graphql = fs.readFileSync(`${stitched}/index.graphql`, "utf8");
52
+ const index = path.join(stitched, 'index.graphql');
53
+ const graphql = fs.readFileSync(index, "utf8");
53
54
  let ast = graphql_1.parse(graphql);
54
55
  original = graphql_1.parse(graphql);
55
56
  // you can transpile schema ast here
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const graphql_1 = require("graphql");
4
4
  const fs = require("fs-extra");
5
+ const os = require("os");
6
+ const path = require("path");
5
7
  const ensure_query_and_mutation_types_1 = require("../utils/ensure-query-and-mutation-types");
6
8
  const constants_1 = require("../utils/constants");
7
9
  const validators_1 = require("../validators");
@@ -17,9 +19,10 @@ exports.default = (source, options = {
17
19
  throw new Error(`Cannot find index.graphql in ${source}`);
18
20
  }
19
21
  // Get the index.graphql file
20
- const index = fs.readFileSync(`${source}/index.graphql`, "utf8");
22
+ const file = path.join(source, 'index.graphql');
23
+ const index = fs.readFileSync(file, "utf8");
21
24
  // Parse
22
- let ast = graphql_1.parse(`${constants_1.EXPERIMENTAL_EXTENSIONS}\n${options.extensions}\n${index}`, {
25
+ let ast = graphql_1.parse(`${constants_1.EXPERIMENTAL_EXTENSIONS}${os.EOL}${options.extensions}${os.EOL}${index}`, {
23
26
  noLocation: true,
24
27
  });
25
28
  // We make an exception for 'no type Query' or 'no type Mutation'
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const configure_1 = require("./actions/configure");
4
+ const lint_1 = require("./actions/lint");
4
5
  const merge_1 = require("./actions/merge");
5
6
  const print_1 = require("./actions/print");
6
7
  const stitch_1 = require("./actions/stitch");
@@ -8,6 +9,7 @@ const transpile_1 = require("./actions/transpile");
8
9
  const validate_1 = require("./actions/validate");
9
10
  module.exports = {
10
11
  configure: configure_1.default,
12
+ lint: lint_1.default,
11
13
  merge: merge_1.default,
12
14
  print: print_1.default,
13
15
  stitch: stitch_1.default,
@@ -7,6 +7,7 @@ const node_fetch_1 = require("node-fetch");
7
7
  const fs = require("fs-extra");
8
8
  const glob = require("glob");
9
9
  const os = require("os");
10
+ const path = require("path");
10
11
  const graphql_helpers_1 = require("../utils/graphql-helpers");
11
12
  const constants_1 = require("./constants");
12
13
  const configure_1 = require("../actions/configure");
@@ -46,7 +47,7 @@ const findTypeInSchema = (name, files) => {
46
47
  };
47
48
  exports.findTypeInSchema = findTypeInSchema;
48
49
  const getConfiguration = async (directories, silent = false, answers = {}) => {
49
- const tmp = `${os.tmpdir()}/stepzen-tmp-config-${Date.now()}`;
50
+ const tmp = path.join(os.tmpdir(), `stepzen-tmp-config-${Date.now()}`);
50
51
  fs.ensureDirSync(tmp);
51
52
  for (const directory of directories) {
52
53
  const configs = [
@@ -54,9 +55,11 @@ const getConfiguration = async (directories, silent = false, answers = {}) => {
54
55
  ...glob.sync("**/stepzen.config.json", { cwd: directory }),
55
56
  ];
56
57
  for (const config of configs) {
57
- const content = fs.readFileSync(`${directory}/${config}`, "utf8");
58
- fs.ensureFileSync(`${tmp}/${directory}/${config}`);
59
- fs.writeFileSync(`${tmp}/${directory}/${config}`, content);
58
+ const configFolder = path.join(directory, config);
59
+ const writeFolder = path.join(tmp, directory, config);
60
+ const content = fs.readFileSync(configFolder, "utf8");
61
+ fs.ensureFileSync(writeFolder);
62
+ fs.writeFileSync(writeFolder, content);
60
63
  }
61
64
  }
62
65
  const configuration = await configure_1.default(tmp, silent, answers);
@@ -74,7 +77,7 @@ exports.getExtensions = getExtensions;
74
77
  const getSchema = async (directory) => {
75
78
  const extensions = await exports.getExtensions();
76
79
  const transpiled = await transpile_1.default(directory);
77
- return utilities_1.buildSchema(`${extensions}\n${transpiled.schema}`);
80
+ return utilities_1.buildSchema(`${extensions}${os.EOL}${transpiled.schema}`);
78
81
  };
79
82
  exports.getSchema = getSchema;
80
83
  const mergeQueryMutationIntoSchema = (type, files) => {
@@ -3,13 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const fs = require("fs");
4
4
  const glob = require("glob");
5
5
  const graphql_1 = require("graphql");
6
+ const path = require("path");
6
7
  const graphql_helpers_1 = require("./graphql-helpers");
7
8
  const print_1 = require("../actions/print");
8
9
  exports.default = (source) => {
9
10
  const files = glob
10
11
  .sync("**/*.graphql", { cwd: source })
11
12
  .filter((file) => file !== "index.graphql");
12
- const index = fs.readFileSync(`${source}/index.graphql`, "utf8");
13
+ const output = path.join(source, 'index.graphql');
14
+ const index = fs.readFileSync(output, "utf8");
13
15
  let ast = graphql_1.parse(index);
14
16
  ast = graphql_1.visit(ast, {
15
17
  Directive(node) {
@@ -28,5 +30,5 @@ exports.default = (source) => {
28
30
  }
29
31
  },
30
32
  });
31
- fs.writeFileSync(`${source}/index.graphql`, print_1.default(ast));
33
+ fs.writeFileSync(output, print_1.default(ast));
32
34
  };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const graphql_1 = require("graphql");
4
4
  const lodash_1 = require("lodash");
5
5
  const fs = require("fs");
6
+ const path = require("path");
6
7
  const yaml = require("yaml");
7
8
  const STEPZEN_DEFAULTS = [
8
9
  'fedex_default',
@@ -27,10 +28,11 @@ exports.default = (ast, source) => {
27
28
  configs = lodash_1.uniq(configs);
28
29
  configs = lodash_1.filter(configs, (config) => !STEPZEN_DEFAULTS.includes(config));
29
30
  if (configs.length > 0) {
30
- if (!fs.existsSync(`${source}/config.yaml`)) {
31
+ const sourceConfig = path.join(source, 'config.yaml');
32
+ if (!fs.existsSync(sourceConfig)) {
31
33
  throw new Error('No config.yaml found');
32
34
  }
33
- const content = fs.readFileSync(`${source}/config.yaml`, 'utf8');
35
+ const content = fs.readFileSync(sourceConfig, 'utf8');
34
36
  const asYaml = yaml.parse(content);
35
37
  for (const config of configs) {
36
38
  const found = (_a = asYaml === null || asYaml === void 0 ? void 0 : asYaml.configurationset) === null || _a === void 0 ? void 0 : _a.find((c) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stepzen/transpiler",
3
3
  "description": "The StepZen transpiler",
4
- "version": "0.0.24",
4
+ "version": "0.0.28",
5
5
  "author": "Darren Waddell <darren@stepzen.com>",
6
6
  "license": "MIT",
7
7
  "files": [
@@ -27,6 +27,7 @@
27
27
  "lodash": "^4.17.21",
28
28
  "node-fetch": "^2.6.1",
29
29
  "prettier": "^2.2.1",
30
+ "shelljs": "^0.8.4",
30
31
  "yaml": "^1.10.2"
31
32
  },
32
33
  "devDependencies": {
@@ -39,6 +40,7 @@
39
40
  "@types/mock-fs": "^4.13.0",
40
41
  "@types/node-fetch": "^2.5.10",
41
42
  "@types/prettier": "^2.2.3",
43
+ "@types/shelljs": "^0.8.9",
42
44
  "chai": "^4.3.4",
43
45
  "fancy-test": "^1.4.10",
44
46
  "mocha": "^9.0.1",