Skip to content

Commit

Permalink
Gulp cleanup
Browse files Browse the repository at this point in the history
* Created new tasks for updating semvar tags
* Created separate setup file
* Standardised version in all packages
  • Loading branch information
MKHenson committed Oct 17, 2016
1 parent 3fe57cc commit 4b81770
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 60 deletions.
89 changes: 89 additions & 0 deletions gulp/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
var fs = require( 'fs' );
var download = require( 'gulp-download' );
var rename = require( "gulp-rename" );
var gulp = require( 'gulp' );

/**
* Goes through each of the main config files and increments
* the version
* @param {( currentVersion: string ) => string} f A function to transform the version into a new version
* @returns {string}
*/
module.exports.bumpVersion = function( f, files ) {
let fileStr = '';
let version = '';

if ( !fs.existsSync( './package.json' ) )
throw new Error( `You dont seem to have a package json file. This is needed to identify the version.` );

version = JSON.parse( fs.readFileSync( './package.json' ) ).version;
const bumpedVersion = f( version );

return Promise.all( files.map( function( file ) {
return new Promise( function( resolve, reject ) {
if ( !fs.existsSync( file ) )
throw new Error( `File ${file} does not exist` );

fileStr = fs.readFileSync( file ).toString();
const matchedVersion = fileStr.match( new RegExp( version, 'i' ) );
if ( !matchedVersion || matchedVersion.length === 0 )
throw new Error( `File ${file} does not have a consistent version number of '${version}'` );

fileStr = fileStr.replace( version, bumpedVersion );
fs.writeFileSync( file, fileStr );
});
}) );
}

/**
* Increments a semvar version patch number
* @param {string} version The version coming in. E.g. 1.0.1
* @returns {string}
*/
module.exports.bumpPatchNum = function( version ) {
const segments = version.split( '.' );
const patch = parseInt( segments[ 2 ] ) + 1;
return `${segments[ 0 ]}.${segments[ 1 ]}.${patch}`
};

/**
* Increments a semvar version mid number
* @param {string} version The version coming in. E.g. 1.0.1
* @returns {string}
*/
module.exports.bumpMidNum = function( version ) {
const segments = version.split( '.' );
const minor = parseInt( segments[ 1 ] ) + 1;
return `${segments[ 0 ]}.${minor}.0`
};

/**
* Increments a semvar version major number
* @param {string} version The version coming in. E.g. 1.0.1
* @returns {string}
*/
module.exports.bumpMajorNum = function( version ) {
const segments = version.split( '.' );
const major = parseInt( segments[ 0 ] ) + 1;
return `${major}.0.0`
};

/**
* This function downloads a definition file from github and writes it to a destination
* @param {string} url The url of the file to download
* @param {string} dest The destination folder to move the file to
* @param {string} name The name of the downloaded file
*/
module.exports.getDefinition = function( url, dest, name ) {
return new Promise( function( resolve, reject ) {
download( url )
.pipe( rename( name ) )
.pipe( gulp.dest( dest ) )
.on( 'error', function( err ) {
throw ( err )
})
.on( 'end', function() {
resolve( true );
})
});
}
51 changes: 23 additions & 28 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
var gulp = require( 'gulp' );
var ts = require( 'gulp-typescript' );
var tslint = require( 'gulp-tslint' );
var download = require( 'gulp-download' );
var rename = require( "gulp-rename" );
var setup = require( './gulp/setup.js' );

// CONFIG
// ==============================
const tsProject = ts.createProject( 'tsconfig.json' );
const configFiles = [
'./readme.md',
'./install-script.sh',
'./test/package.json',
'./src/dist-files/package.json',
'./package.json'
];


// Builds each of the ts files into JS files in the output folder
/**
* Builds each of the ts files into JS files in the output folder
*/
gulp.task( 'ts-code', function() {
var tsResult = tsProject.src()
.pipe( tsProject() );
Expand All @@ -31,45 +38,33 @@ gulp.task( 'tslint', [ 'ts-code' ], function() {
}) )
});

/**
* This function downloads a definition file from github and writes it to a destination
* @param {string} url The url of the file to download
* @param {string} dest The destination folder to move the file to
*/
function getDefinition( url, dest, name ) {
return new Promise( function( resolve, reject ) {
download( url )
.pipe( rename( name ) )
.pipe( gulp.dest( dest ) )
.on( 'error', function( err ) {
throw ( err )
})
.on( 'end', function() {
resolve( true );
})
});
}

/**
* Downloads the definition files used in the development of the application and moves them into the definitions folder
*/
gulp.task( 'install', function() {
gulp.task( 'install-definitions', function() {
return Promise.all( [
getDefinition( "https://raw.githubusercontent.com/MKHenson/users/dev/src/definitions/generated/users.d.ts", "src/definitions/required/", "users.d.ts" )
setup.getDefinition( "https://raw.githubusercontent.com/MKHenson/users/dev/src/definitions/generated/users.d.ts", "src/definitions/required/", "users.d.ts" )
] );
});

// Copies the distribution files from src to the dist folder
/**
* Copies the distribution files from src to the dist folder
*/
gulp.task( 'dist-files', function() {

return gulp.src( [ 'src/dist-src/*.json', 'src/dist-src/modepress-api/*.json' ], { base: "src/dist-src/" })
.pipe( gulp.dest( './dist' ) );
});

// Copies the modepress definition into a definitions/generated folder
/**
* Copies the modepress definition into a definitions/generated folder
*/
gulp.task( 'ts-code-definitions', function() {
return gulp.src( [ 'src/definitions/custom/modepress.d.ts' ], { base: 'src/definitions/custom/' })
.pipe( gulp.dest( './src/definitions/generated' ) );
});

gulp.task( 'bump-patch', function() { return setup.bumpVersion( setup.bumpPatchNum, configFiles ) });
gulp.task( 'bump-minor', function() { return setup.bumpVersion( setup.bumpMidNum, configFiles ) });
gulp.task( 'bump-major', function() { return setup.bumpVersion( setup.bumpMajorNum, configFiles ) });
gulp.task( 'install', [ 'install-definitions' ] );
gulp.task( 'build', [ 'ts-code', 'dist-files', 'ts-code-definitions' ] );
46 changes: 23 additions & 23 deletions src/dist-src/package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"name": "modepress",
"version": "0.3.1",
"description": "A simple Mongo-Node CMS server. The server can be accessed via an admin panel and content can be requested via its RESTful API",
"main": "main.js",
"author": "Mathew Henson",
"dependencies": {
"winston": "^2.2.0",
"entities": "^1.1.1",
"express": "^4.13.4",
"mongodb": "^2.1.12",
"morgan": "^1.5.1",
"body-parser": "^1.12.0",
"method-override": "^2.3.1",
"node-inspector": "^0.12.7",
"request": "^2.69.0",
"jade": "^1.10.0",
"yargs": "^3.12.0",
"compression": "^1.5.1",
"modepress-api": "./modepress-api",
"ws": "^1.0.1",
"sanitize-html": "^1.11.4",
"jsdom": "^8.2.0"
}
"name": "modepress",
"version": "0.4.3",
"description": "A simple Mongo-Node CMS server. The server can be accessed via an admin panel and content can be requested via its RESTful API",
"main": "main.js",
"author": "Mathew Henson",
"dependencies": {
"winston": "^2.2.0",
"entities": "^1.1.1",
"express": "^4.13.4",
"mongodb": "^2.1.12",
"morgan": "^1.5.1",
"body-parser": "^1.12.0",
"method-override": "^2.3.1",
"node-inspector": "^0.12.7",
"request": "^2.69.0",
"jade": "^1.10.0",
"yargs": "^3.12.0",
"compression": "^1.5.1",
"modepress-api": "./modepress-api",
"ws": "^1.0.1",
"sanitize-html": "^1.11.4",
"jsdom": "^8.2.0"
}
}
18 changes: 9 additions & 9 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "modepress-tests",
"version": "0.1.0",
"main": "tests.js",
"author": "Mathew Henson",
"dependencies": {
"unit.js": "^2.0.0",
"yargs": "^3.12.0"
}
}
"name": "modepress-tests",
"version": "0.4.3",
"main": "tests.js",
"author": "Mathew Henson",
"dependencies": {
"unit.js": "^2.0.0",
"yargs": "^3.12.0"
}
}

0 comments on commit 4b81770

Please sign in to comment.