-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setting up typescript integration - depends on a new release of types…
…cript generator
- Loading branch information
1 parent
a46fec8
commit 11f347a
Showing
253 changed files
with
1,198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
= NPM support | ||
|
||
IMPORTANT: this support is experimental for now, if validated we would envision to release the bundles on npm registry and therefore this documentation would be outdated. | ||
|
||
== Features | ||
|
||
From the build, we generate `.d.ts` enabling you to get typescript build validation and completion in your preferred IDE to write script. | ||
|
||
== How to use | ||
|
||
For now, the declaration files are only pushed to Maven central repository and not yet to npm registry. | ||
Therefore to set it up, you must download the files from your build. | ||
Here is how to do it: | ||
|
||
[source,java] | ||
.package.json | ||
---- | ||
{ | ||
"devDependencies": { // <1> | ||
"typescript": "^4.7.4", | ||
"ts-node": "^10.9.1" | ||
}, | ||
"scripts": { // <2> | ||
"postinstall": "node build/kubernetes-descriptors.install.mjs" | ||
} | ||
} | ||
---- | ||
<.> We import typescript to ensure we have the syntax validation and the completion in the editors, | ||
<.> We execute after the installation - could be before - a script which will download and setup the `kubernetes-descriptors` dependency. | ||
|
||
`kubernetes-descriptors.install.mjs` will simply download the `d.ts` and `package.json` file in the right version from central: | ||
|
||
[source,js] | ||
.kubernetes-descriptors.install.mjs | ||
---- | ||
import { copyFileSync, mkdirSync } from 'fs'; | ||
// VARIABLES | ||
const KUBERNETES_VERSION = process.env.KUBERNETES_VERSION || '1.24.3'; | ||
const KUBERNETES_DESCRIPTOR_VERSION = process.env.KUBERNETES_DESCRIPTOR_VERSION || '1.0-SNAPSHOT'; | ||
const REPOSITORY = process.env.MAVEN_CENTRAL || 'https://repo.maven.apache.org/maven2'; | ||
const TARGET = process.env.OUTPUT || 'node_modules/kubernetes-descriptors'; | ||
// START OF THE SCRIPT | ||
const baseUrl = `${REPOSITORY}/io/yupiik/kubernetes/kubernetes-java-${KUBERNETES_VERSION}/${KUBERNETES_DESCRIPTOR_VERSION}/kubernetes-java-${KUBERNETES_VERSION}-${KUBERNETES_DESCRIPTOR_VERSION}-`; | ||
const declarationTs = `${baseUrl}d.ts`; | ||
const packageJson = `${baseUrl}package.json`; | ||
async function download(url, target) { | ||
const response = await fetch(url); | ||
if (response.status != 200) { | ||
throw new Error(`Invalid download: HTTP ${response.status}`); | ||
} | ||
const out = fs.createWriteStream(target); | ||
await new Promise((resolve, reject) => { | ||
res.body.pipe(out); | ||
res.body.on('error', reject); | ||
fileStream.on('finish', resolve); | ||
}); | ||
} | ||
mkdirSync(TARGET, { recursive: true }); | ||
const dTsName = declarationTs.substring(declarationTs.lastIndexOf('/') + 1); | ||
download(declarationTs, `${TARGET}/${dTsName.substring(0, dTsName.length - KUBERNETES_DESCRIPTOR_VERSION.length - '--d.ts'.length)}.d.ts`); | ||
download(packageJson, `${TARGET}/package.json`); | ||
---- | ||
|
||
Now just execute `npm i` and you will be set up. | ||
|
||
To start generating kubernetes descriptors, just create a script `index.ts` you will execute with `npm run generate`. | ||
To do it we first add this script in our `package.json`: | ||
|
||
[source,json] | ||
.package.json | ||
---- | ||
{ | ||
"scripts": { | ||
"postinstall": "node build/kubernetes-descriptors.install.mjs", | ||
"generate": "npx ts-node index.ts" <1> | ||
}, | ||
// ... as before | ||
} | ||
---- | ||
<.> The new command we add to generate the descriptors. | ||
|
||
And the script itself will import the model to modelize the descriptors and will serialize the model as JSON: | ||
|
||
[source,js] | ||
.index.ts | ||
---- | ||
import { writeFileSync, mkdirSync } from 'fs'; | ||
import { io } from 'kubernetes-descriptors'; // <1> | ||
import v1 = io.yupiik.kubernetes.bindings.v1_9_8.v1; // <2> | ||
const deployment = <v1.Deployment>{ // <3> | ||
apiVersion: 'v1', | ||
kind: 'Deployment', | ||
spec: { | ||
template: { | ||
metadata: { | ||
name: 'my-deployment', | ||
}, | ||
spec: { | ||
containers: [ | ||
{ | ||
name: 'hello-world', | ||
image: 'hello-world', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
// <4> | ||
const target = 'dist/'; | ||
const bundlebeeDescriptors = `${target}/bundlebee/kubernetes`; | ||
mkdirSync(bundlebeeDescriptors, { recursive: true }); | ||
writeFileSync(`${bundlebeeDescriptors}/deployment.json`, JSON.stringify(deployment, null, 2)); | ||
---- | ||
<.> Import the root namespace of the descriptors, | ||
<.> Alias (to be shorter) the actual namespace we target (the version in general), | ||
<.> Create a model matching the expected type (here `Deployment`), this is what gives us completion and validation, | ||
<.> Just export the model writing the model (`deployment`) as JSON (thanks `JSON.stringify`) on the filesystem. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.