Skip to content

Commit

Permalink
fixing ci issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tnylea committed Jan 2, 2025
1 parent 035bbb5 commit 408c6a2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 70 deletions.
93 changes: 52 additions & 41 deletions bin/static.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const execSync = require('child_process').execSync;
const packageJson = require('../package.json');
const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');

function executeCommand(command) {
return execSync(command, { encoding: 'utf8' });
}

function cleanTestDirectory() {
if (fs.existsSync('testProject')) {
fs.removeSync('testProject');
}
}

describe('bin/static CLI', () => {
beforeEach(() => {
process.env.NODE_ENV = 'test';
// Clean up test directory if it exists
if (fs.existsSync('testProject')) {
execSync('rm -rf testProject');
}
cleanTestDirectory();
});

afterEach(() => {
// Clean up test directory
if (fs.existsSync('testProject')) {
execSync('rm -rf testProject');
}
cleanTestDirectory();
});

it('should return version', () => {
Expand All @@ -28,39 +29,49 @@ describe('bin/static CLI', () => {
});

it('should create a new project', (done) => {
process.env.NODE_ENV = 'test';
const output = executeCommand('./bin/static new testProject');

// Give the async operations time to complete
setTimeout(() => {
try {
expect(fs.existsSync('testProject')).toBe(true);
expect(fs.existsSync('testProject/pages')).toBe(true);
expect(output.trim()).toBe([
"New setup initialized",
"Downloading starter template",
"Finished downloading template",
"Extracting template zip file",
"Finished unzipping",
"New site available inside testProject folder"
].join('\n'));
done();
} catch (err) {
done(err);
}
}, 5000); // Wait 5 seconds for async operations
try {
const output = executeCommand('./bin/static new testProject');

// Give the async operations time to complete
setTimeout(() => {
try {
expect(fs.existsSync('testProject')).toBe(true);
expect(output.trim()).toBe([
"New setup initialized",
"Downloading starter template",
"Finished downloading template",
"Extracting template zip file",
"Finished unzipping",
"New site available inside testProject folder"
].join('\n'));
done();
} catch (err) {
done(err);
}
}, 5000);
} catch (err) {
done(err);
}
});

it('should build project', () => {
// First create the project and wait for it to complete
executeCommand('./bin/static new testProject');
// Give it time to finish async operations
execSync('sleep 5');

const output = executeCommand('cd testProject && ../bin/static build relative');
expect(output.trim()).toBe("Successfully built your new static website 🤘");

// Verify the build output exists
expect(fs.existsSync('testProject/_site')).toBe(true);
it('should build project', (done) => {
try {
// First create the project
executeCommand('./bin/static new testProject');

// Wait for project creation to complete
setTimeout(() => {
try {
const output = executeCommand('cd testProject && ../bin/static build relative');
expect(output.trim()).toBe("Successfully built your new static website 🤘");
expect(fs.existsSync(path.join('testProject', '_site'))).toBe(true);
done();
} catch (err) {
done(err);
}
}, 5000);
} catch (err) {
done(err);
}
});
});
58 changes: 29 additions & 29 deletions src/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const request = require('superagent');
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 openurl = require('openurl');
const path = require('path');
Expand Down Expand Up @@ -65,39 +64,40 @@ module.exports = {
throw new Error('Template directory not found after extraction');
}

mv(sourceDir, process.cwd(), {mkdirp: false, clobber: false}, function(err) {
if (err) {
console.error('Failed to move template files:', err);
reject(err);
return;
}
// Move contents of source directory to current directory
const files = fs.readdirSync(sourceDir);
for (const file of files) {
const sourcePath = path.join(sourceDir, file);
const destPath = path.join(process.cwd(), file);
fs.moveSync(sourcePath, destPath, { overwrite: true });
}
fs.removeSync(sourceDir);

console.log('New site available inside ' + folderName + ' folder');
console.log('New site available inside ' + folderName + ' folder');

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

// Start dev server for non-test environment
const devServer = require(require("global-modules-path").getPath("@devdojo/static") + '/src/dev.js');

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);
reject(err);
return;
}

// Start dev server for non-test environment
const devServer = require(require("global-modules-path").getPath("@devdojo/static") + '/src/dev.js');

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);
reject(err);
return;
}

devServer.start(false)
.then((port) => {
openurl.open('http://localhost:' + port);
resolve();
})
.catch(reject);
});
devServer.start(false)
.then((port) => {
openurl.open('http://localhost:' + port);
resolve();
})
.catch(reject);
});
} catch (err) {
console.error('Failed during template extraction:', err);
Expand Down

0 comments on commit 408c6a2

Please sign in to comment.