Skip to content

Commit f6c5167

Browse files
authored
fix: significant chagnes (#2)
1 parent 18e61fb commit f6c5167

File tree

8 files changed

+91
-51
lines changed

8 files changed

+91
-51
lines changed

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
test:
3030
<<: *defaults
3131
docker:
32-
- image: circleci/node:10
32+
- image: circleci/node:13
3333
steps:
3434
- checkout
3535
- restore_cache: *restore-deps-cache
@@ -42,7 +42,7 @@ jobs:
4242
build:
4343
<<: *defaults
4444
docker:
45-
- image: circleci/node:10
45+
- image: circleci/node:13
4646
steps:
4747
- checkout
4848
- restore_cache: *restore-deps-cache
@@ -53,7 +53,7 @@ jobs:
5353
release:
5454
<<: *defaults
5555
docker:
56-
- image: circleci/node:10
56+
- image: circleci/node:13
5757
steps:
5858
- checkout
5959
- restore_cache: *restore-deps-cache

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
13.2.0

package-lock.json

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
"license": "Apache-2.0",
1919
"devDependencies": {
2020
"@types/jest": "^25.1.0",
21+
"@types/lodash": "^4.14.149",
2122
"@types/node": "^13.1.0",
2223
"jest": "^25.1.0",
23-
"ts-jest": "^25.0.0",
24-
"typescript": "^3.5.1"
24+
"ts-jest": "^25.0.0"
2525
},
2626
"dependencies": {
27+
"node-typescript-compiler": "^2.1.1",
28+
"typescript": "^3.5.1",
2729
"@etclabscore/json-schema-to-types": "^1.10.1",
28-
"@open-rpc/meta-schema": "^1.8.0"
30+
"@open-rpc/meta-schema": "^1.8.0",
31+
"lodash": "^4.17.15"
2932
}
3033
}

src/index.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,36 @@ import * as fs from "fs";
44
import * as path from "path";
55
import * as util from "util";
66

7-
const appendFile = util.promisify(fs.appendFile);
87
const readFile = util.promisify(fs.readFile);
98

10-
const p = path.resolve(process.cwd(), "./openrpc.json");
119

10+
describe("json-schema-tools semantic-release plugin", () => {
11+
afterEach(() => fs.rmdirSync("./testeroo", { recursive: true }));
1212

13-
describe("openrpc plugin", () => {
1413
describe("verifyConditions", () => {
1514
it("can error on verifyConditions", () => {
16-
return verifyConditions({ schemaLocation: "./src/test.json", languages: { ts: true } }, {}).catch((e: SemanticReleaseError) => {
15+
return verifyConditions({ outpath: "./testeroo", schemaLocation: "./src/test.json", languages: { ts: true } }, {}).catch((e: SemanticReleaseError) => {
1716
expect(e.message).toContain("Missing json schema document file.");
1817
});
1918
});
2019
it("can pass verifyConditions", () => {
21-
return verifyConditions({ schemaLocation: "./src/test-schema.json", languages: { ts: true } }, {}).then((valid: boolean) => {
20+
return verifyConditions({ outpath: "./testeroo", schemaLocation: "./src/test-schema.json", languages: { ts: true } }, {}).then((valid: boolean) => {
2221
expect(valid).toEqual(true);
2322
});
2423
});
2524
});
2625

2726
describe("prepare", () => {
2827
it("can fail if no next release version", () => {
29-
return prepare({ schemaLocation: "./src/test-schema.json", languages: { ts: true } }, {}).catch((e: SemanticReleaseError) => {
28+
return prepare({ outpath: "./testeroo", schemaLocation: "./src/test-schema.json", languages: { ts: true } }, {}).catch((e: SemanticReleaseError) => {
3029
expect(e.message).toContain("No nextRelease version");
3130
});
3231
});
3332
it("can pass prepare and set the version", async () => {
34-
return prepare({ schemaLocation: "./src/test-schema.json", languages: { ts: true } }, { nextRelease: { version: "1.0.0" } })
33+
return prepare({ outpath: "./testeroo", schemaLocation: "./src/test-schema.json", languages: { ts: true } }, { nextRelease: { version: "1.0.0" } })
3534
.then(async (prepared: boolean) => {
36-
const file = await readFile("./generated-typings.ts", "utf8");
35+
const file = await readFile("./testeroo/src/generated-typings.ts", "utf8");
3736
expect(typeof file).toEqual("string");
38-
fs.unlinkSync("./generated-typings.ts");
3937
});
4038
});
4139
});

src/index.ts

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import checkExists from "./checkExists";
33
import SemanticReleaseError from "./semanticReleaseError";
44
import errors from "./definitions/errors";
55
import JsonSchemaToTypes from "@etclabscore/json-schema-to-types";
6+
import { camelCase } from "lodash";
67
import * as fs from "fs";
78
import { promisify } from "util";
89
const readFile = promisify(fs.readFile);
910
const writeFile = promisify(fs.writeFile);
11+
const cp = promisify(fs.copyFile);
12+
const mkdir = promisify(fs.mkdir);
1013

14+
const tsc = require("node-typescript-compiler");//eslint-disable-line
1115

1216
interface PluginConfig {
13-
schemaLocation: string | string[];
14-
outputName?: string;
17+
outpath: string;
18+
schemaLocation: string;
1519
languages?: {
1620
ts?: boolean;
1721
go?: boolean;
@@ -32,29 +36,18 @@ let verified = false;
3236

3337
export const verifyConditions: PluginFunction = async (pluginConfig, context): Promise<boolean> => {
3438
const cwd = process.cwd();
35-
const config: PluginConfig = {
36-
schemaLocation: pluginConfig.schemaLocation,
37-
};
3839

39-
const paths = [];
40-
if (config.schemaLocation === undefined) {
40+
if (pluginConfig.schemaLocation === undefined) {
4141
throw new SemanticReleaseError("You must provide a schema location", "123321", "json-schema-tools transpiler requires a schema");
42-
} else if (config.schemaLocation instanceof Array) {
43-
config.schemaLocation.forEach((loc) => paths.push(path.resolve(cwd, loc)))
44-
} else {
45-
paths.push(path.resolve(cwd, config.schemaLocation))
4642
}
4743

48-
const existsPromises = paths.map(async (p) => {
49-
const exists = await checkExists(p);
50-
if (!exists) {
51-
const noDocumentError = errors.ENODOCUMENT();
52-
throw new SemanticReleaseError(noDocumentError.message, noDocumentError.code, noDocumentError.details);
53-
}
54-
return exists;
55-
});
44+
const sPath = path.resolve(cwd, pluginConfig.schemaLocation);
5645

57-
await Promise.all(existsPromises);
46+
const exists = await checkExists(sPath);
47+
if (!exists) {
48+
const noDocumentError = errors.ENODOCUMENT();
49+
throw new SemanticReleaseError(noDocumentError.message, noDocumentError.code, noDocumentError.details);
50+
}
5851

5952
verified = true;
6053
return verified;
@@ -68,19 +61,44 @@ export const prepare: PluginFunction = async (pluginConfig, context): Promise<bo
6861
throw new SemanticReleaseError("No nextRelease version", "ENOVERSION", "Something went wrong and there is no next release version"); //tslint:disable-line
6962
}
7063

71-
const cwd = process.cwd();
72-
const schemas = [];
73-
if (pluginConfig.schemaLocation instanceof Array) {
74-
pluginConfig.schemaLocation.forEach(async (loc) => schemas.push(JSON.parse(await readFile(path.resolve(cwd, loc), "utf8"))));
75-
} else {
76-
schemas.push(JSON.parse(await readFile(path.resolve(cwd, pluginConfig.schemaLocation), "utf8")))
64+
const outpath = pluginConfig.outpath || process.cwd();
65+
await mkdir(`./${outpath}/src`, { recursive: true });
66+
67+
const schemaPath = path.resolve(process.cwd(), pluginConfig.schemaLocation);
68+
await cp(schemaPath, `${outpath}/src/schema.json`);
69+
70+
const schema = JSON.parse(await readFile(schemaPath, "utf8"));
71+
72+
if (!schema.title) {
73+
throw new SemanticReleaseError("The schema must have a title", "ENOTITLE", "Schema requires a title");
7774
}
7875

79-
const transpiler = new JsonSchemaToTypes(schemas);
80-
const outTS = path.resolve(cwd, pluginConfig.outputName || "generated-typings");
76+
const transpiler = new JsonSchemaToTypes(schema);
77+
const outTS = path.resolve(outpath, "src/generated-typings");
8178

8279
if (!pluginConfig.languages || pluginConfig.languages.ts) {
8380
await writeFile(`${outTS}.ts`, transpiler.toTs());
81+
82+
const indexTS = path.resolve(outpath, "src/index.ts");
83+
const regularName = camelCase(schema.title);
84+
const ts = [
85+
`import * as Generated from "./generated-typings";`,
86+
`import ${regularName} from "${schemaPath}"`,
87+
`export default { ${regularName}, ...Generated };`
88+
].join("\n");
89+
await writeFile(indexTS, ts)
90+
await tsc.compile({
91+
"target": "es6",
92+
"module": "commonjs",
93+
"lib": [
94+
"es2015",
95+
],
96+
"declaration": true,
97+
"outDir": "./build",
98+
"strict": true,
99+
"esModuleInterop": true,
100+
"resolveJsonModule": true
101+
});
84102
}
85103
if (!pluginConfig.languages || pluginConfig.languages.go) {
86104
await writeFile(`${outTS}.go`, transpiler.toGo());

src/test-schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"title": "foobar",
23
"type": "string"
34
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
"target": "es6",
44
"module": "commonjs",
55
"lib": [
6-
"es2015",
6+
"es2015"
77
],
88
"declaration": true,
99
"outDir": "./build",
1010
"strict": true,
11-
"esModuleInterop": true
11+
"esModuleInterop": true,
12+
"resolveJsonModule": true
1213
}
1314
}

0 commit comments

Comments
 (0)