2020-08-02 22:22:17 +00:00
|
|
|
const {promises: fsp} = require('fs');
|
|
|
|
const nunjucks = require('nunjucks');
|
|
|
|
const {join} = require('path');
|
|
|
|
|
2020-08-03 12:50:45 +00:00
|
|
|
const {readScriptMetadata, scripts} = require('userscripts');
|
2020-08-02 22:22:17 +00:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
// Make sure the `public/userscripts` directory exists.
|
|
|
|
await fsp.mkdir(join(__dirname, '../public/userscripts/'), {recursive: true});
|
|
|
|
|
|
|
|
// Configure Nunjucks to use the current directory for templates.
|
|
|
|
nunjucks.configure(__dirname, {
|
|
|
|
lstripBlocks: true,
|
|
|
|
trimBlocks: true,
|
|
|
|
throwOnUndefined: true
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create an array to hold all the script metadatas we'll use to render the template.
|
|
|
|
const scriptMetadatas = [];
|
|
|
|
|
|
|
|
// Define the source directory where the userscripts are located.
|
2020-08-03 12:50:45 +00:00
|
|
|
const sourceDir = join(__dirname, '../node_modules/userscripts/source/');
|
2020-08-02 22:22:17 +00:00
|
|
|
|
|
|
|
for (const script of scripts) {
|
|
|
|
// Read the script metadata and add it to our array.
|
|
|
|
const metadata = await readScriptMetadata(script);
|
|
|
|
|
|
|
|
// The way the metadata is parsed is inside arrays, so extract
|
|
|
|
// all of them so we don't have to in the template.
|
|
|
|
metadata.description = metadata.description[0];
|
|
|
|
metadata.downloadURL = metadata.downloadURL[0];
|
|
|
|
metadata.name = metadata.name[0];
|
|
|
|
metadata.version = metadata.version[0];
|
|
|
|
// Remove the `https://` and trailing `/*` from the domain.
|
|
|
|
metadata.domain = metadata.match[0].slice(
|
|
|
|
metadata.match[0].indexOf('://') + 3,
|
|
|
|
metadata.match[0].lastIndexOf('/')
|
|
|
|
);
|
|
|
|
|
|
|
|
scriptMetadatas.push(metadata);
|
|
|
|
|
|
|
|
// Define the filename for the script.
|
|
|
|
const filename = `${script}.user.js`;
|
|
|
|
|
|
|
|
// Copy all the userscripts to the `public/userscripts/` directory.
|
|
|
|
await fsp.copyFile(
|
|
|
|
join(sourceDir, filename),
|
|
|
|
join(__dirname, '../public/userscripts/', filename)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the userscripts template and write it to file.
|
|
|
|
await fsp.writeFile(
|
|
|
|
join(__dirname, '../public/userscripts/index.html'),
|
|
|
|
nunjucks.render('userscripts/index.html', {scripts: scriptMetadatas})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 13:36:48 +00:00
|
|
|
module.exports = {
|
|
|
|
build: main
|
|
|
|
};
|
|
|
|
|
2020-08-02 22:22:17 +00:00
|
|
|
// Run `main()` if this script was called directly (like `node source/userscripts.js`).
|
|
|
|
if (require.main === module) {
|
|
|
|
main();
|
|
|
|
}
|