Skip to content

Commit f07bc89

Browse files
committed
✨ (gulpfile) split gulpfile tasks into sub-tasks files
1 parent 20eb4df commit f07bc89

19 files changed

+1893
-1444
lines changed

.tasks/_utils.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
join,
3+
dirname
4+
} from 'path'
5+
import { readFileSync } from 'fs'
6+
import { fileURLToPath } from 'url'
7+
8+
// We cannot use directly the import.meta.dirname or filename because all IDE usage do not produce them
9+
// We use
10+
function getDirname() {
11+
12+
let __dirname
13+
14+
if ( import.meta.dirname ) {
15+
__dirname = import.meta.dirname
16+
} else if ( import.meta.filename ) {
17+
__dirname = dirname( import.meta.filename )
18+
} else if ( import.meta.url ) {
19+
const __filename = fileURLToPath( import.meta.url )
20+
__dirname = join( dirname( __filename ), '..' )
21+
} else {
22+
throw new Error( 'Unable to retrieve module dirname.' )
23+
}
24+
25+
return __dirname
26+
27+
}
28+
29+
const __dirname = getDirname()
30+
const packagePath = join( __dirname, 'package.json' )
31+
const packageData = readFileSync( packagePath )
32+
const packageInfos = JSON.parse( packageData )
33+
34+
class Indenter {
35+
36+
_ = ''
37+
__ = ''
38+
___ = ''
39+
____ = ''
40+
_____ = ''
41+
______ = ''
42+
_______ = ''
43+
________ = ''
44+
_________ = ''
45+
__________ = ''
46+
47+
constructor( indentationChar = '\t', maxIndentationLevel = 10 ) {
48+
49+
let currentProperty = '_'
50+
for ( let currentIndentationLevel = 1 ; currentIndentationLevel <= maxIndentationLevel ; currentIndentationLevel++ ) {
51+
this[ currentProperty ] = indentationChar.repeat( currentIndentationLevel )
52+
currentProperty += '_'
53+
}
54+
55+
}
56+
57+
}
58+
59+
export {
60+
packageInfos,
61+
Indenter,
62+
getDirname
63+
}

.tasks/builds/build.mjs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { join } from 'path'
2+
import rollupConfigurator from '../../configs/rollup.conf.js'
3+
import parseArgs from 'minimist'
4+
import { getDirname } from '../_utils.mjs'
5+
import { packageInfos } from '../_utils.mjs'
6+
import { rollup } from 'rollup'
7+
import log from 'fancy-log'
8+
9+
function build( done ) {
10+
11+
const __dirname = getDirname()
12+
13+
const options = parseArgs( process.argv, {
14+
string: [ 'n', 'i', 'f', 'e' ],
15+
boolean: [ 's', 't' ],
16+
default: {
17+
i: join( __dirname, 'sources', `${ packageInfos.name }.js` ),
18+
o: join( __dirname, 'builds' ),
19+
f: [ 'esm', 'cjs', 'iife' ],
20+
e: [ 'dev', 'prod' ],
21+
s: true,
22+
t: true
23+
},
24+
alias: {
25+
i: 'input',
26+
o: 'output',
27+
f: 'formats',
28+
e: 'envs',
29+
s: 'sourcemap',
30+
t: 'treeshake'
31+
}
32+
} )
33+
34+
const configs = rollupConfigurator( options )
35+
36+
nextBuild()
37+
38+
function nextBuild( error ) {
39+
'use strict'
40+
41+
if ( error ) {
42+
43+
done( error )
44+
45+
} else if ( configs.length === 0 ) {
46+
47+
done()
48+
49+
} else {
50+
51+
const config = configs.pop()
52+
log( `Building ${ config.output.file }` )
53+
54+
rollup( config )
55+
.then( ( bundle ) => { return bundle.write( config.output ) } )
56+
.then( () => { nextBuild() } )
57+
.catch( nextBuild )
58+
59+
}
60+
61+
}
62+
63+
}
64+
65+
export { build }

.tasks/cleans/clean.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getGulpConfigForTask } from '../../configs/gulp.conf.mjs'
2+
import { deleteAsync } from 'del'
3+
import log from 'fancy-log'
4+
import colors from 'ansi-colors'
5+
6+
const red = colors.red
7+
8+
function clean( done ) {
9+
10+
const filesToClean = getGulpConfigForTask( 'clean' )
11+
12+
for ( let fileIndex = 0, numberOfFiles = filesToClean.length ; fileIndex < numberOfFiles ; fileIndex++ ) {
13+
log( red( `[${ fileIndex + 1 }/${ numberOfFiles }] Delete ${ filesToClean[ fileIndex ] }` ) )
14+
}
15+
16+
return deleteAsync( filesToClean )
17+
18+
}
19+
20+
export { clean }

.tasks/docs/doc.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import log from 'fancy-log'
2+
import jsdocConfiguration from '../../configs/jsdoc.conf.js'
3+
import { getGulpConfigForTask } from '../../configs/gulp.conf.mjs'
4+
import gulp from 'gulp'
5+
import jsdoc from 'gulp-jsdoc3'
6+
7+
8+
async function doc( done ) {
9+
10+
const config = jsdocConfiguration
11+
const filesToDoc = getGulpConfigForTask( 'doc' )
12+
13+
for ( let fileIndex = 0, numberOfFiles = filesToDoc.length ; fileIndex < numberOfFiles ; fileIndex++ ) {
14+
log( `[${ fileIndex + 1 }/${ numberOfFiles }] Documenting ${ filesToDoc[ fileIndex ] }` )
15+
}
16+
17+
// gulp
18+
// .src( filesToDoc, {
19+
// read: false,
20+
// allowEmpty: true
21+
// } )
22+
// .pipe( jsdoc( config, done ) )
23+
// .on( 'error', done )
24+
25+
await new Promise( ( ( resolve, reject ) => {
26+
gulp.src( filesToDoc, {
27+
read: false,
28+
allowEmpty: true
29+
} )
30+
.pipe( jsdoc( config, resolve ) )
31+
.on( 'error', reject )
32+
} ) )
33+
done()
34+
35+
}
36+
37+
export { doc }

.tasks/helps/help.mjs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import childProcess from 'child_process'
2+
import log from 'fancy-log'
3+
import colors from 'ansi-colors'
4+
import {
5+
packageInfos,
6+
Indenter
7+
} from '../_utils.mjs'
8+
9+
const {
10+
red,
11+
green,
12+
blue,
13+
cyan,
14+
yellow,
15+
magenta,
16+
unstyle
17+
} = colors
18+
19+
function getPrettyPackageName() {
20+
21+
let packageName = ''
22+
23+
const nameSplits = packageInfos.name.split( '-' )
24+
for ( const nameSplit of nameSplits ) {
25+
packageName += nameSplit.charAt( 0 ).toUpperCase() + nameSplit.slice( 1 ) + ' '
26+
}
27+
packageName = packageName.slice( 0, -1 )
28+
29+
return packageName
30+
31+
}
32+
33+
function getPrettyPackageVersion() {
34+
35+
return 'v' + packageInfos.version
36+
37+
}
38+
39+
function getPrettyNodeVersion() {
40+
41+
let nodeVersion = 'vX.x.ₓ'
42+
43+
try {
44+
nodeVersion = childProcess.execFileSync( 'node', [ '--version' ] )
45+
.toString()
46+
.replace( /(\r\n|\n|\r)/gm, '' )
47+
} catch ( e ) {
48+
log( red( e ) )
49+
50+
if ( e.message.includes( 'ENOENT' ) ) {
51+
nodeVersion += yellow( ' Not seems to be accessible from the path environment.' )
52+
}
53+
}
54+
55+
return ' node: ' + nodeVersion
56+
57+
}
58+
59+
function getPrettyNpmVersion() {
60+
61+
let npmVersion = 'X.x.ₓ'
62+
63+
try {
64+
npmVersion = childProcess.execFileSync( 'npm', [ '--version' ] )
65+
.toString()
66+
.replace( /(\r\n|\n|\r)/gm, '' )
67+
} catch ( e ) {
68+
log( red( e ) )
69+
70+
if ( e.message.includes( 'ENOENT' ) ) {
71+
npmVersion += yellow( ' Not seems to be accessible from the path environment.' )
72+
}
73+
}
74+
75+
return ' npm: v' + npmVersion
76+
77+
}
78+
79+
function alignTextCenter( text, width ) {
80+
81+
const unstyledText = unstyle( text.repeat( 1 ) )
82+
const marginLength = ( width - unstyledText.length ) / 2
83+
84+
let leftMargin, rightMargin
85+
if ( Number.isInteger( marginLength ) ) {
86+
leftMargin = marginLength
87+
rightMargin = marginLength
88+
} else {
89+
const flooredMargin = Math.floor( marginLength )
90+
leftMargin = flooredMargin
91+
rightMargin = flooredMargin + 1
92+
}
93+
94+
return ' '.repeat( leftMargin ) + text + ' '.repeat( rightMargin )
95+
96+
}
97+
98+
function alignTextLeft( text, width ) {
99+
100+
const unstyledText = unstyle( text.repeat( 1 ) )
101+
let repeatTime = width - unstyledText.length
102+
repeatTime = ( repeatTime > 0 ) ? repeatTime : 0
103+
104+
return text + ' '.repeat( repeatTime )
105+
106+
}
107+
108+
function alignTextRight( text, width ) {
109+
110+
const unstyledText = unstyle( text.repeat( 1 ) )
111+
let repeatTime = width - unstyledText.length
112+
repeatTime = ( repeatTime > 0 ) ? repeatTime : 0
113+
114+
return ' '.repeat( repeatTime ) + text
115+
116+
}
117+
118+
function help( done ) {
119+
120+
const bannerWidth = 70
121+
const prettyPackageName = getPrettyPackageName()
122+
const prettyPackageVersion = getPrettyPackageVersion()
123+
const prettyNodeVersion = getPrettyNodeVersion()
124+
const prettyNpmVersion = getPrettyNpmVersion()
125+
126+
const tableCharset = {
127+
topLeftCorner: '┏',
128+
topRightCorner: '┓',
129+
bottomRightCorner: '┛',
130+
bottomLeftCorner: '┗',
131+
horizontalBorder: '━',
132+
horizontalSeparator: '─',
133+
leftJoinSeparator: '┠',
134+
rightJoinSeparator: '┨',
135+
verticalBorder: '┃',
136+
verticalSeparator: '',
137+
}
138+
139+
const mainBorder = tableCharset.horizontalBorder.repeat( bannerWidth )
140+
const thinBorder = tableCharset.horizontalSeparator.repeat( bannerWidth )
141+
const tableTop = `${ tableCharset.topLeftCorner }${ mainBorder }${ tableCharset.topRightCorner }`
142+
const tableSeparator = `${ tableCharset.leftJoinSeparator }${ thinBorder }${ tableCharset.rightJoinSeparator }`
143+
const tableBottom = `${ tableCharset.bottomLeftCorner }${ mainBorder }${ tableCharset.bottomRightCorner }`
144+
const tableLine = ( innerText ) => `${ tableCharset.verticalBorder }${ innerText }${ tableCharset.verticalBorder }`
145+
146+
const I = new Indenter( ' ', 5 )
147+
const npmRun = blue( 'npm run' )
148+
149+
log( '' )
150+
log( tableTop )
151+
log( tableLine( alignTextCenter( 'HELP', bannerWidth ) ) )
152+
log( tableLine( alignTextCenter( prettyPackageName, bannerWidth ) ) )
153+
log( tableLine( alignTextCenter( prettyPackageVersion, bannerWidth ) ) )
154+
log( tableSeparator )
155+
log( tableLine( alignTextLeft( prettyNodeVersion, bannerWidth ) ) )
156+
log( tableLine( alignTextLeft( prettyNpmVersion, bannerWidth ) ) )
157+
log( tableBottom )
158+
log( I._, 'Available commands are:' )
159+
log( I.__, npmRun, cyan( 'help' ), '- Display this help.' )
160+
log( I.__, npmRun, cyan( 'patch' ), '- Will apply some patch/replacements in dependencies.', red( '(Apply only once after run "npm install")' ) )
161+
log( I.__, npmRun, cyan( 'clean' ), '- Will delete builds and temporary folders.' )
162+
log( I.__, npmRun, cyan( 'lint' ), '- Will run the eslint in pedantic mode with auto fix when possible.' )
163+
log( I.__, npmRun, cyan( 'doc' ), '- Will run jsdoc, and create documentation under `documentation` folder, using the docdash theme' )
164+
log( I.__, npmRun, cyan( 'test' ), '- Will run the test framworks (unit and bench), and create reports under `documentation/report` folder, using the mochawesome theme' )
165+
log( I.__, npmRun, cyan( 'unit' ), '- Will run the karma server for unit tests.' )
166+
log( I.__, npmRun, cyan( 'bench' ), '- Will run the karma server for benchmarks.' )
167+
log( I.__, npmRun, cyan( 'build' ), yellow( '--' ), green( '<options>' ), '- Will build the application for development and/or production environments.' )
168+
log( I.___, yellow( 'Note: The two dash are only required if you provide options !' ) )
169+
log( I.___, 'The available', green( '<options>' ), 'are:' )
170+
log( I.____, green( '-i' ), 'or', green( '--input' ), '- The main file path to build', cyan( '[Default: "sources/main.js"]' ), '.' )
171+
log( I.____, green( '-o' ), 'or', green( '--output' ), '- The folder where output the build', cyan( '[Default: "builds"]' ), '.' )
172+
log(
173+
I.____,
174+
green( '-f:' ),
175+
magenta( '<format>' ),
176+
'or',
177+
green( '--format:' ),
178+
magenta( '<format>' ),
179+
' - to specify the output build type. Where format could be any of:', magenta( 'cjs, esm, iife, umd' ), '.'
180+
)
181+
log( I.____, green( '-e:' ), magenta( '<env>' ), 'or', green( '--env:' ), magenta( '<env>' ), ' - to specify the build environment. Where env could be any of:', magenta(
182+
'dev' ), magenta( 'prod' ), cyan( '[Default: "dev"]' ), '.' )
183+
log( I.____, green( '-s' ), 'or', green( '--sourcemap' ), ' - to build with related source map', cyan( '[Default: true]' ), '.' )
184+
log( I.____, green( '-t' ), 'or', green( '--treeshake' ), ' - allow to perform treeshaking when building', cyan( '[Default: true]' ), '.' )
185+
log( I.__, npmRun, cyan( 'release' ), '- Will run all the lint, test stuff, and if succeed will build the application.' )
186+
log( '' )
187+
log( I._, 'In case you have', blue( 'gulp' ), 'installed globally, you could use also:' )
188+
log( I.__, blue( 'gulp' ), cyan( 'command' ), '- It will perform the command like using "npm run" but with less characters to type... Because you\'re a developer, right ?' )
189+
log( '' )
190+
191+
done()
192+
193+
}
194+
195+
export { help }

0 commit comments

Comments
 (0)