From 6c28afa92e820b2c5da75422390bd6d469ea5224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=93=E6=97=B8?= Date: Mon, 23 Sep 2019 12:04:43 +0800 Subject: [PATCH] feat: init & add network type * feat: init * feat: init * fix: rename module * fix: init --- .editorconfig | 15 ++++ .eslintignore | 5 ++ .eslintrc.js | 5 ++ .gitignore | 15 ++++ README.md | 14 +++- lerna.json | 21 +++++ package.json | 16 ++++ packages/universal-network-type/README.md | 11 +++ packages/universal-network-type/build.json | 11 +++ .../universal-network-type/demo/index.jsx | 19 +++++ packages/universal-network-type/package.json | 43 ++++++++++ packages/universal-network-type/src/index.ts | 14 ++++ .../src/miniapp/ali/index.ts | 9 ++ .../src/miniapp/factory.ts | 21 +++++ .../src/miniapp/wechat/index.ts | 9 ++ packages/universal-network-type/src/types.ts | 13 +++ packages/universal-network-type/tsconfig.json | 25 ++++++ scripts/compile-packages.js | 5 ++ scripts/compile.js | 83 +++++++++++++++++++ 19 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 .editorconfig create mode 100644 .eslintignore create mode 100644 .eslintrc.js create mode 100644 .gitignore create mode 100644 lerna.json create mode 100644 package.json create mode 100644 packages/universal-network-type/README.md create mode 100644 packages/universal-network-type/build.json create mode 100644 packages/universal-network-type/demo/index.jsx create mode 100644 packages/universal-network-type/package.json create mode 100644 packages/universal-network-type/src/index.ts create mode 100644 packages/universal-network-type/src/miniapp/ali/index.ts create mode 100644 packages/universal-network-type/src/miniapp/factory.ts create mode 100644 packages/universal-network-type/src/miniapp/wechat/index.ts create mode 100644 packages/universal-network-type/src/types.ts create mode 100644 packages/universal-network-type/tsconfig.json create mode 100644 scripts/compile-packages.js create mode 100644 scripts/compile.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..37789897 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true + +[*.gradle] +indent_size = 4 diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..f8e68539 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +node_modules +lib +dist +build +coverage diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..c748f7b2 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,5 @@ +module.exports = { + extends: ['eslint-config-rax/react', 'eslint-config-rax/typescript'], + globals: {}, + rules: {} +}; diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..106ee86c --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*~ +*.swp +.DS_Store +npm-debug.log +lerna-debug.log +npm-debug.log* +lib/ +dist/ +build/ +coverage/ +node_modules/ +examples/test +.idea/ +yarn.lock +package-lock.json diff --git a/README.md b/README.md index 9835f63e..ca77504d 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ -# universal-api \ No newline at end of file +# universal-api + +To extend the capabilities of the Rax system, we provide a series of Universal apis that developers can use to quickly develop multiple applications. + +## quick-start +```bash +$ npm run start +``` + +## build +```bash +$ npm run build +``` diff --git a/lerna.json b/lerna.json new file mode 100644 index 00000000..ff36ac73 --- /dev/null +++ b/lerna.json @@ -0,0 +1,21 @@ +{ + "packages": [ + "packages/*" + ], + "npmClient": "yarn", + "version": "1.0.0", + "command": { + "bootstrap": { + "npmClientArgs": [ + "--no-package-lock" + ] + }, + "publish": { + "skipGit": true, + "allowBranch": "master", + "ignoreChanges": [ + "*.md" + ] + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..1bede389 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "root", + "private": true, + "scripts": { + "start": "node ./scripts/compile-packages.js --watch", + "build": "node ./scripts/compile-packages.js" + }, + "devDependencies": { + "chalk": "^2.4.2", + "chokidar": "^3.1.0", + "eslint-config-rax": "^0.0.0", + "glob": "^7.1.4", + "lerna": "^3.13.2", + "minimist": "^1.2.0" + } +} diff --git a/packages/universal-network-type/README.md b/packages/universal-network-type/README.md new file mode 100644 index 00000000..f67484e5 --- /dev/null +++ b/packages/universal-network-type/README.md @@ -0,0 +1,11 @@ +# `universal-network-type` + +> TODO: description + +## Usage + +``` +const universalNetworkType = require('universal-network-type'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/universal-network-type/build.json b/packages/universal-network-type/build.json new file mode 100644 index 00000000..4fa7d03f --- /dev/null +++ b/packages/universal-network-type/build.json @@ -0,0 +1,11 @@ +{ + "plugins": [ + [ + "rax-plugin-component", + { + "enableTypescript": true, + "targets": ["web", "weex"] + } + ] + ] +} \ No newline at end of file diff --git a/packages/universal-network-type/demo/index.jsx b/packages/universal-network-type/demo/index.jsx new file mode 100644 index 00000000..a604d3b5 --- /dev/null +++ b/packages/universal-network-type/demo/index.jsx @@ -0,0 +1,19 @@ +/* eslint-disable import/no-extraneous-dependencies */ +import { createElement, render } from 'rax'; +import DriverUniversal from 'driver-universal'; +import View from 'rax-view'; +import Text from 'rax-text'; +import NetworkType from '../src'; + +const App = () => { + const handleClick = () => { + NetworkType.getNetworkType(); + }; + return ( + + click it! + + ); +}; + +render(, document.body, { driver: DriverUniversal }); diff --git a/packages/universal-network-type/package.json b/packages/universal-network-type/package.json new file mode 100644 index 00000000..3670515b --- /dev/null +++ b/packages/universal-network-type/package.json @@ -0,0 +1,43 @@ +{ + "name": "universal-network-type", + "version": "1.0.0", + "description": "network type", + "keywords": [ + "rax" + ], + "author": "balloonzzq ", + "homepage": "", + "license": "BSD-3-Clause", + "main": "./lib/index.js", + "directories": { + "lib": "lib" + }, + "files": [ + "lib" + ], + "scripts": { + "start": "rax-scripts start", + "build": "rax-scripts build", + "prepublish": "npm run build" + }, + "dependencies": { + "universal-env": "^2.0.0" + }, + "devDependencies": { + "@types/jest": "^24.0.12", + "@typescript-eslint/eslint-plugin": "^1.7.0", + "@typescript-eslint/parser": "^1.7.0", + "babel-eslint": "^10.0.1", + "driver-universal": "^1.0.1", + "eslint": "^5.15.1", + "eslint-config-rax": "^0.0.0", + "eslint-plugin-import": "^2.17.2", + "eslint-plugin-react": "^7.12.4", + "rax": "^1.0.4", + "rax-plugin-component": "^0.1.8", + "rax-scripts": "^2.0.1", + "rax-text": "^1.0.1", + "rax-view": "^1.0.2", + "typescript": "^3.4.5" + } +} diff --git a/packages/universal-network-type/src/index.ts b/packages/universal-network-type/src/index.ts new file mode 100644 index 00000000..68792941 --- /dev/null +++ b/packages/universal-network-type/src/index.ts @@ -0,0 +1,14 @@ +import { isMiniApp, isWeChatMiniprogram } from 'universal-env'; +import { NetworkType } from './types'; + +let NetworkType: NetworkType = {} as NetworkType; + +if (isMiniApp) { + NetworkType = require('./miniapp/ali').default; +} + +if (isWeChatMiniprogram) { + NetworkType = require('./miniapp/wechat').default; +} + +export default NetworkType; diff --git a/packages/universal-network-type/src/miniapp/ali/index.ts b/packages/universal-network-type/src/miniapp/ali/index.ts new file mode 100644 index 00000000..206362f2 --- /dev/null +++ b/packages/universal-network-type/src/miniapp/ali/index.ts @@ -0,0 +1,9 @@ +import { getNetworkTypeFactory } from '../factory'; + +declare const my: any; + +const getNetworkType = getNetworkTypeFactory(my); + +export default { + getNetworkType +}; diff --git a/packages/universal-network-type/src/miniapp/factory.ts b/packages/universal-network-type/src/miniapp/factory.ts new file mode 100644 index 00000000..abd85729 --- /dev/null +++ b/packages/universal-network-type/src/miniapp/factory.ts @@ -0,0 +1,21 @@ +import { Options, NetworkResult } from '../types'; + +export const getNetworkTypeFactory = (api: any) => { + return (options: Options = {}) => { + return new Promise((resolve, reject): void => { + api.getNetworkType({ + success: (res: NetworkResult) => { + options['success'] && options['success'](res); + resolve(res); + }, + fail: (err: any): void => { + options['fail'] && options['fail'](err); + reject(err); + }, + complete: (res: any): void => { + options['complete'] && options['complete'](res); + } + }); + }); + }; +}; diff --git a/packages/universal-network-type/src/miniapp/wechat/index.ts b/packages/universal-network-type/src/miniapp/wechat/index.ts new file mode 100644 index 00000000..68e4318b --- /dev/null +++ b/packages/universal-network-type/src/miniapp/wechat/index.ts @@ -0,0 +1,9 @@ +import { getNetworkTypeFactory } from '../factory'; + +declare const wx: any; + +const getNetworkType = getNetworkTypeFactory(wx); + +export default { + getNetworkType +}; diff --git a/packages/universal-network-type/src/types.ts b/packages/universal-network-type/src/types.ts new file mode 100644 index 00000000..11f77a4b --- /dev/null +++ b/packages/universal-network-type/src/types.ts @@ -0,0 +1,13 @@ +export interface Options { + success?: (res: NetworkResult) => {}; + fail?: (err: any) => {}; + complete?: (res: any) => {}; +} +export interface NetworkResult { + networkType: string; + networkAvailable?: boolean; +} + +export interface NetworkType { + getNetworkType(options?: Options): void +} diff --git a/packages/universal-network-type/tsconfig.json b/packages/universal-network-type/tsconfig.json new file mode 100644 index 00000000..70b1d932 --- /dev/null +++ b/packages/universal-network-type/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "module": "esNext", + "target": "es5", + "jsx": "preserve", + "jsxFactory": "createElement", + + "moduleResolution": "node", + "baseUrl": ".", + + "rootDir": "src", + "downlevelIteration": true, + "alwaysStrict": true, + "outDir": "lib", + "strict": true, + "preserveConstEnums": true, + "experimentalDecorators": true, + "noImplicitReturns": true, + "noUnusedLocals": true, + "noImplicitAny": false, + "noImplicitThis": false + }, + "include": ["src/**/*"], + "exclude": ["**/__tests__"] +} diff --git a/scripts/compile-packages.js b/scripts/compile-packages.js new file mode 100644 index 00000000..f8d9d2c6 --- /dev/null +++ b/scripts/compile-packages.js @@ -0,0 +1,5 @@ +'use strict'; + +const compile = require('./compile'); + +compile('packages'); diff --git a/scripts/compile.js b/scripts/compile.js new file mode 100644 index 00000000..88afa048 --- /dev/null +++ b/scripts/compile.js @@ -0,0 +1,83 @@ +'use strict'; +const fs = require('fs'); +const path = require('path'); +const { spawnSync } = require('child_process'); + +const glob = require('glob'); +const chalk = require('chalk'); +const parseArgs = require('minimist'); +const chokidar = require('chokidar'); + +const SRC_DIR = 'src'; +const BUILD_DIR = 'lib'; +const JS_FILES_PATTERN = '**/*.js'; +const IGNORE_PATTERN = '**/{__tests__,__mocks__}/**'; + +const args = parseArgs(process.argv); +const customPackages = args.packages; + +const fixedWidth = str => { + const WIDTH = 80; + const strs = str.match(new RegExp(`(.{1,${WIDTH}})`, 'g')); + let lastString = strs[strs.length - 1]; + if (lastString.length < WIDTH) { + lastString += Array(WIDTH - lastString.length).join(chalk.dim('.')); + } + return strs.slice(0, -1).concat(lastString).join('\n'); +}; + +function buildPackage(packagesDir, packageDir) { + const srcDir = path.resolve(packageDir, SRC_DIR); + + process.stdout.write( + fixedWidth(`${path.basename(packageDir)}\n`) + ); + spawnSync('npm', ['run', 'build'], { + cwd: packageDir + }); + process.stdout.write(`[ ${chalk.green('OK')} ]\n`); +} + +function getPackages(packagesDir, customPackages) { + return fs.readdirSync(packagesDir) + .map(file => path.resolve(packagesDir, file)) + .filter(f => { + if (customPackages) { + const packageName = path.relative(packagesDir, f).split(path.sep)[0]; + return packageName.indexOf(customPackages) !== -1; + } else { + return true; + } + }) + .filter(f => fs.lstatSync(path.resolve(f)).isDirectory()); +} + +module.exports = function compile(packagesName = 'packages') { + const packagesDir = path.resolve(__dirname, `../${packagesName}`); + + if (args.watch) { + // watch packages + const packages = getPackages(packagesDir, customPackages); + const watchPackagesDir = packages.map(dir => path.resolve(dir, SRC_DIR)); + + console.log(chalk.green('watch packages compile', packages)); + + chokidar.watch(watchPackagesDir, { + ignored: IGNORE_PATTERN + }).on('change', (filePath) => { + const packageName = filePath.match( new RegExp(`\/${packagesName}\/([^\/]*)`))[1]; + const packagePath = path.resolve(__dirname, `../${packagesName}/`, packageName); + process.stdout.write(chalk.bold.inverse(`Compiling package ${packageName} \n`)); + try { + buildPackage(packagesDir, packagePath); + } catch (e) {} + process.stdout.write('\n'); + }); + } else { + process.stdout.write(chalk.bold.inverse('Compiling packages\n')); + getPackages(packagesDir, customPackages).forEach(buildPackage.bind(null, packagesDir)); + process.stdout.write('\n'); + } +} + +// spawnSync('mkdir', ['-p', '/Users/balloon/Desktop/workSpace/raxjs/universal-api/packages/universal-network-type/dist']); \ No newline at end of file