├── bin └── dister ├── .gitignore ├── .npmignore ├── package.json └── index.js /bin/dister: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | require('../index.js')(process.argv); 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | node_modules/ 3 | 4 | .DS_Store 5 | *.db 6 | *.bak 7 | *.tmp 8 | *.cmd 9 | ~* 10 | 11 | upload.py -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.settings 3 | /.project 4 | /.gitignore 5 | /node_modules 6 | /test 7 | /.tmp 8 | 9 | .DS_Store 10 | 11 | *.db 12 | *.bak 13 | *.tmp 14 | *.cmd 15 | ~* 16 | 17 | upload.py -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dister", 3 | "version": "0.2.0", 4 | "description": "publish tool", 5 | "main": "index.js", 6 | 7 | "bin" : { 8 | "dister" : "bin/dister" 9 | }, 10 | 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "dependencies" : { 15 | "commander": "2.1.0" 16 | }, 17 | "keywords": [ 18 | "fis", 19 | "dister" 20 | ], 21 | "author": "fansekey", 22 | "license": "MIT" 23 | } 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var commander = require('commander'); 2 | var fs = require('fs'); 3 | var os = require('os'); 4 | var spawn = require('child_process').spawn; 5 | 6 | module.exports = function (argv) { 7 | 8 | var pkg_info = read_package_information(); 9 | 10 | commander 11 | .version(pkg_info.version); 12 | 13 | commander 14 | .command('publish') 15 | .description('publish package') 16 | .action(function (options) { 17 | var cmd = os.platform == 'win32' ? 'npm.cmd' : 'npm'; 18 | var npm = spawn(cmd, ['publish'], { detach: true }); 19 | 20 | var output = ''; 21 | npm.stdout.on('data', function (chunk) { 22 | console.log(chunk.toString()); 23 | output += chunk.toString(); 24 | }); 25 | 26 | npm.stdout.on('end', function () { 27 | var version = output.replace(/^\+\s*/, ''); 28 | get_npm_package_information(version, commit_git); 29 | }); 30 | npm.stderr.on('data', function (chunk) { 31 | process.stdout.write(chunk.toString()); 32 | }); 33 | }); 34 | 35 | commander.parse(argv); 36 | 37 | if (argv.length <= 2) { 38 | commander.outputHelp(); 39 | } 40 | }; 41 | 42 | function get_npm_package_information(version, cb) { 43 | 44 | var cmd = os.platform == 'win32' ? 'npm.cmd' : 'npm'; 45 | 46 | var npm_info = spawn(cmd, ['info', version], {detach: true}); 47 | var info = ''; 48 | 49 | npm_info.stdout.on('data', function (chunk) { 50 | info += chunk.toString(); 51 | }); 52 | 53 | npm_info.stdout.on('end', function () { 54 | info = eval("[" + info.replace(/"/g, '\"') + "]")[0]; 55 | cb(info); 56 | }); 57 | 58 | npm_info.stderr.on('data', function (chunk) { 59 | process.stdout.write(chunk.toString()); 60 | }); 61 | } 62 | 63 | function commit_git(info) { 64 | var shasum_filepath = process.cwd() + '/shasum.json'; 65 | var shasum_info = {}; 66 | 67 | if (fs.existsSync(shasum_filepath)) { 68 | shasum_info = JSON.parse(fs.readFileSync(shasum_filepath)); 69 | } 70 | 71 | shasum_info[info.name + '@' + info.version] = { 72 | 'shasum': info.dist.shasum 73 | }; 74 | 75 | fs.writeFileSync(shasum_filepath, JSON.stringify(shasum_info)); 76 | 77 | var git_add = spawn('git', ['add', './shasum.json'], {detach: true}); 78 | git_add.stdout.on('end', function () { 79 | var git = spawn('git', ['commit', '-m', '"update shasum"', './shasum.json'], {detach: true}); 80 | 81 | git.stdout.on('end', function () { 82 | process.stdout.write('+ publish [' + info.name + '@' + info.version + ' - ' + info.dist.shasum + ']'); 83 | }); 84 | 85 | git.stdout.on('data', function (chunk) { 86 | process.stdout.write(chunk.toString()); 87 | }); 88 | 89 | git.stderr.on('data', function (chunk) { 90 | process.stdout.write(chunk.toString()); 91 | }) 92 | 93 | }); 94 | 95 | git_add.stdout.on('data', function (chunk) { 96 | process.stdout.write(chunk.toString()); 97 | }); 98 | } 99 | 100 | function read_package_information() { 101 | var pkg_content = fs.readFileSync(__dirname + '/package.json', {encoding: 'utf-8'}); 102 | return JSON.parse(pkg_content); 103 | } 104 | --------------------------------------------------------------------------------