1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include "NodeJSPackageJSON.h"
#include "json_node.h"
#include "NoteJSWorkspace.h"
NodeJSPackageJSON::NodeJSPackageJSON() {}
NodeJSPackageJSON::~NodeJSPackageJSON() {}
bool NodeJSPackageJSON::Load(const wxString& projectPath)
{
wxFileName filename(projectPath, "package.json");
filename.AppendDir(".codelite");
if(!filename.FileExists()) {
return false;
}
JSONRoot root(filename);
if(!root.isOk()) return false;
m_name = root.toElement().namedObject("name").toString();
m_version = root.toElement().namedObject("version").toString();
m_description = root.toElement().namedObject("description").toString();
m_script = root.toElement().namedObject("main").toString();
m_args = root.toElement().namedObject("args").toArrayString();
return true;
}
bool NodeJSPackageJSON::Create(const wxString& projectPath)
{
wxFileName filename(projectPath, "package.json");
if(!filename.FileExists()) {
return false;
}
JSONRoot root(filename);
if(!root.isOk()) return false;
m_name = root.toElement().namedObject("name").toString();
m_version = root.toElement().namedObject("version").toString();
m_description = root.toElement().namedObject("description").toString();
m_script = root.toElement().namedObject("main").toString();
// Convert the script into absolute path
m_script.MakeAbsolute(filename.GetPath());
// Ensure that the folder .codelite exists and "move" the file
// to that folder
filename.AppendDir(".codelite");
filename.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
return Save(projectPath);
}
bool NodeJSPackageJSON::Save(const wxString& projectPath)
{
wxFileName filename(projectPath, "package.json");
filename.AppendDir(".codelite");
// Override the previous settings
JSONRoot root(cJSON_Object);
JSONElement json = root.toElement();
json.addProperty("name", m_name);
json.addProperty("version", m_version);
json.addProperty("description", m_description);
if(!m_script.IsAbsolute()) {
m_script.MakeAbsolute(filename.GetPath());
}
json.addProperty("main", m_script.GetFullPath());
json.addProperty("args", m_args);
filename.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
root.save(filename);
return true;
}
|