Skip to content

Commit

Permalink
feat: init & add network type
Browse files Browse the repository at this point in the history
* feat: init

* feat: init

* fix: rename module

* fix: init
  • Loading branch information
balloonzzq authored and SoloJiang committed Sep 23, 2019
1 parent 28e37e1 commit 6c28afa
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 1 deletion.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
lib
dist
build
coverage
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
extends: ['eslint-config-rax/react', 'eslint-config-rax/typescript'],
globals: {},
rules: {}
};
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# universal-api
# 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
```
21 changes: 21 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
11 changes: 11 additions & 0 deletions packages/universal-network-type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `universal-network-type`

> TODO: description
## Usage

```
const universalNetworkType = require('universal-network-type');
// TODO: DEMONSTRATE API
```
11 changes: 11 additions & 0 deletions packages/universal-network-type/build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"rax-plugin-component",
{
"enableTypescript": true,
"targets": ["web", "weex"]
}
]
]
}
19 changes: 19 additions & 0 deletions packages/universal-network-type/demo/index.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<View onClick={handleClick}>
<Text>click it!</Text>
</View>
);
};

render(<App />, document.body, { driver: DriverUniversal });
43 changes: 43 additions & 0 deletions packages/universal-network-type/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "universal-network-type",
"version": "1.0.0",
"description": "network type",
"keywords": [
"rax"
],
"author": "balloonzzq <zqzhuang888@qq.com>",
"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"
}
}
14 changes: 14 additions & 0 deletions packages/universal-network-type/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions packages/universal-network-type/src/miniapp/ali/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getNetworkTypeFactory } from '../factory';

declare const my: any;

const getNetworkType = getNetworkTypeFactory(my);

export default {
getNetworkType
};
21 changes: 21 additions & 0 deletions packages/universal-network-type/src/miniapp/factory.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
};
};
9 changes: 9 additions & 0 deletions packages/universal-network-type/src/miniapp/wechat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getNetworkTypeFactory } from '../factory';

declare const wx: any;

const getNetworkType = getNetworkTypeFactory(wx);

export default {
getNetworkType
};
13 changes: 13 additions & 0 deletions packages/universal-network-type/src/types.ts
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions packages/universal-network-type/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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__"]
}
5 changes: 5 additions & 0 deletions scripts/compile-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

const compile = require('./compile');

compile('packages');
83 changes: 83 additions & 0 deletions scripts/compile.js
Original file line number Diff line number Diff line change
@@ -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']);

0 comments on commit 6c28afa

Please sign in to comment.