/
PackagePlugin.ts
108 lines (94 loc) · 3.4 KB
/
PackagePlugin.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
107
108
import * as Path from "path";
import * as FS from "fs";
import { Component, ConverterComponent } from "../components";
import { Converter } from "../converter";
import { Context } from "../context";
import { BindOption, readFile } from "../../utils";
import { getCommonDirectory } from "../../utils/fs";
import { join } from "path";
/**
* A handler that tries to find the package.json and readme.md files of the
* current project.
*/
@Component({ name: "package" })
export class PackagePlugin extends ConverterComponent {
@BindOption("readme")
readme!: string;
@BindOption("includeVersion")
includeVersion!: boolean;
/**
* The file name of the found readme.md file.
*/
private readmeFile?: string;
/**
* The file name of the found package.json file.
*/
private packageFile?: string;
/**
* Create a new PackageHandler instance.
*/
initialize() {
this.listenTo(this.owner, {
[Converter.EVENT_BEGIN]: this.onBegin,
[Converter.EVENT_RESOLVE_BEGIN]: this.onBeginResolve,
});
}
/**
* Triggered when the converter begins converting a project.
*/
private onBegin(_context: Context) {
this.readmeFile = undefined;
this.packageFile = undefined;
// Path will be resolved already. This is kind of ugly, but...
const noReadmeFile = this.readme == join(process.cwd(), "none");
if (!noReadmeFile && this.readme) {
if (FS.existsSync(this.readme)) {
this.readmeFile = this.readme;
}
}
const packageAndReadmeFound = () =>
(noReadmeFile || this.readmeFile) && this.packageFile;
const reachedTopDirectory = (dirName: string) =>
dirName === Path.resolve(Path.join(dirName, ".."));
let dirName = Path.resolve(
getCommonDirectory(this.application.options.getValue("entryPoints"))
);
this.application.logger.verbose(`Begin readme search at ${dirName}`);
while (!packageAndReadmeFound() && !reachedTopDirectory(dirName)) {
FS.readdirSync(dirName).forEach((file) => {
const lowercaseFileName = file.toLowerCase();
if (
!noReadmeFile &&
!this.readmeFile &&
lowercaseFileName === "readme.md"
) {
this.readmeFile = Path.join(dirName, file);
}
if (!this.packageFile && lowercaseFileName === "package.json") {
this.packageFile = Path.join(dirName, file);
}
});
dirName = Path.resolve(Path.join(dirName, ".."));
}
}
/**
* Triggered when the converter begins resolving a project.
*
* @param context The context object describing the current state the converter is in.
*/
private onBeginResolve(context: Context) {
const project = context.project;
if (this.readmeFile) {
project.readme = readFile(this.readmeFile);
}
if (this.packageFile) {
project.packageInfo = JSON.parse(readFile(this.packageFile));
if (!project.name) {
project.name = String(project.packageInfo.name);
}
if (this.includeVersion) {
project.name = `${project.name} - v${project.packageInfo.version}`;
}
}
}
}