From 56495062c441d09e9e61234c5f9831678d92a52a Mon Sep 17 00:00:00 2001 From: tdzl2003 Date: Tue, 23 Feb 2016 00:36:23 +0800 Subject: [PATCH] working --- .eslintrc | 29 ++++++++++++ .gitignore | 5 ++ .npmignore | 7 +++ local-cli/.babelrc | 14 ++++++ local-cli/cli.json | 35 +++++++++++++- local-cli/index.js | 28 +---------- local-cli/{ => src}/api.js | 0 local-cli/src/bundle.js | 62 +++++++++++++++++++++++++ local-cli/src/index.js | 45 ++++++++++++++++++ local-cli/{ => src}/user.js | 2 +- local-cli/src/utils.js | 28 +++++++++++ local-cli/utils.js | 13 ------ react-native-pushy-cli/.babelrc | 6 +++ react-native-pushy-cli/.npmignore | 7 +++ react-native-pushy-cli/package.json | 11 +++-- react-native-pushy-cli/{ => src}/cli.js | 25 +++------- 16 files changed, 254 insertions(+), 63 deletions(-) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 local-cli/.babelrc rename local-cli/{ => src}/api.js (100%) create mode 100644 local-cli/src/bundle.js create mode 100644 local-cli/src/index.js rename local-cli/{ => src}/user.js (96%) create mode 100644 local-cli/src/utils.js delete mode 100644 local-cli/utils.js create mode 100644 react-native-pushy-cli/.babelrc create mode 100644 react-native-pushy-cli/.npmignore rename react-native-pushy-cli/{ => src}/cli.js (67%) diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..f877c81 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,29 @@ +{ + "extends": "eslint-config-airbnb/base", + "parser": "babel-eslint", + "env": { + "browser": true, + "node": true, + "mocha": true + }, + "rules": { + // Disable for console/alert + "no-console": 0, + "no-alert": 0, + + "indent": [2, 2, {"SwitchCase": 1}] + }, + "plugins": [ + "import" + ], + "settings": { + "import/parser": "babel-eslint", + "import/resolve": { + "moduleDirectory": ["node_modules", "src"] + } + }, + "globals": { + "__DEV__": true, + "__OPTION__": true + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6a24665 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +/node_modules +/local-cli/lib +/react-native-pushy-cli/node_modules +/react-native-pushy-cli/lib diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..fe9797a --- /dev/null +++ b/.npmignore @@ -0,0 +1,7 @@ +/.idea +/.babelrc +/.npmignore +/.eslintrc +/.nvmrc +/.travis.yml +/local-cli/src diff --git a/local-cli/.babelrc b/local-cli/.babelrc new file mode 100644 index 0000000..88d637f --- /dev/null +++ b/local-cli/.babelrc @@ -0,0 +1,14 @@ +{ + "plugins": [ + "syntax-object-rest-spread", + "syntax-async-functions", + "transform-es2015-arrow-functions", + "transform-async-to-generator", + "transform-es2015-modules-commonjs", + "transform-es2015-destructuring", + "transform-es2015-spread", + "transform-object-rest-spread", + "transform-es2015-parameters", + "transform-strict-mode" + ] +} diff --git a/local-cli/cli.json b/local-cli/cli.json index 52e6e96..9bf4d19 100644 --- a/local-cli/cli.json +++ b/local-cli/cli.json @@ -17,8 +17,41 @@ }, "help": { + }, + "use": { + "description": "Select app created on web and create token for future release." + }, + "build": { + "description": "Bundle javascript and copy assets." + }, + "bundle": { + "description": "Bundle javascript code only." + }, + "release": { + "description": "Push builded file to server." } }, "globalOptions":{ + "dev": { + "default": false + }, + "platform": { + "hasValue": true + }, + "entryFile": { + "default": "index.${platform}.js", + "hasValue": true + }, + "intermediaDir": { + "default": "build/intermedia/${platform}", + "hasValue": true + }, + "output": { + "default": "build/output/${platform}.ppk", + "hasValue": true + }, + "verbose": { + + } } -} \ No newline at end of file +} diff --git a/local-cli/index.js b/local-cli/index.js index d36266c..0f38a3d 100644 --- a/local-cli/index.js +++ b/local-cli/index.js @@ -1,28 +1,4 @@ /** - * Created by tdzl2003 on 2/13/16. + * Created by tdzl2003 on 2/22/16. */ - -const {loadSession} = require('./api'); -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')); - - loadSession() - .then(()=>commands[argv.command](argv)) - .catch(err=>console.error(err.stack)); -}; +module.exports = require('./lib'); \ No newline at end of file diff --git a/local-cli/api.js b/local-cli/src/api.js similarity index 100% rename from local-cli/api.js rename to local-cli/src/api.js diff --git a/local-cli/src/bundle.js b/local-cli/src/bundle.js new file mode 100644 index 0000000..b8fca6a --- /dev/null +++ b/local-cli/src/bundle.js @@ -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)); + } +}; diff --git a/local-cli/src/index.js b/local-cli/src/index.js new file mode 100644 index 0000000..e3aec95 --- /dev/null +++ b/local-cli/src/index.js @@ -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; + })}); +}; diff --git a/local-cli/user.js b/local-cli/src/user.js similarity index 96% rename from local-cli/user.js rename to local-cli/src/user.js index 1f1ad3f..b2733c3 100644 --- a/local-cli/user.js +++ b/local-cli/src/user.js @@ -2,7 +2,7 @@ * Created by tdzl2003 on 2/13/16. */ -const {question} = require('./utils'); +import {question} from './utils'; const { post, get, diff --git a/local-cli/src/utils.js b/local-cli/src/utils.js new file mode 100644 index 0000000..778f384 --- /dev/null +++ b/local-cli/src/utils.js @@ -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, + }; +} \ No newline at end of file diff --git a/local-cli/utils.js b/local-cli/utils.js deleted file mode 100644 index 3992247..0000000 --- a/local-cli/utils.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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))); -} \ No newline at end of file diff --git a/react-native-pushy-cli/.babelrc b/react-native-pushy-cli/.babelrc new file mode 100644 index 0000000..c4755eb --- /dev/null +++ b/react-native-pushy-cli/.babelrc @@ -0,0 +1,6 @@ +{ + "plugins": [ + "transform-es2015-modules-commonjs", + "transform-strict-mode" + ] +} diff --git a/react-native-pushy-cli/.npmignore b/react-native-pushy-cli/.npmignore new file mode 100644 index 0000000..b8121b2 --- /dev/null +++ b/react-native-pushy-cli/.npmignore @@ -0,0 +1,7 @@ +/.idea +/src +/.babelrc +/.npmignore +/.eslintrc +/.nvmrc +/.travis.yml diff --git a/react-native-pushy-cli/package.json b/react-native-pushy-cli/package.json index be831ba..b276b16 100644 --- a/react-native-pushy-cli/package.json +++ b/react-native-pushy-cli/package.json @@ -4,10 +4,11 @@ "description": "Command tools for javaScript updater with `pushy` service for react native apps.", "main": "index.js", "bin": { - "pushy": "cli.js" + "pushy": "lib/cli.js" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "prepublish": "node_modules/.bin/babel src --out-dir lib" }, "repository": { "type": "git", @@ -25,7 +26,9 @@ "url": "https://github.com/reactnativecn/react-native-pushy/issues" }, "homepage": "https://github.com/reactnativecn/react-native-pushy/tree/master/react-native-pushy-cli", - "dependencies": { - "babel-register": "^6.5.2" + "devDependencies": { + "babel-cli": "^6.5.1", + "babel-plugin-transform-es2015-modules-commonjs": "^6.5.2", + "babel-plugin-transform-strict-mode": "^6.5.2" } } diff --git a/react-native-pushy-cli/cli.js b/react-native-pushy-cli/src/cli.js similarity index 67% rename from react-native-pushy-cli/cli.js rename to react-native-pushy-cli/src/cli.js index c13731b..b5655ca 100755 --- a/react-native-pushy-cli/cli.js +++ b/react-native-pushy-cli/src/cli.js @@ -3,11 +3,10 @@ * Created by tdzl2003 on 2/13/16. */ -var path = require('path'); -var fs = require('fs'); +import * as path from 'path'; +import * as fs from 'fs-promise'; - -var CLI_MODULE_PATH = function() { +const CLI_MODULE_PATH = function() { return path.resolve( process.cwd(), 'node_modules', @@ -16,7 +15,7 @@ var CLI_MODULE_PATH = function() { ); }; -var PACKAGE_JSON_PATH = function() { +const PACKAGE_JSON_PATH = function() { return path.resolve( process.cwd(), 'node_modules', @@ -27,18 +26,8 @@ var PACKAGE_JSON_PATH = function() { checkForVersionCommand(); -require('babel-register')({ - 'plugins': [ - 'transform-async-to-generator', - 'transform-strict-mode', - 'transform-object-rest-spread', - 'transform-es2015-parameters', - 'transform-es2015-destructuring', - ], -}); - -var cli; -var cliPath = CLI_MODULE_PATH(); +let cli; +const cliPath = CLI_MODULE_PATH(); if (fs.existsSync(cliPath)) { cli = require(cliPath); } @@ -53,7 +42,7 @@ if (cli) { function checkForVersionCommand() { if (process.argv.indexOf('-v') >= 0 || process.argv[2] === 'version') { - console.log('react-native-pushy-cli: ' + require('./package.json').version); + console.log('react-native-pushy-cli: ' + require('../package.json').version); try { console.log('react-native-pushy: ' + require(PACKAGE_JSON_PATH()).version); } catch (e) {