2021-05-05 15:55:24 +00:00
|
|
|
import { join } from 'path';
|
2021-04-04 14:52:41 +00:00
|
|
|
import { readFileSync, writeFileSync, mkdirSync, rmSync } from 'fs';
|
2021-05-05 15:55:24 +00:00
|
|
|
|
2021-04-24 17:09:02 +00:00
|
|
|
import sass from 'sass';
|
2021-05-15 21:08:34 +00:00
|
|
|
import glob from 'glob';
|
2021-04-04 14:52:41 +00:00
|
|
|
|
|
|
|
export default (manifestPath, repo) => {
|
2021-05-05 15:55:24 +00:00
|
|
|
const baseDir = join(manifestPath, '..');
|
|
|
|
|
2021-04-04 14:52:41 +00:00
|
|
|
const pcManifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
|
|
|
|
|
|
let manifest = {
|
2021-05-05 15:55:24 +00:00
|
|
|
main: 'index.js',
|
2021-04-04 14:52:41 +00:00
|
|
|
tags: ['port'],
|
|
|
|
|
|
|
|
name: pcManifest.name,
|
|
|
|
description: pcManifest.description,
|
|
|
|
|
|
|
|
version: pcManifest.version,
|
|
|
|
authors: [ pcManifest.author ]
|
|
|
|
};
|
|
|
|
|
2021-04-22 19:45:28 +00:00
|
|
|
let content = readFileSync(pcManifest.main || 'index.js', 'utf8');//.replace(/\\/g, '\\\\').replace(/`/g, '\\`');
|
2021-04-04 14:52:41 +00:00
|
|
|
|
2021-04-24 17:09:02 +00:00
|
|
|
content = content.replace(`module.exports = `, `export default new `);
|
2021-04-22 19:45:28 +00:00
|
|
|
content = content.replace(/this\.loadStylesheet\(['"`](.*)['"`]\)/g, (_, relative) => {
|
2021-04-24 17:09:02 +00:00
|
|
|
const path = manifestPath.split('/').slice(0, -1).concat('').join('/') + relative;
|
2021-04-22 19:45:28 +00:00
|
|
|
|
|
|
|
let css;
|
|
|
|
|
|
|
|
if (path.split('.').pop() === 'scss') {
|
2021-04-24 17:09:02 +00:00
|
|
|
css = (sass.renderSync({ file: path })).css.toString('utf8');
|
2021-04-22 19:45:28 +00:00
|
|
|
} else {
|
2021-04-24 17:09:02 +00:00
|
|
|
css = readFileSync(path, 'utf8');
|
2021-04-22 19:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
css = css.replace(/\\/g, '\\\\').replace(/\`/g, '\`'); // Escape backticks
|
|
|
|
|
2021-04-24 17:09:02 +00:00
|
|
|
return `this.loadStylesheet(\`${css}\`)`;
|
2021-04-22 19:45:28 +00:00
|
|
|
});
|
|
|
|
|
2021-05-15 21:08:34 +00:00
|
|
|
writeFileSync(join(baseDir, `index.js`), content);
|
|
|
|
|
2021-06-07 17:49:22 +00:00
|
|
|
for (const jsFile of glob.sync(`${baseDir}/**/*.{js,jsx,ts,tsx}`)) {
|
2021-05-15 21:08:34 +00:00
|
|
|
console.log(jsFile);
|
|
|
|
let content = readFileSync(jsFile, 'utf8');
|
|
|
|
|
|
|
|
/* Here we preappend some JS to each file to match context and fix some weird bundler bugs:
|
|
|
|
- Import our own Powercord global
|
|
|
|
- Fix dynamic imports (like using require("powercord/" + someVariable)) causing errors (due to no static analyse). Do this because some plugins use it for PC + VZ compat with the same JS
|
|
|
|
*/
|
|
|
|
|
|
|
|
content = `import powercord from '_powercord/global';
|
|
|
|
require.cache['powercord/entities'] = require('powercord/entities');
|
|
|
|
require.cache['powercord/components/settings'] = require('powercord/components/settings');
|
|
|
|
require.cache['powercord/webpack'] = require('powercord/webpack');
|
|
|
|
|
|
|
|
${content}`;
|
|
|
|
|
|
|
|
writeFileSync(jsFile, content);
|
|
|
|
}
|
2021-04-04 14:52:41 +00:00
|
|
|
|
2021-05-05 15:55:24 +00:00
|
|
|
writeFileSync(join(baseDir, `goosemodModule.json`), JSON.stringify(manifest));
|
|
|
|
|
|
|
|
return '';
|
2021-04-04 14:52:41 +00:00
|
|
|
};
|