boilersuit
Advanced tools
Comparing version 0.3.2 to 0.3.3
const colors = require('colors'); // eslint-disable-line | ||
const fs = require('fs'); | ||
const diff = require('deep-diff'); | ||
const Cases = require('../../tools/cases'); | ||
const writeIndex = require('./writeIndex'); | ||
@@ -16,8 +14,12 @@ const writeSelectors = require('./writeSelectors'); | ||
const printError = require('../../tools/printError'); | ||
const printWarning = require('../../tools/printWarning'); | ||
const printMessages = require('../../tools/printMessages'); | ||
const checkIfBadBuffer = require('../../tools/checkIfBadBuffer'); | ||
const printWarning = require('../../tools/printWarning'); | ||
const checkErrorsInSchema = require('../../tools/checkErrorsInSchema'); | ||
const checkWarningsInSchema = require('../../tools/checkWarningsInSchema'); | ||
const checkForConfigFile = require('./checkForConfigFile'); | ||
const checkForChanges = require('./checkForChanges'); | ||
const detectDiff = require('./detectDiff'); | ||
const runPrettier = require('./runPrettier'); | ||
const Cases = require('../../tools/cases'); | ||
const { | ||
@@ -31,321 +33,270 @@ parseCamelCaseToArray, | ||
const up = (schemaFile, { quiet = false, force = false } = {}) => { | ||
const schemaBuf = fs.readFileSync(schemaFile).toString(); | ||
/** Gives us the folder where the schema file lives */ | ||
const folder = schemaFile | ||
.slice(0, -9) | ||
// This replaces all backslashes with forward slashes on Windows | ||
.replace(/\\/g, '/'); | ||
const dotSuitFolder = folder.replace(/\//g, '-'); | ||
fs.readFile(schemaFile, (err, s) => { | ||
const schemaBuf = s.toString(); | ||
/** Gives us the folder where the schema file lives */ | ||
const folder = schemaFile | ||
.slice(0, -9) | ||
// This replaces all backslashes with forward slashes on Windows | ||
.replace(/\\/g, '/'); | ||
const dotSuitFolder = folder.replace(/\//g, '-'); | ||
let schema; | ||
try { | ||
schema = JSON.parse(schemaBuf.toString()); | ||
} catch (e) { | ||
console.log('Error: ', e); | ||
} | ||
if (!schema) return; | ||
let schema; | ||
try { | ||
schema = JSON.parse(schemaBuf.toString()); | ||
} catch (e) { | ||
console.log('Error: ', e); | ||
} | ||
if (!schema) return; | ||
const arrayOfDomains = Object.keys(schema).map(key => ({ | ||
...schema[key], | ||
domainName: key, | ||
})); | ||
const arrayOfDomains = Object.keys(schema).map(key => ({ | ||
...schema[key], | ||
domainName: key, | ||
})); | ||
/** Checks for an 'extends' keyword */ | ||
/** Checks for an 'extends' keyword */ | ||
const extendsFound = checkExtends({ | ||
arrayOfDomains, | ||
folder, | ||
schemaFile, | ||
schemaBuf, | ||
}); | ||
const extendsFound = checkExtends({ | ||
arrayOfDomains, | ||
folder, | ||
schemaFile, | ||
schemaBuf, | ||
}); | ||
if (extendsFound) return; | ||
if (extendsFound) return; | ||
const errors = [ | ||
...checkErrorsInSchema(schema, folder), | ||
...checkIfBadBuffer(folder), | ||
]; | ||
const errors = [ | ||
...checkErrorsInSchema(schema, folder), | ||
...checkIfBadBuffer(folder), | ||
]; | ||
if (errors.length) { | ||
console.log(`\n ${folder}suit.json `.white.bgRed); | ||
printError(errors); | ||
return; | ||
} | ||
const config = checkForConfigFile(); | ||
const warnings = checkWarningsInSchema(schema, config); | ||
// If no .suit folder exists, create one | ||
if (!fs.existsSync('./.suit')) { | ||
fs.mkdirSync('./.suit'); | ||
} | ||
/** Check for a previous suit file in folder - force prevents this check */ | ||
if (fs.existsSync(`./.suit/${dotSuitFolder}/suit.old.json`) && !force) { | ||
if ( | ||
fs.readFileSync(`./.suit/${dotSuitFolder}/suit.old.json`).toString() === | ||
schemaBuf | ||
) { | ||
if (warnings.length) { | ||
console.log(`\n ${folder}suit.json `.bgYellow.black); | ||
printWarning(warnings); | ||
} else if (!quiet) { | ||
console.log(`\n ${folder}suit.json `.bgGreen.black); | ||
console.log( | ||
`\n NO CHANGES:`.green + | ||
` No changes found in suit file from previous version. Not editing files.`, | ||
); | ||
} | ||
if (errors.length) { | ||
console.log(`\n ${folder}suit.json `.white.bgRed); | ||
printError(errors); | ||
return; | ||
} | ||
} | ||
if (warnings.length) { | ||
console.log(`\n ${folder}suit.json `.bgYellow.black); | ||
} else { | ||
console.log(`\n ${folder}suit.json `.bgGreen.black); | ||
} | ||
const config = checkForConfigFile(); | ||
/** Get a more detailed diff of the changes */ | ||
const warnings = checkWarningsInSchema(schema, config); | ||
let keyChanges = []; | ||
// If no .suit folder exists, create one | ||
if (!fs.existsSync('./.suit')) { | ||
fs.mkdirSync('./.suit'); | ||
} | ||
if (fs.existsSync(`./.suit/${dotSuitFolder}/suit.old.json`)) { | ||
const oldSchemaBuf = fs | ||
.readFileSync(`./.suit/${dotSuitFolder}/suit.old.json`) | ||
.toString(); | ||
/** Check for a previous suit file in folder - force prevents this check */ | ||
const shouldContinue = checkForChanges({ | ||
dotSuitFolder, | ||
quiet, | ||
force, | ||
folder, | ||
schemaBuf, | ||
warnings, | ||
}); | ||
if (!shouldContinue) return; | ||
const differences = | ||
diff(JSON.parse(oldSchemaBuf), JSON.parse(schemaBuf)) || []; | ||
keyChanges = [ | ||
...differences | ||
.filter(({ kind }) => kind === 'D' || kind === 'N') | ||
.map(({ path }, index) => { | ||
if (!differences[index + 1]) return null; | ||
const newPath = differences[index + 1].path; | ||
return JSON.stringify(path.slice(0, path.length - 1)) === | ||
JSON.stringify(newPath.slice(0, newPath.length - 1)) | ||
? { | ||
removed: path, | ||
added: newPath, | ||
removedCases: new Cases( | ||
parseCamelCaseToArray(path[path.length - 1]), | ||
).all(), | ||
addedCases: new Cases( | ||
parseCamelCaseToArray(newPath[newPath.length - 1]), | ||
).all(), | ||
} | ||
: null; | ||
}) | ||
.filter(n => n !== null), | ||
...differences | ||
.filter(({ path, lhs, rhs }) => path.includes('saga') && lhs && rhs) | ||
.map(({ lhs, rhs, path }) => ({ | ||
removed: path, | ||
removedCases: new Cases(parseCamelCaseToArray(`${lhs}`)).all(), | ||
addedCases: new Cases(parseCamelCaseToArray(`${rhs}`)).all(), | ||
})), | ||
]; | ||
} | ||
if (warnings.length) { | ||
console.log(`\n ${folder}suit.json `.bgYellow.black); | ||
} else { | ||
console.log(`\n ${folder}suit.json `.bgGreen.black); | ||
} | ||
/** Write reducer */ | ||
/** Get a more detailed diff of the changes */ | ||
const keyChanges = detectDiff({ dotSuitFolder, schemaBuf }); | ||
const { buffer: newReducerBuffer, errors: domainErrors } = writeReducer({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
/** Write reducer */ | ||
const { buffer: newReducerBuffer, errors: domainErrors } = writeReducer({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
if (domainErrors.length) { | ||
printError(domainErrors); | ||
return; | ||
} | ||
if (domainErrors.length) { | ||
printError(domainErrors); | ||
return; | ||
} | ||
/** Write Actions */ | ||
/** Write Actions */ | ||
const { buffer: newActionsBuffer } = writeActions({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
const { buffer: newActionsBuffer } = writeActions({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
/** Write Constants */ | ||
const { buffer: newConstantsBuffer } = writeConstants({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
/** Write Constants */ | ||
/** Write Selectors */ | ||
const selectorsBuffer = fs | ||
.readFileSync(`${folder}/selectors.js`) | ||
.toString(); | ||
const { buffer: newConstantsBuffer } = writeConstants({ | ||
folder, | ||
arrayOfDomains, | ||
}); | ||
const newSelectorsBuffer = transforms(selectorsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
/** Write Selectors */ | ||
return writeSelectors({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
initialState, | ||
folder, | ||
}); | ||
}), | ||
]); | ||
const selectorsBuffer = fs.readFileSync(`${folder}/selectors.js`).toString(); | ||
/** Write Index */ | ||
const indexBuffer = fs.readFileSync(`${folder}/index.js`).toString(); | ||
const newSelectorsBuffer = transforms(selectorsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
const newIndexBuffer = transforms(indexBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, actions, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
return writeSelectors({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
initialState, | ||
folder, | ||
}); | ||
}), | ||
]); | ||
return writeIndex({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
initialState, | ||
keyChanges, | ||
actions, | ||
}); | ||
}), | ||
]); | ||
/** Write Index */ | ||
/** Write Saga */ | ||
const saga = writeSaga({ | ||
folder, | ||
arrayOfDomains, | ||
keyChanges, | ||
}); | ||
const indexBuffer = fs.readFileSync(`${folder}/index.js`).toString(); | ||
if (saga.errors.length) { | ||
printError(saga.errors); | ||
return; | ||
} | ||
const newIndexBuffer = transforms(indexBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, actions, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
if (saga.messages.length) { | ||
printMessages(saga.messages); | ||
} | ||
return writeIndex({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
initialState, | ||
keyChanges, | ||
actions, | ||
}); | ||
}), | ||
]); | ||
/** Write reducer tests */ | ||
const reducerTestsBuffer = fs.existsSync(`${folder}/tests/reducer.test.js`) | ||
? fs.readFileSync(`${folder}/tests/reducer.test.js`).toString() | ||
: ''; | ||
/** Write Saga */ | ||
const newReducerTestBuffer = transforms(reducerTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState, actions }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
const { errors: sagaErrors, buffer: newSagaBuffer } = writeSaga({ | ||
folder, | ||
arrayOfDomains, | ||
keyChanges, | ||
}); | ||
return writeReducerTests({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
actions, | ||
initialState, | ||
}); | ||
}), | ||
]); | ||
if (sagaErrors.length) { | ||
printError(sagaErrors); | ||
return; | ||
} | ||
/** Write actions tests */ | ||
const actionTestsBuffer = fs.existsSync(`${folder}/tests/actions.test.js`) | ||
? fs.readFileSync(`${folder}/tests/actions.test.js`).toString() | ||
: ''; | ||
/** Write reducer tests */ | ||
const newActionTestsBuffer = transforms(actionTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState, actions }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
const reducerTestsBuffer = fs.existsSync(`${folder}/tests/reducer.test.js`) | ||
? fs.readFileSync(`${folder}/tests/reducer.test.js`).toString() | ||
: ''; | ||
const arrayOfActions = Object.keys(actions).map(key => ({ | ||
...actions[key], | ||
name: key, | ||
cases: new Cases(parseCamelCaseToArray(key)).all(), | ||
})); | ||
const newReducerTestBuffer = transforms(reducerTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState, actions }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
return writeActionTests({ | ||
buffer: b, | ||
domainCases: allDomainCases, | ||
arrayOfActions, | ||
initialState, | ||
}); | ||
}), | ||
]); | ||
return writeReducerTests({ | ||
buffer: b, | ||
cases: allDomainCases, | ||
actions, | ||
initialState, | ||
}); | ||
}), | ||
]); | ||
/** Write selectors tests */ | ||
const selectorsTestsBuffer = fs.existsSync( | ||
`${folder}/tests/selectors.test.js`, | ||
) | ||
? fs.readFileSync(`${folder}/tests/selectors.test.js`).toString() | ||
: ''; | ||
/** Write actions tests */ | ||
const newSelectorsTestsBuffer = transforms(selectorsTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)).all(); | ||
const actionTestsBuffer = fs.existsSync(`${folder}/tests/actions.test.js`) | ||
? fs.readFileSync(`${folder}/tests/actions.test.js`).toString() | ||
: ''; | ||
return writeSelectorTests({ | ||
buffer: b, | ||
cases, | ||
initialState, | ||
folder, | ||
}); | ||
}), | ||
]); | ||
const newActionTestsBuffer = transforms(actionTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState, actions }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
console.log('\nCHANGES:'.green); | ||
const arrayOfActions = Object.keys(actions).map(key => ({ | ||
...actions[key], | ||
name: key, | ||
cases: new Cases(parseCamelCaseToArray(key)).all(), | ||
})); | ||
console.log('- writing reducers'); | ||
fs.writeFileSync(`${folder}/reducer.js`, newReducerBuffer); | ||
return writeActionTests({ | ||
buffer: b, | ||
domainCases: allDomainCases, | ||
arrayOfActions, | ||
initialState, | ||
}); | ||
}), | ||
]); | ||
console.log('- writing reducer tests'); | ||
fs.writeFileSync(`${folder}/tests/reducer.test.js`, newReducerTestBuffer); | ||
/** Write selectors tests */ | ||
console.log('- writing actions'); | ||
fs.writeFileSync(`${folder}/actions.js`, newActionsBuffer); | ||
const selectorsTestsBuffer = fs.existsSync( | ||
`${folder}/tests/selectors.test.js`, | ||
) | ||
? fs.readFileSync(`${folder}/tests/selectors.test.js`).toString() | ||
: ''; | ||
console.log('- writing action tests'); | ||
fs.writeFileSync(`${folder}/tests/actions.test.js`, newActionTestsBuffer); | ||
const newSelectorsTestsBuffer = transforms(selectorsTestsBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, initialState }) => b => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)).all(); | ||
console.log('- writing constants'); | ||
fs.writeFileSync(`${folder}/constants.js`, newConstantsBuffer); | ||
return writeSelectorTests({ | ||
buffer: b, | ||
cases, | ||
initialState, | ||
folder, | ||
}); | ||
}), | ||
]); | ||
console.log('- writing selectors'); | ||
fs.writeFileSync(`${folder}/selectors.js`, newSelectorsBuffer); | ||
console.log('\nCHANGES:'.green); | ||
console.log('- writing selectors tests'); | ||
fs.writeFileSync( | ||
`${folder}/tests/selectors.test.js`, | ||
newSelectorsTestsBuffer, | ||
); | ||
console.log('- writing reducers'); | ||
fs.writeFileSync(`${folder}/reducer.js`, newReducerBuffer); | ||
console.log('- writing index'); | ||
fs.writeFileSync(`${folder}/index.js`, newIndexBuffer); | ||
console.log('- writing reducer tests'); | ||
fs.writeFileSync(`${folder}/tests/reducer.test.js`, newReducerTestBuffer); | ||
console.log('- writing saga'); | ||
fs.writeFileSync(`${folder}/saga.js`, saga.buffer); | ||
console.log('- writing actions'); | ||
fs.writeFileSync(`${folder}/actions.js`, newActionsBuffer); | ||
console.log('- saving old suit file in .suit directory'); | ||
if (!fs.existsSync(`./.suit/${dotSuitFolder}`)) { | ||
fs.mkdirSync(`./.suit/${dotSuitFolder}`); | ||
} | ||
fs.writeFileSync(`./.suit/${dotSuitFolder}/suit.old.json`, schemaBuf); | ||
console.log('- writing action tests'); | ||
fs.writeFileSync(`${folder}/tests/actions.test.js`, newActionTestsBuffer); | ||
/** Runs prettier and checks for prettier warnings */ | ||
const prettierWarnings = runPrettier(folder); | ||
console.log('- writing constants'); | ||
fs.writeFileSync(`${folder}/constants.js`, newConstantsBuffer); | ||
console.log('- writing selectors'); | ||
fs.writeFileSync(`${folder}/selectors.js`, newSelectorsBuffer); | ||
console.log('- writing selectors tests'); | ||
fs.writeFileSync( | ||
`${folder}/tests/selectors.test.js`, | ||
newSelectorsTestsBuffer, | ||
); | ||
console.log('- writing index'); | ||
fs.writeFileSync(`${folder}/index.js`, newIndexBuffer); | ||
console.log('- writing saga'); | ||
fs.writeFileSync(`${folder}/saga.js`, newSagaBuffer); | ||
console.log('- saving old suit file in .suit directory'); | ||
if (!fs.existsSync(`./.suit/${dotSuitFolder}`)) { | ||
fs.mkdirSync(`./.suit/${dotSuitFolder}`); | ||
} | ||
fs.writeFileSync(`./.suit/${dotSuitFolder}/suit.old.json`, schemaBuf); | ||
/** Runs prettier and checks for errors */ | ||
const prettierErrors = runPrettier(folder); | ||
printWarning([...warnings, ...prettierErrors]); | ||
printWarning([...warnings, ...prettierWarnings]); | ||
}); | ||
}; | ||
module.exports = up; |
@@ -1,9 +0,3 @@ | ||
const Cases = require('../../../tools/cases'); | ||
const { | ||
concat, | ||
transforms, | ||
parseCamelCaseToArray, | ||
ensureImport, | ||
prettify, | ||
} = require('../../../tools/utils'); | ||
const { concat, transforms, prettify } = require('../../../tools/utils'); | ||
const writeOneAction = require('./writeOneAction'); | ||
@@ -18,39 +12,4 @@ module.exports = ({ buffer, cases, actions }) => | ||
.reverse() | ||
.map(({ name, set, payload: payloadOverride, describe }, i) => b => { | ||
const c = new Cases(parseCamelCaseToArray(name)); | ||
const actionCases = c.all(); | ||
/** Ensures the imports of the constants */ | ||
const newBuffer = ensureImport(actionCases.constant, './constants', { | ||
destructure: true, | ||
})(b); | ||
const searchTerm = `/** ${cases.display} actions */`; | ||
const index = newBuffer.indexOf(searchTerm) + searchTerm.length; | ||
let content = ''; | ||
const hasPayload = | ||
Object.values(set).filter( | ||
value => `${value}`.indexOf('payload') !== -1, | ||
).length > 0 || payloadOverride; | ||
content += concat([ | ||
``, | ||
describe ? `// ${describe}` : null, | ||
`export const ${actionCases.camel} = (${ | ||
hasPayload ? 'payload' : '' | ||
}) => ({`, | ||
` type: ${actionCases.constant},`, | ||
hasPayload ? ` payload,` : null, | ||
`});`, | ||
]); | ||
if (i === 0) { | ||
content += '\n\n// @suit-end'; | ||
} | ||
return concat([ | ||
newBuffer.slice(0, index), | ||
content, | ||
newBuffer.slice(index), | ||
]); | ||
}), | ||
.map(writeOneAction(cases)), | ||
prettify, | ||
]); |
@@ -1,8 +0,3 @@ | ||
const Cases = require('../../../tools/cases'); | ||
const { | ||
concat, | ||
transforms, | ||
parseCamelCaseToArray, | ||
prettify, | ||
} = require('../../../tools/utils'); | ||
const { concat, transforms, prettify } = require('../../../tools/utils'); | ||
const writeOneConstant = require('./writeOneConstant'); | ||
@@ -17,22 +12,4 @@ module.exports = ({ buffer, cases, actions, folder }) => | ||
.reverse() | ||
.map(({ name }, i) => b => { | ||
const c = new Cases(parseCamelCaseToArray(name)); | ||
const actionCases = c.all(); | ||
const searchTerm = `/** ${cases.display} constants */`; | ||
const index = b.indexOf(searchTerm) + searchTerm.length; | ||
let content = ''; | ||
content += concat([ | ||
``, | ||
`export const ${actionCases.constant} =`, | ||
` '${folder}${actionCases.constant}';`, | ||
]); | ||
if (i === 0) { | ||
content += '\n\n// @suit-end'; | ||
} | ||
return concat([b.slice(0, index), content, b.slice(index)]); | ||
}), | ||
.map(writeOneConstant({ cases, folder })), | ||
prettify, | ||
]); |
const fs = require('fs'); | ||
const writeReducer = require('./writeReducer'); | ||
const writeOneReducer = require('./writeOneReducer'); | ||
const Cases = require('../../../tools/cases'); | ||
@@ -20,2 +20,3 @@ const { | ||
fixInlineImports, | ||
/** Writes a reducer for each domain */ | ||
...arrayOfDomains.map( | ||
@@ -30,3 +31,3 @@ ({ domainName, initialState, actions, describe }) => b => { | ||
return writeReducer({ | ||
return writeOneReducer({ | ||
buffer: b, | ||
@@ -33,0 +34,0 @@ cases: allDomainCases, |
@@ -12,3 +12,2 @@ const fs = require('fs'); | ||
} = require('../../../tools/utils'); | ||
const printError = require('../../../tools/printError'); | ||
const checkIfNoAllSagas = require('../../../tools/checkIfNoAllSagas'); | ||
@@ -19,97 +18,110 @@ | ||
const sagaErrors = checkIfNoAllSagas(sagaBuffer); | ||
return { | ||
errors: sagaErrors, | ||
buffer: sagaErrors.length | ||
? null | ||
: transforms(sagaBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, actions }) => buffer => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
const actionsWithSagas = Object.keys(actions).filter( | ||
key => typeof actions[key].saga !== 'undefined', | ||
); | ||
if (actionsWithSagas > 1) { | ||
printError([ | ||
concat([ | ||
`More than one action in ${ | ||
allDomainCases.display | ||
} has been given a saga`, | ||
`- Only one action can be assigned a saga per reducer.`, | ||
]), | ||
]); | ||
return buffer; | ||
} | ||
let sagaErrors = checkIfNoAllSagas(sagaBuffer); | ||
let sagaMessages = []; | ||
const newBuffer = transforms(sagaBuffer, [ | ||
cleanFile, | ||
fixInlineImports, | ||
...arrayOfDomains.map(({ domainName, actions }) => buffer => { | ||
const cases = new Cases(parseCamelCaseToArray(domainName)); | ||
const allDomainCases = cases.all(); | ||
const actionsWithSagas = Object.keys(actions).filter( | ||
key => typeof actions[key].saga !== 'undefined', | ||
); | ||
if (actionsWithSagas > 1) { | ||
sagaErrors.push( | ||
concat([ | ||
`More than one action in ${ | ||
allDomainCases.display | ||
} has been given a saga`, | ||
`- Only one action can be assigned a saga per reducer.`, | ||
]), | ||
); | ||
return buffer; | ||
} | ||
if (actionsWithSagas < 1) { | ||
return buffer; | ||
} | ||
if (actionsWithSagas < 1) { | ||
return buffer; | ||
} | ||
const actionCases = new Cases( | ||
parseCamelCaseToArray(actionsWithSagas[0]), | ||
).all(); | ||
const actionObject = actions[actionCases.camel]; | ||
const actionCases = new Cases( | ||
parseCamelCaseToArray(actionsWithSagas[0]), | ||
).all(); | ||
const actionObject = actions[actionCases.camel]; | ||
if (actionObject.saga === true) { | ||
return writeSaga({ | ||
buffer, | ||
cases: allDomainCases, | ||
actionCases, | ||
action: actionObject, | ||
}); | ||
} | ||
if (actionObject.saga === true) { | ||
const { | ||
buffer: uncontrolledSagaBuffer, | ||
errors: uncontrolledSagaErrors, | ||
messages: uncontrolledSagaMessages, | ||
} = writeSaga({ | ||
buffer, | ||
cases: allDomainCases, | ||
actionCases, | ||
action: actionObject, | ||
}); | ||
sagaErrors = [...sagaErrors, ...uncontrolledSagaErrors]; | ||
sagaMessages = [...sagaMessages, ...uncontrolledSagaMessages]; | ||
return uncontrolledSagaBuffer; | ||
} | ||
if (actionObject.saga.onFail && actionObject.saga.onSuccess) { | ||
return writeNameControlSaga({ | ||
buffer, | ||
domainCases: allDomainCases, | ||
action: actionObject, | ||
actionCases, | ||
failCases: new Cases( | ||
parseCamelCaseToArray(actionObject.saga.onFail), | ||
).all(), | ||
successCases: new Cases( | ||
parseCamelCaseToArray(actionObject.saga.onSuccess), | ||
).all(), | ||
keyChanges, | ||
}); | ||
} | ||
if (!actionObject.saga.onFail) { | ||
printError( | ||
concat([ | ||
`The saga in ${ | ||
actionCases.display | ||
} does not have an 'onFail' key.`, | ||
`- This means it won't report an error when it fails.`, | ||
`- try this:`, | ||
`- {`, | ||
`- "saga": {`, | ||
`- "onFail": "actionToFireOnFail"`, | ||
`- }`, | ||
`- }`, | ||
]), | ||
); | ||
} | ||
if (!actionObject.saga.onFail) { | ||
printError( | ||
concat([ | ||
`The saga in ${ | ||
actionCases.display | ||
} does not have an 'onSuccess' key.`, | ||
`- This means it won't report an error when it fails.`, | ||
`- try this:`, | ||
`- {`, | ||
`- "saga": {`, | ||
`- "onSuccess": "actionToFireOnSuccess"`, | ||
`- }`, | ||
`- }`, | ||
]), | ||
); | ||
} | ||
return buffer; | ||
}), | ||
]), | ||
if (!actionObject.saga.onFail) { | ||
sagaErrors.push( | ||
concat([ | ||
`The saga in ${actionCases.display} does not have an 'onFail' key.`, | ||
`- This means it won't report an error when it fails.`, | ||
`- try this:`, | ||
`- {`, | ||
`- "saga": {`, | ||
`- "onFail": "actionToFireOnFail"`, | ||
`- }`, | ||
`- }`, | ||
]), | ||
); | ||
} | ||
if (!actionObject.saga.onFail) { | ||
sagaErrors.push( | ||
concat([ | ||
`The saga in ${ | ||
actionCases.display | ||
} does not have an 'onSuccess' key.`, | ||
`- This means it won't report an error when it fails.`, | ||
`- try this:`, | ||
`- {`, | ||
`- "saga": {`, | ||
`- "onSuccess": "actionToFireOnSuccess"`, | ||
`- }`, | ||
`- }`, | ||
]), | ||
); | ||
} | ||
if (actionObject.saga.onFail && actionObject.saga.onSuccess) { | ||
const { | ||
buffer: nameControlBuffer, | ||
errors: nameControlErrors, | ||
messages: nameControlMessages, | ||
} = writeNameControlSaga({ | ||
buffer, | ||
domainCases: allDomainCases, | ||
action: actionObject, | ||
actionCases, | ||
failCases: new Cases( | ||
parseCamelCaseToArray(actionObject.saga.onFail), | ||
).all(), | ||
successCases: new Cases( | ||
parseCamelCaseToArray(actionObject.saga.onSuccess), | ||
).all(), | ||
keyChanges, | ||
}); | ||
sagaMessages = [...sagaMessages, ...nameControlMessages]; | ||
sagaErrors = [...sagaErrors, ...nameControlErrors]; | ||
return nameControlBuffer; | ||
} | ||
return buffer; | ||
}), | ||
]); | ||
return { | ||
buffer: newBuffer, | ||
messages: sagaMessages, | ||
errors: sagaErrors, | ||
}; | ||
}; |
@@ -17,4 +17,5 @@ const { | ||
keyChanges, | ||
}) => | ||
transforms(buffer, [ | ||
}) => { | ||
const messages = []; | ||
const newBuffer = transforms(buffer, [ | ||
ensureImport(failCases.camel, './actions', { destructure: true }), | ||
@@ -49,3 +50,3 @@ ensureImport(successCases.camel, './actions', { destructure: true }), | ||
if (sagaPresent) { | ||
console.log( | ||
messages.push( | ||
`\nSAGA:`.green + | ||
@@ -60,3 +61,3 @@ ` ${ | ||
console.log( | ||
messages.push( | ||
concat([ | ||
@@ -97,1 +98,7 @@ `\nSAGA:`.green + ` ${domainCases.camel} saga not found in file.`, | ||
]); | ||
return { | ||
buffer: newBuffer, | ||
messages, | ||
errors: [], | ||
}; | ||
}; |
@@ -8,4 +8,5 @@ const { | ||
module.exports = ({ buffer, cases, actionCases, action }) => | ||
transforms(buffer, [ | ||
module.exports = ({ buffer, cases, actionCases, action }) => { | ||
const messages = []; | ||
const newBuffer = transforms(buffer, [ | ||
ensureImport('takeLatest', 'redux-saga/effects', { destructure: true }), | ||
@@ -26,3 +27,3 @@ ensureImport('call', 'redux-saga/effects', { destructure: true }), | ||
if (sagaPresent) { | ||
console.log( | ||
messages.push( | ||
`\nSAGA:`.green + | ||
@@ -37,3 +38,3 @@ ` ${ | ||
console.log( | ||
messages.push( | ||
concat([ | ||
@@ -76,1 +77,7 @@ `\nSAGA:`.green + ` ${cases.camel} saga not found in file.`, | ||
]); | ||
return { | ||
buffer: newBuffer, | ||
messages, | ||
errors: [], | ||
}; | ||
}; |
19
index.js
@@ -5,2 +5,3 @@ #!/usr/bin/env node | ||
const gaze = require('gaze'); | ||
const fs = require('fs'); | ||
const up = require('./commands/up'); | ||
@@ -10,3 +11,3 @@ const ajax = require('./commands/ajax'); | ||
program.version('0.3.2'); | ||
program.version('0.3.3'); | ||
@@ -17,3 +18,17 @@ program | ||
.action(cmd => { | ||
gaze(['**/suit.json', '!node_modules/**/*'], (err, watcher) => { | ||
let watchedDirectories = ['!node_modules/**/*']; | ||
if (fs.existsSync('./.suitrc')) { | ||
const config = JSON.parse(fs.readFileSync('./.suitrc').toString()); | ||
if (config.include && config.include.length) { | ||
watchedDirectories = [ | ||
...watchedDirectories, | ||
config.include.map(path => `${path}/**/suit.json`), | ||
]; | ||
} else { | ||
watchedDirectories.push(['**/suit.json']); | ||
} | ||
} else { | ||
watchedDirectories.push(['**/suit.json']); | ||
} | ||
gaze(watchedDirectories, (err, watcher) => { | ||
// Resets the console | ||
@@ -20,0 +35,0 @@ process.stdout.write('\x1Bc'); |
{ | ||
"name": "boilersuit", | ||
"description": "A CLI tool for generating selectors, reducers, actions, constants and sagas in react-boilerplate", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "author": "matt <matt.pocock@thevirtualforge.com>", |
@@ -408,4 +408,8 @@ <div style="text-align: center"><img src="https://raw.githubusercontent.com/mattpocock/boilersuit/master/logo.png" max-height="240px" alt="boilerplate logo"/></div> | ||
You can add a .suitrc (or .suitrc.json) file to the root of your folder to configure boilersuit. We're planning on making this a lot more extensible, but for now: | ||
You can add a .suitrc (or .suitrc.json) file to the root of your folder to configure boilersuit. We're planning on making this a lot more extensible. | ||
### showDescribeWarnings | ||
You can configure suit to give you warnings if you don't specify a 'describe' key. Handy for keeping discipline on large codebases. | ||
```json | ||
@@ -416,1 +420,11 @@ { | ||
``` | ||
### include | ||
You can speed up suit like crazy by specifying which folders you have placed your suit files in. | ||
```json | ||
{ | ||
"include": ["app/containers"] | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
441581
60
2842
429
18