From 7386e80b4045e0e3dd97db8d721776327ae801dc Mon Sep 17 00:00:00 2001 From: Albert Li Date: Sun, 10 Mar 2019 01:53:10 -0500 Subject: [PATCH] init populate structure 1# --- package.json | 8 +++++- scripts/copy-files.js | 58 ++++++++++++++++++++++++++++++++++++++++++ scripts/test.js | 49 +++++++++++++++++++++++++++++++++++ src/GlobalTypes.ts | 8 ++++++ src/index.ts | 5 ++++ src/init.ts | 23 +++++++++++++++++ src/middleware.ts | 28 ++++++++++++++++++++ src/utils/createFSA.ts | 27 ++++++++++++++++++++ src/utils/makeType.ts | 4 +++ src/utils/noop.ts | 1 + 10 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 scripts/copy-files.js create mode 100644 scripts/test.js create mode 100644 src/GlobalTypes.ts create mode 100644 src/index.ts create mode 100644 src/init.ts create mode 100644 src/middleware.ts create mode 100644 src/utils/createFSA.ts create mode 100644 src/utils/makeType.ts create mode 100644 src/utils/noop.ts diff --git a/package.json b/package.json index 409e818..07cdeca 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/copy-files.js b/scripts/copy-files.js new file mode 100644 index 0000000..e1e5081 --- /dev/null +++ b/scripts/copy-files.js @@ -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(); diff --git a/scripts/test.js b/scripts/test.js new file mode 100644 index 0000000..f22998c --- /dev/null +++ b/scripts/test.js @@ -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); diff --git a/src/GlobalTypes.ts b/src/GlobalTypes.ts new file mode 100644 index 0000000..9500916 --- /dev/null +++ b/src/GlobalTypes.ts @@ -0,0 +1,8 @@ +import { Store } from 'redux'; +import { Action } from 'redux-actions'; + +export type ActionHandlerParams = { + action:Action, + store:Store, + next:Function, +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..2e27176 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,5 @@ +import init from './init'; + +init(); + +export { default as createSolaceMiddleware } from './middleware'; \ No newline at end of file diff --git a/src/init.ts b/src/init.ts new file mode 100644 index 0000000..aeb26ba --- /dev/null +++ b/src/init.ts @@ -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; \ No newline at end of file diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..ec674f5 --- /dev/null +++ b/src/middleware.ts @@ -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); + } + }; + +} \ No newline at end of file diff --git a/src/utils/createFSA.ts b/src/utils/createFSA.ts new file mode 100644 index 0000000..98193b2 --- /dev/null +++ b/src/utils/createFSA.ts @@ -0,0 +1,27 @@ +import {createAction, ActionFunctionAny, Action} from 'redux-actions'; + +import noop from './noop'; + +export type ActionCreator = ((options:T)=> Action) + +export default function createFSA(type:string, payloadCreator:(payload:T)=>any):ActionCreator { + + const actionCreator:ActionFunctionAny> = createAction(type,payloadCreator); + + return (payload:any) => { + + let oriPayload:Action = 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 > { + ...oriPayload, + error:payload && payload.name === 'Error', + } + + } + +} diff --git a/src/utils/makeType.ts b/src/utils/makeType.ts new file mode 100644 index 0000000..f863c7a --- /dev/null +++ b/src/utils/makeType.ts @@ -0,0 +1,4 @@ +export const PREFIX:string = 'REDUX_SOLACE'; +export const DELIMITER:string = '::'; + +export default (type:string) => `${PREFIX}${DELIMITER}${type}`; diff --git a/src/utils/noop.ts b/src/utils/noop.ts new file mode 100644 index 0000000..c33ea8b --- /dev/null +++ b/src/utils/noop.ts @@ -0,0 +1 @@ +export default (...args:any[]) => {};