Commit a6becde5 authored by Florent Chehab's avatar Florent Chehab

Move to python for generating frontend files, no need for Node or extra packages

parent a3cf92a7
let swig = require('swig');
let fs = require('fs');
let path = require('path');
let saving_dir = path.normalize(path.dirname(__filename) + "/../src/generated/")
if (!fs.existsSync(saving_dir)){
fs.mkdirSync(saving_dir);
}
let templates_dir = path.dirname(__filename) + '/templates/';
let templates = [
'action-types',
'actions',
'reducers'
]
let config = [
{
NAME: 'UNIVERSITIES',
name: 'universities'
}, {
NAME: 'COUNTRIES',
name: 'countries'
}
]
templates.forEach(function (filename) {
let template = swig.compileFile(templates_dir + filename + '.tpl');
let output = "";
config.forEach(function (obj) {
output += template(obj) + "\n\n";
})
fs.writeFile(saving_dir + filename + ".js", output, function (err) {
if (err) {
return console.log(err);
};
});
});
#####
# This python file is used to generate js files for redux
import os
from django import template
############
# Need to do this first so that Django template engine is working
import django
from django.conf import settings
settings.configure(TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['.'], # if you want the templates from a file
'APP_DIRS': False, # we have no apps
},
])
django.setup()
##########
current_dir = os.path.dirname(os.path.realpath(__file__))
templates_dir = current_dir + '/templates/'
saving_dir = os.path.realpath(current_dir + "/../src/generated/")
if not os.path.exists(saving_dir):
os.makedirs(saving_dir)
templates = [
'action-types',
'actions',
'reducers'
]
contexts = [
{
'NAME': 'UNIVERSITIES',
'name': 'universities'
}, {
'NAME': 'COUNTRIES',
'name': 'countries'
}
]
def read_file(file):
with open(file, "r") as myfile:
return myfile.read()
def write_file(file, data):
with open(file, 'w') as the_file:
the_file.write(data)
for filename in templates:
t = template.Template(
read_file(os.path.join(templates_dir, filename + '.tpl')))
output = ""
for tmpC in contexts:
c = template.Context(tmpC)
output += t.render(c) + "\n\n"
write_file(os.path.join(saving_dir, filename + '.js'), output)
export const {{NAME}}_HAS_ERROR = '{{NAME}}_HAS_ERROR'
export const {{NAME}}_IS_LOADING = '{{NAME}}_IS_LOADING'
export const {{NAME}}_FETCH_DATA_SUCCESS = '{{NAME}}_FETCH_DATA_SUCCESS'
export const {{NAME}}_INVALIDATED = '{{NAME}}_INVALIDATED'
// WARNING
// THIS FILE HAS BEEN AUTOMATICALLY GENERATED
// WITH /frontend/generate/generate_frontend_files.js
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
// THIS WARNING DOESN'T APPLY TO .tpl FILES...
export const {{NAME}}_HAS_ERROR = '{{NAME}}_HAS_ERROR';
export const {{NAME}}_IS_LOADING = '{{NAME}}_IS_LOADING';
export const {{NAME}}_FETCH_DATA_SUCCESS = '{{NAME}}_FETCH_DATA_SUCCESS';
export const {{NAME}}_INVALIDATED = '{{NAME}}_INVALIDATED';
// WARNING
// THIS FILE HAS BEEN AUTOMATICALLY GENERATED
// WITH /frontend/generate/generate_frontend_files.js
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
// THIS WARNING DOESN'T APPLY TO .tpl FILES...
import {
{{NAME}}_HAS_ERROR,
{{NAME}}_IS_LOADING,
......
// WARNING
// THIS FILE HAS BEEN AUTOMATICALLY GENERATED
// WITH /frontend/generate/generate_frontend_files.js
// MODIFY THE FILE ABOVE IF YOUR NOT SATISFIED
// THIS WARNING DOESN'T APPLY TO .tpl FILES...
import {
{{NAME}}_HAS_ERROR,
{{NAME}}_IS_LOADING,
......
......@@ -339,12 +339,6 @@
"integrity": "sha1-6GuBnGAs+IIa1jdBNpjx3sAhhHo=",
"dev": true
},
"amdefine": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/amdefine/-/amdefine-1.0.1.tgz",
"integrity": "sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU=",
"dev": true
},
"ansi-escapes": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz",
......@@ -485,12 +479,6 @@
"integrity": "sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c=",
"dev": true
},
"async": {
"version": "0.2.10",
"resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz",
"integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=",
"dev": true
},
"async-each": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.1.tgz",
......@@ -4940,24 +4928,6 @@
"mimic-fn": "^1.0.0"
}
},
"optimist": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
"integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=",
"dev": true,
"requires": {
"minimist": "~0.0.1",
"wordwrap": "~0.0.2"
},
"dependencies": {
"wordwrap": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
"integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=",
"dev": true
}
}
},
"optionator": {
"version": "0.8.2",
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.2.tgz",
......@@ -6377,69 +6347,6 @@
"has-flag": "^3.0.0"
}
},
"swig": {
"version": "1.4.2",
"resolved": "https://registry.npmjs.org/swig/-/swig-1.4.2.tgz",
"integrity": "sha1-QIXKBFM2kQS11IPihBs5t64aq6U=",
"dev": true,
"requires": {
"optimist": "~0.6",
"uglify-js": "~2.4"
},
"dependencies": {
"camelcase": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/camelcase/-/camelcase-1.2.1.tgz",
"integrity": "sha1-m7UwTS4LVmmLLHWLCKPqqdqlijk=",
"dev": true
},
"decamelize": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz",
"integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=",
"dev": true
},
"source-map": {
"version": "0.1.34",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.1.34.tgz",
"integrity": "sha1-p8/omux7FoLDsZjQrPtH19CQVms=",
"dev": true,
"requires": {
"amdefine": ">=0.0.4"
}
},
"uglify-js": {
"version": "2.4.24",
"resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-2.4.24.tgz",
"integrity": "sha1-+tV1XB4Vd2WLsG/5q25UjJW+vW4=",
"dev": true,
"requires": {
"async": "~0.2.6",
"source-map": "0.1.34",
"uglify-to-browserify": "~1.0.0",
"yargs": "~3.5.4"
}
},
"wordwrap": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz",
"integrity": "sha1-t5Zpu0LstAn4PVg8rVLKF+qhZD8=",
"dev": true
},
"yargs": {
"version": "3.5.4",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-3.5.4.tgz",
"integrity": "sha1-2K/49mXpTDS9JZvevRv68N3TU2E=",
"dev": true,
"requires": {
"camelcase": "^1.0.2",
"decamelize": "^1.0.0",
"window-size": "0.1.0",
"wordwrap": "0.0.2"
}
}
}
},
"symbol-observable": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.0.4.tgz",
......@@ -6629,12 +6536,6 @@
}
}
},
"uglify-to-browserify": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz",
"integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=",
"dev": true
},
"uglifyjs-webpack-plugin": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/uglifyjs-webpack-plugin/-/uglifyjs-webpack-plugin-1.3.0.tgz",
......@@ -6950,12 +6851,6 @@
"integrity": "sha1-2e8H3Od7mQK4o6j6SzHD4/fm6Ho=",
"dev": true
},
"window-size": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/window-size/-/window-size-0.1.0.tgz",
"integrity": "sha1-VDjNLqk7IC76Ohn+iIeu58lPnJ0=",
"dev": true
},
"wordwrap": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
......
......@@ -4,7 +4,7 @@
"description": "[![build](/../badges/master/build.svg)](https://gitlab.utc.fr/chehabfl/outgoing_rex/pipelines) [![coverage](/../badges/master/coverage.svg)](https://chehabfl.gitlab.utc.fr/outgoing_rex/) [![License](https://img.shields.io/badge/License-BSD%202--Clause-green.svg)](https://opensource.org/licenses/BSD-2-Clause)",
"main": "manage.py",
"scripts": {
"gen": "node ./frontend/generate/generate_frontend_files.js",
"gen": "python ./frontend/generate/generate_frontend_files.py",
"dev": "npm run gen && webpack --mode development ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"build": "npm run gen && webpack --mode production ./frontend/src/index.js --output ./frontend/static/frontend/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
......@@ -46,7 +46,6 @@
"react-script": "^2.0.5",
"recharts": "^1.1.0",
"redux-devtools": "^3.4.1",
"swig": "^1.4.2",
"weak-key": "^1.0.1",
"webpack": "^4.17.0",
"webpack-cli": "^3.1.0"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment