Skip to content
Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
97 changes: 68 additions & 29 deletions gulp/Tasks/TsTranspile.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -60,6 +59,6 @@
},
"volta": {
"node": "24.13.1",
"npm": "11.10.1"
"npm": "11.18.0"
}
}