Pas 1: Iniciar un nou projecte
Executa les següents comandes:
mkdir koobin-control-panel
cd koobin-control-panel
yarn init
Omplena els camps que va demanant el prompt
.
El resultat ha deser un nou arxiu package.json
amb la següent estructura:
{
"name" : " koobin-control-panel" ,
"version" : " 1.0.0" ,
"description" : " Aplicació per generar automàticament noves instàncies Koobin." ,
"main" : " index.js" ,
"author" : " David Asensio Cañas" ,
"license" : " MIT" ,
"private" : false
}
Crea un nou arxiu anomenat index.js
:
Pas 2: Afegir nova dependència
Pas 3: Hello colorful world!
Crea la carpeta src
:
Crea un nou arxiu DyeIt.js
dins de la carpeta src
:
Implementa el codi:
const chalk = require ( 'chalk' )
const colors = [
chalk . red ,
chalk . green ,
chalk . yellow ,
chalk . blue ,
chalk . magenta ,
chalk . cyan ,
chalk . white ,
chalk . gray
] ;
function PickRandomColor ( ) {
return colors [ Math . floor ( Math . random ( ) * ( colors . length - 1 ) ) ] ;
}
function DyeIt ( textToDye ) {
let dyedText = '' ;
let charCount = textToDye . length ;
while ( charCount -- ) {
dyedText = PickRandomColor ( ) ( textToDye [ charCount ] ) + dyedText ;
}
return dyedText ;
}
module . exports = DyeIt ;
const DyeIt = require ( './src/DyeIt' ) ;
console . log ( `Hello ${ DyeIt ( 'colorful' ) } world!` ) ;
Pas 5: Registra un nou script npm
Afegeix al package.json
la següent clau:
{
"scripts" : {
"start" : "node index.js"
}
}
Pas 6: Executa l'script via Yarn
.
└── koobin-control-panel
├── index.js
├── node_modules
│ │...
├── package.json
├── src
│ └── DyeIt.js
└── yarn.lock
You're all set!! 👍