1 | var _ = require('underscore');
|
2 | var spawn = require('child_process').spawn;
|
3 | var async = require('async');
|
4 | var rimraf = require('rimraf');
|
5 |
|
6 | function git(cwd){
|
7 | return function (args, callback){
|
8 | var cp = spawn('git', args, {cwd:cwd });
|
9 | var output = "";
|
10 | cp.stdout.on('data', function(data){
|
11 | process.stdout.write(data);
|
12 | output+=data;
|
13 | });
|
14 |
|
15 | cp.stderr.on('data', function(data){
|
16 | process.stderr.write(data);
|
17 | });
|
18 |
|
19 | cp.on('close', function(code){
|
20 | callback(code, output.toString());
|
21 | });
|
22 | }
|
23 | };
|
24 |
|
25 | module.exports = function(grunt){
|
26 | grunt.registerTask('publish', function(){
|
27 | var done = this.async();
|
28 | var config = grunt.config('componentConfig');
|
29 |
|
30 | git(process.cwd())(['rev-parse','--abbrev-ref','HEAD'], function(err, branch){
|
31 | if(branch === "master\n"){
|
32 | return console.log("You can't publish from master branch. master is for published component. Your dev code should be elsewere. And by compy publish it will be published in master.");
|
33 | }
|
34 | branchChecked();
|
35 | });
|
36 |
|
37 | function branchChecked(){
|
38 |
|
39 | ['images','fonts','scripts','styles','templates'].forEach(function(asset){
|
40 | if(config[asset]){
|
41 | config[asset] = grunt.file.expand(config[asset]);
|
42 | }
|
43 | });
|
44 | grunt.config.get('utils.ignoreSources')(config, grunt.config('src.tests'));
|
45 | Object.keys(config).forEach(function(val){
|
46 | if(_(config[val]).isEmpty()){
|
47 | delete config[val];
|
48 | }
|
49 | });
|
50 | grunt.file.mkdir("_pub");
|
51 |
|
52 | async.eachSeries([
|
53 | ['init'],
|
54 | ['remote', 'add', 'github', grunt.config('pkg.repository.url')],
|
55 | ['pull', 'github', 'master', '--tags']
|
56 | ], git(process.cwd() + "/_pub"), getTags);
|
57 | }
|
58 |
|
59 | function getTags(err){
|
60 | console.log("get tags");
|
61 | git(process.cwd() + "/_pub")(['log',
|
62 | '--tags',
|
63 | '--simplify-by-decoration',
|
64 | '--pretty=%ai %d'], gotTagsData);
|
65 | }
|
66 |
|
67 | function gotTagsData(err, data){
|
68 | var data = _(data.split("\n")).without("");
|
69 | data = data.map(function(line){
|
70 | var parsed = line.match(/^([^\(]*)\s{2}\((.*)\)/);
|
71 | if (!parsed) return null;
|
72 | var ver = parsed[2].match(/(\d+\.\d+\.\d+)/);
|
73 | if (!ver) return null;
|
74 | return {date: parsed[1], version: ver[1]}
|
75 | })
|
76 |
|
77 | var isAlreadyPublished = _.chain(data).without(null).findWhere({version: grunt.config.get('pkg.version')}).value();
|
78 |
|
79 | if(isAlreadyPublished){
|
80 | console.log("This version is already published. Please update version in package.json file");
|
81 | return cleanup();
|
82 | }
|
83 | repoPrepared();
|
84 | }
|
85 |
|
86 | function repoPrepared(err){
|
87 | grunt.file.expand('_pub/*').forEach(grunt.file.delete);
|
88 | ['images','fonts','scripts','styles','templates'].forEach(function(asset){
|
89 | if(config[asset]){
|
90 | config[asset].forEach(function(file){
|
91 | grunt.file.copy(file, "_pub/" + file);
|
92 | })
|
93 | }
|
94 | });
|
95 | grunt.file.write("_pub/component.json", JSON.stringify(config, null, 2));
|
96 | publishToRepo();
|
97 |
|
98 | }
|
99 |
|
100 | function publishToRepo(){
|
101 | async.eachSeries([
|
102 | ['add','-A'],
|
103 | ['commit','-m','Component published: ' + new Date().toString()],
|
104 | ['tag', '-a', 'v' + grunt.config.get('pkg.version'), '-m', 'version ' + grunt.config.get('pkg.version')],
|
105 | ['push', 'github', 'master', '--force', '--tags']
|
106 | ], git(process.cwd() + "/_pub"), cleanup);
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | }
|
119 |
|
120 | function cleanup(){
|
121 | grunt.file.delete('_pub');
|
122 | done();
|
123 | }
|
124 | })
|
125 | }
|