Use temporary working directory for files.

This commit is contained in:
chris062689 2017-08-20 01:45:31 -04:00
parent 9c98fa2144
commit ca68b117d4
3 changed files with 32 additions and 30 deletions

View file

@ -1,6 +1,9 @@
# Output directory # Output directory
dist/ dist/
# Temporary directory
temp/
# Logs # Logs
logs logs
*.log *.log
@ -59,4 +62,3 @@ typings/
# dotenv environment variables file # dotenv environment variables file
.env .env

View file

@ -9,11 +9,8 @@ const logger = require('winston');
logger.exitOnError = false; logger.exitOnError = false;
logger.add(logger.transports.File, { filename: '/var/log/citra-qt-installer/citra-qt-installer-repository.log' }); logger.add(logger.transports.File, { filename: '/var/log/citra-qt-installer/citra-qt-installer-repository.log' });
const distDir = "./dist"; const tempDir = './temp';
const distDir = './dist';
function mkdirIfNotExists(path) {
if (!fs.existsSync(path)) { fs.mkdirSync(path); }
}
async function getReleases(repo) { async function getReleases(repo) {
const result = await req({ const result = await req({
@ -76,7 +73,11 @@ let targets = [
]; ];
async function execute() { async function execute() {
mkdirIfNotExists(distDir); // Verify there's a dist directory.
fs.ensureDirSync(distDir);
// Clean up any temporary directories.
fs.emptyDirSync(tempDir);
// Get Git information // Get Git information
logger.debug("Getting release info..."); logger.debug("Getting release info...");
@ -111,9 +112,9 @@ async function execute() {
const scriptName = platform + "-" + target_source.ScriptName; const scriptName = platform + "-" + target_source.ScriptName;
const version = release_data.release_id; const version = release_data.release_id;
const targetMetadataPath = `${distDir}/${name}/${version}meta.7z`;
if (fs.existsSync(targetMetadataPath)) { const targetMetadataFilePath = `${distDir}/${name}/${version}meta.7z`;
if (fs.existsSync(targetMetadataFilePath)) {
logger.debug(`Metadata information already exists for ${name} ${version}, skipping.`); logger.debug(`Metadata information already exists for ${name} ${version}, skipping.`);
return; return;
} }
@ -121,32 +122,35 @@ async function execute() {
logger.info(`Building release information for ${name} ${version}.`); logger.info(`Building release information for ${name} ${version}.`);
updatesAvailable = true; updatesAvailable = true;
// Build directory structure // Create the temporary working directory.
mkdirIfNotExists(`${distDir}/${name}`); const workingDirectoryPath = `${tempDir}/${name}_tmp`;
fs.ensureDirSync(workingDirectoryPath);
// Copy license // Copy license
fs.copySync("license.txt", `${distDir}/${name}/license.txt`); fs.copySync("license.txt", `${workingDirectoryPath}/license.txt`);
fs.copySync("scripts/" + scriptName + ".qs", `${distDir}/${name}/installscript.qs`); fs.copySync(`scripts/${scriptName}.qs`, `${workingDirectoryPath}/installscript.qs`);
// Create 7zip archive // Create 7zip archive
fs.removeSync("meta.7z"); exec.sync(zip_bin, ["a", "../meta.7z"], {"cwd": workingDirectoryPath});
exec.sync(zip_bin, ["a", "../meta.7z", name], {"cwd": distDir});
fs.removeSync(`${distDir}/${name}`);
const sha = sha1("meta.7z");
// Setup final structure // Copy the metadata file into the target path.
logger.debug(`Creating target metadata for ${name}`); logger.debug(`Creating target metadata for ${name} at ${targetMetadataFilePath}`);
fs.moveSync("meta.7z", targetMetadataPath); fs.moveSync(`${tempDir}/meta.7z`, targetMetadataFilePath);
// Create metadata for the Update.xml
var metaHash = sha1(targetMetadataFilePath);
// Cleanup temporary working directory.
fs.removeSync(workingDirectoryPath);
// Create metadata
let target = []; let target = [];
target.push({"Name": name}); target.push({"Name": name});
target.push({"DisplayName": target_source.DisplayName.replace("%platform%", platform)}); target.push({"DisplayName": target_source.DisplayName.replace("%platform%", platform)});
target.push({"Version": version}); target.push({"Version": version});
target.push({"DownloadableArchives": release_data.name}); target.push({"DownloadableArchives": release_data.name});
// Because we cannot compute the uncompressed size ourselves, just give a generous estimate (to make // Because we cannot compute the uncompressed size ourselves, just give a generous estimate
// sure they have enough disk space). // (to make sure they have enough disk space).
// OS flag is useless - i.e the installer stubs it :P // OS flag is useless - i.e the installer stubs it :P
target.push({"UpdateFile": [{_attr: {UncompressedSize: release_data.size * 2, target.push({"UpdateFile": [{_attr: {UncompressedSize: release_data.size * 2,
CompressedSize: release_data.size, OS: "Any"}}]}); CompressedSize: release_data.size, OS: "Any"}}]});
@ -158,7 +162,7 @@ async function execute() {
target.push({"Default": target_source.Default}); target.push({"Default": target_source.Default});
target.push({"Licenses": target_source.Licenses}); target.push({"Licenses": target_source.Licenses});
target.push({"Script": "installscript.qs"}); target.push({"Script": "installscript.qs"});
target.push({"SHA": sha}); target.push({"SHA": metaHash});
updates.push({"PackageUpdate": target}); updates.push({"PackageUpdate": target});
}); });
@ -168,11 +172,7 @@ async function execute() {
const updatesXml = xml({"Updates": updates}, {indent: " "}); const updatesXml = xml({"Updates": updates}, {indent: " "});
// Save Updates.xml // Save Updates.xml
fs.writeFile(`${distDir}/Updates.xml`, updatesXml, function (err) { fs.writeFileSync(`${distDir}/Updates.xml`, updatesXml);
if (err) {
throw err;
}
});
logger.info('Wrote a new Updates.xml file -- updates are available.'); logger.info('Wrote a new Updates.xml file -- updates are available.');
} else { } else {
logger.info('No updates are available -- nothing to do.'); logger.info('No updates are available -- nothing to do.');

View file

@ -8,7 +8,7 @@
"dependencies": { "dependencies": {
"7zip-bin": "^2.1.0", "7zip-bin": "^2.1.0",
"execa": "^0.7.0", "execa": "^0.7.0",
"fs-extra": "^4.0.0", "fs-extra": "^4.0.1",
"request": "^2.81.0", "request": "^2.81.0",
"request-promise": "^4.2.1", "request-promise": "^4.2.1",
"sha1-file": "^1.0.0", "sha1-file": "^1.0.0",