From ca68b117d4f89ccc6ab26e37f4fae0bce8599ebe Mon Sep 17 00:00:00 2001 From: chris062689 Date: Sun, 20 Aug 2017 01:45:31 -0400 Subject: [PATCH] Use temporary working directory for files. --- repository/.gitignore | 4 ++- repository/app.js | 56 ++++++++++++++++++++--------------------- repository/package.json | 2 +- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/repository/.gitignore b/repository/.gitignore index de25328..2ed4c2a 100644 --- a/repository/.gitignore +++ b/repository/.gitignore @@ -1,6 +1,9 @@ # Output directory dist/ +# Temporary directory +temp/ + # Logs logs *.log @@ -59,4 +62,3 @@ typings/ # dotenv environment variables file .env - diff --git a/repository/app.js b/repository/app.js index d32d638..a5bf9b4 100644 --- a/repository/app.js +++ b/repository/app.js @@ -9,11 +9,8 @@ const logger = require('winston'); logger.exitOnError = false; logger.add(logger.transports.File, { filename: '/var/log/citra-qt-installer/citra-qt-installer-repository.log' }); -const distDir = "./dist"; - -function mkdirIfNotExists(path) { - if (!fs.existsSync(path)) { fs.mkdirSync(path); } -} +const tempDir = './temp'; +const distDir = './dist'; async function getReleases(repo) { const result = await req({ @@ -76,7 +73,11 @@ let targets = [ ]; 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 logger.debug("Getting release info..."); @@ -111,9 +112,9 @@ async function execute() { const scriptName = platform + "-" + target_source.ScriptName; 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.`); return; } @@ -121,32 +122,35 @@ async function execute() { logger.info(`Building release information for ${name} ${version}.`); updatesAvailable = true; - // Build directory structure - mkdirIfNotExists(`${distDir}/${name}`); + // Create the temporary working directory. + const workingDirectoryPath = `${tempDir}/${name}_tmp`; + fs.ensureDirSync(workingDirectoryPath); // Copy license - fs.copySync("license.txt", `${distDir}/${name}/license.txt`); - fs.copySync("scripts/" + scriptName + ".qs", `${distDir}/${name}/installscript.qs`); + fs.copySync("license.txt", `${workingDirectoryPath}/license.txt`); + fs.copySync(`scripts/${scriptName}.qs`, `${workingDirectoryPath}/installscript.qs`); // Create 7zip archive - fs.removeSync("meta.7z"); - exec.sync(zip_bin, ["a", "../meta.7z", name], {"cwd": distDir}); - fs.removeSync(`${distDir}/${name}`); - const sha = sha1("meta.7z"); + exec.sync(zip_bin, ["a", "../meta.7z"], {"cwd": workingDirectoryPath}); - // Setup final structure - logger.debug(`Creating target metadata for ${name}`); - fs.moveSync("meta.7z", targetMetadataPath); + // Copy the metadata file into the target path. + logger.debug(`Creating target metadata for ${name} at ${targetMetadataFilePath}`); + 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 = []; target.push({"Name": name}); target.push({"DisplayName": target_source.DisplayName.replace("%platform%", platform)}); target.push({"Version": version}); target.push({"DownloadableArchives": release_data.name}); - // Because we cannot compute the uncompressed size ourselves, just give a generous estimate (to make - // sure they have enough disk space). + // Because we cannot compute the uncompressed size ourselves, just give a generous estimate + // (to make sure they have enough disk space). // OS flag is useless - i.e the installer stubs it :P target.push({"UpdateFile": [{_attr: {UncompressedSize: release_data.size * 2, CompressedSize: release_data.size, OS: "Any"}}]}); @@ -158,7 +162,7 @@ async function execute() { target.push({"Default": target_source.Default}); target.push({"Licenses": target_source.Licenses}); target.push({"Script": "installscript.qs"}); - target.push({"SHA": sha}); + target.push({"SHA": metaHash}); updates.push({"PackageUpdate": target}); }); @@ -168,11 +172,7 @@ async function execute() { const updatesXml = xml({"Updates": updates}, {indent: " "}); // Save Updates.xml - fs.writeFile(`${distDir}/Updates.xml`, updatesXml, function (err) { - if (err) { - throw err; - } - }); + fs.writeFileSync(`${distDir}/Updates.xml`, updatesXml); logger.info('Wrote a new Updates.xml file -- updates are available.'); } else { logger.info('No updates are available -- nothing to do.'); diff --git a/repository/package.json b/repository/package.json index bbbef5e..76a83f6 100644 --- a/repository/package.json +++ b/repository/package.json @@ -8,7 +8,7 @@ "dependencies": { "7zip-bin": "^2.1.0", "execa": "^0.7.0", - "fs-extra": "^4.0.0", + "fs-extra": "^4.0.1", "request": "^2.81.0", "request-promise": "^4.2.1", "sha1-file": "^1.0.0",