Repository navigation
Expand file tree
/
Copy pathdefault-projects.js
More file actions
96 lines (89 loc) · 4.07 KB
/
Copy pathdefault-projects.js
File metadata and controls
96 lines (89 loc) · 4.07 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*globals Phoenix, JSZip, Filer, fs*/
define(function (require, exports, module) {
const ProjectManager = require("project/ProjectManager"),
Strings = require("strings"),
FileSystem = require("filesystem/FileSystem"),
ZipUtils = require("utils/ZipUtils");
async function setupStartupProject(forceCreate) {
console.log("setting up startup project", ProjectManager.getWelcomeProjectPath());
if(!forceCreate){
let exists = await Phoenix.VFS.existsAsync(ProjectManager.getWelcomeProjectPath());
if(exists){
console.log("Startup project already exists, using", ProjectManager.getWelcomeProjectPath());
return;
}
}
await ZipUtils.unzipURLToLocation('assets/default-project/en.zip', ProjectManager.getWelcomeProjectPath());
const indexHtmlPath = `${ProjectManager.getWelcomeProjectPath()}index.html`;
fs.readFile(indexHtmlPath, 'utf8', function (err, text) {
if(err || !text){
return;
}
if(!text.includes("CLICK_HERE")){
console.error("Default project index.html doesnt have CLICK_HERE!!!");
return;
}
const newText = text.replace("CLICK_HERE", Strings.DEFAULT_PROJECT_HTML_CLICK_HERE);
fs.writeFile(indexHtmlPath, newText, 'utf8', (writErr)=>{
if(writErr){
console.error("Error translating default project index.html", writErr);
}
});
});
}
/**
* https://github.com/orgs/phcode-dev/discussions/1930
* Strange_insults.html uses some words inappropriate for a school setting. so we delete the file as well
* as it was installed wrongly previously.
* This code can be removed after June 2025 to give 6 months for users to update.
*/
async function removeOffendingFile() {
let offendingFilePath = ProjectManager.getExploreProjectPath() + "/strange_insults.html";
let exists = await Phoenix.VFS.existsAsync(offendingFilePath);
if(exists){
FileSystem.getFileForPath(offendingFilePath).unlink();
}
}
async function setupExploreProject() {
let exploreProjectPath = ProjectManager.getExploreProjectPath();
let exists = await Phoenix.VFS.existsAsync(exploreProjectPath);
if(!exists){
console.log("setting up explore project", exploreProjectPath);
await ZipUtils.unzipURLToLocation('assets/sample-projects/explore.zip', exploreProjectPath);
}
}
exports.setupExploreProject = setupExploreProject;
exports.setupStartupProject = setupStartupProject;
exports.init = async function () {
if(Phoenix.firstBoot){
setupStartupProject(true);
}
if(!Phoenix.isNativeApp) {
// in browsers, we do this as the user wont see the explore project in documents folder anyway and will
// help in improved ux of fast project open. In desktop, we got complaint that users document dir is getting
// polluted with unwanted projects, so we dont do this on desktop and only create this when user explicitly
// clicks to open explore project.
setupExploreProject();
}
removeOffendingFile();
};
});