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

View File

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