1- const print = console . log ;
2- const swal = require ( 'sweetalert' ) ;
3- const global = require ( "../../lib/global.js" )
4- const childprocess = require ( 'child_process' ) ;
5- var fs = require ( 'fs' ) ;
61const pythonFunction = require ( "../../lib/datafunctions" ) ;
7- const path = require ( 'path' ) ;
82const amdLoader = require ( 'monaco-editor/min/vs/loader.js' ) ;
93const amdRequire = amdLoader . require ;
104const amdDefine = amdLoader . require . define ;
115
12-
13- $ ( "#project-name" ) . text ( global . projectDetails . name ) ;
14- $ ( "#project-details" ) . text ( global . projectDetails . details . substr ( 0 , 20 ) + "..." ) ;
15-
16- let dir = global . projectDetails . name ;
17- let basepath = path . join ( process . cwd ( ) , "/../testing/Projects/" ) ;
18- global . editorText = fs . readFileSync ( path . join ( basepath , dir , "editor.py" ) , "utf8" ) ;
6+ var codeeditor ;
197
208function uriFromPath ( _path ) {
219 var pathName = path . resolve ( _path ) . replace ( / \\ / g, '/' ) ;
@@ -25,72 +13,40 @@ function uriFromPath(_path) {
2513 return encodeURI ( 'file://' + pathName ) ;
2614}
2715
28- amdRequire . config ( {
29- baseUrl : uriFromPath ( path . join ( __dirname , '../../node_modules/monaco-editor/min' ) )
30- } ) ;
31-
32-
33- amdRequire ( [ 'vs/editor/editor.main' ] , ( ) => {
34- var codeeditor = monaco . editor . create ( document . getElementById ( 'code-editor' ) , {
35- value : global . editorText ,
36- language : 'python' ,
37- autoIndent : true
38- } ) ;
39-
40- function loadPage ( page_path ) {
41- $ ( "#main-content" ) . html ( '' ) ;
42- $ ( "#main-content" ) . load ( page_path ) ;
43- }
44-
45- function testPython ( ) {
46- let codepath = path . join ( basepath , dir , "editor.py" ) ;
47- try {
48- fs . writeFileSync ( codepath , codeeditor . getValue ( ) , 'utf-8' ) ;
49- } catch ( e ) {
50- console . log ( 'Failed to save the file !' ) ;
16+ function saveProject ( isShow ) {
17+ let dir = globaljs . projectDetails . name ;
18+ globaljs . editorText = codeeditor . getValue ( ) ;
19+ fs . writeFile ( path . join ( projects_path , dir , "editor.py" ) , globaljs . editorText , 'utf-8' , err => {
20+ if ( err ) {
21+ swal ( "Saving Project" , "Failed to save project." , "error" ) ;
22+ print ( "Error writing file" , err ) ;
23+ } else {
24+ if ( isShow )
25+ swal ( "Saving Project" , "Project saved successfully." , "success" ) ;
5126 }
52-
53- var env = Object . create ( process . env ) ;
54- console . log ( env ) ;
55- var pythoncmd = process . platform == "win32" ? path . join ( env [ 'CONDA_PREFIX' ] , 'python.exe' ) : 'python3' ;
56- var pythonprocess = childprocess . spawn ( pythoncmd , [ '-m' , 'py_compile' , codepath ] , {
57- env : env
58- } ) ;
59-
60- pythonprocess . on ( 'close' , ( code ) => {
61- if ( code != 0 ) {
62- swal ( "Oops!" , "Error in code! Please correct the code and try again!" , "error" ) ;
63- } else {
64- global . editorText = codeeditor . getValue ( ) ;
65- loadPage ( "training/training.html" ) ;
66- }
67- console . log ( `child process exited with code ${ code } ` ) ;
68- } ) ;
69- }
70-
71-
72- function saveProject ( isShow ) {
73- global . editorText = codeeditor . getValue ( ) ;
74- fs . writeFile ( path . join ( basepath , dir , "editor.py" ) , global . editorText , 'utf-8' , err => {
75- if ( err ) {
76- swal ( "Saving Project" , "Failed to save project." , "error" ) ;
77- print ( "Error writing file" , err ) ;
78- } else {
79- if ( isShow )
80- swal ( "Saving Project" , "Project saved successfully." , "success" ) ;
81- }
82- } ) ;
83- }
84-
85- $ ( "#trainbutton" ) . click ( function ( ) {
86- testPython ( ) ;
8727 } ) ;
28+ }
8829
89- $ ( "#saveProject" ) . click ( function ( ) {
90- saveProject ( true ) ;
30+ function init ( ) {
31+ codeeditor = undefined ;
32+ $ ( "#project-name" ) . text ( globaljs . projectDetails . name ) ;
33+ $ ( "#project-details" ) . text ( globaljs . projectDetails . details . substr ( 0 , 20 ) + "..." ) ;
34+ let dir = globaljs . projectDetails . name ;
35+ globaljs . editorText = fs . readFileSync ( path . join ( projects_path , dir , "editor.py" ) , "utf8" ) ;
36+ amdRequire . config ( {
37+ baseUrl : uriFromPath ( path . join ( __dirname , '../../node_modules/monaco-editor/min' ) )
9138 } ) ;
92-
93- $ ( "#backButton" ) . click ( function ( ) {
94- loadPage ( "user/user.html" ) ;
39+
40+ amdRequire ( [ 'vs/editor/editor.main' ] , ( ) => {
41+ codeeditor = monaco . editor . create ( document . getElementById ( 'code-editor' ) , {
42+ value : globaljs . editorText ,
43+ language : 'python' ,
44+ autoIndent : true
45+ } ) ;
9546 } ) ;
96- } ) ;
47+ }
48+
49+ module . exports = {
50+ init : init ,
51+ saveProject : saveProject
52+ }
0 commit comments