@cumulus/schemas 17.0.0 → 18.1.0

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.
@@ -6,7 +6,7 @@ declare type SchemaReplacements = {
6
6
  *
7
7
  * @param {string} schemaTemplatePath - Input schema template path
8
8
  * @param {string} schemaOutputPath - Path to write updated output schema
9
- * @param {Object} replacements
9
+ * @param {object} replacements
10
10
  * Object map specifying values to replace in schema template
11
11
  * @returns {void}
12
12
  */
@@ -14,8 +14,8 @@ export declare function templateJsonSchema(schemaTemplatePath: string, schemaOut
14
14
  /**
15
15
  * Generate output JSON schema from template with file properties updated.
16
16
  *
17
- * @param {string} schemaTemplatePath - Input schema template path
18
- * @param {string} schemaOutputPath - Path to write updated output schema
17
+ * @param schemaTemplatePath - Input schema template path
18
+ * @param schemaOutputPath - Path to write updated output schema
19
19
  * @returns {void}
20
20
  */
21
21
  export declare function templateJsonSchemaWithFiles(schemaTemplatePath: string, schemaOutputPath: string): void;
@@ -12,7 +12,7 @@ const filesJsonSchema = require('../files.schema.json');
12
12
  *
13
13
  * @param {string} schemaTemplatePath - Input schema template path
14
14
  * @param {string} schemaOutputPath - Path to write updated output schema
15
- * @param {Object} replacements
15
+ * @param {object} replacements
16
16
  * Object map specifying values to replace in schema template
17
17
  * @returns {void}
18
18
  */
@@ -27,8 +27,8 @@ exports.templateJsonSchema = templateJsonSchema;
27
27
  /**
28
28
  * Generate output JSON schema from template with file properties updated.
29
29
  *
30
- * @param {string} schemaTemplatePath - Input schema template path
31
- * @param {string} schemaOutputPath - Path to write updated output schema
30
+ * @param schemaTemplatePath - Input schema template path
31
+ * @param schemaOutputPath - Path to write updated output schema
32
32
  * @returns {void}
33
33
  */
34
34
  function templateJsonSchemaWithFiles(schemaTemplatePath, schemaOutputPath) {
@@ -4,17 +4,48 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- const fs_1 = __importDefault(require("fs"));
7
+ const promises_1 = __importDefault(require("fs/promises"));
8
8
  const path_1 = __importDefault(require("path"));
9
- const { templateJsonSchemaWithFiles, } = require('./generate-schemas');
10
- const taskDirectory = process.argv[2];
11
- const command = process.argv[3];
12
- const taskSchemasDirectory = path_1.default.join(taskDirectory, 'schemas');
13
- if (command === 'files') {
14
- const schemaTemplateFiles = fs_1.default.readdirSync(taskSchemasDirectory)
15
- .filter((filename) => filename.endsWith('.template'));
16
- schemaTemplateFiles.forEach((schemaTemplateFile) => {
17
- templateJsonSchemaWithFiles(path_1.default.join(taskSchemasDirectory, schemaTemplateFile), path_1.default.join(taskSchemasDirectory, schemaTemplateFile.replace('.template', '')));
9
+ const generate_schemas_1 = require("./generate-schemas");
10
+ const generate_types_1 = __importDefault(require("./generate-types"));
11
+ const TYPESCRIPT_OPTION = '--typescript';
12
+ function parseOptions(taskDirectory, args) {
13
+ let tsFile;
14
+ if (args.includes(TYPESCRIPT_OPTION)) {
15
+ const position = args.indexOf(TYPESCRIPT_OPTION);
16
+ if (args.length <= position + 1) {
17
+ throw new Error('Missing filepath for typescript option');
18
+ }
19
+ tsFile = promises_1.default.open(path_1.default.join(taskDirectory, args[position + 1]), 'w');
20
+ }
21
+ return { tsFile };
22
+ }
23
+ async function runFilesCommand(taskDirectory, rawOptions) {
24
+ const taskSchemasDirectory = path_1.default.join(taskDirectory, 'schemas');
25
+ const options = parseOptions(taskDirectory, rawOptions);
26
+ const schemaFiles = await promises_1.default.readdir(taskSchemasDirectory);
27
+ schemaFiles.filter((filename) => filename.endsWith('.template'))
28
+ .forEach((schemaTemplateFile) => {
29
+ const inputFile = path_1.default.join(taskSchemasDirectory, schemaTemplateFile);
30
+ const outputFile = path_1.default.join(taskSchemasDirectory, schemaTemplateFile.replace('.template', ''));
31
+ (0, generate_schemas_1.templateJsonSchemaWithFiles)(inputFile, outputFile);
18
32
  });
33
+ if (options.tsFile) {
34
+ const outputFile = await options.tsFile;
35
+ await (0, generate_types_1.default)(taskSchemasDirectory, outputFile);
36
+ }
37
+ }
38
+ async function main() {
39
+ const taskDirectory = process.argv[2];
40
+ const command = process.argv[3];
41
+ const options = process.argv.slice(4);
42
+ switch (command) {
43
+ case 'files':
44
+ await runFilesCommand(taskDirectory, options);
45
+ return;
46
+ default:
47
+ console.error('Unknown command');
48
+ }
19
49
  }
50
+ main();
20
51
  //# sourceMappingURL=generate-task-schemas.js.map
@@ -0,0 +1,4 @@
1
+ /// <reference types="node" />
2
+ import type { FileHandle } from 'fs/promises';
3
+ export default function generateTypes(folderPath: string, outputFile: FileHandle): Promise<void>;
4
+ //# sourceMappingURL=generate-types.d.ts.map
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const json_schema_to_typescript_1 = require("json-schema-to-typescript");
7
+ const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
8
+ const camelCase_1 = __importDefault(require("lodash/camelCase"));
9
+ const promises_1 = __importDefault(require("fs/promises"));
10
+ const path_1 = __importDefault(require("path"));
11
+ const BANNER_COMMENT = `
12
+ /**
13
+ * This file is generated using @cumulus/schema. Any modifications made to this file
14
+ * will be overwritten when the build script is rerun. Please do not modify this file.
15
+ */
16
+ `;
17
+ async function generateTypeFromFile(filePath, bannerComment) {
18
+ const rawSchema = await promises_1.default.readFile(filePath, { encoding: 'utf-8' });
19
+ const jsonSchema = JSON.parse(rawSchema);
20
+ if (!jsonSchema.title) {
21
+ throw new Error(`Must have a title defined in the JSONSchema defined in ${filePath}.`);
22
+ }
23
+ return await (0, json_schema_to_typescript_1.compile)(jsonSchema, (0, upperFirst_1.default)((0, camelCase_1.default)(jsonSchema.title)), {
24
+ additionalProperties: false,
25
+ bannerComment,
26
+ });
27
+ }
28
+ async function generateTypes(folderPath, outputFile) {
29
+ const schemaFiles = await promises_1.default.readdir(folderPath);
30
+ const types = await Promise.all(schemaFiles.filter((file) => /^\w+\.json$/.test(file))
31
+ .map((filePath, idx) => generateTypeFromFile(path_1.default.join(folderPath, filePath), idx === 0 ? BANNER_COMMENT : '')));
32
+ await promises_1.default.writeFile(outputFile, types.join('\n'));
33
+ }
34
+ exports.default = generateTypes;
35
+ //# sourceMappingURL=generate-types.js.map
@@ -0,0 +1,45 @@
1
+ {
2
+ "type": "array",
3
+ "items": {
4
+ "additionalProperties": false,
5
+ "type": "object",
6
+ "required": [
7
+ "bucket",
8
+ "key"
9
+ ],
10
+ "properties": {
11
+ "bucket": {
12
+ "description": "Bucket where file is archived in S3",
13
+ "type": "string"
14
+ },
15
+ "checksum": {
16
+ "description": "Checksum value for file",
17
+ "type": "string"
18
+ },
19
+ "checksumType": {
20
+ "description": "Type of checksum (e.g. md5, sha256, etc)",
21
+ "type": "string"
22
+ },
23
+ "fileName": {
24
+ "description": "Name of file (e.g. file.txt)",
25
+ "type": "string"
26
+ },
27
+ "key": {
28
+ "description": "S3 Key for archived file",
29
+ "type": "string"
30
+ },
31
+ "size": {
32
+ "description": "Size of file (in bytes)",
33
+ "type": "number"
34
+ },
35
+ "source": {
36
+ "description": "Source URI of the file from origin system (e.g. S3, FTP, HTTP)",
37
+ "type": "string"
38
+ },
39
+ "type": {
40
+ "description": "Type of file (e.g. data, metadata, browse)",
41
+ "type": "string"
42
+ }
43
+ }
44
+ }
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/schemas",
3
- "version": "17.0.0",
3
+ "version": "18.1.0",
4
4
  "description": "Helpers for managing Cumulus task schemas",
5
5
  "homepage": "https://github.com/nasa/cumulus/tree/master/packages/schemas",
6
6
  "repository": {
@@ -14,7 +14,8 @@
14
14
  "types": "dist/index.d.ts",
15
15
  "files": [
16
16
  "dist/**/*.js",
17
- "dist/**/*.d.ts"
17
+ "dist/**/*.d.ts",
18
+ "*.schema.json"
18
19
  ],
19
20
  "scripts": {
20
21
  "clean": "git clean -d -x -e node_modules -f",
@@ -41,7 +42,9 @@
41
42
  "author": "Cumulus Authors",
42
43
  "license": "Apache-2.0",
43
44
  "dependencies": {
44
- "json-templates": "^4.1.0"
45
+ "json-schema-to-typescript": "^13.0.2",
46
+ "json-templates": "^4.1.0",
47
+ "lodash": "^4.17.21"
45
48
  },
46
- "gitHead": "a5acb591242a334a730cebd6159f3a88a466432b"
49
+ "gitHead": "8428a02e0903d8d303c7dd36215aa6a24906e07b"
47
50
  }