forked from ourongxing/ohmymn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.ts
146 lines (132 loc) · 3.25 KB
/
build.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { build } from "@ourongxing/estrella"
import type { Plugin } from "esbuild"
import mainfest from "./mainfest"
import copy from "esbuild-plugin-mxn-copy"
import autoImport from "unplugin-auto-import/esbuild"
import { homedir } from "os"
import fs from "fs-extra"
import { exec } from "child_process"
const isProd = process.env.NODE_ENV === "production"
const bannerText = `
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBuild
MIT License
Copyright (c) 2022 MarginNote
If you want to view the source code, please visit the github repository.
Github: ${mainfest.github}
Welcome to contribute to this project!
*/
try {
`
const footerText = `
} catch (e) {
Application.sharedInstance().alert("${mainfest.title}-"+String(e))
}
`
const enum OutDirType {
MNE,
MN,
ConsoleMN
}
const outDir = isProd
? "./dist/"
: [
homedir() +
`/Library/Containers/QReader.MarginStudy.easy/Data/Library/MarginNote Extensions/marginnote.extension.${mainfest.key}/`,
homedir() +
`/Library/Containers/QReader.MarginStudyMac/Data/Library/MarginNote Extensions/marginnote.extension.${mainfest.key}/`,
homedir() +
`/Library/MarginNote Extensions/marginnote.extension.${mainfest.key}/`
][OutDirType.MN]
function clear(): Plugin {
return {
name: "Clear",
setup(build) {
build.onStart(() => {
if (fs.existsSync(outDir)) {
fs.rmSync(outDir, { recursive: true })
}
})
}
}
}
function zip(): Plugin {
return {
name: "Zip",
setup(build) {
build.onEnd(() => {
if (isProd && fs.existsSync(outDir)) {
const fileName = `${mainfest.key} v${mainfest.version} ${
mainfest.certKey ? "signed" : "unsigned"
}${
mainfest.files.find(k => k.includes("AutoCompleteData"))
? " local database autocomplete"
: ""
}`.replace(/[ .]/g, "_")
exec(`cd ${outDir} && zip -qr ${fileName}.mnaddon *`)
console.log(`Generated at ${outDir}${fileName}.mnaddon`)
}
})
}
}
}
function genMainfest(): Plugin {
return {
name: "GenMainfest",
setup(build) {
build.onEnd(() => {
const mnaddon = {
addonid: `marginnote.extension.${mainfest.key}`,
// author: mainfest.author,
author: "MN(ourongxing",
title: mainfest.title,
version: mainfest.version,
marginnote_version_min: mainfest.minMarginNoteVersion,
cert_key: ""
}
if (fs.existsSync(outDir))
fs.writeJSONSync(outDir + "mnaddon.json", mnaddon, { spaces: 2 })
})
}
}
}
const plugins: Plugin[] = [
clear(),
autoImport({
imports: [
{
marginnote: ["dev", "MN"]
}
],
dts: false
}),
mainfest.files?.length &&
copy({
copy: mainfest.files.map(k => ({
from: k,
to: outDir
}))
}),
genMainfest(),
isProd && zip()
].filter(k => k)
build({
entry: "src/main.ts",
tslint: !isProd,
outfile: outDir + "main.js",
splitting: false,
sourcemap: false,
clear: true,
watch: !isProd,
minify: isProd,
banner: {
js: bannerText
},
footer: {
js: footerText
},
pure: ["dev.log", "dev.error", "dev.assert"],
bundle: true,
target: "safari13",
plugins
})