diff --git a/index.js b/index.js index 0b26b16..8fcc8cb 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,94 @@ -const cron = require('node-schedule'); -const git = require('simple-git')(); +/* + * @Author: IoTcat (https://iotcat.me) + * @Date: 2019-12-31 11:59:49 + * @Last Modified by: IoTcat + * @Last Modified time: 2019-12-31 18:28:21 + */ +module.exports = function(o_params){ + /* factory mode */ + var o = { + dir: './', + debug: false, + allowNotice: true, + pull: (params, callback) => pull(params, callback), + push: (params, callback) => push(params, callback), + sync: (callback, params_pull, params_push) => sync(callback, params_pull, params_push), + schedule: (time, callback, params_pull, params_push) => schedule(time, callback, params_pull, params_push) + } -/* - cron.scheduleJob('1-50 * * * * *', function(){ - console.log('scheduleCronstyle:' + new Date()); - }); -*/ + /* merge params */ + Object.assign(o, o_params); + + + /* modules import */ + const cron = require('node-schedule'); + const git = require('simple-git')(o.dir); + + /* Git pull action */ + var pull = function(params_user, callback){ + var params = { + remote: { + repo: 'origin', + branch: 'master' + }, + pull_params: {} + } + Object.assign(params, params_user); + + git.pull(params.remote.repo, + params.remote.branch, + params.pull_params, + function(){ + if(o.allowNotice){ + console.log(new Date() + ' - cron-git: git pull done.'); + } + if(callback !== undefined){ + callback(); + } + }); + } + + /* Git push action */ + var push = function(params_user, callback){ + var params = { + add: { + path: './*' + }, + commit: { + message: "Committed by cron-git from "+ require('os').hostname() + }, + push_params: ['-u', 'origin', 'master'] + } + Object.assign(params, params_user); + git.add(params.add.path) + .commit(params.commit.message) + .push(params.push_params, function(){ + if(o.allowNotice){ + console.log(new Date() + ' - cron-git: git push done.'); + } + if(callback !== undefined){ + callback(); + } + }); + } -git.pull('origin', 'master', {'--no-rebase': null}, ()=>{console.log(new Date() + ' - cron-git: git pull done.' + require('os').hostname())}); + /* sync action */ + var sync = function(callback, params_pull, params_push){ + push(params_push); + pull(params_pull); + push(params_push, callback); + } + /* cron sync */ + var schedule = function(time, callback, params_pull, params_push){ + cron.scheduleJob(time, function(){ + console.log(new Date() + ' - cron-git: Scheduled sync begin.'); + sync(callback, params_pull, params_push); + }); + } -git.add('./*') - .commit("Committed by cron-git auto") - .push(['-u', 'origin', 'master'], () => console.log(new Date() + ' - cron-git: git push done.')); \ No newline at end of file + return o; +} diff --git a/node_modules/os/.npmignore b/node_modules/os/.npmignore new file mode 100644 index 0000000..e920c16 --- /dev/null +++ b/node_modules/os/.npmignore @@ -0,0 +1,33 @@ +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +node_modules + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history diff --git a/node_modules/os/LICENSE b/node_modules/os/LICENSE new file mode 100644 index 0000000..39b96a4 --- /dev/null +++ b/node_modules/os/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Diego Rodríguez Baquero + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/node_modules/os/README.md b/node_modules/os/README.md new file mode 100644 index 0000000..a7138d5 --- /dev/null +++ b/node_modules/os/README.md @@ -0,0 +1,24 @@ +# node-os [![npm](https://img.shields.io/npm/v/os.svg)](https://www.npmjs.com/package/os) [![npm](https://img.shields.io/npm/dm/os.svg)](https://www.npmjs.com/package/os) [![npm](https://img.shields.io/npm/l/os.svg)](LICENSE) +#### NodeJS Core Module Extended + +## Installation + +```bash +npm i os --save +``` + +## Usage + +In your node program: + +```js +var os = require('os') +``` + +## API + +https://nodejs.org/api/os.html + +## License + +MIT. Copyright (c) [Diego Rodríguez Baquero](https://diegorbaquero.com). diff --git a/node_modules/os/index.js b/node_modules/os/index.js new file mode 100644 index 0000000..889b75f --- /dev/null +++ b/node_modules/os/index.js @@ -0,0 +1 @@ +module.exports = require('os') diff --git a/node_modules/os/package.json b/node_modules/os/package.json new file mode 100644 index 0000000..e5fcd99 --- /dev/null +++ b/node_modules/os/package.json @@ -0,0 +1,55 @@ +{ + "_from": "os", + "_id": "os@0.1.1", + "_inBundle": false, + "_integrity": "sha1-IIhF6J4ZOtTZcUdLk5R3NqVtE/M=", + "_location": "/os", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "os", + "name": "os", + "escapedName": "os", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/os/-/os-0.1.1.tgz", + "_shasum": "208845e89e193ad4d971474b93947736a56d13f3", + "_spec": "os", + "_where": "e:\\git\\cron-git", + "author": { + "name": "Diego Rodríguez Baquero", + "email": "diegorbaquero@gmail.com", + "url": "https://diegorbaquero.com" + }, + "bugs": { + "url": "https://github.com/DiegoRBaquero/node-os/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "NodeJS Core Module Extended", + "homepage": "https://github.com/DiegoRBaquero/node-os#readme", + "keywords": [ + "node", + "os", + "core", + "module" + ], + "license": "MIT", + "main": "index.js", + "name": "os", + "repository": { + "type": "git", + "url": "git+https://github.com/DiegoRBaquero/node-os.git" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "version": "0.1.1" +} diff --git a/package-lock.json b/package-lock.json index ef9a9d4..b6d1374 100644 --- a/package-lock.json +++ b/package-lock.json @@ -75,6 +75,11 @@ "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", "integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==" }, + "os": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/os/-/os-0.1.1.tgz", + "integrity": "sha1-IIhF6J4ZOtTZcUdLk5R3NqVtE/M=" + }, "simple-git": { "version": "1.129.0", "resolved": "https://registry.npmjs.org/simple-git/-/simple-git-1.129.0.tgz", diff --git a/package.json b/package.json index 76562c2..2e172fc 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "homepage": "https://github.com/IoTcat/cron-git#readme", "dependencies": { "node-schedule": "^1.3.2", + "os": "^0.1.1", "simple-git": "^1.129.0" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..97abbc3 --- /dev/null +++ b/test.js @@ -0,0 +1,3 @@ +const crongit = require('./index.js')(); + +crongit.sync();