-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.ts
106 lines (101 loc) · 3.45 KB
/
convert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { flags, SfdxCommand } from "@salesforce/command";
import { Messages } from "@salesforce/core";
import { join } from "path";
import * as fs from "fs";
import * as uuidv4 from "uuid/v4";
import * as Papa from "papaparse";
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages("sfdx-dmg-plugin", "knowledge");
export default class KnowledgeConversion extends SfdxCommand {
public static description = messages.getMessage("convert.description");
public static examples = [];
public static readonly flagsConfig = {
source: flags.string({
char: "s",
description: messages.getMessage("convert.flags.source"),
required: true,
}),
target: flags.string({
char: "t",
description: messages.getMessage("convert.flags.target"),
required: true,
}),
htmlcolumns: flags.string({
char: "h",
description: messages.getMessage("convert.flags.htmlcolumns"),
}),
filenamecolumn: flags.string({
char: "f",
description: messages.getMessage("convert.flags.filenamecolumn"),
}),
base64column: flags.string({
char: "c",
description: messages.getMessage("convert.flags.base64column"),
}),
};
public async run(): Promise<any> {
const source = this.flags.source;
const target = this.flags.target;
let htmlcolumns = [];
if (this.flags.htmlcolumns && this.flags.htmlcolumns.includes(",")) {
htmlcolumns = this.flags.htmlcolumns.split(",");
} else if (this.flags.htmlcolumns) {
htmlcolumns.push(this.flags.htmlcolumns);
}
const filenamecolumn = this.flags.filenamecolumn;
const base64column = this.flags.base64column;
const sourceFile = fs.createReadStream(source);
let count = 0;
let targetJson = {
meta: {},
data: [],
};
await fs.promises.mkdir(join(target, "attachments"), { recursive: true });
await fs.promises.mkdir(join(target, "html"), { recursive: true });
Papa.parse(sourceFile, {
worker: true,
header: true,
step: function (result) {
targetJson.meta = result.meta;
let csvRow = result.data;
htmlcolumns.forEach(function (htmlheader) {
if (result.data[htmlheader]) {
let htmFileName = uuidv4().replace(/-/g, "") + ".htm";
fs.writeFileSync(
join(target, "html", htmFileName),
result.data[htmlheader]
);
csvRow[htmlheader] = join("html", htmFileName);
}
});
if (
filenamecolumn &&
base64column &&
result.data[base64column] &&
result.data[filenamecolumn]
) {
fs.writeFileSync(
join(target, "attachments", result.data[filenamecolumn]),
result.data[base64column],
"base64"
);
csvRow[base64column] = join(
"attachments",
result.data[filenamecolumn]
);
}
targetJson.data.push(csvRow);
count++;
},
complete: function (results, file) {
const targetCsv = Papa.unparse(targetJson);
fs.writeFileSync(join(target, "knowledge.csv"), targetCsv);
console.log("Processed", count, "rows.");
},
});
return;
}
}