Skip to content

Commit

Permalink
pubdoc script dev started
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 14, 2023
1 parent 29ff54f commit aae1026
Show file tree
Hide file tree
Showing 10 changed files with 611 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ packages/project-editor/flow/runtime/cpp/lvgl-runtime/build
npm-module/packages
npm-module/libs
npm-module/resources

tools/pubdoc/build
42 changes: 15 additions & 27 deletions docs/components/html/en-US/Animate.html

Large diffs are not rendered by default.

286 changes: 278 additions & 8 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
"dist-raspbian": "USE_SYSTEM_FPM=true npm run dist",
"tsc-version": "tsc --version",
"electron-version": "electron --version",
"publish-types": "cd packages/eez-studio-types && npm publish"
"publish-types": "cd packages/eez-studio-types && npm publish",
"pubdoc-dev": "cd tools/pubdoc && tsc && cd build && node pubdoc.js --config ../config/dev.json",
"pubdoc-prod": "cd tools/pubdoc && tsc && cd build && node pubdoc.js --config ../config/prod.json"
},
"prettier": {
"printWidth": 80,
Expand Down Expand Up @@ -73,6 +75,7 @@
"@types/serialport": "^8.0.2",
"@types/sha256": "^0.2.0",
"@types/tinycolor2": "^1.4.3",
"@types/wpapi": "^1.1.1",
"@types/xml-formatter": "^2.1.1",
"@types/xterm": "^3.0.0",
"electron": "20.3.8",
Expand All @@ -91,7 +94,9 @@
"request": "^2.88.2",
"request-promise-native": "^1.0.8",
"run-script-os": "^1.1.6",
"typescript": "^4.9.5"
"sha256": "^0.2.0",
"typescript": "^4.9.5",
"wpapi": "^1.2.2"
},
"dependencies": {
"@electron/remote": "^2.0.9",
Expand Down Expand Up @@ -148,7 +153,6 @@
"react-window": "^1.8.8",
"rimraf": "^3.0.2",
"serialport": "^10.4.0",
"sha256": "^0.2.0",
"showdown": "^1.9.1",
"simple-git": "^3.10.0",
"tinycolor2": "^1.4.1",
Expand Down
40 changes: 40 additions & 0 deletions tools/pubdoc/config/dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"server": {
"address": "https://dev.envox.eu/wp-json",
"username": "mvladic"
},
"filePathBase": "../../../docs/components/html/en-US/",
"toc": {
"action": [
{
"category": {
"name": "Common",
"id": 205
},
"articles": [
{
"content": "Start.html",
"status": "publish",
"id": 354620,
"sha256": "da616a3ef4fbbabf2909e056dd46f7c0013518abeb6623c6be6f5065ae435819"
}
]
},
{
"category": {
"name": "GUI",
"id": 207
},
"articles": [
{
"content": "Animate.html",
"status": "publish",
"id": 354619,
"sha256": "3ee610da52ed83731e4bdfdabf1985d1ae36a768d96aa322eddb73c9580c6ee6"
}
]
}
],
"widget": []
}
}
Empty file added tools/pubdoc/config/prod.json
Empty file.
107 changes: 107 additions & 0 deletions tools/pubdoc/pubdoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { argv } from "node:process";
import fs from "fs";
import path from "path";
import { WPClient } from "./wp-client";
import { Config } from "./types";

var sha256 = require("sha256");

let configFilePath;

// print process.argv
for (let i = 2; i < argv.length; ) {
if (argv[i] === "--config") {
configFilePath = argv[i + 1];
i += 2;
} else {
i++;
}
}

if (!configFilePath) {
console.error("Missing --config parameter");
console.error(`Windows PowerShell: $env:WP_PASSWORD="password"`);
process.exit(1);
}

const password = process.env.WP_PASSWORD;
if (!password) {
console.log("Missing WP_PASSWORD environment variable");
process.exit(1);
}

(async () => {
let configFile = await fs.promises.readFile(configFilePath, "utf8");
let config: Config = JSON.parse(configFile);

const wpClient = new WPClient(config.server, password);

for (const key in config.toc) {
for (let group of config.toc[key]) {
for (let article of group.articles) {
const content = await fs.promises.readFile(
__dirname +
"/" +
path.dirname(configFilePath) +
"/" +
config.filePathBase +
article.content,
"utf8"
);

const title = path.basename(article.content, ".html");

const slug =
key +
"-" +
group.category.name.toLowerCase() +
"-" +
title.toLowerCase().replace(/ /g, "-");

const postsha256 = sha256(
content + title + slug + article.status
);

if (!article.id) {
console.log(`Creating article: ${title}`);
try {
const id = await wpClient.createArticle(
title,
content,
slug,
group.category.id,
article.status
);
article.id = id;
article.sha256 = postsha256;
} catch (err) {
console.error(err);
}
} else {
if (article.sha256 !== postsha256) {
try {
console.log(`Updating article: ${title}`);
await wpClient.updateArticle(
article.id,
title,
content,
slug,
group.category.id,
article.status
);
article.sha256 = postsha256;
} catch (err) {
console.error(err);
}
}
}
}
}
}

await fs.promises.writeFile(
configFilePath,
JSON.stringify(config, null, 4),
"utf-8"
);
})();
21 changes: 21 additions & 0 deletions tools/pubdoc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"strictNullChecks": true,
"baseUrl": "../src",
"sourceMap": false,
"outDir": "./build",
"pretty": false,
"skipLibCheck": true
}
}
29 changes: 29 additions & 0 deletions tools/pubdoc/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface Config {
server: {
address: string;
username: string;
};

filePathBase: string;

toc: {
[key: string]: [
{
category: {
name: string;
id: number;
};

articles: {
content: string;
status: string;

sha256?: string;
id?: number;
}[];
}
];
};
}

export type Server = Config["server"];
112 changes: 112 additions & 0 deletions tools/pubdoc/wp-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import WPAPI from "wpapi";
import { Server } from "./types";

export class WPClient {
username = "mvladic";
password = "txA79bc12";

wp: WPAPI;

constructor(private server: Server, password: string) {
this.wp = new WPAPI({
endpoint: this.server.address,
username: this.server.username,
password: password
});

// this.wp.categories().then(result => console.log(result));

this.wp.epkb_post_type_2 = this.wp.registerRoute(
"wp/v2",
"/epkb_post_type_2/(?P<id>)"
);

//this.wp.epkb_post_type_2().then((result: any) => console.log(result));

// this.wp
// .epkb_post_type_2()
// .id(354614)
// .then((result: any) => console.log(result));
}

async createPage(title: string, content: string, slug: string) {
return new Promise<number>(resolve => {
this.wp
.pages()
.create({
// "title" and "content" are the only required properties
title,
content,
slug
})
.then(function (page) {
resolve(page.id);
});
});
}

async updatePage(id: number, title: string, content: string, slug: string) {
return new Promise<void>(resolve => {
this.wp
.pages()
.id(id)
.update({
title,
content,
slug
})
.then(function (response) {
resolve();
});
});
}

async createArticle(
title: string,
content: string,
slug: string,
category: number,
status: string
) {
return new Promise<number>(resolve => {
this.wp
.epkb_post_type_2()
.create({
// "title" and "content" are the only required properties
title,
content,
slug,
epkb_post_type_2_category: [category],
status
})
.then((post: any) => {
resolve(post.id);
});
});
}

async updateArticle(
id: number,
title: string,
content: string,
slug: string,
category: number,
status: string
) {
return new Promise<void>(resolve => {
this.wp
.epkb_post_type_2()
.id(id)
.update({
title,
content,
slug,
epkb_post_type_2_category: [category],
status
})
.then((post: any) => {
resolve();
});
});
}
}

0 comments on commit aae1026

Please sign in to comment.