Skip to content

Commit

Permalink
Bugfix: Properly decompress the template archive
Browse files Browse the repository at this point in the history
Handle URL opening on systems without a GUI
  • Loading branch information
ma2t committed Oct 28, 2023
1 parent 296d0aa commit 6bbea3b
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions src/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const globalModulesPath = require("global-modules-path");
const process = require('process');
const admZip = require('adm-zip');
var mv = require('mv');
const { exec } = require("child_process");
const {
exec
} = require("child_process");
const openurl = require('openurl');
const path = require('path');
const templates = require('./templates.js');
Expand All @@ -17,16 +19,20 @@ module.exports = {
},
createFolder(folderName) {
console.log('creating new folder ' + folderName);
fs.mkdir('./' + folderName , { recursive: true }, (err) => {
fs.mkdir('./' + folderName, {
recursive: true
}, (err) => {
if (err) throw err;
});
});
},
newProject(folderName, template = '--starter') {
console.log('New setup initialized');
template = templates.get(template.replace('--', ''));
templateZipFile = `${template.repo}/archive/${zipFile}`;
repoName = template.repo.split('/').pop();
fs.mkdirSync('./' + folderName , { recursive: true });
fs.mkdirSync('./' + folderName, {
recursive: true
});

process.chdir(process.cwd() + '/' + folderName);
console.log('Downloading ' + template.slug + ' template');
Expand All @@ -40,43 +46,65 @@ module.exports = {
console.log('Finished downloading template');
var zip = new admZip(zipFile);
console.log('Extracting template zip file');
zip.extractAllTo(process.cwd());
console.log('Finished unzipping');
fs.unlinkSync(`./${zipFile}`);

mv(process.cwd() + '/' + repoName + '-main', process.cwd(), {mkdirp: false, clobber: false}, function(err) {
console.log('New site available inside ' + folderName + ' folder');
var zipEntries = zip.getEntries();

let devServer = require(require("global-modules-path").getPath("@devdojo/static") + '/src/dev.js');
zipEntries.forEach(function(zipEntry) {
if (zipEntry.entryName.startsWith(repoName + '-main/')) {
var newEntryName = zipEntry.entryName.replace(repoName + '-main/', '');

if(process.env.NODE_ENV == 'test'){
return;
if (newEntryName) {
// Check if the entry is a directory
if (/\/$/.test(newEntryName)) {
// Create the directory if it's a subfolder
fs.mkdirSync(path.join(process.cwd(), newEntryName), {
recursive: true
});
} else {
zip.extractEntryTo(zipEntry.entryName, process.cwd(), false, true, newEntryName);
}
}
}
});

process.chdir(process.cwd());


console.log('Finished unzipping');
fs.unlinkSync(`./${zipFile}`);

console.log('processing template builds and starting dev server');
exec("cd " + process.cwd() + " && npm install && static build", (err, stdout, stderr) => {
if (err) {
console.error("Error building assets, please re-run static dev command.");
console.error(err);
}
let devServerPort = devServer.start(false);
console.log('New site available inside ' + folderName + ' folder');

devServerPort.then((port) => {
openurl.open('http://localhost:' + port);
});
let devServer = require(require("global-modules-path").getPath("@devdojo/static") + '/src/dev.js');

});
if (process.env.NODE_ENV == 'test') {
return;
}

});
process.chdir(process.cwd());

console.log('processing template builds and starting dev server');
exec("npm install && static build", (err, stdout, stderr) => {
if (err) {
console.error("Error building assets, please re-run static dev command.");
console.error(err);
}
let devServerPort = devServer.start(false);


const {
spawn
} = require('child_process');


devServerPort.then((port) => {
console.log(`Server running at http://localhost:${port}`);
try {
const child = spawn('xdg-open', [`http://localhost:${port}`]);

child.on('error', (error) => {
console.error('Failed to open URL:', error);
});
} catch (error) {
console.error('Failed to spawn process:', error);
}
});
});
});
}
}

0 comments on commit 6bbea3b

Please sign in to comment.