From 56704dacae966b4152d40490c48723d551da6c3f Mon Sep 17 00:00:00 2001 From: Tine Kondo Date: Wed, 28 Dec 2016 17:17:37 +0100 Subject: [PATCH] feat(publishing): remove unnecessary files from npm package & reduce library peer dependencies --- gulpfile.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-- package.json | 23 ++++++++++---------- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 9d4b64e0..304e81de 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -36,6 +36,15 @@ const cssnano = require('cssnano'); const scss = require('postcss-scss'); const stripInlineComments = require('postcss-strip-inline-comments'); +/** External command runner */ +const exec = require('child_process').exec; + +/** File Access */ +const fs = require('fs'); +const file = require('gulp-file'); + + +const LIBRARY_NAME = 'ng2-sharebuttons'; const tsProject = typescript.createProject('tsconfig.json'); @@ -117,5 +126,52 @@ gulp.task('watch', function() { gulp.watch([config.allSass, config.allHtml], ['styles']); }); -gulp.task('default', ['ts-lint', 'compile-ts']); -gulp.task('default', ['compile-ts']); \ No newline at end of file +/** Npm Packaging Task */ +gulp.task('npm', function () { + var pkgJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); + var targetPkgJson = {}; + var fieldsToCopy = ['version', 'description', 'keywords', 'author', 'repository', 'license', 'bugs', 'homepage']; + + targetPkgJson['name'] = LIBRARY_NAME; + + //only copy needed properties from project's package json + fieldsToCopy.forEach(function (field) { targetPkgJson[field] = pkgJson[field]; }); + + targetPkgJson['main'] = `index.js`; + targetPkgJson['module'] = 'index.js'; + targetPkgJson['typings'] = 'index.d.ts'; + + // defines project's dependencies as 'peerDependencies' for final users + targetPkgJson.peerDependencies = {}; + Object.keys(pkgJson.dependencies).forEach(function (dependency) { + targetPkgJson.peerDependencies[dependency] = `^${pkgJson.dependencies[dependency]}`; + }); + + // copy the needed additional files in the 'dist' folder + return gulp.src(['README.md', 'LICENSE', 'CHANGELOG.md']) + .pipe(file('package.json', JSON.stringify(targetPkgJson, null, 2))) + .pipe(gulp.dest('dist/')); +}); + +gulp.task('compile', ['ts-lint', 'compile-ts']); + +/** Npm Publication Task */ +gulp.task('publish', [ 'compile', 'npm'], function (done) { + // run npm publish terminal command to publish the 'dist' folder only + exec('npm publish ./dist', + function (error, stdout, stderr) { + if (stderr) { + gutil.log(gutil.colors.red(stderr)); + } else if (stdout) { + gutil.log(gutil.colors.green(stdout)); + } + // execute callback when its done + if (done) { + done(); + } + } + ); +}); + + +gulp.task('default', ['compile']); diff --git a/package.json b/package.json index 4ef9a180..a6dfc531 100644 --- a/package.json +++ b/package.json @@ -31,23 +31,22 @@ "start": "gulp", "prepublish": "gulp" }, - "peerDependencies": { - "@angular/common": "^2.2.4", - "@angular/compiler": "^2.2.4", - "@angular/core": "^2.2.4", - "@angular/http": "^2.2.4", - "@angular/platform-browser": "^2.2.4", - "@angular/router": "^3.2.4", - "core-js": "^2.4.1", - "rxjs": "^5.0.0-beta.12", - "zone.js": "^0.6.17" + "dependencies": { + "@angular/common": "2.0.0", + "@angular/core": "2.0.0", + "@angular/http": "2.0.0" }, "devDependencies": { + "@angular/compiler": "2.0.0", + "@angular/platform-browser": "2.0.0", + "@angular/router": "3.0.0", "angular2-template-loader": "^0.5.0", "autoprefixer": "^6.4.0", + "core-js": "2.4.1", "cssnano": "^3.7.4", "del": "^2.2.2", "gulp": "^3.9.1", + "gulp-file": "^0.3.0", "gulp-inline-ng2-template": "^3.0.0", "gulp-postcss": "^6.1.1", "gulp-sass": "^2.3.2", @@ -59,7 +58,9 @@ "merge2": "^1.0.2", "postcss-scss": "^0.3.0", "postcss-strip-inline-comments": "^0.1.5", + "rxjs": "5.0.0-beta.12", "tslint": "^3.15.1", - "typings": "^1.3.3" + "typings": "^1.3.3", + "zone.js": "0.6.21" } }