Skip to content

Commit

Permalink
init populate structure 1#
Browse files Browse the repository at this point in the history
  • Loading branch information
albertleigh committed Mar 10, 2019
1 parent ecfe356 commit 7386e80
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 1 deletion.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
"description": "solace redux middleware wrapper",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prebuild": "rimraf build",
"build:es2015": "tsc",
"build:es2015-prod": "tsc -p tsconfig.prod.json",
"build:copy-files": "node ./scripts/copy-files.js",
"build": "yarn prebuild && yarn build:es2015 && yarn build:copy-files",
"build-prod": "yarn prebuild && yarn build:es2015-prod && yarn build:copy-files",
"test": "node scripts/test/js"
},
"repository": {
"type": "git",
Expand Down
58 changes: 58 additions & 0 deletions scripts/copy-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var path = require('path');
var fse = require('fs-extra');
var glob = require('glob');

async function copyFile(file){
const buildPath = path.resolve(__dirname,'../build/',path.basename(file));
await fse.copy(file,buildPath);
console.log(`Copied ${file} to ${buildPath}`);
}

async function createPackageFile() {
const packageData = await fse.readFile(path.resolve(__dirname,'../package.json'),'utf-8');
const {nyc, scripts, devDependencies, workspaces, ...packageDataOther} = JSON.parse(packageData);

const newPackageData = {
...packageDataOther,
main:'./index.js',
private:false,
};

const buildPath = path.resolve(__dirname,'../build/package.json');

await fse.writeFile(buildPath,JSON.stringify(newPackageData,null,2),'utf8');
console.log(`Created package.json in ${buildPath}`);

return newPackageData;
}

async function prepend(file,string) {
const data = await fse.readFile(file,'utf8');
await fse.writeFile(file,string+data,'utf8');
}

async function addLicense(packageData){
const license = `/** @license Redux solace v${packageData.version}
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
`;

await Promise.all(
[
'../build/index.js',
].map(file => prepend(path.resolve(__dirname, file), license)),
);

}

async function run(){
await Promise.all(
['./README.md','./CHANGELOG.md','./LICENSE.md'].map(file => copyFile(file)),
);
const packageData = await createPackageFile();
await addLicense(packageData);
}

run();
49 changes: 49 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

// Do this as the first thing so that any code reading it knows the right env.
process.env.NODE_ENV = 'test';

// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});

// Ensure environment variables are read.

const jest = require('jest');
const execSync = require('child_process').execSync;
let argv = process.argv.slice(2);

function isInGitRepository() {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}

function isInMercurialRepository() {
try {
execSync('hg --cwd . root', { stdio: 'ignore' });
return true;
} catch (e) {
return false;
}
}

// Watch unless on CI, in coverage mode, or explicitly running all tests
if (
!process.env.CI &&
argv.indexOf('--coverage') === -1 &&
argv.indexOf('--watchAll') === -1
) {
// https://github.com/facebook/create-react-app/issues/5210
const hasSourceControl = isInGitRepository() || isInMercurialRepository();
argv.push(hasSourceControl ? '--watch' : '--watchAll');
}


jest.run(argv);
8 changes: 8 additions & 0 deletions src/GlobalTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Store } from 'redux';
import { Action } from 'redux-actions';

export type ActionHandlerParams = {
action:Action<any>,
store:Store<any>,
next:Function,
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import init from './init';

init();

export { default as createSolaceMiddleware } from './middleware';
23 changes: 23 additions & 0 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import solace from 'solclientjs/lib-browser/solclient';

import SolaceContext from './utils/SolaceContext';

declare const window;

export interface IInitState {
solace:any,
solaceContext:SolaceContext,
}

export const initState:IInitState = {
solace: solace,
solaceContext: null,
};

function init() {
initState.solaceContext = new SolaceContext(solace);
window.solace = solace;
window.solaceContext = initState.solaceContext;
}

export default init;
28 changes: 28 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ActionHandlerParams } from './GlobalTypes';

import { initState } from './init';

declare const window;

const actionHandlers = {

};

export default () => {

return (store)=>(next)=>(action)=>{
const actionHandlerParams:ActionHandlerParams = {
store, next, action,
// solace:initState.solace,
// solaceContext:initState.solaceContext,
};

const handler = actionHandlers[action.type];
if (handler){
handler(actionHandlerParams);
}else{
return next(action);
}
};

}
27 changes: 27 additions & 0 deletions src/utils/createFSA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {createAction, ActionFunctionAny, Action} from 'redux-actions';

import noop from './noop';

export type ActionCreator<T> = ((options:T)=> Action<any>)

export default function createFSA<T>(type:string, payloadCreator:(payload:T)=>any):ActionCreator<T> {

const actionCreator:ActionFunctionAny<Action<any>> = createAction(type,payloadCreator);

return (payload:any) => {

let oriPayload:Action<any> = actionCreator(payload);

oriPayload.payload = oriPayload.payload?oriPayload.payload:{};

// oriPayload.payload.callback = oriPayload.payload.callback?oriPayload.payload.callback:noop;
// oriPayload.payload.errorCallback = oriPayload.payload.errorCallback?oriPayload.payload.errorCallback:noop;

return <Action<any>> {
...oriPayload,
error:payload && payload.name === 'Error',
}

}

}
4 changes: 4 additions & 0 deletions src/utils/makeType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const PREFIX:string = 'REDUX_SOLACE';
export const DELIMITER:string = '::';

export default (type:string) => `${PREFIX}${DELIMITER}${type}`;
1 change: 1 addition & 0 deletions src/utils/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default (...args:any[]) => {};

0 comments on commit 7386e80

Please sign in to comment.