diff --git a/README.md b/README.md index 710a8fc2..54c7c9cb 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,24 @@ We highly recommend the usage of the following tools: * Document This * ESLint * Prettier - Code formatter +* [Volta](https://volta.sh/) — manages Node.js and npm versions for this project + +#### Node.js and npm (Volta) + +This project pins Node.js and npm versions in `package.json`. Install [Volta](https://volta.sh/) once and the correct versions are applied automatically when you work in this repository. + +**Install Volta:** follow the official [Getting Started guide](https://docs.volta.sh/guide/getting-started) (macOS, Windows, and Linux). + +**Verify Volta is active:** + +```bash +volta --version # Volta is installed +volta which node # pinned Node version for this project +volta which npm # pinned npm version for this project +node -v +npm -v +``` + ### How to use change this code? 1. Create a branch based in the branch **master** (lastest & greatest release) diff --git a/gulp/Tasks/TsTranspile.js b/gulp/Tasks/TsTranspile.js index 10d925a4..ffe7e824 100644 --- a/gulp/Tasks/TsTranspile.js +++ b/gulp/Tasks/TsTranspile.js @@ -1,8 +1,7 @@ -const gulp = require('gulp'); const { series } = require('gulp'); const fs = require('fs'); -const sourcemaps = require('gulp-sourcemaps'); -const ts = require('gulp-typescript'); +const path = require('node:path'); +const ts = require('typescript'); const distFolder = './dist'; const project = require('../DefaultSpecs'); @@ -53,6 +52,22 @@ function rollBackTsConfigFile() { defaultTsConfigText = ''; } +function formatDiagnostics(diagnostics) { + const formatHost = { + getCanonicalFileName: (fileName) => fileName, + getCurrentDirectory: ts.sys.getCurrentDirectory, + getNewLine: () => ts.sys.newLine, + }; + + diagnostics.forEach((diagnostic) => { + console.error(ts.formatDiagnosticWithColorAndContext(diagnostic, formatHost)); + }); +} + +function hasCompilationErrors(diagnostics) { + return diagnostics.some((diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error); +} + // Compile TypeScript function tsTranspile(cb, envMode, platformType) { // Store the default platformType @@ -87,39 +102,63 @@ function tsTranspile(cb, envMode, platformType) { } // Method that will trigger the transpile of Ts according if it's development or production mode and platform type (O11 or ODC) -async function tsTranspileBasedOnPlatform(cb, envMode, platformType, shouldCreateAll) { - let tsProject = ts.createProject('tsconfig.json', { - outDir: distFolder, - declaration: envMode === project.globalConsts.envType.production ? true : false, - outFile: `${envMode === project.globalConsts.envType.production ? '' : envMode + '.'}${ - platformType !== '' ? platformType + '.' : '' - }${project.globalConsts.fileName}.js`, +function tsTranspileBasedOnPlatform(cb, envMode, platformType, shouldCreateAll) { + const outFileName = `${envMode === project.globalConsts.envType.production ? '' : envMode + '.'}${ + platformType !== '' ? platformType + '.' : '' + }${project.globalConsts.fileName}.js`; + const configPath = path.resolve('tsconfig.json'); + const configFile = ts.readConfigFile(configPath, ts.sys.readFile); + + if (configFile.error) { + formatDiagnostics([configFile.error]); + if (defaultTsConfigText !== '') { + rollBackTsConfigFile(); + } + cb(new Error('Failed to read tsconfig.json')); + return; + } + + const parsedConfig = ts.parseJsonConfigFileContent( + configFile.config, + ts.sys, + path.dirname(configPath), + { + outFile: path.join(distFolder, outFileName), + declaration: envMode === project.globalConsts.envType.production, + sourceMap: envMode === project.globalConsts.envType.development, + }, + configPath + ); + + const compilerHost = ts.createCompilerHost(parsedConfig.options, true); + const program = ts.createProgram({ + rootNames: parsedConfig.fileNames, + options: parsedConfig.options, + host: compilerHost, }); - if (envMode === project.globalConsts.envType.development) { - tsProject - .src() - .pipe(sourcemaps.init()) - .pipe(tsProject()) - .js.pipe(sourcemaps.write('.')) - .pipe(gulp.dest(distFolder)) - .on('finish', () => { - onTsCompileFinish(platformType, cb, shouldCreateAll); - }); - } else { - tsProject - .src() - .pipe(tsProject()) - .pipe(gulp.dest(distFolder)) - .on('finish', () => { - onTsCompileFinish(platformType, cb, shouldCreateAll); - }); + const emitResult = program.emit(); + const diagnostics = [ + ...parsedConfig.errors, + ...program.getSemanticDiagnostics(), + ...program.getDeclarationDiagnostics(), + ...emitResult.diagnostics, + ]; + + if (diagnostics.length > 0) { + formatDiagnostics(diagnostics); } - // Rollback tsconfig file to the default state if (defaultTsConfigText !== '') { rollBackTsConfigFile(); } + + if (hasCompilationErrors(diagnostics)) { + cb(new Error('TypeScript compilation failed')); + return; + } + + onTsCompileFinish(platformType, cb, shouldCreateAll); } // Set as Development Mode diff --git a/package.json b/package.json index e1c55eda..d2930049 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "gulp-clean": "^0.4.0", "gulp-confirm": "^1.0.8", "gulp-sourcemaps": "^3.0.0", - "gulp-typescript": "^6.0.0-alpha.1", "prettier-eslint": "^16.3.0", "prompts": "^2.4.2", "stylelint": "^14.16.1", @@ -60,6 +59,6 @@ }, "volta": { "node": "24.13.1", - "npm": "11.10.1" + "npm": "11.18.0" } }