[Dev] Initial add

This commit is contained in:
Ducko 2021-03-05 22:16:41 +00:00 committed by フズキ
parent 82b56d2067
commit f3948a0daf
No known key found for this signature in database
GPG key ID: AD7750AB4625F1DD
4 changed files with 7392 additions and 11 deletions

5
.gitignore vendored
View file

@ -3,4 +3,7 @@ node_modules
clones clones
src/gh_pat.json src/gh_pat.json
dist # dist
devDist
devTemp

7216
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -3,24 +3,30 @@
"version": "1.0.0", "version": "1.0.0",
"description": "Builder for Module Store v2.", "description": "Builder for Module Store v2.",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/GooseMod/MS2Builder.git" "url": "git+https://github.com/GooseMod/MS2Builder.git"
}, },
"author": "GooseMod", "author": "GooseMod",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
"url": "https://github.com/GooseMod/MS2Builder/issues" "url": "https://github.com/GooseMod/MS2Builder/issues"
}, },
"homepage": "https://github.com/GooseMod/MS2Builder#readme", "homepage": "https://github.com/GooseMod/MS2Builder#readme",
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"glob": "^7.1.6", "glob": "^7.1.6",
"parcel-bundler": "^1.12.4" "parcel-bundler": "^1.12.4"
}, },
"alias": { "alias": {
"@goosemod/patcher": "./moduleWrappers/patcher.js", "@goosemod/patcher": "./moduleWrappers/patcher.js",
"@goosemod/webpack": "./moduleWrappers/webpack.js", "@goosemod/webpack": "./moduleWrappers/webpack.js",
@ -29,5 +35,6 @@
"@goosemod/toast": "./moduleWrappers/toast.js", "@goosemod/toast": "./moduleWrappers/toast.js",
"@goosemod/settings": "./moduleWrappers/settings.js" "@goosemod/settings": "./moduleWrappers/settings.js"
}, },
"type": "module" "type": "module"
} }

173
src/dev.js Normal file
View file

@ -0,0 +1,173 @@
import { dirname, sep, join } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
import AutoTag from './autoTag.js';
import Parcel from 'parcel-bundler';
import glob from 'glob';
import chokidar from 'chokidar';
import { createServer } from 'http';
import { rmSync, mkdirSync, readFileSync, writeFileSync, readFile, readdirSync, copyFileSync, rmdirSync, statSync, existsSync } from 'fs';
import { createHash } from 'crypto';
var copyRecursiveSync = function(src, dest) { // https://stackoverflow.com/a/22185855
var exists = existsSync(src);
var stats = exists && statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
mkdirSync(dest);
readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(join(src, childItemName),
join(dest, childItemName));
});
} else {
copyFileSync(src, dest);
}
};
const dir = process.argv[2];
const port = parseInt(process.argv[3]);
const rootDir = __dirname.replace(`${sep}src`, '');
const distDir = `${rootDir}/devDist`;
const modulesDir = `${distDir}/module`;
const tempDir = `${rootDir}/devTemp`;
console.log(dir, distDir, modulesDir);
const parcelOptions = {
minify: true,
watch: false,
sourceMaps: false,
outDir: modulesDir,
logLevel: 0
};
createServer((req, res) => {
const basedPath = `${distDir}${req.url.split('?').shift().replace(/\.\./g, '')}`;
let contentType = 'text/plain';
switch (req.url.split('.').pop()) {
case 'js':
contentType = 'text/javascript';
break;
case 'json':
contentType = 'application/json';
break;
}
console.log(basedPath, contentType);
readFile(basedPath, (err, content) => {
if (err) {
res.writeHead(400, { 'Content-Type': contentType });
res.end('Request error', 'utf-8');
return;
}
res.writeHead(200, { 'Content-Type': contentType, 'Access-Control-Allow-Origin': '*' });
res.end(content, 'utf-8');
});
}).listen(port);
const resetDir = (dir, make = true) => {
rmSync(dir, { recursive: true, force: true });
if (make) mkdirSync(dir, { recursive: true });
};
const build = async () => {
console.log('building');
resetDir(tempDir, false);
copyRecursiveSync(dir, tempDir)
resetDir(distDir);
resetDir(modulesDir);
const modules = glob.sync(`${tempDir}/*`);
const jsonOut = {
modules: [],
meta: {
name: 'MS2Builder Dev Repo',
description: 'Local repo for testing, generated by MS2Builder Dev'
}
};
for (const m of modules) {
const dirname = m.split('/').pop();
console.time(dirname);
const manifest = JSON.parse(readFileSync(`${m}/goosemodModule.json`));
// console.log(manifest);
const outFile = `${manifest.name}.js`;
const bundler = new Parcel(`${m}/${manifest.main}`, Object.assign(parcelOptions, {
outFile
}));
const bundle = await bundler.bundle();
const outPath = `${modulesDir}/${outFile}`;
let jsCode = readFileSync(outPath, 'utf8');
jsCode = `${jsCode};parcelRequire('${bundle.entryAsset.basename}').default`; // Make eval return the index module's default export
// console.log(jsCode);
writeFileSync(outPath, jsCode);
const jsHash = createHash('sha512').update(jsCode).digest('hex');
const manifestJson = {
name: manifest.name,
description: manifest.description,
version: manifest.version,
tags: manifest.tags.concat(AutoTag(jsCode)),
authors: manifest.authors,
hash: jsHash,
github: {
stars: 0,
repo: 'GooseMod/MS2Builder'
}
};
if (manifest.images) manifestJson.images = manifest.images;
if (manifest.dependencies) manifestJson.dependencies = manifest.dependencies;
console.timeEnd(dirname);
jsonOut.modules.push(manifestJson);
}
writeFileSync(`${distDir}/modules.json`, JSON.stringify(jsonOut));
};
build();
chokidar.watch(dir).on('change', (ev, pa) => {
console.log(ev, pa);
// build();
});
// await new Promise(() => {});
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));