@uns-kit/core 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/app-config.d.ts +177 -0
- package/dist/app-config.js +1 -0
- package/dist/base-path.d.ts +1 -0
- package/dist/base-path.js +5 -0
- package/dist/config/project.config.extension.d.ts +3 -0
- package/dist/config/project.config.extension.js +3 -0
- package/dist/config-file.d.ts +12 -0
- package/dist/config-file.js +44 -0
- package/dist/examples/data-example.d.ts +1 -0
- package/dist/examples/data-example.js +44 -0
- package/dist/examples/load-test-data.d.ts +1 -0
- package/dist/examples/load-test-data.js +72 -0
- package/dist/examples/table-example.d.ts +4 -0
- package/dist/examples/table-example.js +52 -0
- package/dist/examples/uns-gateway.d.ts +1 -0
- package/dist/examples/uns-gateway.js +5 -0
- package/dist/graphql/schema.d.ts +377 -0
- package/dist/graphql/schema.js +13 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/logger.d.ts +2 -0
- package/dist/logger.js +18 -0
- package/dist/tools/auth/auth-client.d.ts +23 -0
- package/dist/tools/auth/auth-client.js +172 -0
- package/dist/tools/auth/index.d.ts +1 -0
- package/dist/tools/auth/index.js +1 -0
- package/dist/tools/auth/secure-store.d.ts +17 -0
- package/dist/tools/auth/secure-store.js +110 -0
- package/dist/tools/base-path.d.ts +1 -0
- package/dist/tools/base-path.js +5 -0
- package/dist/tools/generate-config-schema.d.ts +1 -0
- package/dist/tools/generate-config-schema.js +23 -0
- package/dist/tools/initialize.d.ts +1 -0
- package/dist/tools/initialize.js +103 -0
- package/dist/tools/make.d.ts +1 -0
- package/dist/tools/make.js +27 -0
- package/dist/tools/pull-request.d.ts +1 -0
- package/dist/tools/pull-request.js +157 -0
- package/dist/tools/refresh-uns.d.ts +1 -0
- package/dist/tools/refresh-uns.js +109 -0
- package/dist/tools/schema.d.ts +208 -0
- package/dist/tools/schema.js +1 -0
- package/dist/tools/update-rtt.d.ts +1 -0
- package/dist/tools/update-rtt.js +169 -0
- package/dist/tools/update-tools.d.ts +1 -0
- package/dist/tools/update-tools.js +72 -0
- package/dist/uns/handover-manager-event-emitter.d.ts +6 -0
- package/dist/uns/handover-manager-event-emitter.js +19 -0
- package/dist/uns/handover-manager.d.ts +34 -0
- package/dist/uns/handover-manager.js +227 -0
- package/dist/uns/process-config.d.ts +10 -0
- package/dist/uns/process-config.js +12 -0
- package/dist/uns/process-name-service.d.ts +7 -0
- package/dist/uns/process-name-service.js +28 -0
- package/dist/uns/status-monitor.d.ts +35 -0
- package/dist/uns/status-monitor.js +82 -0
- package/dist/uns/uns-event-emitter.d.ts +6 -0
- package/dist/uns/uns-event-emitter.js +19 -0
- package/dist/uns/uns-interfaces.d.ts +156 -0
- package/dist/uns/uns-interfaces.js +5 -0
- package/dist/uns/uns-measurements.d.ts +95 -0
- package/dist/uns/uns-measurements.js +146 -0
- package/dist/uns/uns-packet.d.ts +28 -0
- package/dist/uns/uns-packet.js +223 -0
- package/dist/uns/uns-proxy-process.d.ts +56 -0
- package/dist/uns/uns-proxy-process.js +179 -0
- package/dist/uns/uns-proxy.d.ts +31 -0
- package/dist/uns/uns-proxy.js +120 -0
- package/dist/uns/uns-tags.d.ts +1 -0
- package/dist/uns/uns-tags.js +1 -0
- package/dist/uns/uns-topic-matcher.d.ts +9 -0
- package/dist/uns/uns-topic-matcher.js +34 -0
- package/dist/uns/uns-topics.d.ts +1 -0
- package/dist/uns/uns-topics.js +1 -0
- package/dist/uns-config/config-schema.d.ts +7 -0
- package/dist/uns-config/config-schema.js +5 -0
- package/dist/uns-config/host-placeholders.d.ts +139 -0
- package/dist/uns-config/host-placeholders.js +70 -0
- package/dist/uns-config/schema-tolls.d.ts +2 -0
- package/dist/uns-config/schema-tolls.js +4 -0
- package/dist/uns-config/schema-tools.d.ts +2 -0
- package/dist/uns-config/schema-tools.js +18 -0
- package/dist/uns-config/secret-placeholders.d.ts +128 -0
- package/dist/uns-config/secret-placeholders.js +64 -0
- package/dist/uns-config/secret-resolver.d.ts +77 -0
- package/dist/uns-config/secret-resolver.js +285 -0
- package/dist/uns-config/uns-core-schema.d.ts +705 -0
- package/dist/uns-config/uns-core-schema.js +25 -0
- package/dist/uns-grpc/uns-gateway-cli.d.ts +2 -0
- package/dist/uns-grpc/uns-gateway-cli.js +32 -0
- package/dist/uns-grpc/uns-gateway-server.d.ts +47 -0
- package/dist/uns-grpc/uns-gateway-server.js +424 -0
- package/dist/uns-mqtt/mqtt-interfaces.d.ts +22 -0
- package/dist/uns-mqtt/mqtt-interfaces.js +1 -0
- package/dist/uns-mqtt/mqtt-proxy.d.ts +34 -0
- package/dist/uns-mqtt/mqtt-proxy.js +245 -0
- package/dist/uns-mqtt/mqtt-topic-builder.d.ts +51 -0
- package/dist/uns-mqtt/mqtt-topic-builder.js +70 -0
- package/dist/uns-mqtt/mqtt-worker-init.d.ts +1 -0
- package/dist/uns-mqtt/mqtt-worker-init.js +5 -0
- package/dist/uns-mqtt/mqtt-worker.d.ts +20 -0
- package/dist/uns-mqtt/mqtt-worker.js +120 -0
- package/dist/uns-mqtt/throttled-queue.d.ts +166 -0
- package/dist/uns-mqtt/throttled-queue.js +388 -0
- package/dist/uns-mqtt/uns-mqtt-proxy.d.ts +107 -0
- package/dist/uns-mqtt/uns-mqtt-proxy.js +349 -0
- package/dist/uns-mqtt/ws-proxy.d.ts +38 -0
- package/dist/uns-mqtt/ws-proxy.js +86 -0
- package/package.json +48 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
/**
|
|
5
|
+
* Try to lazily import keytar without making it a hard dependency.
|
|
6
|
+
*/
|
|
7
|
+
async function tryLoadKeytar() {
|
|
8
|
+
try {
|
|
9
|
+
// Avoid static import to prevent type resolution errors when not installed
|
|
10
|
+
const dynamicImport = new Function("m", "return import(m)");
|
|
11
|
+
const mod = await dynamicImport("keytar");
|
|
12
|
+
return (mod && (mod.default || mod)) ?? null;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class KeytarStore {
|
|
19
|
+
service;
|
|
20
|
+
keytar;
|
|
21
|
+
constructor(service, keytar) {
|
|
22
|
+
this.service = service;
|
|
23
|
+
this.keytar = keytar;
|
|
24
|
+
}
|
|
25
|
+
async get(key) {
|
|
26
|
+
return (await this.keytar.getPassword(this.service, key)) ?? null;
|
|
27
|
+
}
|
|
28
|
+
async set(key, value) {
|
|
29
|
+
await this.keytar.setPassword(this.service, key, value);
|
|
30
|
+
}
|
|
31
|
+
async del(key) {
|
|
32
|
+
await this.keytar.deletePassword(this.service, key);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class FileStore {
|
|
36
|
+
filePath;
|
|
37
|
+
constructor(namespace) {
|
|
38
|
+
const baseDir = FileStore.resolveBaseDir();
|
|
39
|
+
const folder = path.join(baseDir, "uns-auth");
|
|
40
|
+
if (!fs.existsSync(folder)) {
|
|
41
|
+
fs.mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
42
|
+
}
|
|
43
|
+
this.filePath = path.join(folder, `${FileStore.sanitize(namespace)}.json`);
|
|
44
|
+
if (!fs.existsSync(this.filePath)) {
|
|
45
|
+
fs.writeFileSync(this.filePath, "{}", { mode: 0o600 });
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
// tighten permissions if possible (no-op on Windows)
|
|
49
|
+
fs.chmodSync(this.filePath, 0o600);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// ignore
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
static resolveBaseDir() {
|
|
56
|
+
if (process.platform === "win32") {
|
|
57
|
+
return process.env.APPDATA || path.join(os.homedir(), "AppData", "Roaming");
|
|
58
|
+
}
|
|
59
|
+
// Linux and macOS default to XDG_CONFIG_HOME or ~/.config
|
|
60
|
+
return process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config");
|
|
61
|
+
}
|
|
62
|
+
static sanitize(name) {
|
|
63
|
+
return name.replace(/[^a-zA-Z0-9_.-]/g, "_");
|
|
64
|
+
}
|
|
65
|
+
async load() {
|
|
66
|
+
try {
|
|
67
|
+
const raw = await fs.promises.readFile(this.filePath, "utf-8");
|
|
68
|
+
return JSON.parse(raw || "{}");
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async save(data) {
|
|
75
|
+
const json = JSON.stringify(data, null, 2);
|
|
76
|
+
await fs.promises.writeFile(this.filePath, json, { mode: 0o600 });
|
|
77
|
+
try {
|
|
78
|
+
fs.chmodSync(this.filePath, 0o600);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// ignore
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async get(key) {
|
|
85
|
+
const data = await this.load();
|
|
86
|
+
return data[key] ?? null;
|
|
87
|
+
}
|
|
88
|
+
async set(key, value) {
|
|
89
|
+
const data = await this.load();
|
|
90
|
+
data[key] = value;
|
|
91
|
+
await this.save(data);
|
|
92
|
+
}
|
|
93
|
+
async del(key) {
|
|
94
|
+
const data = await this.load();
|
|
95
|
+
delete data[key];
|
|
96
|
+
await this.save(data);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
export class SecureStoreFactory {
|
|
100
|
+
/**
|
|
101
|
+
* Creates a secure store for a namespace. Tries keytar first; falls back to file store.
|
|
102
|
+
*/
|
|
103
|
+
static async create(namespace) {
|
|
104
|
+
const keytar = await tryLoadKeytar();
|
|
105
|
+
if (keytar) {
|
|
106
|
+
return new KeytarStore(namespace, keytar);
|
|
107
|
+
}
|
|
108
|
+
return new FileStore(namespace);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const basePath: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// scripts/generate-config-artifacts.ts
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
5
|
+
import { zodToTs, printNode } from "zod-to-ts";
|
|
6
|
+
// Import the plain ZodObject (no superRefine)
|
|
7
|
+
import { baseSchema } from "../uns-config/config-schema.js";
|
|
8
|
+
function write(filePath, data) {
|
|
9
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
10
|
+
fs.writeFileSync(filePath, data);
|
|
11
|
+
}
|
|
12
|
+
// 1) JSON Schema for VS Code $schema
|
|
13
|
+
const jsonSchema = zodToJsonSchema(baseSchema, "AppConfig");
|
|
14
|
+
write(path.resolve("config.schema.json"), JSON.stringify(jsonSchema, null, 2));
|
|
15
|
+
// 2) TypeScript `export type AppConfig = {...}`
|
|
16
|
+
const { node } = zodToTs(baseSchema, "AppConfig");
|
|
17
|
+
const tsContent = "/* Auto-generated. Do not edit by hand. */\n" +
|
|
18
|
+
"export type AppConfig = " +
|
|
19
|
+
printNode(node) +
|
|
20
|
+
"\n";
|
|
21
|
+
// Put it wherever you want. You earlier asked for root (beside config-file.ts):
|
|
22
|
+
write(path.resolve("./src/app-config.ts"), tsContent);
|
|
23
|
+
console.log("Generated config.schema.json and app-config.ts");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const packageJsonPath: string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import * as azdev from "azure-devops-node-api";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import { readFile } from "fs/promises";
|
|
4
|
+
import readline from "node:readline";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import { CleanOptions, simpleGit } from "simple-git";
|
|
7
|
+
import fs from "fs-extra";
|
|
8
|
+
import { basePath } from "./base-path.js";
|
|
9
|
+
const git = simpleGit("./").clean(CleanOptions.FORCE);
|
|
10
|
+
export const packageJsonPath = path.join(basePath, "package.json");
|
|
11
|
+
const orgUrl = "https://sijit@dev.azure.com/sijit";
|
|
12
|
+
const rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout,
|
|
15
|
+
});
|
|
16
|
+
let token = "";
|
|
17
|
+
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
|
|
18
|
+
const repoName = packageJson.name;
|
|
19
|
+
async function run() {
|
|
20
|
+
let repos;
|
|
21
|
+
let gitApi;
|
|
22
|
+
const project = "industry40";
|
|
23
|
+
try {
|
|
24
|
+
const authHandler = azdev.getPersonalAccessTokenHandler(token);
|
|
25
|
+
const connection = new azdev.WebApi(orgUrl, authHandler);
|
|
26
|
+
gitApi = await connection.getGitApi();
|
|
27
|
+
repos = await gitApi.getRepositories(project);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.error("Your AZURE_PAT environment variable has probably expired.");
|
|
31
|
+
console.log("Please update your AZURE_PAT environment with a new one on.");
|
|
32
|
+
console.log("You can create a new one at[https://dev.azure.com/sijit/_usersSettings/tokens]");
|
|
33
|
+
}
|
|
34
|
+
if (repos.filter((x) => x.name == repoName).length > 0) {
|
|
35
|
+
console.log(chalk.red.bold(`Error: Repository ${repoName} in project ${project} already exists.`));
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
process.stdout.write(`Create ${repoName} in project ${project}`);
|
|
39
|
+
try {
|
|
40
|
+
await gitApi.createRepository({ name: repoName }, project);
|
|
41
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error(chalk.red.bold(`\n${error}`));
|
|
45
|
+
}
|
|
46
|
+
process.stdout.write(`Initialize local git repository on branch master`);
|
|
47
|
+
try {
|
|
48
|
+
await git.init();
|
|
49
|
+
await git.checkoutLocalBranch("master");
|
|
50
|
+
await git.add(".");
|
|
51
|
+
await git.commit("Initial commit");
|
|
52
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
console.error(chalk.red.bold(`\n${error}`));
|
|
56
|
+
}
|
|
57
|
+
process.stdout.write(`Add remote origin.`);
|
|
58
|
+
try {
|
|
59
|
+
await git.remote([
|
|
60
|
+
"add",
|
|
61
|
+
"origin",
|
|
62
|
+
`git@ssh.dev.azure.com:v3/sijit/industry40/${repoName}`,
|
|
63
|
+
]);
|
|
64
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.error(chalk.red.bold(`\n${error}`));
|
|
68
|
+
}
|
|
69
|
+
process.stdout.write(`Push master to remote`);
|
|
70
|
+
try {
|
|
71
|
+
await git.push("origin", "master");
|
|
72
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error(chalk.red.bold(`\n${error}`));
|
|
76
|
+
}
|
|
77
|
+
process.stdout.write(`Create default config.json from config-template.json`);
|
|
78
|
+
try {
|
|
79
|
+
fs.copyFileSync(path.join(basePath, 'config-template.json'), path.join(basePath, 'config.json'));
|
|
80
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error(chalk.red.bold(`\n${error}`));
|
|
84
|
+
}
|
|
85
|
+
console.log(chalk.green.bold(`\nNow you can add pipeline for ${repoName} in Azure DevOps pipelines for the project ${project}.`));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
console.log(chalk.green.bold(`Create new RTT: ${repoName}\n`));
|
|
89
|
+
const envPat = process.env.AZURE_PAT;
|
|
90
|
+
if (envPat && envPat.length > 10) {
|
|
91
|
+
rl.close();
|
|
92
|
+
console.log("Using PAT from your AZURE_PAT environment");
|
|
93
|
+
token = envPat;
|
|
94
|
+
run();
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
console.log("Could not find AZURE_PAT environment.");
|
|
98
|
+
rl.question(`Please enter your PAT, you can create one at [https://dev.azure.com/sijit/_usersSettings/tokens]: `, (newToken) => {
|
|
99
|
+
rl.close();
|
|
100
|
+
token = newToken;
|
|
101
|
+
run();
|
|
102
|
+
});
|
|
103
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import fs from "fs-extra";
|
|
2
|
+
import { basePath } from "./base-path.js";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { execSync } from 'child_process';
|
|
5
|
+
const binDir = path.join(basePath, 'bin/');
|
|
6
|
+
try {
|
|
7
|
+
const ua = process.env.npm_config_user_agent || "";
|
|
8
|
+
const pm = ua.startsWith("pnpm") ? "pnpm" : ua.startsWith("yarn") ? "yarn" : "npm";
|
|
9
|
+
execSync(`${pm} run build`, { stdio: 'inherit' });
|
|
10
|
+
fs.rmSync(binDir, {
|
|
11
|
+
recursive: true,
|
|
12
|
+
force: true,
|
|
13
|
+
});
|
|
14
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
15
|
+
fs.copyFileSync(path.join(basePath, 'LICENSE'), path.join(binDir, 'LICENSE'));
|
|
16
|
+
fs.copyFileSync(path.join(basePath, 'package.json'), path.join(binDir, 'package.json'));
|
|
17
|
+
fs.copySync(path.join(basePath, 'dist'), path.join(binDir, 'dist/'));
|
|
18
|
+
// After all files are copied, check for ../local-tools/make.js and run it if exists
|
|
19
|
+
const localToolsDir = path.resolve(basePath, 'dist/local-tools');
|
|
20
|
+
const makeJsPath = path.join(localToolsDir, 'make.js');
|
|
21
|
+
if (fs.existsSync(makeJsPath)) {
|
|
22
|
+
execSync(`node "${makeJsPath}"`, { stdio: 'inherit' });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.log(`error: ${error}`);
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as azdev from "azure-devops-node-api";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
import { execSync } from "child_process";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import { readFile } from "fs/promises";
|
|
6
|
+
import readline from "node:readline";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as prettier from "prettier";
|
|
9
|
+
import { CleanOptions, simpleGit } from "simple-git";
|
|
10
|
+
import util from "util";
|
|
11
|
+
import { basePath } from "../base-path.js";
|
|
12
|
+
const rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout,
|
|
15
|
+
});
|
|
16
|
+
const question = util.promisify(rl.question).bind(rl);
|
|
17
|
+
const git = simpleGit("./").clean(CleanOptions.FORCE);
|
|
18
|
+
const packageJsonPath = path.join(basePath, "package.json");
|
|
19
|
+
const unsLibraryPath = path.join(basePath, "uns-library.json");
|
|
20
|
+
const orgUrl = "https://sijit@dev.azure.com/sijit";
|
|
21
|
+
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf8"));
|
|
22
|
+
const repoName = packageJson.name;
|
|
23
|
+
const version = packageJson.version;
|
|
24
|
+
const branches = await git.branchLocal();
|
|
25
|
+
const currentBranch = branches.current;
|
|
26
|
+
const gitStatus = await git.status();
|
|
27
|
+
let token = "";
|
|
28
|
+
try {
|
|
29
|
+
await main();
|
|
30
|
+
rl.close();
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.log(chalk.red.bold(`\n${error}`));
|
|
34
|
+
rl.close();
|
|
35
|
+
}
|
|
36
|
+
async function main() {
|
|
37
|
+
if (!gitStatus.isClean()) {
|
|
38
|
+
throw new Error(`Repository needs to be clean. Please commit or stash the changes.`);
|
|
39
|
+
}
|
|
40
|
+
else if (currentBranch == "master") {
|
|
41
|
+
throw new Error(`You can not create pull request from master to master.`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
const envPat = process.env.AZURE_PAT;
|
|
45
|
+
while (!token) {
|
|
46
|
+
if (envPat && envPat.length > 10) {
|
|
47
|
+
token = envPat;
|
|
48
|
+
const authHandler = azdev.getPersonalAccessTokenHandler(token);
|
|
49
|
+
const connection = new azdev.WebApi(orgUrl, authHandler);
|
|
50
|
+
try {
|
|
51
|
+
console.log("Using PAT from your AZURE_PAT environment");
|
|
52
|
+
await connection.connect();
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
console.log("The provided PAT is invalid or expired. Please provide a valid PAT.");
|
|
56
|
+
token = "";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!token) {
|
|
60
|
+
token = await question(`Please enter your PAT, you can create one at [https://dev.azure.com/sijit/_usersSettings/tokens]: `);
|
|
61
|
+
const authHandler = azdev.getPersonalAccessTokenHandler(token);
|
|
62
|
+
const connection = new azdev.WebApi(orgUrl, authHandler);
|
|
63
|
+
try {
|
|
64
|
+
await connection.connect();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
console.log("The provided PAT is invalid or expired. Please provide a valid PAT.");
|
|
68
|
+
token = "";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const version = await getVersion();
|
|
73
|
+
await setVersion(version);
|
|
74
|
+
await runMake();
|
|
75
|
+
await commitChanges(version);
|
|
76
|
+
await pushChanges();
|
|
77
|
+
await createPullRequest(version);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function getVersion() {
|
|
81
|
+
let versionExists = true;
|
|
82
|
+
let newVersion = version;
|
|
83
|
+
while (versionExists) {
|
|
84
|
+
newVersion = await question(`Every PR needs a unique version, please accept current version or enter a new one [${version}]: `) || version;
|
|
85
|
+
const remoteTags = await git.listRemote(["--tags"]);
|
|
86
|
+
if (remoteTags.indexOf(`refs/tags/${newVersion}`) > 0) {
|
|
87
|
+
console.log(chalk.bold.red(`Version ${newVersion} already exists on the server`));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
console.log(`Using version ${newVersion}`);
|
|
91
|
+
versionExists = false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return newVersion;
|
|
95
|
+
}
|
|
96
|
+
async function runMake() {
|
|
97
|
+
execSync("npm run make");
|
|
98
|
+
}
|
|
99
|
+
async function commitChanges(newVersion) {
|
|
100
|
+
process.stdout.write(`Commit changes to branch ${currentBranch} `);
|
|
101
|
+
await git.add(".");
|
|
102
|
+
await git.commit(`Set new production version: ${newVersion}`);
|
|
103
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
104
|
+
}
|
|
105
|
+
async function pushChanges() {
|
|
106
|
+
process.stdout.write(`Push changes to remote branch ${currentBranch} `);
|
|
107
|
+
await git.push("origin", currentBranch);
|
|
108
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
109
|
+
}
|
|
110
|
+
async function setVersion(newVersion) {
|
|
111
|
+
packageJson.version = newVersion;
|
|
112
|
+
const docString = await prettier.format(JSON.stringify(packageJson), {
|
|
113
|
+
parser: "json",
|
|
114
|
+
});
|
|
115
|
+
fs.writeFileSync("package.json", docString, "utf8");
|
|
116
|
+
if (repoName === "template-uns-rtt") {
|
|
117
|
+
try {
|
|
118
|
+
const unsLibraryFile = await readFile(unsLibraryPath, "utf8");
|
|
119
|
+
const unsLibrary = JSON.parse(unsLibraryFile);
|
|
120
|
+
unsLibrary.version = newVersion;
|
|
121
|
+
const unsLibraryDoc = await prettier.format(JSON.stringify(unsLibrary), {
|
|
122
|
+
parser: "json",
|
|
123
|
+
});
|
|
124
|
+
fs.writeFileSync(unsLibraryPath, unsLibraryDoc, "utf8");
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error.code === "ENOENT") {
|
|
128
|
+
const unsLibraryDoc = await prettier.format(JSON.stringify({ name: repoName, version: newVersion }), { parser: "json" });
|
|
129
|
+
fs.writeFileSync(unsLibraryPath, unsLibraryDoc, "utf8");
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function createPullRequest(tag) {
|
|
138
|
+
const title = await question(`Title for the pull request: `);
|
|
139
|
+
const description = await question(`Description for the pull request: `);
|
|
140
|
+
process.stdout.write(`Create new pull request from ${currentBranch} to master `);
|
|
141
|
+
const authHandler = azdev.getPersonalAccessTokenHandler(token);
|
|
142
|
+
const connection = new azdev.WebApi(orgUrl, authHandler);
|
|
143
|
+
const gitApi = await connection.getGitApi();
|
|
144
|
+
const project = "industry40";
|
|
145
|
+
const repos = await gitApi.getRepositories(project);
|
|
146
|
+
const repoId = repos.filter((x) => x.name == repoName)[0].id;
|
|
147
|
+
const gitPullRequestToCreate = {
|
|
148
|
+
sourceRefName: `refs/heads/${currentBranch}`,
|
|
149
|
+
targetRefName: "refs/heads/master",
|
|
150
|
+
title,
|
|
151
|
+
description,
|
|
152
|
+
labels: [{ active: true, name: tag }]
|
|
153
|
+
};
|
|
154
|
+
gitApi.createPullRequest(gitPullRequestToCreate, repoId, project);
|
|
155
|
+
console.log(chalk.green.bold(` ... OK`));
|
|
156
|
+
console.log(`Pull request created at ` + chalk.green.bold(`[https://dev.azure.com/sijit/${project}/_git/${repoName}/pullrequests]`));
|
|
157
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { GraphQLClient, gql, ClientError } from "graphql-request";
|
|
2
|
+
import { basePath } from "./base-path.js";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import { ConfigFile } from "../config-file.js";
|
|
6
|
+
import { AuthClient } from "./auth/index.js";
|
|
7
|
+
const config = await ConfigFile.loadConfig();
|
|
8
|
+
// Helper function to write content to a file
|
|
9
|
+
async function writeToFile(filePath, content) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
fs.writeFile(filePath, content, (error) => {
|
|
12
|
+
if (error) {
|
|
13
|
+
console.error('Error writing file:', error);
|
|
14
|
+
return reject(error);
|
|
15
|
+
}
|
|
16
|
+
console.log(`${path.basename(filePath)} updated.`);
|
|
17
|
+
resolve();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
// Function to process tree structure recursively
|
|
22
|
+
function generateUnsTopics(tree, currentPath) {
|
|
23
|
+
return tree
|
|
24
|
+
.map(element => {
|
|
25
|
+
const newPath = `${currentPath}${element.unsNode}/`;
|
|
26
|
+
const childrenTopics = element.children?.length
|
|
27
|
+
? generateUnsTopics(element.children, newPath)
|
|
28
|
+
: '';
|
|
29
|
+
return `"${newPath}" |${childrenTopics}`;
|
|
30
|
+
})
|
|
31
|
+
.join('');
|
|
32
|
+
}
|
|
33
|
+
// Prepare GraphQL client with Authorization from auth library
|
|
34
|
+
const auth = await AuthClient.create();
|
|
35
|
+
let accessToken = await auth.getAccessToken();
|
|
36
|
+
let client = new GraphQLClient(config.uns.graphql, {
|
|
37
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
38
|
+
});
|
|
39
|
+
async function requestWithAuth(document) {
|
|
40
|
+
try {
|
|
41
|
+
return await client.request(document);
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
const isAuthErr = err instanceof ClientError && (err.response.status === 401 || err.response.status === 403);
|
|
45
|
+
if (isAuthErr) {
|
|
46
|
+
// Attempt to get a fresh token (will try refresh, then prompt)
|
|
47
|
+
accessToken = await auth.getAccessToken();
|
|
48
|
+
client.setHeader('Authorization', `Bearer ${accessToken}`);
|
|
49
|
+
return await client.request(document);
|
|
50
|
+
}
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Fetch and generate UnsTopics
|
|
55
|
+
async function refreshUnsTopics() {
|
|
56
|
+
const document = gql `
|
|
57
|
+
query GetTreeStructure {
|
|
58
|
+
GetTreeStructure {
|
|
59
|
+
id
|
|
60
|
+
parent
|
|
61
|
+
unsNode
|
|
62
|
+
children {
|
|
63
|
+
id
|
|
64
|
+
parent
|
|
65
|
+
unsNode
|
|
66
|
+
children {
|
|
67
|
+
id
|
|
68
|
+
parent
|
|
69
|
+
unsNode
|
|
70
|
+
children {
|
|
71
|
+
id
|
|
72
|
+
parent
|
|
73
|
+
unsNode
|
|
74
|
+
children {
|
|
75
|
+
id
|
|
76
|
+
parent
|
|
77
|
+
unsNode
|
|
78
|
+
children {
|
|
79
|
+
id
|
|
80
|
+
parent
|
|
81
|
+
unsNode
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}`;
|
|
89
|
+
const query = await requestWithAuth(document);
|
|
90
|
+
const tree = query.GetTreeStructure;
|
|
91
|
+
const vsebina = `export type UnsTopics = ${generateUnsTopics(tree, '')}\n(string & {});`;
|
|
92
|
+
await writeToFile(path.join(basePath, "src/uns/uns-topics.ts"), vsebina);
|
|
93
|
+
}
|
|
94
|
+
// Fetch and generate UnsTags
|
|
95
|
+
async function refreshUnsTags() {
|
|
96
|
+
const document = gql `
|
|
97
|
+
query Query {
|
|
98
|
+
GetTags
|
|
99
|
+
}`;
|
|
100
|
+
const query = await requestWithAuth(document);
|
|
101
|
+
const tags = query.GetTags;
|
|
102
|
+
const vsebina = `export type UnsTags = ${tags.map(tag => `"${tag}" |`).join('')}\n(string & {});`;
|
|
103
|
+
await writeToFile(path.join(basePath, "src/uns/uns-tags.ts"), vsebina);
|
|
104
|
+
}
|
|
105
|
+
// Execute the refresh processes
|
|
106
|
+
async function refresh() {
|
|
107
|
+
await Promise.all([refreshUnsTopics(), refreshUnsTags()]);
|
|
108
|
+
}
|
|
109
|
+
refresh().catch(error => console.error('Error during refresh:', error));
|