@node-turbodev
is a powerful development environment manager designed to provide developers with flexibility in managing various development environments. With @node-turbodev
, you can easily configure and customize your development workflow according to your project's requirements.
To install @node-turbodev
, simply run:
npm install @iorp/node-turbodev
To start using @node-turbodev
, create a configuration file named turbodev.config.js
in the root directory of your project. This configuration file allows you to define your development environment settings.
// turbodev.config.js
// Example configuration
module.exports = {
// Define your development environment settings here
};
Call the turbodev script runner from the required link
"start": "node node_modules/@iorp/node-turbodev/src/scripts/run.js before development && node node_modules/@iorp/node-turbodev/src/scripts/run.js init development && react-scripts start",
The turbodev.config.js
file allows you to define various environment settings for your development workflow. You can specify configurations such as environment variables, middleware, custom scripts, and more.
// turbodev.config.js
// Example configuration
module.exports = {
// Define your development environment settings here
};
In your package.json
file, you can use custom scripts to manage different development tasks using @node-turbodev
. Here's an example of how you can implement environment settings via scripts:
This scripts manage 3 environments.
"scripts": {
"start": "node node_modules/@iorp/node-turbodev/src/scripts/run.js before development && node node_modules/@iorp/node-turbodev/src/scripts/run.js init development && react-scripts start",
"build": "node node_modules/@iorp/node-turbodev/src/scripts/run.js before build && node node_modules/@iorp/node-turbodev/src/scripts/run.js init build && react-scripts build && node node_modules/@iorp/node-turbodev/src/scripts/run.js after build",
"dist": "node node_modules/@iorp/node-turbodev/src/scripts/run.js before dist && node node_modules/@iorp/node-turbodev/src/scripts/run.js init dist && react-scripts build BUILD_PATH='./dist' && node node_modules/@iorp/node-turbodev/src/scripts/run.js after dist",
}
Full turbodev.config.js
file example , used elsewhere
const os = require('os');
const fs = require('fs');
const path = require('path');
const initEnv = require("../node-turbodev/src/methods/initEnv.js");
const copy = require("@iorp/node-aid/src/filesystem/copy.js");
const makeDirectory = require("@iorp/node-aid/src/filesystem/makeDirectory.js");
const makeFile = require("@iorp/node-aid/src/filesystem/makeFile.js");
/**
* Environments object containing configuration for different environments.
* It includes keys like REACT_APP_API_URL, REACT_APP_TEST, and REACT_APP_HOMEPAGE.
*/
const Environments = {
// React
development: {
REACT_APP_API_URL: 'http://localhost/github/php-aid-app-resume',
REACT_APP_VERBOSE:true,
REACT_APP_HOMEPAGE: './',
REACT_APP_BASENAME:'/',
BASE_NAME: ''
},
// Build
build: {
// Shared with react also by webpack
REACT_APP_API_URL: 'http://localhost/github/php-aid-app-resume',
REACT_APP_VERBOSE:true,
REACT_APP_HOMEPAGE: 'http://localhost/github/react-aid-app-resume/build/',
REACT_APP_BASENAME:'/github/react-aid-app-resume/build',
// turbodev internal parameters
BUILD_PATH:'./build',
},
// Dist
dist: {
REACT_APP_API_URL: 'https://truegrains.vn/iorp/cvapi',
REACT_APP_HOMEPAGE: 'https://iorp.github.io/resume/', // this is where dist goes, rename dist as web and put in repo
REACT_APP_BASENAME: '/resume',
BUILD_PATH:'./dist', // where the dist folder is copied after build
APP_OUTPUT:"c:/xampp/htdocs/iorp.github.io/resume",
}
}
/**
* CommandCollection object containing development command logic.
*/
const CommandCollection = {
before: class {
/**
* Constructor for the before command.
* @param {Array} argv - The command line arguments.
*/
constructor(argv) {
//@todo This function must go to the turbodev
const makeWebpackConfig=(envName)=>{
const filePath = path.join("./webpack.config.js");
console.log('turbodev: makeWebpackConfig' );
const getReactAppConstants=(envName)=>{
// let r='';
// const environment = Environments[envName];
// for (const key in environment) {
// if (environment.hasOwnProperty(key) && key.startsWith('REACT_APP')) {
// r+=`'process.env.${key}': process.env.${key},\n` ;
// }
// }
const environment = Environments[envName];
let r = '';
const keys = Object.keys(environment).filter(key => key.startsWith('REACT_APP'));
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
r += `'process.env.${key}': process.env.${key}`;
if (i !== keys.length - 1) r += ',';
r+='\n';
}
return r ;
}
const code = `module.exports = {
plugins: [
new webpack.DefinePlugin({
${getReactAppConstants(envName)},
}),
],
};`;
let r= makeFile(filePath,code,{overwrite:true});
if(r.error){ console.log(r); process.exit(1);}
console.log('turbodev: Success')
};
//@todo, bring this 3 functions to turbodev END
(async ()=>{
try{
let r;
const envName = argv[0];
console.log('turbodev before: Begin');
makeWebpackConfig(envName);
console.log('turbodev before: Done');
}catch(error){
console.error( {error:true,code:"AFTER_COPY_EXCEPTION",exception:error});
}
})();
}
},
after: class {
/**
* Constructor for the after command.
* @param {Array} argv - The command line arguments.
*/
constructor(argv) {
//@todo, bring to turbodev library
const copyToOutputPath=async(envName)=>{
let r;
const environment = Environments[envName];
const targetPath =path.join('./'+envName);
console.log('turbodev: copyToOutputPath');
if(environment.APP_OUTPUT && environment.APP_OUTPUT.length>0){
console.log('Copy from environment '+envName + ' copied to ./'+targetPath);
makeDirectory(path.join(environment.APP_OUTPUT));
r= await copy( path.join( targetPath), path.normalize(environment.APP_OUTPUT), { verbose:true, overwrite:true });
if(r.error){ console.log(r); process.exit(1);}
console.log('turbodev: Success');
}
};
//@todo, bring to turbodev library
const makeHtaccess=(envName)=>{
const filePath = path.join("./",(envName=='development')?'':envName,'/.htaccess');
let r;
console.log('turbodev: makeHtaccess');
const code = `Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]`;
r = makeFile(filePath,code),{overwrite:true};
if(r.error){ console.log(r); process.exit(1);}
console.log('turbodev: Success')
};
(async ()=>{
try{
let r;
const envName = argv[0];
// Copy .htaccess
console.log('turbodev: Begin');
makeHtaccess(envName);
copyToOutputPath(envName);
console.log('turbodev: Done');
}catch(error){
console.error( {error:true,code:"AFTER_COPY_EXCEPTION",exception:error});
}
})();
}
},
init: class {
/**
* Constructor for the prestart command.
* @param {Array} argv - The command line arguments.
* @param {string} argv.0 - The environmnet to initialize
* call as init development|production|distribution
*/
constructor(argv) {
// Initialize the development environment using initEnv from @iorp/turbodev.development.devjs
// Note: Before using the 'init' command, ensure you have a dev.js file in the root of your project. And
// in this case also a package.json.
// The dev.js file should include the necessary configurations and environment variables.
// You can customize the Environments object based on your project requirements.
// The second parameter tells it to pass
const envName = argv[0];
let r= initEnv(envName,Environments, { 'homepage': 'REACT_APP_HOMEPAGE' },(envName)=>{
});
console.log(r)
}
}
}
module.exports = {
Environments,
CommandCollection
}
@node-turbodev
works by allowing developers to define environment settings and custom scripts in a turbodev.config.js
file. This configuration file enables developers to specify various aspects of their development workflow, such as environment variables, middleware, and custom scripts. The provided scripts in the package.json
file utilize @node-turbodev
to manage different development tasks, such as starting a development server, building the project, and creating a distribution version.