Skip to content

Commit

Permalink
⚡ Change build, projects', export folders to be stored under `~/ct.js…
Browse files Browse the repository at this point in the history
…/` directory
  • Loading branch information
CosmoMyzrailGorynych committed Aug 25, 2020
1 parent 3ba237a commit f4df658
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
57 changes: 42 additions & 15 deletions src/node_requires/platformUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const isMac = !(isWin || isLinux);

const mod = {
isWin,
isWindows: isWin,
isLinux,
isMac,

Expand Down Expand Up @@ -48,17 +49,22 @@ const mod = {
return new Promise((resolve, reject) => {
// writing to an exec path on Mac is not a good idea,
// as it will be hidden inside an app's directory, which is shown as one file.
if (isMac || !(execWritable)) {
if (!homeWritable) {
reject(new Error(`Could not write to folders ${home} and ${exec}.`));
} else {
fs.ensureDir(path.join(home, 'ct.js'))
.then(() => {
resolve(path.join(home, 'ct.js'));
})
.catch(reject);
}
if (isMac && !homeWritable) {
reject(new Error(`Could not write to folder ${home}. It is needed to create builds and run debugger. Check rights to these folders, and tweak sandbox settings if it is used.`));
return;
}
// Home directory takes priority
if (homeWritable) {
fs.ensureDir(path.join(home, 'ct.js'))
.then(() => {
resolve(path.join(home, 'ct.js'));
})
.catch(reject);
} else {
if (!execWritable) {
reject(new Error(`Could not write to folders ${home} and ${exec}. A folder is needed to create builds and run debugger. Check access rights to these folders, and tweak sandbox settings if it is used.`));
return;
}
resolve(exec);
}
});
Expand All @@ -69,7 +75,9 @@ const mod = {
let exportDir,
exportDirPromise,
buildDir,
buildDirPromise;
buildDirPromise,
projectsDir,
projectsDirPromise;
// We compute a directory once and store it forever
mod.getExportDir = () => {
if (exportDir) {
Expand All @@ -78,8 +86,10 @@ const mod = {
if (exportDirPromise) {
return exportDirPromise;
}
exportDirPromise = mod.getWritableDir().then(dir => {
exportDir = require('path').join(dir, 'export');
exportDirPromise = mod.getWritableDir().then(async ctjsDir => {
const dir = require('path').join(ctjsDir, 'Exported');
await fs.ensureDir(dir);
exportDir = dir;
return exportDir;
});
return exportDirPromise;
Expand All @@ -91,12 +101,29 @@ const mod = {
if (buildDirPromise) {
return buildDirPromise;
}
buildDirPromise = mod.getWritableDir().then(dir => {
buildDir = require('path').join(dir, 'build');
buildDirPromise = mod.getWritableDir().then(async ctjsDir => {
const dir = require('path').join(ctjsDir, 'Builds');
await fs.ensureDir(dir);
buildDir = dir;
return buildDir;
});
return buildDirPromise;
};
mod.getProjectsDir = () => {
if (projectsDir) {
return Promise.resolve(projectsDir);
}
if (projectsDirPromise) {
return projectsDirPromise;
}
projectsDirPromise = mod.getWritableDir().then(async ctjsDir => {
const dir = require('path').join(ctjsDir, 'Projects');
await fs.ensureDir(dir);
projectsDir = dir;
return projectsDir;
});
return projectsDirPromise;
};
}

module.exports = mod;
8 changes: 6 additions & 2 deletions src/node_requires/resources/projects/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const defaultProject = require('./defaultProject');

/**
* @returns {Promise<string>} A promise that resolves into the absolute path
* to the projects' directory
*/
const getDefaultProjectDir = function () {
const path = require('path');
return path.join(nw.App.startPath, 'projects');
const {getProjectsDir} = require('./../../platformUtils');
return getProjectsDir();
};

const getExamplesDir = function () {
Expand Down
11 changes: 7 additions & 4 deletions src/riotTags/main-menu.tag
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,14 @@ main-menu.flexcol
}
};
this.zipExport = async () => {
const {getWritableDir, getExportDir} = require('./data/node_requires/platformUtils');
const writable = await getWritableDir();
const {getBuildDir, getExportDir} = require('./data/node_requires/platformUtils');
const buildFolder = await getBuildDir();
const runCtExport = require('./data/node_requires/exporter');
const exportFile = path.join(writable, '/export.zip'),
inDir = await getExportDir();
const exportFile = path.join(
buildFolder,
`${currentProject.settings.authoring.title || 'ct.js game'}.zip`
);
const inDir = await getExportDir();
await fs.remove(exportFile);
runCtExport(global.currentProject, global.projdir)
.then(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/riotTags/project-selector.tag
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ project-selector
* Handler for a manual search for a project folder, triggered by an input[type="file"]
*/
this.chooseProjectFolder = async () => {
const defaultProjectDir = require('./data/node_requires/resources/projects').getDefaultProjectDir();
const {getProjectsDir} = require('./data/node_requires/platformUtils');
const defaultProjectDir = await getProjectsDir() + '/';
const projPath = await window.showOpenDialog({
title: this.voc.newProject.selectProjectFolder,
defaultPath: defaultProjectDir,
Expand Down

0 comments on commit f4df658

Please sign in to comment.