mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-07 23:45:13 +08:00
working
This commit is contained in:
86
local-cli/src/api.js
Normal file
86
local-cli/src/api.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Created by tdzl2003 on 2/13/16.
|
||||
*/
|
||||
|
||||
const fetch = require('isomorphic-fetch');
|
||||
let host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn';
|
||||
const fs = require('fs-promise');
|
||||
|
||||
let session = undefined;
|
||||
let savedSession = undefined;
|
||||
|
||||
exports.loadSession = async function() {
|
||||
if (await fs.exists('.pushy')) {
|
||||
try {
|
||||
exports.replaceSession(JSON.parse(await fs.readFile('.pushy', 'utf8')));
|
||||
savedSession = session;
|
||||
} 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 !== savedSession) {
|
||||
const current = session;
|
||||
const data = JSON.stringify(current, null, 4);
|
||||
await fs.writeFile('.pushy', data, 'utf8');
|
||||
savedSession = current;
|
||||
}
|
||||
}
|
||||
|
||||
exports.closeSession = async function(){
|
||||
if (await fs.exists('.pushy')) {
|
||||
await fs.unlink('.pushy');
|
||||
savedSession = undefined;
|
||||
}
|
||||
session = undefined;
|
||||
host = process.env.PUSHY_REGISTRY || 'http://pushy.reactnative.cn';
|
||||
}
|
||||
|
||||
async function query(url, options) {
|
||||
const resp = await fetch(url, options);
|
||||
const json = await resp.json();
|
||||
if (resp.status !== 200) {
|
||||
throw Object.assign(new Error(json.message || json.error), {status: resp.status});
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
function queryWithoutBody(method) {
|
||||
return function(api) {
|
||||
return query(host + api, {
|
||||
method,
|
||||
headers: {
|
||||
'X-AccessToken': session ? session.token : '',
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function queryWithBody(method) {
|
||||
return function(api, body) {
|
||||
return query(host + api, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-AccessToken': session ? session.token : '',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
exports.get = queryWithoutBody('GET');
|
||||
exports.post = queryWithBody('POST');
|
||||
exports.put = queryWithBody('PUT');
|
62
local-cli/src/bundle.js
Normal file
62
local-cli/src/bundle.js
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Created by tdzl2003 on 2/22/16.
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
import { mkdir } from 'mkdir-recursive';
|
||||
import { getRNVersion } from './utils';
|
||||
|
||||
|
||||
export const commands = {
|
||||
bundle: async function({options}){
|
||||
const {
|
||||
entryFile,
|
||||
intermediaDir,
|
||||
platform,
|
||||
output,
|
||||
dev,
|
||||
verbose
|
||||
} = options;
|
||||
|
||||
if (!platform) {
|
||||
throw new Error('Platform must be specified.');
|
||||
}
|
||||
|
||||
const { version, major, minor} = getRNVersion();
|
||||
|
||||
console.log('Bundling with React Native version: ', version);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
mkdir(intermediaDir, err => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
require(path.resolve('node_modules/react-native/packager/babelRegisterOnly'))([
|
||||
/private-cli\/src/,
|
||||
/local-cli/,
|
||||
]);
|
||||
const Config = require(path.resolve('node_modules/react-native/local-cli/util/Config'));
|
||||
const bundle = require(path.resolve('node_modules/react-native/local-cli/bundle/bundle'));
|
||||
const defaultConfig = require(path.resolve('node_modules/react-native/local-cli/default.config'));
|
||||
|
||||
bundle([
|
||||
'--entry-file',
|
||||
entryFile,
|
||||
'--platform',
|
||||
platform,
|
||||
'--dev',
|
||||
'' + !!dev,
|
||||
'--bundle-output',
|
||||
`${intermediaDir}/index.bundlejs`,
|
||||
'--assets-dest',
|
||||
`${intermediaDir}/assets`,
|
||||
'--verbose',
|
||||
'' + !!verbose,
|
||||
], Config.get(path.resolve('node_modules/react-native/local-cli'), defaultConfig));
|
||||
}
|
||||
};
|
45
local-cli/src/index.js
Normal file
45
local-cli/src/index.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Created by tdzl2003 on 2/13/16.
|
||||
*/
|
||||
|
||||
const {loadSession} = require('./api');
|
||||
const userCommands = require('./user').commands;
|
||||
import {commands as bundleCommands} from './bundle';
|
||||
|
||||
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,
|
||||
...bundleCommands,
|
||||
help: printUsage,
|
||||
};
|
||||
|
||||
function translateOptions(options){
|
||||
for (let key in options) {
|
||||
const v = options[key];
|
||||
if (typeof(v) === 'string') {
|
||||
options[key] = v.replace(/\$\{(\w+)\}/, function (v, n){
|
||||
return options[n] || process.env[n] || v;
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.run = function () {
|
||||
const argv = require('cli-arguments').parse(require('../cli.json'));
|
||||
|
||||
translateOptions(argv.options);
|
||||
|
||||
loadSession()
|
||||
.then(()=>commands[argv.command](argv))
|
||||
.catch(err=>{setTimeout(()=>{
|
||||
throw err;
|
||||
})});
|
||||
};
|
47
local-cli/src/user.js
Normal file
47
local-cli/src/user.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Created by tdzl2003 on 2/13/16.
|
||||
*/
|
||||
|
||||
import {question} from './utils';
|
||||
const {
|
||||
post,
|
||||
get,
|
||||
replaceSession,
|
||||
saveSession,
|
||||
closeSession,
|
||||
} = require('./api');
|
||||
const crypto = require('crypto');
|
||||
|
||||
function md5(str) {
|
||||
return crypto.createHash('md5').update(str).digest('base64');
|
||||
}
|
||||
|
||||
exports.commands = {
|
||||
login: async function ({args}){
|
||||
const login = args[0] || await question('user:');
|
||||
const pwd = args[1] || await question('password:', true);
|
||||
const {token} = await post('/user/login', {
|
||||
login,
|
||||
pwd: md5(pwd),
|
||||
});
|
||||
replaceSession({token});
|
||||
await saveSession();
|
||||
console.log('OK.');
|
||||
},
|
||||
logout: async function (){
|
||||
await closeSession();
|
||||
console.log('Logged out.');
|
||||
},
|
||||
me: async function (){
|
||||
try {
|
||||
const me = await get('/user/me');
|
||||
console.log(me);
|
||||
} catch (e) {
|
||||
if (e.status === 401) {
|
||||
console.log('Not loggined.\nRun `pushy login` at your project directory to login.');
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
28
local-cli/src/utils.js
Normal file
28
local-cli/src/utils.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Created by tdzl2003 on 2/13/16.
|
||||
*/
|
||||
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
var read = require('read');
|
||||
|
||||
export function question(query, password) {
|
||||
return new Promise((resolve, reject)=>read({
|
||||
prompt: query,
|
||||
silent: password,
|
||||
replace: password ? '*' : undefined,
|
||||
}, (err, result)=> err ? reject(err) : resolve(result)));
|
||||
}
|
||||
|
||||
export function getRNVersion() {
|
||||
const version = JSON.parse(fs.readFileSync(path.resolve('node_modules/react-native/package.json'))).version;
|
||||
|
||||
// We only care about major and minor version.
|
||||
const match = /^(\d+)\.(\d+)\./.exec(version);
|
||||
return {
|
||||
version,
|
||||
major: match[1] | 0,
|
||||
minor: match[2] | 0,
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user