-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
611 additions
and
38 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,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.
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,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" | ||
); | ||
})(); |
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,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 | ||
} | ||
} |
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,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"]; |
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,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(); | ||
}); | ||
}); | ||
} | ||
} |