Skip to content

Commit d5ecbdf

Browse files
committed
added auto-font removal
1 parent 48cfe69 commit d5ecbdf

File tree

1 file changed

+77
-25
lines changed

1 file changed

+77
-25
lines changed

packages/nexrender-action-fonts/index.js

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,93 @@ const notifyWin = async (job) => {
5656
execSync(`cscript //nologo ${script}`);
5757
}
5858

59+
const uninstallWin = async (settings, job, fontpath) => {
60+
const fontdir = path.join(process.env.LOCALAPPDATA, "Microsoft", "Windows", "Fonts");
61+
const fontdest = path.join(fontdir, path.basename(fontpath));
62+
63+
settings.logger.log(`[action-fonts] Uninstalling font ${fontdest}...`);
64+
65+
if (fs.existsSync(fontdest)) {
66+
fs.unlinkSync(fontdest);
67+
}
68+
69+
/* remove from registry */
70+
const fontdisplayname = path.basename(fontpath, path.extname(fontpath));
71+
const fontreg = `reg delete "HKCU\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts" /v "${fontdisplayname} (TrueType)" /f`;
72+
73+
execSync(fontreg);
74+
return 1;
75+
}
76+
77+
const uninstallMac = async (settings, job, fontpath) => {
78+
const fontdir = path.join(process.env.HOME, "Library", "Fonts");
79+
const fontdest = path.join(fontdir, path.basename(fontpath));
80+
81+
settings.logger.log(`[action-fonts] Uninstalling font ${fontdest}...`);
82+
83+
if (fs.existsSync(fontdest)) {
84+
fs.unlinkSync(fontdest);
85+
}
86+
}
87+
5988
module.exports = async (job, settings, params, type) => {
60-
if (type != "prerender") {
89+
if (type != "prerender" && type != "postrender") {
6190
throw new Error(
62-
`Action ${name} can be only run in prerender mode, you provided: ${type}.`,
91+
`Action ${name} can be only run in prerender or postrender mode, you provided: ${type}.`,
6392
);
6493
}
6594

66-
let fontsAdded = 0;
67-
68-
/* iterate over assets, and install all assets which are fonts */
69-
for (const asset of job.assets) {
70-
if (asset.type !== "static") {
71-
continue;
72-
}
73-
74-
if (!asset.src.match(/\.(ttf)$/)) {
75-
continue;
95+
/* add this action to postrender if it's not already there, to clean up the fonts after the render */
96+
if (type == "prerender") {
97+
job.actions.postrender.push({
98+
module: __filename,
99+
});
100+
101+
let fontsAdded = 0;
102+
103+
/* iterate over assets, and install all assets which are fonts */
104+
for (const asset of job.assets) {
105+
if (asset.type !== "static") {
106+
continue;
107+
}
108+
109+
if (!asset.src.match(/\.(ttf)$/)) {
110+
continue;
111+
}
112+
113+
if (!asset.name) {
114+
throw new Error(`Asset ${asset.src} has to be named using the "name" property that would contain the font name as it is used to be then used in the After Effets project.`);
115+
}
116+
117+
if (process.platform === "darwin") {
118+
fontsAdded += await installMac(settings, job, asset.dest);
119+
} else if (process.platform === "win32") {
120+
fontsAdded += await installWin(settings, job, asset.dest);
121+
} else {
122+
throw new Error(`Platform ${process.platform} is not supported.`);
123+
}
76124
}
77125

78-
if (!asset.name) {
79-
throw new Error(`Asset ${asset.src} has to be named using the "name" property that would contain the font name as it is used to be then used in the After Effets project.`);
126+
if (fontsAdded > 0 && process.platform === "win32") {
127+
await notifyWin(job);
80128
}
81-
82-
if (process.platform === "darwin") {
83-
fontsAdded += await installMac(settings, job, asset.dest);
84-
} else if (process.platform === "win32") {
85-
fontsAdded += await installWin(settings, job, asset.dest);
86-
} else {
87-
throw new Error(`Platform ${process.platform} is not supported.`);
129+
} else if (type == "postrender") {
130+
for (const asset of job.assets) {
131+
if (asset.type !== "static") {
132+
continue;
133+
}
134+
135+
if (!asset.src.match(/\.(ttf)$/)) {
136+
continue;
137+
}
138+
139+
if (process.platform === "darwin") {
140+
await uninstallMac(settings, job, asset.dest);
141+
} else if (process.platform === "win32") {
142+
await uninstallWin(settings, job, asset.dest);
143+
}
88144
}
89145
}
90146

91-
if (fontsAdded > 0 && process.platform === "win32") {
92-
await notifyWin(job);
93-
}
94-
95147
return job;
96148
};

0 commit comments

Comments
 (0)