Fix error on Windows (#9)

This commit is contained in:
PandaDriver 2021-07-01 21:13:52 +02:00 committed by Alyxia Sother
parent 0300abe06e
commit ae21d6d6cc
No known key found for this signature in database
GPG Key ID: 355968D14144B739
1 changed files with 45 additions and 44 deletions

View File

@ -1,10 +1,7 @@
import ModuleRepos from './modules/index.js'; import ModuleRepos from './modules/index.js';
import AutoTag from './autoTag.js'; import AutoTag from './autoTag.js';
<<<<<<< HEAD
import WebhookSend from './webhook.js'; import WebhookSend from './webhook.js';
=======
import ImageCDN from './imageCdn.js'; import ImageCDN from './imageCdn.js';
>>>>>>> 606a100 ([Feat] Local images building)
import Parcel from 'parcel-bundler'; import Parcel from 'parcel-bundler';
import axios from 'axios'; import axios from 'axios';
@ -13,7 +10,7 @@ import glob from 'glob';
import { rmSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, existsSync, rmdirSync } from 'fs'; import { rmSync, mkdirSync, readFileSync, writeFileSync, copyFileSync, existsSync, rmdirSync } from 'fs';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
import { dirname } from 'path'; import { dirname, sep } from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
let file; let file;
@ -28,9 +25,9 @@ try {
const __dirname = dirname(fileURLToPath(import.meta.url)); const __dirname = dirname(fileURLToPath(import.meta.url));
const clonesDir = `${__dirname.replace('/src', '')}/clones`; const clonesDir = `${__dirname.replace(`${sep}src`, '')}/clones`;
const distDir = `${__dirname.replace('/src', '')}/dist`; const distDir = `${__dirname.replace(`${sep}src`, '')}/dist`;
global.distDir = distDir; global.distDir = distDir;
const modulesDir = `${distDir}/module`; const modulesDir = `${distDir}/module`;
@ -42,7 +39,7 @@ const resetDir = (dir) => {
if (process.argv[2] === '-f') { if (process.argv[2] === '-f') {
resetDir(clonesDir); resetDir(clonesDir);
resetDir(distDir); resetDir(distDir);
resetDir(modulesDir); resetDir(modulesDir);
} }
@ -51,7 +48,7 @@ let previous = [];
if (existsSync(clonesDir)) { if (existsSync(clonesDir)) {
for (const cloneDir of glob.sync(`${clonesDir}/*/*`)) { for (const cloneDir of glob.sync(`${clonesDir}/*/*`)) {
process.chdir(cloneDir); process.chdir(cloneDir);
const currentHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim()))); const currentHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim())));
const moduleInRepos = ModuleRepos.map( const moduleInRepos = ModuleRepos.map(
@ -80,7 +77,7 @@ const githubCache = {};
const getGithubInfo = async (repo) => { const getGithubInfo = async (repo) => {
if (githubCache[repo]) return githubCache[repo]; if (githubCache[repo]) return githubCache[repo];
const info = (await axios.get(`https://api.github.com/repos/${repo}`, { const info = (await axios.get(`https://api.github.com/repos/${repo}`, {
headers: { headers: {
'Authorization': `token ${githubPAT}` 'Authorization': `token ${githubPAT}`
@ -105,7 +102,7 @@ for (const parentRepo of ModuleRepos) {
for (const repo of parentRepo.modules) { for (const repo of parentRepo.modules) {
console.time(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`); console.time(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);
const name = repo[0]; const name = repo[0];
const cloneDir = `${clonesDir}/${name}`; const cloneDir = `${clonesDir}/${name}`;
let moduleDir = repo[2] || ''; let moduleDir = repo[2] || '';
@ -120,28 +117,28 @@ for (const parentRepo of ModuleRepos) {
} else { } else {
currentModule = currentModule[0]; currentModule = currentModule[0];
} }
moduleJson.modules.push(currentModule); moduleJson.modules.push(currentModule);
process.stdout.write('[SKIP] '); process.stdout.write('[SKIP] ');
console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`); console.timeEnd(repo.slice(0, 2).join(' @ ') + `${repo[2] ? ` ${repo[2]}` : ''}`);
continue; continue;
} }
} catch (e) { } catch (e) {
console.log('Cache fail', repo[0], e); console.log('Cache fail', repo[0], e);
} }
let githubInfo = getGithubInfo(repo[0]); let githubInfo = getGithubInfo(repo[0]);
// console.log(repo); // console.log(repo);
const url = `https://github.com/${repo[0]}.git`; const url = `https://github.com/${repo[0]}.git`;
const commitHash = repo[1]; const commitHash = repo[1];
const preprocessor = repo[3]; const preprocessor = repo[3];
// resetDir(cloneDir); // resetDir(cloneDir);
// rmSync(cloneDir, { recursive: true, force: true }); // rmSync(cloneDir, { recursive: true, force: true });
@ -152,17 +149,17 @@ for (const parentRepo of ModuleRepos) {
if (currentHash !== repo[1] && repo[1] !== '') rmSync(cloneDir, { recursive: true, force: true }); if (currentHash !== repo[1] && repo[1] !== '') rmSync(cloneDir, { recursive: true, force: true });
} }
process.chdir(distDir); // Incase current wd is broken, in which case exec / git crashes process.chdir(distDir); // Incase current wd is broken, in which case exec / git crashes
await new Promise((res) => exec(`git clone ${url} ${cloneDir}`, res)); await new Promise((res) => exec(`git clone ${url} ${cloneDir}`, res));
process.chdir(cloneDir); process.chdir(cloneDir);
const lastHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim()))); const lastHash = await new Promise((res) => exec(`git rev-parse HEAD`, (err, stdout) => res(stdout.trim())));
await new Promise((res) => exec(`git checkout ${commitHash}`, res)); await new Promise((res) => exec(`git checkout ${commitHash}`, res));
const commitTimestamp = await new Promise((res) => exec(`git log -1 --format="%at" | xargs -I{} date -d @{} +%s`, (err, stdout) => res(stdout.trim()))); const commitTimestamp = await new Promise((res) => exec(`git log -1 --format="%at" | xargs -I{} date -d @{} +%s`, (err, stdout) => res(stdout.trim())));
if (preprocessor) { if (preprocessor) {
@ -172,44 +169,44 @@ for (const parentRepo of ModuleRepos) {
moduleDir = preOut; moduleDir = preOut;
} }
} }
const manifest = JSON.parse(readFileSync(`${cloneDir}${moduleDir}/goosemodModule.json`)); const manifest = JSON.parse(readFileSync(`${cloneDir}${moduleDir}/goosemodModule.json`));
// console.log(manifest); // console.log(manifest);
const outFile = `${manifest.name}.js`; const outFile = `${manifest.name}.js`;
const bundler = new Parcel(`${cloneDir}${moduleDir}/${manifest.main}`, Object.assign(parcelOptions, { const bundler = new Parcel(`${cloneDir}${moduleDir}/${manifest.main}`, Object.assign(parcelOptions, {
outFile outFile
})); }));
const bundle = await bundler.bundle(); const bundle = await bundler.bundle();
const outPath = `${modulesDir}/${outFile}`; const outPath = `${modulesDir}/${outFile}`;
let jsCode = readFileSync(outPath, 'utf8'); let jsCode = readFileSync(outPath, 'utf8');
jsCode = `${jsCode};parcelRequire('${bundle.entryAsset.basename}').default`; // Make eval return the index module's default export jsCode = `${jsCode};parcelRequire('${bundle.entryAsset.basename}').default`; // Make eval return the index module's default export
// console.log(jsCode); // console.log(jsCode);
writeFileSync(outPath, jsCode); writeFileSync(outPath, jsCode);
const jsHash = createHash('sha512').update(jsCode).digest('hex'); const jsHash = createHash('sha512').update(jsCode).digest('hex');
githubInfo = await githubInfo; // GitHub info is gotten async during other stuff to reduce time githubInfo = await githubInfo; // GitHub info is gotten async during other stuff to reduce time
const manifestJson = { const manifestJson = {
name: manifest.name, name: manifest.name,
description: manifest.description, description: manifest.description,
version: manifest.version, version: manifest.version,
tags: AutoTag(jsCode, manifest.tags), tags: AutoTag(jsCode, manifest.tags),
authors: manifest.authors, authors: manifest.authors,
hash: jsHash, hash: jsHash,
github: { github: {
stars: githubInfo.stargazers_count, stars: githubInfo.stargazers_count,
repo: repo[0] repo: repo[0]
@ -219,18 +216,18 @@ for (const parentRepo of ModuleRepos) {
...repo[4] ...repo[4]
}; };
if (manifest.images) manifestJson.images = manifest.images; if (manifest.images) manifestJson.images = manifest.images;
if (manifest.dependencies) manifestJson.dependencies = manifest.dependencies; if (manifest.dependencies) manifestJson.dependencies = manifest.dependencies;
manifestJson.images = await ImageCDN(manifestJson); manifestJson.images = await ImageCDN(manifestJson);
moduleJson.modules.push(manifestJson); moduleJson.modules.push(manifestJson);
console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`); console.timeEnd(repo.slice(0, 2).join(' @ ')+`${repo[2] ? ` ${repo[2]}` : ''}`);
// console.log(lastHash); // console.log(lastHash);
if (commitHash !== '' && lastHash !== commitHash) { if (commitHash !== '' && lastHash !== commitHash) {
console.log('[Warning] Commit hash in modules does not match latest commit in repo'); console.log('[Warning] Commit hash in modules does not match latest commit in repo');
} }
@ -245,6 +242,10 @@ for (const parentRepo of ModuleRepos) {
writeFileSync(`${distDir}/modules.json`, JSON.stringify(oldTotalModulesJson)); writeFileSync(`${distDir}/modules.json`, JSON.stringify(oldTotalModulesJson));
<<<<<<< HEAD
copyFileSync(`${__dirname.replace('/src', '')}/_headers`, `${distDir}/_headers`); copyFileSync(`${__dirname.replace('/src', '')}/_headers`, `${distDir}/_headers`);
WebhookSend(); WebhookSend();
=======
copyFileSync(`${__dirname.replace(`${sep}src`, '')}/_headers`, `${distDir}/_headers`);
>>>>>>> 66b474c (Fix error on Windows (#9))