2021-01-27 22:05:15 +00:00
|
|
|
import ModuleRepos from './modules/index.js';
|
2021-01-22 21:26:27 +00:00
|
|
|
|
|
|
|
import Parcel from 'parcel-bundler';
|
2021-01-24 10:49:41 +00:00
|
|
|
import axios from 'axios';
|
2021-01-28 15:24:07 +00:00
|
|
|
import glob from 'glob';
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-28 15:24:07 +00:00
|
|
|
import { rmSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, existsSync } from 'fs';
|
2021-01-23 19:51:49 +00:00
|
|
|
import { createHash } from 'crypto';
|
2021-01-22 21:26:27 +00:00
|
|
|
|
|
|
|
import { dirname } from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
2021-01-24 10:49:41 +00:00
|
|
|
import githubPAT from './gh_pat.js';
|
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
const clonesDir = `${__dirname.replace('/src', '')}/clones`;
|
2021-01-23 10:38:44 +00:00
|
|
|
|
|
|
|
const distDir = `${__dirname.replace('/src', '')}/dist`;
|
|
|
|
|
2021-01-23 19:51:49 +00:00
|
|
|
const modulesDir = `${distDir}/module`;
|
2021-01-22 21:26:27 +00:00
|
|
|
|
|
|
|
const resetDir = (dir) => {
|
|
|
|
rmSync(dir, { recursive: true, force: true });
|
2021-01-30 13:35:08 +00:00
|
|
|
mkdirSync(dir, { recursive: true });
|
2021-01-22 21:26:27 +00:00
|
|
|
};
|
|
|
|
|
2021-01-30 13:35:08 +00:00
|
|
|
if (process.argv[2] === '-f') {
|
|
|
|
resetDir(clonesDir);
|
|
|
|
|
|
|
|
resetDir(distDir);
|
|
|
|
resetDir(modulesDir);
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:24:07 +00:00
|
|
|
let previous = [];
|
|
|
|
if (existsSync(clonesDir)) {
|
|
|
|
for (const cloneDir of glob.sync(`${clonesDir}/*/*`)) {
|
|
|
|
process.chdir(cloneDir);
|
2021-01-23 10:47:15 +00:00
|
|
|
|
2021-01-28 15:24:07 +00:00
|
|
|
const currentHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim())));
|
|
|
|
|
|
|
|
previous = previous.concat(ModuleRepos.filter((x) => x[0] === cloneDir.replace(`${clonesDir}/`, '') && x[1] === currentHash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
import { exec } from 'child_process';
|
|
|
|
|
|
|
|
const parcelOptions = {
|
|
|
|
minify: true,
|
|
|
|
watch: false,
|
|
|
|
sourceMaps: false,
|
2021-01-23 10:22:37 +00:00
|
|
|
outDir: modulesDir,
|
|
|
|
logLevel: 0
|
2021-01-22 21:26:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let moduleJson = [];
|
|
|
|
|
2021-01-24 10:49:41 +00:00
|
|
|
const githubCache = {};
|
|
|
|
|
|
|
|
const getGithubInfo = async (repo) => {
|
|
|
|
if (githubCache[repo]) return githubCache[repo];
|
|
|
|
|
|
|
|
const info = (await axios.get(`https://api.github.com/repos/${repo}`, {
|
|
|
|
headers: {
|
|
|
|
'Authorization': `token ${githubPAT}`
|
|
|
|
}
|
|
|
|
})).data;
|
|
|
|
|
|
|
|
githubCache[repo] = info;
|
|
|
|
return info;
|
|
|
|
};
|
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
for (const repo of ModuleRepos) {
|
2021-01-23 14:19:00 +00:00
|
|
|
console.time(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-24 10:49:41 +00:00
|
|
|
const githubInfo = await getGithubInfo(repo[0]);
|
|
|
|
|
2021-01-23 11:00:42 +00:00
|
|
|
const name = repo[0];
|
2021-01-28 15:24:07 +00:00
|
|
|
const cloneDir = `${clonesDir}/${name}`;
|
2021-01-23 11:00:42 +00:00
|
|
|
|
|
|
|
const moduleDir = repo[2] || '';
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-28 15:24:07 +00:00
|
|
|
if (previous.includes(repo)) {
|
|
|
|
const manifest = JSON.parse(readFileSync(`${cloneDir}${moduleDir}/goosemodModule.json`));
|
|
|
|
|
|
|
|
const jsHash = createHash('sha512').update(readFileSync(`${modulesDir}/${manifest.name}.js`)).digest('hex');
|
|
|
|
|
|
|
|
moduleJson.push({
|
|
|
|
name: manifest.name,
|
|
|
|
description: manifest.description,
|
|
|
|
version: manifest.version,
|
|
|
|
tags: manifest.tags,
|
|
|
|
authors: manifest.authors,
|
|
|
|
hash: jsHash,
|
|
|
|
|
|
|
|
github: {
|
|
|
|
stars: githubInfo.stargazers_count,
|
|
|
|
repo: repo[0]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
process.stdout.write('[SKIP] ');
|
|
|
|
|
|
|
|
console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log(repo);
|
|
|
|
|
|
|
|
const url = `https://github.com/${repo[0]}.git`;
|
|
|
|
const commitHash = repo[1];
|
|
|
|
|
|
|
|
const preprocessor = repo[3];
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-30 13:35:08 +00:00
|
|
|
// resetDir(cloneDir);
|
|
|
|
// rmSync(cloneDir, { recursive: true, force: true });
|
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
await new Promise((res) => exec(`git clone ${url} ${cloneDir}`, res));
|
|
|
|
|
|
|
|
process.chdir(cloneDir);
|
|
|
|
|
2021-01-23 10:22:37 +00:00
|
|
|
const lastHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim())));
|
|
|
|
|
2021-01-23 14:19:00 +00:00
|
|
|
await new Promise((res) => exec(`git checkout ${commitHash}`, res));
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-27 22:05:15 +00:00
|
|
|
if (preprocessor) {
|
|
|
|
(await import(`./preprocessors/${preprocessor}.js`)).default(`${cloneDir}${moduleDir}`, repo);
|
|
|
|
}
|
|
|
|
|
2021-01-23 11:00:42 +00:00
|
|
|
const manifest = JSON.parse(readFileSync(`${cloneDir}${moduleDir}/goosemodModule.json`));
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-23 10:22:37 +00:00
|
|
|
// console.log(manifest);
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-23 20:04:24 +00:00
|
|
|
const outFile = `${manifest.name}.js`;
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-23 11:00:42 +00:00
|
|
|
const bundler = new Parcel(`${cloneDir}${moduleDir}/${manifest.main}`, Object.assign(parcelOptions, {
|
2021-01-22 21:26:27 +00:00
|
|
|
outFile
|
|
|
|
}));
|
|
|
|
|
|
|
|
const bundle = await bundler.bundle();
|
|
|
|
|
2021-01-23 20:04:24 +00:00
|
|
|
const outPath = `${modulesDir}/${outFile}`;
|
|
|
|
let jsCode = readFileSync(outPath, 'utf8');
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-23 11:00:42 +00:00
|
|
|
jsCode = `${jsCode};parcelRequire('${bundle.entryAsset.basename}').default`; // Make eval return the index module's default export
|
2021-01-22 21:26:27 +00:00
|
|
|
|
2021-01-23 10:22:37 +00:00
|
|
|
// console.log(jsCode);
|
2021-01-22 21:26:27 +00:00
|
|
|
|
|
|
|
writeFileSync(outPath, jsCode);
|
2021-01-23 19:51:49 +00:00
|
|
|
|
|
|
|
const jsHash = createHash('sha512').update(jsCode).digest('hex');
|
|
|
|
|
2021-01-30 13:35:08 +00:00
|
|
|
const manifestJson = {
|
2021-01-22 21:26:27 +00:00
|
|
|
name: manifest.name,
|
|
|
|
description: manifest.description,
|
2021-01-30 13:35:08 +00:00
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
version: manifest.version,
|
2021-01-30 13:35:08 +00:00
|
|
|
|
2021-01-22 21:26:27 +00:00
|
|
|
tags: manifest.tags,
|
2021-01-30 13:35:08 +00:00
|
|
|
|
2021-01-23 20:04:24 +00:00
|
|
|
authors: manifest.authors,
|
2021-01-30 13:35:08 +00:00
|
|
|
|
2021-01-24 10:49:41 +00:00
|
|
|
hash: jsHash,
|
|
|
|
|
|
|
|
github: {
|
|
|
|
stars: githubInfo.stargazers_count,
|
|
|
|
repo: repo[0]
|
|
|
|
}
|
2021-01-30 13:35:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (manifest.images) manifestJson.images = manifest.images;
|
2021-02-01 11:30:59 +00:00
|
|
|
if (manifest.dependencies) manifestJson.dependencies = manifest.dependencies;
|
2021-01-30 13:35:08 +00:00
|
|
|
|
|
|
|
moduleJson.push(manifestJson);
|
2021-01-23 10:22:37 +00:00
|
|
|
|
2021-01-23 14:19:00 +00:00
|
|
|
console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);
|
2021-01-23 10:34:45 +00:00
|
|
|
|
|
|
|
// console.log(lastHash);
|
|
|
|
|
|
|
|
if (lastHash !== commitHash) {
|
|
|
|
console.log('[Warning] Commit hash in modules does not match latest commit in repo');
|
|
|
|
}
|
2021-01-23 10:47:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 20:19:40 +00:00
|
|
|
writeFileSync(`${distDir}/modules.json`, JSON.stringify(moduleJson));
|
2021-02-01 11:30:59 +00:00
|
|
|
copyFileSync(`${__dirname.replace('/src', '')}/_headers`, `${distDir}/_headers`);
|