1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-17 23:36:11 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
tdzl2003
2016-02-14 00:04:42 +08:00
commit 74396f8426
10 changed files with 382 additions and 0 deletions

55
local-cli/api.js Normal file
View File

@@ -0,0 +1,55 @@
/**
* Created by tdzl2003 on 2/13/16.
*/
const fetch = require('isomorphic-fetch');
const host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn';
const fs = require('fs-promise');
let session = undefined;
let fileSession = undefined;
exports.loadSession = async function() {
if (await fs.exists('.pushy')) {
try {
session = JSON.parse(await fs.readFile('.pushy', 'utf8'));
} catch (e) {
console.error('Failed to parse file `.pushy`. Try to remove it manually.');
throw e;
}
}
}
exports.getSession = function(){
return session;
}
exports.replaceSession = function(newSession) {
session = newSession;
}
exports.saveSession = async function(){
// Only save on change.
if (session != fileSession) {
const current = session;
const data = JSON.stringify(current, null, 4);
await fs.writeFile('.pushy', data, 'utf8');
fileSession = current;
}
}
function queryWithBody(method){
return async function(api, body) {
const resp = await fetch(host + api, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'X-AccessToken': session ? session.token : '',
},
body: JSON.stringify(body),
});
}
}
exports.post = queryWithBody('POST');
exports.put = queryWithBody('PUT');

18
local-cli/cli.json Normal file
View File

@@ -0,0 +1,18 @@
{
"useCommand": true,
"defaultCommand": "help",
"commands": {
"login":{
"options": {
"username": {
}
}
},
"help": {
}
},
"globalOptions":{
}
}

24
local-cli/index.js Normal file
View File

@@ -0,0 +1,24 @@
/**
* Created by tdzl2003 on 2/13/16.
*/
const userCommands = require('./user').commands;
function printUsage({args}) {
// const commandName = args[0];
// TODO: print usage of commandName, or print global usage.
console.log('Usage is under development now.')
console.log('Visit `https://github.com/reactnativecn/react-native-pushy` for early document.');
process.exit(1);
}
const commands = {
...userCommands,
help: printUsage,
};
exports.run = function () {
const argv = require('cli-arguments').parse(require('./cli.json'));
commands[argv.command](argv).catch(err=>console.error(err.stack));
};

13
local-cli/user.js Normal file
View File

@@ -0,0 +1,13 @@
/**
* Created by tdzl2003 on 2/13/16.
*/
const {question} = require('./utils');
exports.commands = {
login: async function ({args}){
const username = args[0] || await question('user:');
const password = args[1] || await question('password:', true);
}
}

13
local-cli/utils.js Normal file
View File

@@ -0,0 +1,13 @@
/**
* Created by tdzl2003 on 2/13/16.
*/
var read = require('read');
exports.question = function(query, password) {
return new Promise((resolve, reject)=>read({
prompt: query,
silent: password,
replace: password ? '*' : undefined,
}, (err, result)=> err ? reject(err) : resolve(result)));
}