Skip to content

Commit

Permalink
Merge pull request #14 from iuricmp/lint
Browse files Browse the repository at this point in the history
Adding Prettier
  • Loading branch information
D4ryl00 authored Aug 11, 2023
2 parents 1da26d9 + 6fb7708 commit 2769218
Show file tree
Hide file tree
Showing 29 changed files with 2,388 additions and 1,439 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ indent_size = 4
[*.tmpl]
indent_size = 2

[*.js]
[*.{js,jsx,cjs,ts,tsx}]
indent_size = 2
block_comment_start = /*
block_comment_end = */
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.pb.*
*.gen.*
build
53 changes: 53 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'@react-native',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'import'],
rules: {
'prettier/prettier': 0,
semi: ['error', 'never'],
'@typescript-eslint/quotes': ['error', 'single'],
quotes: ['error', 'single'],
'react-native/no-inline-styles': 0,
'jsx-quotes': ['error', 'prefer-single'],
'no-shadow': 0,
'no-catch-shadow': 0,
indent: ['error', 2],
'no-mixed-spaces-and-tabs': ['error', 'smart-tabs'],
'react/react-in-jsx-scope': 2,
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/ban-ts-comment': 'warn',
'import/order': [
'error',
{
groups: [
['builtin', 'external'],
'internal',
['sibling', 'parent', 'index'],
'object',
'type',
],
pathGroups: [
{
pattern: '@berty/**',
group: 'internal',
position: 'before',
},
],
pathGroupsExcludedImportTypes: [],
'newlines-between': 'always',
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
},
],
},
globals: {
JSX: 'readonly',
},
}
5 changes: 0 additions & 5 deletions .eslintrc.js

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Cache
.cache
.eslintcache

# VSCode
.vscode/
Expand Down
12 changes: 6 additions & 6 deletions .releaserc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
branch: 'main',
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/github',
],
branch: 'main',
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
'@semantic-release/github',
],
};
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ generate: api.generate

# Clean all generated files
clean: api.clean bind.clean
rm -rf .eslintcache

# Force clean (clean and remove node_modules)
fclean: clean
Expand Down Expand Up @@ -145,3 +146,15 @@ _bind.clean.android:

bind.clean: _bind.clean.ios _bind.clean.android
rm -f $(bind_init_files)

# lint

lint: node_modules
npx eslint --cache --quiet --ext=.js,.jsx,.ts,.tsx .
npx tsc # check static typing

lint.fix: node_modules
npx eslint --cache --quiet --ext=.js,.jsx,.ts,.tsx --fix .
npx tsc # check static typing

include makefiles/asdf.mk
50 changes: 24 additions & 26 deletions example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import { StyleSheet, Text, View } from 'react-native';
import { useState, useEffect } from 'react';
import * as WeshnetExpo from '@weshnet/expo';
import * as WeshnetExpo from '@weshnet/expo'
import React, { useState, useEffect } from 'react'
import { StyleSheet, Text, View } from 'react-native'

export default function App() {
const [peerID, setPeerID] = useState<string>()
const [peerID, setPeerID] = useState<string>()

useEffect(() => {
WeshnetExpo.init().then(client => {
client.serviceGetConfiguration({}).then((res) => {
setPeerID(res.peerId)
console.log(res)
})
})
}, [])
useEffect(() => {
WeshnetExpo.init().then((client) => {
client.serviceGetConfiguration({}).then((res) => {
setPeerID(res.peerId)
console.log(res)
})
})
}, [])

const loadingView = (<Text> Loading Weshnet... </Text>)
const weshView = (<Text>hello my peerid is: {peerID}</Text>)
return (
<View style={styles.container}>
{!peerID ? loadingView : weshView}
</View>
);
const loadingView = <Text> Loading Weshnet... </Text>
const weshView = <Text>hello my peerid is: {peerID}</Text>
return (
<View style={styles.container}>{!peerID ? loadingView : weshView}</View>
)
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
})
8 changes: 4 additions & 4 deletions example/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const path = require('path') /* eslint-disable-line @typescript-eslint/no-var-requires */
module.exports = function (api) {
api.cache(true);
api.cache(true)
return {
presets: ['babel-preset-expo'],
plugins: [
Expand All @@ -15,5 +15,5 @@ module.exports = function (api) {
},
],
],
};
};
}
}
6 changes: 3 additions & 3 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { registerRootComponent } from 'expo';
import { registerRootComponent } from 'expo'

import App from './App';
import App from './App'

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App);
registerRootComponent(App)
17 changes: 9 additions & 8 deletions example/metro.config.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require('expo/metro-config');
const path = require('path');
const { getDefaultConfig } = require('expo/metro-config') /* eslint-disable-line @typescript-eslint/no-var-requires */
const path = require('path') /* eslint-disable-line @typescript-eslint/no-var-requires */

const config = getDefaultConfig(__dirname);
const config = getDefaultConfig(__dirname)

// npm v7+ will install ../node_modules/react-native because of peerDependencies.
// To prevent the incompatible react-native bewtween ./node_modules/react-native and ../node_modules/react-native,
// excludes the one from the parent folder when bundling.
config.resolver.blockList = [
...Array.from(config.resolver.blockList ?? []),
new RegExp(path.resolve('..', 'node_modules', 'react-native')),
];
]

config.resolver.nodeModulesPaths = [
path.resolve(__dirname, './node_modules'),
path.resolve(__dirname, '../node_modules'),
];
]

config.watchFolders = [path.resolve(__dirname, '..')];
config.watchFolders = [path.resolve(__dirname, '..')]

config.transformer.getTransformOptions = async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
});
})

module.exports = config

module.exports = config;
12 changes: 6 additions & 6 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const createConfigAsync = require('@expo/webpack-config');
const path = require('path');
const createConfigAsync = require('@expo/webpack-config') /* eslint-disable-line @typescript-eslint/no-var-requires */
const path = require('path') /* eslint-disable-line @typescript-eslint/no-var-requires */

module.exports = async (env, argv) => {
const config = await createConfigAsync(
Expand All @@ -10,11 +10,11 @@ module.exports = async (env, argv) => {
},
},
argv
);
)
config.resolve.modules = [
path.resolve(__dirname, './node_modules'),
path.resolve(__dirname, '../node_modules'),
];
]

return config;
};
return config
}
58 changes: 28 additions & 30 deletions gen-clients.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,82 @@
import pbjs from 'protobufjs';
import Handlebars from 'handlebars';
import Handlebars from 'handlebars'
import pbjs from 'protobufjs'

// Load Protocol Buffer from specified .proto file
const pb = pbjs.loadSync('api/protocoltypes.proto');
const pb = pbjs.loadSync('api/protocoltypes.proto')

// Function to decapitalize the first letter of a string
const uncap = (str) => str.charAt(0).toLowerCase() + str.slice(1);
const uncap = (str) => str.charAt(0).toLowerCase() + str.slice(1)

const getInterface = (typ) => {
const parts = typ.split('.');
const ilast = `I${parts.pop()}`;
parts.push(ilast);
return parts.join('.');
const parts = typ.split('.')
const ilast = `I${parts.pop()}`
parts.push(ilast)
return parts.join('.')
}

// Define an array of services
const services = [
'weshnet.protocol.v1.ProtocolService',
];
const services = ['weshnet.protocol.v1.ProtocolService']

// Prepare Handlebars templates
const serviceClientTemplate = Handlebars.compile(`
import api from './api/index.d'
import { Unary, ResponseStream, RequestStream } from './types'
export type ServiceClientType<S> = {{#each services}} {{this}} {{/each}}never;
`);
`)

const ClientTemplate = Handlebars.compile(`
export interface {{name}}Client {
{{#each methods}}
{{this.name}}: {{this.type}}<api.{{this.svcName}}.{{this.request}}, api.{{this.svcName}}.{{this.reply}}>,
{{/each}}
}
`);
`)

// Gather data for ServiceClientType
let serviceData = {
services: services.map(svcType => {
const svc = pb.lookup(svcType);
return `S extends typeof api.${svc.parent.parent.name}.${svc.name} ? ${svc.name}Client :`;
})
};
services: services.map((svcType) => {
const svc = pb.lookup(svcType)
return `S extends typeof api.${svc.parent.parent.name}.${svc.name} ? ${svc.name}Client :`
}),
}

// Generate and output ServiceClientType
console.log(serviceClientTemplate(serviceData));
console.log(serviceClientTemplate(serviceData))

// Gather data for each Client and output
for (const svcType of services) {
const svc = pb.lookup(svcType);
const svc = pb.lookup(svcType)
const methodsData = Object.entries(svc.methods).map(([key, method]) => {
let rpcType = '';
let rpcType = ''
if (!method.requestStream && !method.responseStream) {
// Unary
rpcType = 'Unary';
rpcType = 'Unary'
} else if (method.requestStream && !method.responseStream) {
// Request Stream
rpcType = 'RequestStream';
rpcType = 'RequestStream'
} else if (!method.requestStream && method.responseStream) {
// Response Stream
rpcType = 'ResponseStream';
rpcType = 'ResponseStream'
} else {
// Dubplex
rpcType = 'never';
rpcType = 'never'
}

const name = uncap(key);
const name = uncap(key)
return {
name: name,
request: getInterface(method.requestType),
reply: method.responseType,
type: rpcType,
svcName: `${svc.parent.parent.name}`,
};
});
}
})

let ClientData = {
name: svc.name,
methods: methodsData,
};
}

console.log(ClientTemplate(ClientData));
console.log(ClientTemplate(ClientData))
}
Loading

0 comments on commit 2769218

Please sign in to comment.