Replaced fs sync functions
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2022-06-09 10:13:45 +02:00
parent a824da3908
commit 7fe7fb2e8f
3 changed files with 26 additions and 15 deletions

View File

@ -62,12 +62,15 @@ module.exports = Self => {
tempDir = `${tempPath}/${fileName}`;
tempFile = `${tempPath}/${fileName}.zip`;
if (!fs.existsSync(tempFile)) {
const downloadOutput = await downloadFile(remoteFile, tempFile);
if (downloadOutput.error)
continue;
} else
console.log('Already downloaded, skipping...');
try {
await fs.readFile(tempFile);
} catch (error) {
if (error.code === 'ENOENT') {
const downloadOutput = await downloadFile(remoteFile, tempFile);
if (downloadOutput.error)
continue;
}
}
console.debug(`Extracting file ${fileName}...`);
await extractFile(tempFile, tempDir);
@ -86,8 +89,12 @@ module.exports = Self => {
}
// Clean files
if (fs.existsSync(tempPath))
fs.rmSync(tempPath, {recursive: true, force: true});
try {
await fs.remove(tempPath);
} catch (error) {
if (error.code !== 'ENOENT')
throw e;
}
return true;
};
@ -165,8 +172,12 @@ module.exports = Self => {
async function extractFile(tempFile, tempDir) {
const JSZip = require('jszip');
if (!fs.existsSync(tempDir))
fs.mkdirSync(tempDir);
try {
await fs.mkdir(tempDir);
} catch (error) {
if (error.code !== 'EEXIST')
throw e;
}
const fileStream = await fs.readFile(tempFile);
if (fileStream) {
@ -180,7 +191,7 @@ module.exports = Self => {
for (const fileName of fileNames) {
const fileContent = await zip.file(fileName).async('nodebuffer');
const dest = path.join(tempDir, fileName);
fs.writeFileSync(dest, fileContent);
await fs.writeFile(dest, fileContent);
}
}
}
@ -197,14 +208,14 @@ module.exports = Self => {
const tableName = `edi.${toTable}`;
await Self.rawSql(`DELETE FROM ??`, [tableName], options);
const files = fs.readdirSync(tempDir)
.filter(file => file.startsWith(baseName));
const dirFiles = await fs.readdir(tempDir);
const files = dirFiles.filter(file => file.startsWith(baseName));
for (const file of files) {
console.log(`Dumping data from file ${file}...`);
const templatePath = path.join(__dirname, `./sql/${toTable}.sql`);
const sqlTemplate = fs.readFileSync(templatePath, 'utf8');
const sqlTemplate = await fs.readFile(templatePath, 'utf8');
const filePath = path.join(tempDir, file);
await Self.rawSql(sqlTemplate, [filePath], options);

View File

@ -8,7 +8,7 @@ CREATE TABLE `edi`.`fileConfig`
);
create unique index fileConfig_name_uindex
on fileConfig (name);
on `edi`.`fileConfig` (name);
INSERT INTO `edi`.`fileConfig` (name, checksum, keyValue)