Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
136 changes: 136 additions & 0 deletions .gtconfig/setup-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🔄 Setting up Jest...');

// Ensure src directory exists
if (!fs.existsSync('src')) {
fs.mkdirSync('src');
console.log('✅ Created src directory');
}

// Clean up Mocha files
try {
if (fs.existsSync('.mocharc.json')) {
fs.unlinkSync('.mocharc.json');
console.log('✅ Removed .mocharc.json');
}
} catch (error) {
console.log('⚠️ Could not remove .mocharc.json:', error.message);
}

// Uninstall Mocha dependencies
console.log('📦 Uninstalling Mocha dependencies...');
try {
execSync('npm uninstall @types/chai @types/mocha chai mocha', { stdio: 'inherit' });
console.log('✅ Mocha dependencies uninstalled');
} catch (error) {
console.log('⚠️ Some Mocha dependencies may not have been installed');
}

// Install Jest dependencies
console.log('📦 Installing Jest dependencies...');
try {
execSync('npm install --save-dev jest ts-jest @types/jest', { stdio: 'inherit' });
console.log('✅ Jest dependencies installed');
} catch (error) {
console.error('❌ Failed to install Jest dependencies:', error.message);
process.exit(1);
}

// Create Jest config
try {
const jestConfig = `const { createDefaultPreset } = require("ts-jest");

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import("jest").Config} **/
module.exports = {
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};`;
fs.writeFileSync('jest.config.js', jestConfig);
console.log('✅ Created jest.config.js');
} catch (error) {
console.error('❌ Failed to setup Jest config:', error.message);
process.exit(1);
}

// Create Jest test example
try {
const jestTest = `// This is a sample test written for Jest.
//
// If you prefer using Mocha+Chai instead, run \`npm run init:mocha\` in the terminal to setup Mocha dependencies.
// This will switch your project to use Mocha as the test runner with Chai for assertions.

import assert from "assert";

describe('Jest Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});`;
fs.writeFileSync('src/main.test.ts', jestTest);
console.log('✅ Created Jest test example in src/main.test.ts');
} catch (error) {
console.error('❌ Failed to setup Jest test:', error.message);
process.exit(1);
}

// Update package.json test script
try {
const packageJsonPath = 'package.json';
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = 'jest';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Updated package.json test script');
}
} catch (error) {
console.error('❌ Failed to update package.json:', error.message);
}

console.log('🎉 Jest setup complete! Reloading window now ...');


try {
const vscodeDir = '.vscode';
const settingsFile = path.join(vscodeDir, 'settings.json');

if (!fs.existsSync(vscodeDir)) {
fs.mkdirSync(vscodeDir);
}

let settings = {};
if (fs.existsSync(settingsFile)) {
try {
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
settings = JSON.parse(settingsContent);
} catch (error) {
console.log('⚠️ Could not parse existing settings.json, creating new one');
settings = {};
}
}

// Increment reload trigger counter
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;
settings["jest.enable"] = true;
settings["jest.runMode"] = "on-demand”;

// Write updated settings
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
} catch (error) {
console.log('⚠️ Could not reload workspace:', error.message);
console.log('Please reload the window manually...');
}

console.log('✅ Reloaded workspace');
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');
130 changes: 130 additions & 0 deletions .gtconfig/setup-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🔄 Setting up Mocha...');

// Ensure src directory exists
if (!fs.existsSync('src')) {
fs.mkdirSync('src');
console.log('✅ Created src directory');
}

// Clean up Jest files
try {
if (fs.existsSync('jest.config.js')) {
fs.unlinkSync('jest.config.js');
console.log('✅ Removed jest.config.js');
}
} catch (error) {
console.log('⚠️ Could not remove jest.config.js:', error.message);
}

// Uninstall Jest dependencies
console.log('📦 Uninstalling Jest dependencies...');
try {
execSync('npm uninstall jest ts-jest @types/jest', { stdio: 'inherit' });
console.log('✅ Jest dependencies uninstalled');
} catch (error) {
console.log('⚠️ Some Jest dependencies may not have been installed');
}

// Install Mocha dependencies
console.log('📦 Installing Mocha dependencies...');
try {
execSync('npm install --save-dev @types/chai @types/mocha chai mocha ts-node', { stdio: 'inherit' });
console.log('✅ Mocha dependencies installed');
} catch (error) {
console.error('❌ Failed to install Mocha dependencies:', error.message);
process.exit(1);
}

// Create Mocha config
try {
const mochaConfig = `{
"extension": [
"ts"
],
"spec": "src/**/*.test.ts",
"require": "ts-node/register"
}`;
fs.writeFileSync('.mocharc.json', mochaConfig);
console.log('✅ Created .mocharc.json');
} catch (error) {
console.error('❌ Failed to setup Mocha config:', error.message);
process.exit(1);
}

// Create Mocha test example
try {
const mochaTest = `// We support Mocha + Chai for unit testing by default.
//
// If you prefer using Jest instead, run \`npm run init:jest\` in the terminal to setup Jest dependencies.
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.

import { assert } from 'chai';

describe('Mocha Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});`;
fs.writeFileSync('src/main.test.ts', mochaTest);
console.log('✅ Created Mocha test example in src/main.test.ts');
} catch (error) {
console.error('❌ Failed to setup Mocha test:', error.message);
process.exit(1);
}

// Update package.json test script
try {
const packageJsonPath = 'package.json';
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = 'mocha';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Updated package.json test script');
}
} catch (error) {
console.error('❌ Failed to update package.json:', error.message);
}

console.log('🎉 Mocha setup complete! Reloading window now ...');

try {
const vscodeDir = '.vscode';
const settingsFile = path.join(vscodeDir, 'settings.json');

if (!fs.existsSync(vscodeDir)) {
fs.mkdirSync(vscodeDir);
}

let settings = {};
if (fs.existsSync(settingsFile)) {
try {
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
settings = JSON.parse(settingsContent);
} catch (error) {
console.log('⚠️ Could not parse existing settings.json, creating new one');
settings = {};
}
}

// Increment reload trigger counter
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;
settings["jest.enable"] = false;

// Write updated settings
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
} catch (error) {
console.log('⚠️ Could not reload workspace:', error.message);
console.log('Please reload the window manually...');
}

console.log('✅ Reloaded workspace');
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');
5 changes: 5 additions & 0 deletions .gtignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ package.json
yarn.lock
package-lock.json
.theia/
.vscode/
# Ignore all node_modules folders
**/node_modules/
.gtconfig/
.mocharc.json
jest.config.js
dist/
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": [
"ts"
],
"spec": "src/**/*.test.ts",
"require": "ts-node/register"
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jest.runMode": "on-demand",
"editor.formatOnSave": true
}
22 changes: 22 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Install Dependencies",
"type": "shell",
"command": "npm install --include=dev",
"group": "build",
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "dedicated",
"close": true
},
"runOptions": {
"runOn": "folderOpen"
},
"problemMatcher": []
}
]
}
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
"name": "typescript-interview-command-line-starter-kit",
"version": "0.1.0",
"devDependencies": {
"@types/node": "^20.19.7"
"@types/chai": "^5.2.2",
"@types/mocha": "^10.0.10",
"@types/node": "^24.0.14",
"chai": "^5.2.1",
"mocha": "^11.7.1",
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
},
"license": "MIT"
"scripts": {
"test": "mocha",
"init:jest": "node .gtconfig/setup-jest.js",
"init:mocha": "node .gtconfig/setup-mocha.js"
},
"license": "MIT"
}
12 changes: 12 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// We support Mocha + Chai for unit testing by default.
//
// If you prefer using Jest instead, run `npm run init:jest` in the terminal to setup Jest dependencies.
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.

import { assert } from 'chai';

describe('Mocha Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});
10 changes: 6 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ function main() {
throw new Error("No command line arguments passed")
}
/*
* Format of the 'args' array: [`<COMMAND_NAME_1> <ARG1> <ARG2> .. <ARG N>`, `<COMMAND_NAME_2> <ARG1> <ARG2> .. <ARG N>`]
* Example: ["PLACE_ORDER 101 Apple 5", "TOTAL_COST 101"]
* Format of the 'args' array: [
* <COMMAND_NAME_1> <ARG1> <ARG2> .. <ARG N>,
* <COMMAND_NAME_2> <ARG1> <ARG2> .. <ARG N>
*]
*
* The code evaluator will execute this code by using the command
* node main.js 'PLACE_ORDER 101 Apple 5' 'TOTAL_COST 101'
* node main.js 'COMMAND_NAME_1 ARG1 ARG2 ARG3' 'COMMAND_NAME_2 ARG1'
*
* We loop through the list of commands passed in as input arguments and handle each one of them
*/
Expand All @@ -33,4 +35,4 @@ function handle(cmd: string) {
//TODO: implement the logic to handle each input
}

main()
main()
Loading