Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 150 additions & 70 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const createWindow = () => {
}
});
codeWindow.setMenu(defaultMenu);
codeWindow.loadURL('simplycode://index.html')
codeWindow.loadURL('app.simplycode://index.html')
codeWindow.on('close', function() {
codeWindow = false;
});
Expand All @@ -60,7 +60,7 @@ const createTestWindow = () => {
allowRunningInsecureContent : true
}
})
codeWindow.loadURL('simplycode://index.html/tests/triplebinding/index.html')
codeWindow.loadURL('app.simplycode://index.html/tests/triplebinding/index.html')
codeWindow.on('close', function() {
codeWindow = false;
});
Expand All @@ -69,7 +69,7 @@ const createTestWindow = () => {
const createSecondWindow = (dataDir) => {
if (appWindow) {
appWindow.focus();
appWindow.loadURL('simplyapp://generated.html')
appWindow.loadURL('app.simplyapp://generated.html')
return;
}
appWindow = new BrowserWindow({
Expand Down Expand Up @@ -113,7 +113,7 @@ const createSecondWindow = (dataDir) => {
}
});
appWindow.setMenu(menu);
appWindow.loadURL('simplyapp://generated.html')
appWindow.loadURL('app.simplyapp://generated.html')
appWindow.on('close', function() {
appWindow = false;
});
Expand Down Expand Up @@ -157,6 +157,41 @@ function readRecursive(componentPath) {
}
}

function readDataRecursive(componentPath) {
if(componentPath.endsWith('\/')){
componentPath = componentPath.substring(0, (componentPath.length - 1))
}
const pathicles = componentPath.split('\/');
if (pathicles[0] == ''){
pathicles.shift()
}
const componentName = pathicles.pop();
const componentDirectory = pathicles.join('/');

var target = dataDir + componentDirectory + '\/' + componentName;

if (fs.lstatSync(target).isDirectory()) {
const files = fs.readdirSync(target);
const result = [];
files.forEach((file) => {
if (file !== '.' && file !== '..' && !file.startsWith('.')) {
let entries = readDataRecursive(componentPath + '\/' + file);
let ctime = fs.lstatSync(target + '\/' + file).ctime;
let data = {
"id" : file,
"ctime" : ctime,
"ctime-human" : new Date(ctime).toISOString(),
...entries
}
result.push(data);
}
});
return result;
} else {
return JSON.parse(fs.readFileSync(target, 'utf8'));
}
}

function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0,
Expand All @@ -178,15 +213,15 @@ app.commandLine.appendSwitch('disable-site-isolation-trials');

protocol.registerSchemesAsPrivileged([
{
scheme: 'simplycode',
scheme: 'app.simplycode',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true
}
},
{
scheme: 'simplyapp',
scheme: 'app.simplyapp',
privileges: {
standard: true,
secure: true,
Expand Down Expand Up @@ -216,9 +251,9 @@ async function createComponentFile(componentPath, filecontent){
app.whenReady().then(() => {
if (require('electron-squirrel-startup') === true) app.quit(); // prevents Squirrel.Windows from launching your app multiple times during the installation/updating/uninstallation.

protocol.handle('simplycode', (request) => {
protocol.handle('app.simplycode', (request) => {
let componentPath = new URL(request.url).pathname
console.log('[simplycode://]' + componentPath)
console.log('[app.simplycode://]' + componentPath)
if(componentPath.endsWith('\/')){
componentPath = componentPath.substring(0, (componentPath.length - 1))
}
Expand Down Expand Up @@ -329,9 +364,9 @@ app.whenReady().then(() => {
}
})

protocol.handle('simplyapp', (request) => {
protocol.handle('app.simplyapp', (request) => {
let componentPath = new URL(request.url).pathname
console.log('[simplyapp://]' + componentPath)
console.log('[app.simplyapp://]' + componentPath)
if(componentPath.endsWith('\/')){
componentPath = componentPath.substring(0, (componentPath.length - 1))
}
Expand All @@ -345,75 +380,120 @@ app.whenReady().then(() => {
pathicles.shift()
}
let componentDirectory = pathicles.join('/');

switch (request.method){
case 'OPTIONS':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
return new Response('"ok"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
break
case 'DELETE':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
fs.rmSync((dataDir + componentDirectory + "/" + componentName), { recursive: true, force: true })
return new Response('"deleted"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
break
case 'PUT':
return createComponentDirectory(componentDirectory)
.then(createComponentFile(componentDirectory + "/" + componentName, request))
.then(function() { return new Response('"ok"', { status: 200})})
.catch(function(){ return new Response('"something went wrong"', { status : 500 })}) // @TODO : return the error code
break
default:
if(componentPath.endsWith('\/')){
componentPath = componentPath.substring(0, (componentPath.length - 1))
}

if (!componentPath || componentPath === "/") {
const filestuff = fs.readFileSync(dataDir + '/generated.html')
return new Response(filestuff, {
// headers: { 'content-type': 'text/html' }
})

} else {
var target = dataDir + componentDirectory + '\/' + componentName;
let headers = {};
if (componentName.match(/\.svg$/)) {
headers = {
'content-type' : 'image/svg+xml'
}

if(pathicles[0] === "data") {
pathicles.shift();
componentDirectory = pathicles.join('/');

switch (request.method){
case 'OPTIONS':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
return new Response('"ok"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
if (componentName.match(/\.js$/)) {
headers = {
'content-type' : 'text/javascript'
}
break
case 'DELETE':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
fs.rmSync((dataDir + componentDirectory + "/" + componentName), { recursive: true, force: true })
return new Response('"deleted"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
break
case 'POST':
const newId = guid();
return createComponentDirectory(componentDirectory + "/" + componentName)
.then(createComponentFile(componentDirectory + "/" + componentName + "/" + newId, request))
.then(function() { return new Response(JSON.stringify(newId), { status: 201})})
.catch(function(){ return new Response('"something went wrong"', { status : 500 })}) // @TODO : return the error code
break
case 'PUT':
return createComponentDirectory(componentDirectory)
.then(createComponentFile(componentDirectory + "/" + componentName, request))
.then(function() { return new Response('"ok"', { status: 200})})
.catch(function(){ return new Response('"something went wrong"', { status : 500 })}) // @TODO : return the error code
break
case 'GET':
default:
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
const filestuff = readDataRecursive(componentDirectory + "/" + componentName)
return new Response(JSON.stringify(filestuff), {})
} else {
return new Response('"Not found"', { status: 404})
}
if(fs.existsSync(target)) {
const filestuff = fs.readFileSync(target)
break
}
} else {
switch (request.method){
case 'OPTIONS':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
return new Response('"ok"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
break
case 'DELETE':
if(fs.existsSync(dataDir + componentDirectory + "/" + componentName)){
fs.rmSync((dataDir + componentDirectory + "/" + componentName), { recursive: true, force: true })
return new Response('"deleted"', { status: 200})
} else {
return new Response('"Not found"', { status: 404})
}
break
case 'PUT':
return createComponentDirectory(componentDirectory)
.then(createComponentFile(componentDirectory + "/" + componentName, request))
.then(function() { return new Response('"ok"', { status: 200})})
.catch(function(){ return new Response('"something went wrong"', { status : 500 })}) // @TODO : return the error code
break
default:
if(componentPath.endsWith('\/')){
componentPath = componentPath.substring(0, (componentPath.length - 1))
}

if (!componentPath || componentPath === "/") {
const filestuff = fs.readFileSync(dataDir + '/generated.html')
return new Response(filestuff, {
headers: headers
// headers: { 'content-type': 'text/html' }
})

} else {
if (
(componentPath.indexOf("/js/simply") === 0) ||
(componentPath.indexOf("/simply") === 0) ||
(componentPath.indexOf("/assets") === 0) ||
(componentPath.indexOf("/hope") === 0)
) {
const filestuff = fs.readFileSync(__dirname + '/simplycode/' + componentDirectory + '\/' + componentName)
var target = dataDir + componentDirectory + '\/' + componentName;
let headers = {};
if (componentName.match(/\.svg$/)) {
headers = {
'content-type' : 'image/svg+xml'
}
}
if (componentName.match(/\.js$/)) {
headers = {
'content-type' : 'text/javascript'
}
}
if(fs.existsSync(target)) {
const filestuff = fs.readFileSync(target)
return new Response(filestuff, {
headers: headers
})
} else {
if (
(componentPath.indexOf("/js/simply") === 0) ||
(componentPath.indexOf("/simply") === 0) ||
(componentPath.indexOf("/assets") === 0) ||
(componentPath.indexOf("/hope") === 0)
) {
const filestuff = fs.readFileSync(__dirname + '/simplycode/' + componentDirectory + '\/' + componentName)
return new Response(filestuff, {
headers: headers
})
}
return new Response("Not found", {"status" : 404})
}
return new Response("Not found", {"status" : 404})
}
}
break
}
}
break
}
}
})

if (!process.argv[0].match(/electron$/) && process.argv[1]) {
Expand Down
Loading