Skip to content

Commit

Permalink
rework build script
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderPolosatov committed Dec 10, 2023
1 parent 73d0839 commit c5b2943
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
36 changes: 16 additions & 20 deletions build_scripts/prepare_data.mjs
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
import { join } from 'path';
import { writeFileSync, readFileSync } from 'fs';
import { createIfNotExist, getAllDirs } from './utils/utils.mjs';

import { createIfNotExist } from './utils/utils.mjs';

const projectsInfo = [{
projectPath: './projects/atm_data',
fileName: 'data.json'
}, {
projectPath: './projects/regions_data',
fileName: 'data.json'
}];
const STATIC_DATA_FOLDER = './static_data';
const RAW_DATA_FILE_NAME = 'raw_data.json';
const DIST_DATA_FILE_NAME = 'data.json';

function removeSpacesAndNewlinesOutsideQuotes(inputFilePath, outputFilePath) {
try {
const content = readFileSync(inputFilePath, 'utf-8');

const processedContent = content.replace(/("[^"]*")|\s/g, (match, group1) => {
if(group1) {
if (group1) {
return group1;
}
return '';
});

writeFileSync(outputFilePath, processedContent, 'utf-8');
console.log('Data prepared');
console.log(`Data of ${inputFilePath} file prepared`);
} catch (error) {
console.error('Error:', error.message);
}
}

function prepareProjects(projects) {
projects.forEach(project => {
const distFolder = join(project.projectPath, 'dist');
function prepareProjects(projectsFolders) {
projectsFolders.forEach(projectFolder => {
const distFolder = join(projectFolder, 'dist');

createIfNotExist(distFolder);
const inputFilePath = join(project.projectPath, project.fileName);
const outputFilePath = join(distFolder, project.fileName);


const inputFilePath = join(projectFolder, RAW_DATA_FILE_NAME);
const outputFilePath = join(distFolder, DIST_DATA_FILE_NAME);

removeSpacesAndNewlinesOutsideQuotes(inputFilePath, outputFilePath);
});
}

function main() {
prepareProjects(projectsInfo);
const allProjectsFolders = getAllDirs(STATIC_DATA_FOLDER);

prepareProjects(allProjectsFolders);
}

main();
17 changes: 15 additions & 2 deletions build_scripts/utils/utils.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { existsSync, mkdirSync } from 'fs';
import { existsSync, mkdirSync, readdirSync, lstatSync } from 'fs';
import { join } from 'path';

const isDirectory = fileName => {
return lstatSync(fileName).isDirectory();
};

export function createIfNotExist(dirPath) {
if (!existsSync(dirPath)){
mkdirSync(dirPath, { recursive: true });
}
}
}

export function getAllDirs(path) {
const files = readdirSync(path);

return files.map(fileName => {
return join(path, fileName);
}).filter(isDirectory);
}
1 change: 0 additions & 1 deletion projects/atm_data/dist/data.json

This file was deleted.

1 change: 0 additions & 1 deletion projects/regions_data/dist/data.json

This file was deleted.

File renamed without changes.
File renamed without changes.

0 comments on commit c5b2943

Please sign in to comment.