Skip to content

Commit

Permalink
replace patchInFile with a wrapped method so failing doesn't exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ruddell committed Aug 26, 2019
1 parent e225cb1 commit c1ab0cf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
15 changes: 8 additions & 7 deletions src/boilerplate/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = async function (context, props, jhipsterConfig) {
const { filesystem, ignite, print, strings } = context
const { camelCase, upperFirst } = strings
const spinner = print.spin(`using the ${print.colors.blue('JHipster')} boilerplate`).succeed()
const { patchNeedle } = require('../lib/patch-needle')

// this is needed because the "upgrade "command is run from within an app, while the "new" command is run from one level deeper
// if the ignite/ignite.json file exists (created below), it's an upgrade, otherwise it's a new app
Expand Down Expand Up @@ -183,7 +184,7 @@ module.exports = async function (context, props, jhipsterConfig) {
})
const keycloakConfigFile = 'src/main/docker/realm-config/jhipster-realm.json'
if (fs.existsSync(`${jhipsterPathPrefix}${props.jhipsterDirectory}/${keycloakConfigFile}`)) {
await ignite.patchInFile(`${jhipsterPathPrefix}${props.jhipsterDirectory}/${keycloakConfigFile}`,
await patchNeedle(context, `${jhipsterPathPrefix}${props.jhipsterDirectory}/${keycloakConfigFile}`,
{
replace: `"dev.localhost.ionic:*"`,
insert: `"dev.localhost.ionic:*", "${props.name.toLowerCase()}://*"`
Expand All @@ -192,7 +193,7 @@ module.exports = async function (context, props, jhipsterConfig) {
}
const securityConfigFile = 'SecurityConfiguration'
if (isMonolith && fs.existsSync(`${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/${securityConfigFile}.java`)) {
await ignite.patchInFile(`${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/${securityConfigFile}.java`,
await patchNeedle(context, `${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/${securityConfigFile}.java`,
{
replace: '.antMatchers("/api/**").authenticated()',
insert: '.antMatchers("/api/auth-info").permitAll()\n .antMatchers("/api/**").authenticated()'
Expand All @@ -203,7 +204,7 @@ module.exports = async function (context, props, jhipsterConfig) {
const androidAuthRedirectContent = ` manifestPlaceholders = [
appAuthRedirectScheme: '${props.name.toLowerCase()}'
]`
await ignite.patchInFile('android/app/build.gradle', {
await patchNeedle(context, 'android/app/build.gradle', {
before: 'applicationId',
insert: androidAuthRedirectContent,
match: androidAuthRedirectContent
Expand Down Expand Up @@ -264,7 +265,7 @@ end
spinner.text = '▸ setting up websocket code'
spinner.start()
// import ChatRedux in redux/index.js
await ignite.patchInFile('app/shared/reducers/index.js', {
await patchNeedle(context, 'app/shared/reducers/index.js', {
before: 'ignite-jhipster-redux-store-import-needle',
insert: ` chat: require('../../modules/chat/chat.reducer').reducer,`,
match: ` chat: require('../../modules/chat/chat.reducer').reducer,`
Expand All @@ -274,23 +275,23 @@ end
// wire ChatScreen in NavigationRouter
// const navigationRouterFilePath = `${process.cwd()}/app/navigation/navigation-router.js`
// const navigationImportEdit = `import ChatScreen from '../modules/chat/chat-screen'`
// await ignite.patchInFile(navigationRouterFilePath, {
// await patchNeedle(context, navigationRouterFilePath, {
// before: 'ignite-jhipster-navigation-import-needle',
// insert: navigationImportEdit,
// match: navigationImportEdit
// })
//
// // add chat screen to navigation
// const navigationScreen = ` <Scene key='chat' component={ChatScreen} title='Chat' back />`
// await ignite.patchInFile(navigationRouterFilePath, {
// await patchNeedle(context, navigationRouterFilePath, {
// before: 'ignite-jhipster-navigation-needle',
// insert: navigationScreen,
// match: navigationScreen
// })

// copy the WebsocketConfiguration.java to the jhipsterDirectory
if (fs.existsSync(`${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/WebsocketConfiguration.java`)) {
await ignite.patchInFile(`${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/WebsocketConfiguration.java`, { replace: '"/websocket/tracker"', insert: '"/websocket/tracker", "/websocket/chat"' })
await patchNeedle(context, `${jhipsterPathPrefix}${props.jhipsterDirectory}/src/main/java/${props.packageFolder}/config/WebsocketConfiguration.java`, { replace: '"/websocket/tracker"', insert: '"/websocket/tracker", "/websocket/chat"' })
}
spinner.stop()
} else {
Expand Down
43 changes: 22 additions & 21 deletions src/entity/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = async function (generator, igniteContext) {
const semver = require('semver')
const pluralize = require('pluralize')
const fs = require('fs-extra')
const { patchNeedle } = require('../lib/patch-needle')
const { getEntityFormField, getRelationshipFormField } = require('../lib/entity-helpers')
const { ignite, strings, parameters, print } = igniteContext
const { kebabCase, pascalCase, snakeCase, upperCase, camelCase, isBlank, upperFirst } = strings // eslint-disable-line
Expand Down Expand Up @@ -163,42 +164,42 @@ module.exports = async function (generator, igniteContext) {
}

// add methods to api
ignite.patchInFile(apiFilePath, {
await patchNeedle(igniteContext, apiFilePath, {
before: 'ignite-jhipster-api-method-needle',
insert: apiMethods,
match: apiMethods
})
ignite.patchInFile(apiFilePath, {
await patchNeedle(igniteContext, apiFilePath, {
before: 'ignite-jhipster-api-export-needle',
insert: apiMethodsExport,
match: apiMethodsExport
})
ignite.patchInFile(fixtureApiFilePath, {
await patchNeedle(igniteContext, fixtureApiFilePath, {
before: 'ignite-jhipster-api-fixture-needle',
insert: fixtureApiMethods,
match: fixtureApiMethods
})

// import redux in redux/index.js
ignite.patchInFile(reduxIndexFilePath, {
await patchNeedle(igniteContext, reduxIndexFilePath, {
before: 'ignite-jhipster-redux-store-import-needle',
insert: ` ${camelCase(props.pluralName)}: require('../../modules/entities/${props.kebabName}/${props.kebabName}.reducer').reducer,`,
match: ` ${camelCase(props.pluralName)}: require('../../modules/entities/${props.kebabName}/${props.kebabName}.reducer').reducer,`
})

// import saga/redux in sagas/index.js
ignite.patchInFile(sagaIndexFilePath, {
await patchNeedle(igniteContext, sagaIndexFilePath, {
before: 'ignite-jhipster-saga-redux-import-needle',
insert: `import { ${props.name}Types } from '../../modules/entities/${props.kebabName}/${props.kebabName}.reducer'`,
match: `import { ${props.name}Types } from '../../modules/entities/${props.kebabName}/${props.kebabName}.reducer'`
})
ignite.patchInFile(sagaIndexFilePath, {
await patchNeedle(igniteContext, sagaIndexFilePath, {
before: 'ignite-jhipster-saga-method-import-needle',
insert: `import { get${props.name}, get${props.pluralName}, update${props.name}, delete${props.name}${props.searchEngine ? `, search${props.pluralName}` : ''} } from '../../modules/entities/${props.kebabName}/${props.kebabName}.sagas'`,
match: `import { get${props.name}, get${props.pluralName}, update${props.name}, delete${props.name}${props.searchEngine ? `, search${props.pluralName}` : ''} } from '../../modules/entities/${props.kebabName}/${props.kebabName}.sagas'`
})

ignite.patchInFile(sagaIndexFilePath, {
await patchNeedle(igniteContext, sagaIndexFilePath, {
before: 'ignite-jhipster-saga-redux-connect-needle',
insert: sagaConnections,
match: sagaConnections
Expand Down Expand Up @@ -282,19 +283,19 @@ module.exports = async function (generator, igniteContext) {

// import entity screens to navigation
const navigationImport = `import ${props.name}EntityScreen from '../modules/entities/${props.kebabName}/${props.kebabName}-entity-screen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-import-needle',
insert: navigationImport,
match: navigationImport
})
const navigationImportDetail = `import ${props.name}EntityDetailScreen from '../modules/entities/${props.kebabName}/${props.kebabName}-entity-detail-screen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-import-needle',
insert: navigationImportDetail,
match: navigationImportDetail
})
const navigationImportEdit = `import ${props.name}EntityEditScreen from '../modules/entities/${props.kebabName}/${props.kebabName}-entity-edit-screen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-import-needle',
insert: navigationImportEdit,
match: navigationImportEdit
Expand All @@ -305,39 +306,39 @@ module.exports = async function (generator, igniteContext) {

// import entity screens to navigation
const navigationDeclaration = `export const ${upperSnakeCaseName} = 'nav.${props.name}EntityScreen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-declaration-needle',
insert: navigationDeclaration,
match: navigationDeclaration
})
const navigationDeclarationDetail = `export const ${upperSnakeCaseNameDetail} = 'nav.${props.name}EntityDetailScreen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-declaration-needle',
insert: navigationDeclarationDetail,
match: navigationDeclarationDetail
})
const navigationDeclarationEdit = `export const ${upperSnakeCaseNameEdit} = 'nav.${props.name}EntityEditScreen'`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-declaration-needle',
insert: navigationDeclarationEdit,
match: navigationDeclarationEdit
})

// add entity screens to navigation
const navigationScreen = ` Navigation.registerComponentWithRedux(${upperSnakeCaseName}, () => ${props.name}EntityScreen, Provider, store)`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-registration-needle',
insert: navigationScreen,
match: navigationScreen
})
const navigationScreenDetail = ` Navigation.registerComponentWithRedux(${upperSnakeCaseNameDetail}, () => ${props.name}EntityDetailScreen, Provider, store)`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-registration-needle',
insert: navigationScreenDetail,
match: navigationScreenDetail
})
const navigationScreenEdit = ` Navigation.registerComponentWithRedux(${upperSnakeCaseNameEdit}, () => ${props.name}EntityEditScreen, Provider, store)`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-registration-needle',
insert: navigationScreenEdit,
match: navigationScreenEdit
Expand Down Expand Up @@ -399,30 +400,30 @@ export const ${camelCase(props.name)}EntityEditScreen = (data) => Navigation.pus
}
}
})`
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-method-needle',
insert: navigationMethodMain,
match: navigationMethodMain
})
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-method-needle',
insert: navigationMethodEdit,
match: navigationMethodEdit
})
ignite.patchInFile(navigationRouterFilePath, {
await patchNeedle(igniteContext, navigationRouterFilePath, {
before: 'ignite-jhipster-navigation-method-needle',
insert: navigationMethodDetail,
match: navigationMethodDetail
})
// add entity to entities screen
const entityScreenButton = ` <RoundedButton text='${props.name}' onPress={${camelCase(props.name)}EntityScreen} testID='${camelCase(props.name)}EntityScreenButton' />`
ignite.patchInFile(entityScreenFilePath, {
await patchNeedle(igniteContext, entityScreenFilePath, {
before: 'ignite-jhipster-entity-screen-needle',
insert: entityScreenButton,
match: entityScreenButton
})
const entityScreenImport = ` ${camelCase(props.name)}EntityScreen,`
ignite.patchInFile(entityScreenFilePath, {
await patchNeedle(igniteContext, entityScreenFilePath, {
before: 'ignite-jhipster-entity-screen-import-needle',
insert: entityScreenImport,
match: entityScreenImport
Expand Down
13 changes: 13 additions & 0 deletions src/lib/patch-needle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

const patchNeedle = async (context, filePath, params) => {
const { ignite, print } = context
try {
await ignite.patchInFile(filePath, params)
} catch (e) {
print.warning(`Could not find insertion point in ${filePath}:\n${params.before}`)
}
}

module.exports = {
patchNeedle
}
24 changes: 12 additions & 12 deletions src/lib/react-native-navigation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { patchNeedle } = require('../lib/patch-needle')

const patchReactNativeNavigation = async (context = {}, props) => {
// REACT_NATIVE_NAVIGATION_VERSION
const {
Expand Down Expand Up @@ -39,9 +41,8 @@ const patchReactNativeNavigation = async (context = {}, props) => {
await updateAndroidFiles(context, props)
}
const updateIosFiles = async (context, props) => {
const { ignite } = context
/*eslint-disable */
await ignite.patchInFile(`${process.cwd()}/ios/${props.name}/Info.plist`, {
await patchNeedle(context, `${process.cwd()}/ios/${props.name}/Info.plist`, {
before: `<key>CFBundleDisplayName</key>`,
insert: `
<key>CFBundleURLTypes</key>
Expand All @@ -62,54 +63,53 @@ const updateIosFiles = async (context, props) => {
}

const updateAndroidFiles = async (context, props) => {
const { ignite } = context
// settings.gradle
await ignite.patchInFile(`${process.cwd()}/android/settings.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/settings.gradle`, {
after: `include ':app'`,
insert: `include ':react-native-navigation'\nproject(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/lib/android/app/')`
})

// build.gradle
await ignite.patchInFile(`${process.cwd()}/android/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/build.gradle`, {
before: `mavenLocal()`,
insert: ` google()
mavenCentral()
maven { url 'https://jitpack.io' }`
})

await ignite.patchInFile(`${process.cwd()}/android/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/build.gradle`, {
after: `repositories {`,
insert: ` google()
mavenLocal()
mavenCentral()`
})

// react-native-init uses a later version so this is not currently needed
// await ignite.patchInFile(`${process.cwd()}/android/build.gradle`, {
// await patchNeedle(context, `${process.cwd()}/android/build.gradle`, {
// replace: `buildToolsVersion = "26.0.3"`,
// insert: `buildToolsVersion = "28.0.3"`
// })

await ignite.patchInFile(`${process.cwd()}/android/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/build.gradle`, {
replace: `minSdkVersion = 16`,
insert: `minSdkVersion = 19`
})

// app/build.gradle
await ignite.patchInFile(`${process.cwd()}/android/app/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/app/build.gradle`, {
after: `versionCode 1`,
insert: ` missingDimensionStrategy "RNN.reactNativeVersion", "reactNative57_5"`
})

await ignite.patchInFile(`${process.cwd()}/android/app/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/app/build.gradle`, {
before: `buildTypes {`,
insert: ` compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}`
})

await ignite.patchInFile(`${process.cwd()}/android/app/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/app/build.gradle`, {
before: `dependencies {`,
insert: `configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
Expand All @@ -120,7 +120,7 @@ const updateAndroidFiles = async (context, props) => {
}
}`
})
await ignite.patchInFile(`${process.cwd()}/android/app/build.gradle`, {
await patchNeedle(context, `${process.cwd()}/android/app/build.gradle`, {
after: `dependencies {`,
insert: `
implementation "com.facebook.react:react-native:+" // From node_modules
Expand Down

0 comments on commit c1ab0cf

Please sign in to comment.